edges, resistance, snap: unified resistance and snapping engine

This commit is contained in:
Andrew J. Hesford 2024-01-23 13:44:40 -05:00
parent 1b0f1a4c4e
commit e7e6d29237
8 changed files with 748 additions and 469 deletions

View file

@ -4,164 +4,84 @@
#include "common/border.h"
#include "common/macros.h"
#include "config/rcxml.h"
#include "edges.h"
#include "labwc.h"
#include "resistance.h"
#include "view.h"
static void
is_within_resistance_range(struct border view, struct border target,
struct border other, struct border *flags, int strength)
check_edge(int *next, int current, int target,
int oppose, int align, bool lesser, int tolerance)
{
if (view.left >= other.left) {
const int lo = other.left - abs(strength);
const int hi = other.left - MIN(strength, 0);
flags->left = target.left >= lo && target.left < hi;
}
if (!flags->left && view.right <= other.right) {
const int lo = other.right + MIN(strength, 0);
const int hi = other.right + abs(strength);
flags->right = target.right > lo && target.right <= hi;
}
if (view.top >= other.top) {
const int lo = other.top - abs(strength);
const int hi = other.top - MIN(strength, 0);
flags->top = target.top >= lo && target.top < hi;
}
if (!flags->top && view.bottom <= other.bottom) {
const int lo = other.bottom + MIN(strength, 0);
const int hi = other.bottom + abs(strength);
flags->bottom = target.bottom > lo && target.bottom <= hi;
}
}
static void
build_view_edges(struct view *view, struct wlr_box new_geom,
struct border *view_edges, struct border *target_edges, bool move)
{
struct border border = ssd_get_margin(view->ssd);
/* Use the effective height to properly snap shaded views */
int eff_height = view_effective_height(view, /* use_pending */ false);
view_edges->left = view->current.x - border.left + (move ? 1 : 0);
view_edges->top = view->current.y - border.top + (move ? 1 : 0);
view_edges->right = view->current.x + view->current.width + border.right;
view_edges->bottom = view->current.y + eff_height + border.bottom;
target_edges->left = new_geom.x - border.left;
target_edges->top = new_geom.y - border.top;
target_edges->right = new_geom.x + new_geom.width + border.right;
target_edges->bottom = new_geom.y + border.bottom +
(view->shaded ? 0 : new_geom.height);
}
static void
update_nearest_edge(struct border view_edges, struct border target_edges,
struct border region_edges, int strength,
struct border *next_edges)
{
struct border flags = { 0 };
is_within_resistance_range(view_edges,
target_edges, region_edges, &flags, strength);
if (flags.left == 1) {
next_edges->left = MAX(region_edges.left, next_edges->left);
} else if (flags.right == 1) {
next_edges->right = MIN(region_edges.right, next_edges->right);
}
if (flags.top == 1) {
next_edges->top = MAX(region_edges.top, next_edges->top);
} else if (flags.bottom == 1) {
next_edges->bottom = MIN(region_edges.bottom, next_edges->bottom);
}
}
static void
find_neighbor_edges(struct view *view, struct wlr_box new_geom,
struct border *next_edges, bool move)
{
if (rc.window_edge_strength == 0) {
/* Ignore non-moving edges */
if (current == target) {
return;
}
struct border view_edges = { 0 };
struct border target_edges = { 0 };
/*
* The edge defined by current and moving to target may encounter two
* edges of another region: the opposing edge of the region is that in
* the opposite orientation of the moving edge (i.e., left <-> right or
* top <-> bottom); the aligned edge of the region is that in the same
* orientation as the moving edge (i.e., left <->left, top <-> top,
* right <-> right, bottom <-> bottom).
*
* Any opposing or aligned edge of a region is considered "valid" in
* this search if the resist/attract zone (defined by tolerance) of
* that edge contains the target position of the moving edge.
*/
build_view_edges(view, new_geom, &view_edges, &target_edges, move);
/* Direction of motion for the edge */
const bool decreasing = target < current;
struct view *v;
for_each_view(v, &view->server->views, LAB_VIEW_CRITERIA_CURRENT_WORKSPACE) {
if (v == view || !output_is_usable(v->output)) {
continue;
}
/* Check the opposing edge */
bool valid = false;
if (decreasing) {
const int lo = clipped_sub(oppose, abs(tolerance));
const int hi = clipped_sub(oppose, MIN(tolerance, 0));
valid = target >= lo && target < hi;
} else {
/* Check for increasing movement across opposing edge */
const int lo = clipped_add(oppose, MIN(tolerance, 0));
const int hi = clipped_add(oppose, abs(tolerance));
valid = target > lo && target <= hi;
}
struct border border = ssd_get_margin(v->ssd);
if (valid) {
*next = edge_get_best(*next, oppose, decreasing);
}
/*
* The significance of window edges here is inverted with
* respect to the usual orientation, because the edges of the
* view v of interest are those that would be encountered by a
* change in geometry in view along the named edge of view.
* Hence, when moving or resizing view *left*, it is the
* *right* edge of v that would be encountered, and vice versa;
* when moving or resizing view *down* ("bottom"), it is the
* *top* edge of v that would be encountered, and vice versa.
*/
struct border win_edges = {
.top = v->current.y + border.bottom
+ view_effective_height(v, /* use_pending */ false),
.right = v->current.x - border.left,
.bottom = v->current.y - border.top,
.left = v->current.x + v->current.width + border.right,
};
/* Check the aligned edge */
valid = false;
if (decreasing) {
const int lo = clipped_sub(align, abs(tolerance));
const int hi = clipped_sub(align, MIN(tolerance, 0));
valid = target >= lo && target < hi;
} else {
const int lo = clipped_add(align, MIN(tolerance, 0));
const int hi = clipped_add(align, abs(tolerance));
valid = target > lo && target <= hi;
}
update_nearest_edge(view_edges, target_edges,
win_edges, rc.window_edge_strength, next_edges);
if (valid) {
*next = edge_get_best(*next, align, decreasing);
}
}
static void
find_screen_edges(struct view *view, struct wlr_box new_geom,
struct border *next_edges, bool move)
check_edge_output(int *next, int current, int target,
int oppose, int align, bool lesser)
{
if (rc.screen_edge_strength == 0) {
return;
}
check_edge(next, current, target,
oppose, align, lesser, rc.screen_edge_strength);
}
struct border view_edges = { 0 };
struct border target_edges = { 0 };
build_view_edges(view, new_geom, &view_edges, &target_edges, move);
struct output *output;
wl_list_for_each(output, &view->server->outputs, link) {
if (!output_is_usable(output)) {
continue;
}
struct wlr_box mgeom =
output_usable_area_in_layout_coords(output);
struct wlr_box ol;
if (!wlr_box_intersection(&ol, &view->current, &mgeom) &&
!wlr_box_intersection(&ol, &new_geom, &mgeom)) {
continue;
}
struct border screen_edges = {
.top = mgeom.y,
.right = mgeom.x + mgeom.width,
.bottom = mgeom.y + mgeom.height,
.left = mgeom.x,
};
update_nearest_edge(view_edges, target_edges,
screen_edges, rc.screen_edge_strength, next_edges);
}
static void
check_edge_window(int *next, int current, int target,
int oppose, int align, bool lesser)
{
check_edge(next, current, target,
oppose, align, lesser, rc.window_edge_strength);
}
void
@ -169,37 +89,34 @@ resistance_move_apply(struct view *view, double *x, double *y)
{
assert(view);
struct border border = ssd_get_margin(view->ssd);
struct border next_edges;
edges_initialize(&next_edges);
struct border next_edges = {
.top = INT_MIN,
.right = INT_MAX,
.bottom = INT_MAX,
.left = INT_MIN,
};
struct wlr_box new_geom = {
struct wlr_box target = {
.x = *x,
.y = *y,
.width = view->current.width,
.height = view->current.height,
};
find_screen_edges(view, new_geom, &next_edges, /* move */ true);
find_neighbor_edges(view, new_geom, &next_edges, /* move */ true);
if (next_edges.left > INT_MIN) {
*x = next_edges.left + border.left;
} else if (next_edges.right < INT_MAX) {
*x = next_edges.right - view->current.width - border.right;
if (rc.screen_edge_strength != 0) {
/* Find any relevant output edges encountered by this move */
edges_find_outputs(&next_edges, view, target, NULL,
check_edge_output, /* use_pending */ false);
}
if (next_edges.top > INT_MIN) {
*y = next_edges.top + border.top;
} else if (next_edges.bottom < INT_MAX) {
*y = next_edges.bottom - border.bottom
- view_effective_height(view, /* use_pending */ false);
if (rc.window_edge_strength != 0) {
/* Find any relevant window edges encountered by this move */
edges_find_neighbors(&next_edges, view, target, NULL,
check_edge_window, /* use_pending */ false);
}
/* If any "best" edges were encountered during this move, snap motion */
edges_adjust_move_coords(view, next_edges,
&target.x, &target.y, /* use_pending */ false);
*x = target.x;
*y = target.y;
}
void
@ -208,41 +125,22 @@ resistance_resize_apply(struct view *view, struct wlr_box *new_geom)
assert(view);
assert(!view->shaded);
struct border border = ssd_get_margin(view->ssd);
struct border next_edges;
edges_initialize(&next_edges);
struct border next_edges = {
.top = INT_MIN,
.right = INT_MAX,
.bottom = INT_MAX,
.left = INT_MIN,
};
find_screen_edges(view, *new_geom, &next_edges, /* move */ false);
find_neighbor_edges(view, *new_geom, &next_edges, /* move */ false);
if (view->server->resize_edges & WLR_EDGE_LEFT) {
if (next_edges.left > INT_MIN) {
new_geom->x = next_edges.left + border.left;
new_geom->width = view->current.width
+ view->current.x - new_geom->x;
}
} else if (view->server->resize_edges & WLR_EDGE_RIGHT) {
if (next_edges.right < INT_MAX) {
new_geom->width = next_edges.right
- view->current.x - border.right;
}
if (rc.screen_edge_strength != 0) {
/* Find any relevant output edges encountered by this move */
edges_find_outputs(&next_edges, view, *new_geom, NULL,
check_edge_output, /* use_pending */ false);
}
if (view->server->resize_edges & WLR_EDGE_TOP) {
if (next_edges.top > INT_MIN) {
new_geom->y = next_edges.top + border.top;
new_geom->height = view->current.height
+ view->current.y - new_geom->y;
}
} else if (view->server->resize_edges & WLR_EDGE_BOTTOM) {
if (next_edges.bottom < INT_MAX) {
new_geom->height = next_edges.bottom
- view->current.y - border.bottom;
}
if (rc.window_edge_strength != 0) {
/* Find any relevant window edges encountered by this move */
edges_find_neighbors(&next_edges, view, *new_geom, NULL,
check_edge_window, /* use_pending */ false);
}
/* If any "best" edges were encountered during this move, snap motion */
edges_adjust_resize_geom(view, next_edges,
view->server->resize_edges, new_geom, /* use_pending */ false);
}