1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
#include "tree-controller.hpp"
#include <set>
#include <wayfire/nonstd/tracking-allocator.hpp>
#include <algorithm>
#include <wayfire/core.hpp>
#include <wayfire/output.hpp>
#include <wayfire/workspace-set.hpp>
#include <wayfire/util.hpp>
#include <wayfire/nonstd/reverse.hpp>
#include <wayfire/plugins/common/preview-indication.hpp>
#include <wayfire/txn/transaction-manager.hpp>
namespace wf
{
namespace tile
{
void for_each_view(nonstd::observer_ptr<tree_node_t> root,
std::function<void(wayfire_toplevel_view)> callback)
{
if (root->as_view_node())
{
callback(root->as_view_node()->view);
return;
}
for (auto& child : root->children)
{
for_each_view(child, callback);
}
}
nonstd::observer_ptr<view_node_t> find_view_at(
nonstd::observer_ptr<tree_node_t> root, wf::point_t input)
{
if (root->as_view_node())
{
return root->as_view_node();
}
for (auto& child : root->children)
{
if (child->geometry & input)
{
return find_view_at({child}, input);
}
}
/* Children probably empty? */
return nullptr;
}
nonstd::observer_ptr<view_node_t> find_first_view_in_direction(
nonstd::observer_ptr<tree_node_t> from, split_insertion_t direction)
{
auto window = from->geometry;
/* Since nodes are arranged tightly into a grid, we can just find the
* proper edge and find the view there */
wf::point_t point;
switch (direction)
{
case INSERT_ABOVE:
point = {
window.x + window.width / 2,
window.y - 1,
};
break;
case INSERT_BELOW:
point = {
window.x + window.width / 2,
window.y + window.height,
};
break;
case INSERT_LEFT:
point = {
window.x - 1,
window.y + window.height / 2,
};
break;
case INSERT_RIGHT:
point = {
window.x + window.width,
window.y + window.height / 2,
};
break;
default:
assert(false);
}
auto root = from;
while (root->parent)
{
root = root->parent;
}
return find_view_at(root, point);
}
/* ------------------------ move_view_controller_t -------------------------- */
move_view_controller_t::move_view_controller_t(wf::workspace_set_t *set, wayfire_toplevel_view grabbed_view)
{
if (this->drag_helper->view)
{
// Race conditions are possible: spontaneous move requests from xwayland could trigger move without
// a pressed button, and then if the user tries to use simple-tile, it leads to a crash.
return;
}
this->drag_helper->set_pending_drag(wf::get_core().get_cursor_position());
move_drag::drag_options_t drag_options;
drag_options.initial_scale = 1.0;
drag_options.enable_snap_off = true;
drag_options.snap_off_threshold = 20;
drag_options.join_views = false;
this->drag_helper->start_drag(grabbed_view, drag_options);
}
move_view_controller_t::~move_view_controller_t()
{}
void move_view_controller_t::input_motion()
{
drag_helper->handle_motion(wf::get_core().get_cursor_position().round_down());
}
void move_view_controller_t::input_released(bool force_stop)
{
drag_helper->handle_input_released();
}
wf::geometry_t eval(nonstd::observer_ptr<tree_node_t> node)
{
return node ? node->geometry : wf::geometry_t{0, 0, 0, 0};
}
/* ----------------------- resize tile controller --------------------------- */
resize_view_controller_t::resize_view_controller_t(wf::workspace_set_t *wset, wayfire_toplevel_view)
{
this->last_point = get_global_input_coordinates(wset->get_attached_output());
this->grabbed_view = find_view_at(get_root(wset, wset->get_current_workspace()), last_point);
this->output = wset->get_attached_output();
if (this->grabbed_view)
{
this->resizing_edges = calculate_resizing_edges(last_point);
horizontal_pair = this->find_resizing_pair(true);
vertical_pair = this->find_resizing_pair(false);
}
}
resize_view_controller_t::~resize_view_controller_t()
{}
uint32_t resize_view_controller_t::calculate_resizing_edges(wf::point_t grab)
{
uint32_t result_edges = 0;
auto window = this->grabbed_view->geometry;
assert(window & grab);
if (grab.x < window.x + window.width / 2)
{
result_edges |= WLR_EDGE_LEFT;
} else
{
result_edges |= WLR_EDGE_RIGHT;
}
if (grab.y < window.y + window.height / 2)
{
result_edges |= WLR_EDGE_TOP;
} else
{
result_edges |= WLR_EDGE_BOTTOM;
}
return result_edges;
}
resize_view_controller_t::resizing_pair_t resize_view_controller_t::find_resizing_pair(bool horiz)
{
split_insertion_t direction;
/* Calculate the direction in which we are looking for the resizing pair */
if (horiz)
{
if (this->resizing_edges & WLR_EDGE_TOP)
{
direction = INSERT_ABOVE;
} else
{
direction = INSERT_BELOW;
}
} else
{
if (this->resizing_edges & WLR_EDGE_LEFT)
{
direction = INSERT_LEFT;
} else
{
direction = INSERT_RIGHT;
}
}
/* Find a view in the resizing direction, then look for the least common
* ancestor(LCA) of the grabbed view and the found view.
*
* Then the resizing pair is a pair of children of the LCA */
auto pair_view =
find_first_view_in_direction(this->grabbed_view, direction);
if (!pair_view) // no pair
{
return {nullptr, grabbed_view};
}
/* Calculate all ancestors of the grabbed view */
std::set<nonstd::observer_ptr<tree_node_t>> grabbed_view_ancestors;
nonstd::observer_ptr<tree_node_t> ancestor = grabbed_view;
while (ancestor)
{
grabbed_view_ancestors.insert(ancestor);
ancestor = ancestor->parent;
}
/* Find the LCA: this is the first ancestor of the pair_view which is also
* an ancestor of the grabbed view */
nonstd::observer_ptr<tree_node_t> lca = pair_view;
/* The child of lca we came from the second time */
nonstd::observer_ptr<tree_node_t> lca_successor = nullptr;
while (lca && !grabbed_view_ancestors.count({lca}))
{
lca_successor = lca;
lca = lca->parent;
}
/* In the "worst" case, the root of the tree is an LCA.
* Also, an LCA is a split because it is an ancestor of two different
* view nodes */
assert(lca && lca->children.size());
resizing_pair_t result_pair;
for (auto& child : lca->children)
{
if (grabbed_view_ancestors.count({child}))
{
result_pair.first = {child};
break;
}
}
result_pair.second = lca_successor;
/* Make sure the first node in the resizing pair is always to the
* left or above of the second one */
if ((direction == INSERT_LEFT) || (direction == INSERT_ABOVE))
{
std::swap(result_pair.first, result_pair.second);
}
return result_pair;
}
void resize_view_controller_t::adjust_geometry(int32_t& x1, int32_t& len1,
int32_t& x2, int32_t& len2, int32_t delta)
{
/*
* On the line:
*
* x1 (x1+len1)=x2 x2+len2-1
* ._______________.___________________.
*/
constexpr int MIN_SIZE = 50;
int maxPositive = std::max(0, len2 - MIN_SIZE);
int maxNegative = std::max(0, len1 - MIN_SIZE);
/* Make sure we don't shrink one dimension too much */
delta = clamp(delta, -maxNegative, maxPositive);
/* Adjust sizes */
len1 += delta;
x2 += delta;
len2 -= delta;
}
void resize_view_controller_t::input_motion()
{
auto input = get_global_input_coordinates(output);
if (!this->grabbed_view)
{
return;
}
auto tx = wf::txn::transaction_t::create();
if (horizontal_pair.first && horizontal_pair.second)
{
int dy = input.y - last_point.y;
auto g1 = horizontal_pair.first->geometry;
auto g2 = horizontal_pair.second->geometry;
adjust_geometry(g1.y, g1.height, g2.y, g2.height, dy);
horizontal_pair.first->set_geometry(g1, tx);
horizontal_pair.second->set_geometry(g2, tx);
}
if (vertical_pair.first && vertical_pair.second)
{
int dx = input.x - last_point.x;
auto g1 = vertical_pair.first->geometry;
auto g2 = vertical_pair.second->geometry;
adjust_geometry(g1.x, g1.width, g2.x, g2.width, dx);
vertical_pair.first->set_geometry(g1, tx);
vertical_pair.second->set_geometry(g2, tx);
}
wf::get_core().tx_manager->schedule_transaction(std::move(tx));
this->last_point = input;
}
wf::point_t get_global_input_coordinates(wf::output_t *output)
{
wf::pointf_t local = output->get_cursor_position();
auto vp = output->wset()->get_current_workspace();
auto size = output->get_screen_size();
local.x += size.width * vp.x;
local.y += size.height * vp.y;
return {(int)local.x, (int)local.y};
}
} // namespace tile
}
|