From 4181bb5335f4c3be28e45080151b01bd2a0ef199 Mon Sep 17 00:00:00 2001 From: "Andrew J. Hesford" Date: Sun, 21 Jan 2024 12:56:37 -0500 Subject: [PATCH] resistance: support negative strengths to indicate attractive snapping --- docs/labwc-config.5.scd | 27 +++++++++++++++++++-------- src/resistance.c | 36 ++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/docs/labwc-config.5.scd b/docs/labwc-config.5.scd index a6855425..f1f996d3 100644 --- a/docs/labwc-config.5.scd +++ b/docs/labwc-config.5.scd @@ -196,15 +196,26 @@ this is for compatibility with Openbox. ## RESISTANCE -** - Screen-edge strength is the distance, in pixels, past the edge of a - screen the cursor must move before an interactive move or resize of a - window will continue past the edge. Default is 20 pixels. - +**++ ** - Window-edge strength is the distance, in pixels, past the edge of any - other window the cursor must move before an interactive move or resize - of a window will continue past the edge. Default is 20 pixels. + Resist interactive moves and resizes of a window across screen edges or + the edges of any other window, respectively. + + When an edge strength is positive, it indicates a distance, in pixels, + that the cursor must move past any relevant encountered edge before an + interactive move or resize operation will continue across that edge. + + When the strength is negative, any interactive move or resize operation + that brings the cursor within the absolute value of the specified + distance, in pixels, from any relevant edge will snap the operation to + that edge. Thus, as a move or resize approaches an edge, it will + "attract" the cursor to that edge within the specified distance. As the + move or resize continues past the edge, it will provide resistance until + the cursor has moved beyond the distance. + + A strength of zero disables the corresponding resistance effect. + + The default value for both parameters is 20 pixels. ## FOCUS diff --git a/src/resistance.c b/src/resistance.c index 7fe3f3b5..acb1dfe4 100644 --- a/src/resistance.c +++ b/src/resistance.c @@ -12,20 +12,28 @@ static void is_within_resistance_range(struct border view, struct border target, struct border other, struct border *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.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 (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; + 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; } } @@ -75,7 +83,7 @@ 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) { + if (rc.window_edge_strength == 0) { return; } @@ -118,7 +126,7 @@ static void find_screen_edges(struct view *view, struct wlr_box new_geom, struct border *next_edges, bool move) { - if (rc.screen_edge_strength <= 0) { + if (rc.screen_edge_strength == 0) { return; } -- 2.52.0