resistance: improve readability

This commit is contained in:
ARDiDo 2021-10-24 21:14:05 -04:00 committed by Johan Malm
parent c4995d7bc2
commit 4d5b4be608
3 changed files with 101 additions and 65 deletions

View file

@ -345,8 +345,8 @@ void view_child_finish(struct view_child *child);
void subsurface_create(struct view *view, struct wlr_subsurface *wlr_subsurface); void subsurface_create(struct view *view, struct wlr_subsurface *wlr_subsurface);
void view_set_activated(struct view *view, bool activated); void view_set_activated(struct view *view, bool activated);
void move_resistance(struct view *view, double *x, double *y, bool screen_edge); void resistance_move_apply(struct view *view, double *x, double *y, bool screen_edge);
void resize_resistance(struct view *view, struct wlr_box *new_view_geo, void resistance_resize_apply(struct view *view, struct wlr_box *new_view_geo,
bool screen_edge); bool screen_edge);
struct border view_border(struct view *view); struct border view_border(struct view *view);
void view_move_resize(struct view *view, struct wlr_box geo); void view_move_resize(struct view *view, struct wlr_box geo);

View file

@ -81,7 +81,7 @@ process_cursor_move(struct server *server, uint32_t time)
/* Move the grabbed view to the new position. */ /* Move the grabbed view to the new position. */
dx += server->grab_box.x; dx += server->grab_box.x;
dy += server->grab_box.y; dy += server->grab_box.y;
move_resistance(view, &dx, &dy, true); resistance_move_apply(view, &dx, &dy, true);
view_move(view, dx, dy); view_move(view, dx, dy);
} }
@ -125,7 +125,7 @@ process_cursor_resize(struct server *server, uint32_t time)
new_view_geo.width = server->grab_box.width + dx; new_view_geo.width = server->grab_box.width + dx;
} }
resize_resistance(view, &new_view_geo, true); resistance_resize_apply(view, &new_view_geo, true);
view_move_resize(view, new_view_geo); view_move_resize(view, new_view_geo);
} }

View file

@ -1,27 +1,55 @@
#include "labwc.h" #include "labwc.h"
#include "config/rcxml.h" #include "config/rcxml.h"
/* These functions could be extended to strength in the future. */ struct edges {
int left;
int top;
int right;
int bottom;
};
static void
is_within_resistance_range(struct edges view, struct edges target,
struct edges other, struct edges *flags, int strength)
{
if (view.left >= other.left && target.left < other.left
&& target.left >= other.left - strength) {
flags->left = 1;
} else if (view.right <= other.right && target.right > other.right
&& target.right <= other.right + strength) {
flags->right = 1;
}
if (view.top >= other.top && target.top < other.top
&& target.top >= other.top - strength) {
flags->top = 1;
} else if (view.bottom <= other.bottom && target.bottom > other.bottom
&& target.bottom <= other.bottom + strength) {
flags->bottom = 1;
}
}
void void
move_resistance(struct view *view, double *x, double *y, bool screen_edge) resistance_move_apply(struct view *view, double *x, double *y, bool screen_edge)
{ {
struct server *server = view->server; struct server *server = view->server;
struct wlr_box mgeom; struct wlr_box mgeom;
struct output *output; struct output *output;
struct border border = view_border(view); struct border border = view_border(view);
int l, r, t, b; /* The edges of the current view */ struct edges view_edges; /* The edges of the current view */
int tl, tr, tt, tb; /* The desired edges */ struct edges target_edges; /* The desired edges */
int ml, mr, mt, mb; /* The edges of the monitor/other view */ struct edges other_edges; /* The edges of the monitor/other view */
struct edges flags; /* To be set in is_within_resistance_range() */
l = view->x - border.left - rc.gap; view_edges.left = view->x - border.left - rc.gap;
t = view->y - border.top - rc.gap; view_edges.top = view->y - border.top - rc.gap;
r = view->x + view->w + border.right + rc.gap; view_edges.right = view->x + view->w + border.right + rc.gap;
b = view->y + view->h + border.bottom + rc.gap; view_edges.bottom = view->y + view->h + border.bottom + rc.gap;
tl = *x - border.left - rc.gap; target_edges.left = *x - border.left - rc.gap;
tt = *y - border.top - rc.gap; target_edges.top = *y - border.top - rc.gap;
tr = *x + view->w + border.right + rc.gap; target_edges.right = *x + view->w + border.right + rc.gap;
tb = *y + view->h + border.bottom + rc.gap; target_edges.bottom = *y + view->h + border.bottom + rc.gap;
if (screen_edge) { if (screen_edge) {
if (!rc.screen_edge_strength) { if (!rc.screen_edge_strength) {
@ -31,51 +59,58 @@ move_resistance(struct view *view, double *x, double *y, bool screen_edge)
wl_list_for_each(output, &server->outputs, link) { wl_list_for_each(output, &server->outputs, link) {
mgeom = output_usable_area_in_layout_coords(output); mgeom = output_usable_area_in_layout_coords(output);
ml = mgeom.x; other_edges.left = mgeom.x;
mt = mgeom.y; other_edges.top = mgeom.y;
mr = mgeom.x + mgeom.width; other_edges.right = mgeom.x + mgeom.width;
mb = mgeom.y + mgeom.height; other_edges.bottom = mgeom.y + mgeom.height;
if (l >= ml && tl < ml && tl >= ml is_within_resistance_range(view_edges, target_edges,
- rc.screen_edge_strength) { other_edges, &flags, rc.screen_edge_strength);
*x = ml + border.left + rc.gap;
} else if (r <= mr && tr > mr && tr <= mr if (flags.left == 1) {
+ rc.screen_edge_strength) { *x = other_edges.left + border.left + rc.gap;
*x = mr - view->w - border.right - rc.gap; } else if (flags.right == 1) {
*x = other_edges.right - view->w - border.right
- rc.gap;
} }
if (t >= mt && tt < mt && tt >= mt if (flags.top == 1) {
- rc.screen_edge_strength) { *y = other_edges.top + border.top + rc.gap;
*y = mt + border.top + rc.gap; } else if (flags.bottom == 1) {
} else if (b <= mb && tb > mb && tb <= mb *y = other_edges.bottom - view->h
+ rc.screen_edge_strength) { - border.bottom - rc.gap;
*y = mb - view->h - border.bottom - rc.gap;
} }
/* reset the flags */
flags.left = flags.top = flags.right = flags.bottom = 0;
} }
} }
} }
void void
resize_resistance(struct view *view, struct wlr_box *new_view_geo, resistance_resize_apply(struct view *view, struct wlr_box *new_view_geo,
bool screen_edge) bool screen_edge)
{ {
struct server *server = view->server; struct server *server = view->server;
struct output *output; struct output *output;
struct wlr_box mgeom; struct wlr_box mgeom;
struct border border = view_border(view); struct border border = view_border(view);
int l, r, t, b; /* The edges of the current view */ struct edges view_edges; /* The edges of the current view */
int tl, tr, tt, tb; /* The desired edges */ struct edges target_edges; /* The desired edges */
int ml, mr, mt, mb; /* The edges of the monitor/other view */ struct edges other_edges; /* The edges of the monitor/other view */
struct edges flags; /* To be set in is_within_resistance_range() */
l = view->x - border.left - rc.gap; view_edges.left = view->x - border.left - rc.gap;
t = view->y - border.top - rc.gap; view_edges.top = view->y - border.top - rc.gap;
r = view->x + view->w + border.right + rc.gap; view_edges.right = view->x + view->w + border.right + rc.gap;
b = view->y + view->h + border.bottom + rc.gap; view_edges.bottom = view->y + view->h + border.bottom + rc.gap;
tl = new_view_geo->x - border.left - rc.gap; target_edges.left = new_view_geo->x - border.left - rc.gap;
tt = new_view_geo->y - border.top - rc.gap; target_edges.top = new_view_geo->y - border.top - rc.gap;
tr = new_view_geo->x + new_view_geo->width + border.right + rc.gap; target_edges.right = new_view_geo->x + new_view_geo->width
tb = new_view_geo->y + new_view_geo->height + border.bottom + rc.gap; + border.right + rc.gap;
target_edges.bottom = new_view_geo->y + new_view_geo->height
+ border.bottom + rc.gap;
if (screen_edge) { if (screen_edge) {
if (!rc.screen_edge_strength) { if (!rc.screen_edge_strength) {
@ -83,39 +118,40 @@ resize_resistance(struct view *view, struct wlr_box *new_view_geo,
} }
wl_list_for_each(output, &server->outputs, link) { wl_list_for_each(output, &server->outputs, link) {
mgeom = output_usable_area_in_layout_coords(output); mgeom = output_usable_area_in_layout_coords(output);
ml = mgeom.x; other_edges.left = mgeom.x;
mt = mgeom.y; other_edges.top = mgeom.y;
mr = mgeom.x + mgeom.width; other_edges.right = mgeom.x + mgeom.width;
mb = mgeom.y + mgeom.height; other_edges.bottom = mgeom.y + mgeom.height;
is_within_resistance_range(view_edges, target_edges,
other_edges, &flags, rc.screen_edge_strength);
if (server->resize_edges & WLR_EDGE_LEFT) { if (server->resize_edges & WLR_EDGE_LEFT) {
if (l >= ml && tl < ml && tl >= ml if (flags.left == 1) {
- rc.screen_edge_strength) { new_view_geo->x = other_edges.left
new_view_geo->x = ml + border.left + border.left + rc.gap;
+ rc.gap;
new_view_geo->width = view->w; new_view_geo->width = view->w;
} }
} else if (server->resize_edges & WLR_EDGE_RIGHT) { } else if (server->resize_edges & WLR_EDGE_RIGHT) {
if (r <= mr && tr > mr && tr <= mr if (flags.right == 1) {
+ rc.screen_edge_strength) { new_view_geo->width = other_edges.right
new_view_geo->width = mr - l - view_edges.left
- (border.right + rc.gap) * 2 ; - (border.right + rc.gap) * 2 ;
} }
} }
if (server->resize_edges & WLR_EDGE_TOP) { if (server->resize_edges & WLR_EDGE_TOP) {
if (t >= mt && tt < mt && tt >= mt if (flags.top == 1) {
- rc.screen_edge_strength) { new_view_geo->y = other_edges.top
new_view_geo->y = mt + border.top + border.top + rc.gap;
+ rc.gap;
new_view_geo->height = view->h; new_view_geo->height = view->h;
} }
} else if (server->resize_edges & WLR_EDGE_BOTTOM) { } else if (server->resize_edges & WLR_EDGE_BOTTOM) {
if (b <= mb && tb > mb && tb <= mb if (flags.bottom == 1) {
+ rc.screen_edge_strength) { new_view_geo->height =
new_view_geo->height = mb - t other_edges.bottom
- border.bottom - border.top - view_edges.top - border.bottom
- rc.gap * 2; - border.top - rc.gap * 2;
} }
} }
} }