Browse Source

Snap windows to screen edges and resist adjacent windows

Dropping a title-bar drag on the left or right screen edge snaps the
window to that half of the work area at full height; dropping it at the
top edge maximizes it.  The restore geometry is saved only when leaving
the normal state, so snap -> maximize -> restore returns to the original,
and dragging a snapped or maximized window restores it first while
keeping the grab point at the same fraction of the title bar.

While dragging, the screen edges and the facing edges of windows that
overlap on the other axis resist the window: it stays flush until the
pointer overshoots by WM_RESIST (24 px), which makes exact side-by-side
placement easy.

AGENTS.md and docs/graphics.html updated.  Ignore the generated mkrtwtbl
host tool.
Johannes Zellner 1 week ago
parent
commit
d57c67b1e4
4 changed files with 197 additions and 36 deletions
  1. 1 0
      .gitignore
  2. 13 4
      AGENTS.md
  3. 12 3
      docs/graphics.html
  4. 171 29
      ucmd_wm.c

+ 1 - 0
.gitignore

@@ -27,6 +27,7 @@ js_test
 third_party/quickjs/*.o
 js_mu.o
 mkrootfs
+mkrtwtbl
 ujson_test
 uhttp_cookie_test
 chat_login_test

+ 13 - 4
AGENTS.md

@@ -352,10 +352,19 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   dragged past the left, right and bottom edges while at least `WM_DRAG_VISIBLE`
   (48 px) of the title bar stays on-screen; the top bar is never overlapped.
   Double-clicking the title bar toggles maximize/restore against the work area
-  below the bar (`0, WM_BAR_H, fbw, fbh - WM_BAR_H`); dragging a maximized
-  window restores it first, and the resize grip grows a window to the full
-  work area (snapping when dragged into the screen corner).  The kernel hit-test
-  uses the same order.
+  below the bar (`0, WM_BAR_H, fbw, fbh - WM_BAR_H`).  Dropping a title-bar
+  drag with the pointer on the left or right screen edge (`WM_SNAP_EDGE`, 2 px)
+  snaps the window to that half of the work area at full height; on the top
+  edge it maximizes.  `struct uwin.state` is `WIN_NORMAL`/`WIN_MAXIMIZED`/
+  `WIN_SNAP_LEFT`/`WIN_SNAP_RIGHT`, and the restore geometry is saved only
+  when leaving the normal state, so snap -> maximize -> restore returns to
+  the original.  Dragging a maximized or snapped window restores it first,
+  keeping the grab point at the same fraction of the title bar.  While
+  dragging, screen edges and the facing edges of windows that overlap on the
+  other axis resist the window: it stays flush until the pointer overshoots
+  by `WM_RESIST` (24 px) (`resist_geom()` in `ucmd_wm.c`).  The resize grip
+  grows a window to the full work area (snapping when dragged into the screen
+  corner).  The kernel hit-test uses the same order.
   The right-aligned date/time text is itself a click target: `draw_bar()`
   records its rectangle, the pointer highlights it, and a click spawns
   `/ramfs/bin/settime` (the `Clock` launcher still opens the analog clock).

+ 12 - 3
docs/graphics.html

@@ -282,9 +282,18 @@
 		<p>
 			Double-clicking a title bar maximizes the window to the work area
 			below the system bar (<code>0, WM_BAR_H, fbw, fbh - WM_BAR_H</code>);
-			the next double-click restores the saved geometry. Dragging a
-			maximized window restores it first, and a manual resize clears the
-			maximized state. Windows can be dragged past the left, right and
+			the next double-click restores the saved geometry. Dropping a
+			title-bar drag with the pointer on the left or right screen edge
+			(<code>WM_SNAP_EDGE</code>, 2 px) snaps the window to that half of
+			the work area at full height, and the top edge maximizes it. The
+			restore geometry is saved only when a window leaves the normal state,
+			so snapping and then maximizing still restores the original size.
+			Dragging a maximized or snapped window restores it first, with the
+			grab point kept at the same fraction of the title bar, and a manual
+			resize clears the state. While a window is dragged, the screen edges
+			and the facing edges of other windows resist it: the window stays
+			flush until the pointer overshoots by <code>WM_RESIST</code> (24 px),
+			which makes side-by-side placement exact. Windows can be dragged past the left, right and
 			bottom edges; <code>WM_DRAG_VISIBLE</code> (48 px) of the title bar
 			always stays on-screen for recovery, and the top bar is never
 			overlapped. The resize grip is clamped to the full work area and

+ 171 - 29
ucmd_wm.c

@@ -20,8 +20,9 @@ struct uwin {
 	struct wm_map_info map;
 	int mapped;
 	unsigned int ack_seq;
-	/* Double-click title bar: the geometry to return to when maximized. */
-	int maximized;
+	/* WIN_NORMAL, or maximized/snapped to a screen half; restore_* is the
+	 * geometry to return to when the window leaves that state. */
+	int state;
 	int restore_x, restore_y;
 	unsigned int restore_w, restore_h;
 };
@@ -33,12 +34,28 @@ struct uwin {
  * so a plain click on its title bar leaves it maximized. */
 #define WM_DRAG_START    3
 
+/* Window placement states.  Maximize fills the work area below the bar; the
+ * snap states fill its left or right half at full height. */
+#define WIN_NORMAL     0
+#define WIN_MAXIMIZED  1
+#define WIN_SNAP_LEFT  2
+#define WIN_SNAP_RIGHT 3
+
+/* Releasing a title-bar drag with the pointer this close to the left/right
+ * screen edge snaps the window to that half; at the top edge it maximizes. */
+#define WM_SNAP_EDGE     2
+/* Edge resistance: a dragged window's edge stops at a screen edge or at the
+ * facing edge of another window until the pointer overshoots it by this
+ * many pixels, which makes exact side-by-side placement easy. */
+#define WM_RESIST        24
+
 static struct usurf screen;
 static struct uwin wins[WM_MAX_WINDOWS];
 static unsigned int fbw, fbh;
 static int drag_id = -1;
 static int drag_dx, drag_dy;
 static int drag_mx, drag_my;       /* press position, for restore-on-drag */
+static int drag_moved;             /* the pointer passed WM_DRAG_START */
 static int resize_id = -1;
 static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
 static unsigned int last_title_ms;
@@ -1117,30 +1134,126 @@ static int set_window_geometry(struct uwin *u, unsigned int idx,
 
 static int restore_window(struct uwin *u, unsigned int idx)
 {
-	if (!u->maximized)
+	if (u->state == WIN_NORMAL)
 		return 0;
 	if (set_window_geometry(u, idx, u->restore_x, u->restore_y,
 				u->restore_w, u->restore_h) != 0)
 		return -1;
-	u->maximized = 0;
+	u->state = WIN_NORMAL;
+	return 0;
+}
+
+/* Maximize or snap a window to a half of the work area below the system
+ * bar.  The restore geometry is recorded only when the window leaves the
+ * normal state, so maximize -> snap -> restore returns to the original. */
+static int place_window(struct uwin *u, unsigned int idx, int state)
+{
+	unsigned int half = fbw / 2;
+	unsigned int h = fbh - WM_BAR_H;
+	int x = 0;
+	unsigned int w = fbw;
+
+	if (state == u->state)
+		return 0;
+	if (state == WIN_SNAP_LEFT) {
+		w = half;
+	} else if (state == WIN_SNAP_RIGHT) {
+		x = (int)half;
+		w = fbw - half;
+	}
+	if (u->state == WIN_NORMAL) {
+		u->restore_x = u->info.x;
+		u->restore_y = u->info.y;
+		u->restore_w = u->info.w;
+		u->restore_h = u->info.h;
+	}
+	if (set_window_geometry(u, idx, x, WM_BAR_H, w, h) != 0)
+		return -1;
+	u->state = state;
 	return 0;
 }
 
 /* Double-click title bar: fill the work area below the system bar, or go back
- * to the geometry the window had before it was maximized. */
+ * to the geometry the window had before it was maximized.  A snapped window
+ * maximizes (keeping its pre-snap restore geometry). */
 static int toggle_maximize(struct uwin *u, unsigned int idx)
 {
-	if (u->maximized)
+	if (u->state == WIN_MAXIMIZED)
 		return restore_window(u, idx);
-	u->restore_x = u->info.x;
-	u->restore_y = u->info.y;
-	u->restore_w = u->info.w;
-	u->restore_h = u->info.h;
-	if (set_window_geometry(u, idx, 0, WM_BAR_H,
-				fbw, fbh - WM_BAR_H) != 0)
-		return -1;
-	u->maximized = 1;
-	return 0;
+	return place_window(u, idx, WIN_MAXIMIZED);
+}
+
+/* One axis of edge resistance.  lo/len is the window's span on that axis
+ * before the move, *pos the proposed start.  dir < 0: the window's start side
+ * (left/top) resists edge e while moving towards lower coordinates; dir > 0:
+ * its end side (right/bottom) while moving higher.  Within WM_RESIST pixels
+ * of overshoot the window stays on the edge; *best keeps the nearest edge. */
+static void resist_edge(int lo, int len, int *pos, int e, int dir, int *best)
+{
+	int over;
+
+	if (dir < 0 && *pos < lo && lo >= e && *pos < e) {
+		over = e - *pos;
+		if (over <= WM_RESIST && over < *best) {
+			*best = over;
+			*pos = e;
+		}
+	} else if (dir > 0 && *pos > lo && lo + len <= e && *pos + len > e) {
+		over = *pos + len - e;
+		if (over <= WM_RESIST && over < *best) {
+			*best = over;
+			*pos = e - len;
+		}
+	}
+}
+
+/* Span overlap test: edges of another window only resist when the two
+ * windows share some extent on the other axis (they would actually touch). */
+static int spans_overlap(int a, int alen, int b, int blen)
+{
+	return a < b + blen && b < a + alen;
+}
+
+/* Apply edge resistance to a drag from (u->info.x, u->info.y) to (*nx, *ny):
+ * screen edges and the facing edges of every other window.  The proposed
+ * position is still pure pointer arithmetic, so once the overshoot passes
+ * WM_RESIST the window jumps to follow the pointer again. */
+static void resist_geom(struct uwin *u, int idx, int *nx, int *ny)
+{
+	int ox = u->info.x, oy = u->info.y;
+	int w = (int)u->info.w, h = (int)u->info.h;
+	int px = *nx, py = *ny;
+	int bestx = WM_RESIST + 1, besty = WM_RESIST + 1;
+	int i;
+
+	resist_edge(ox, w, &px, 0, -1, &bestx);
+	resist_edge(ox, w, &px, (int)fbw, 1, &bestx);
+	resist_edge(oy, h, &py, (int)fbh, 1, &besty);
+	for (i = 0; i < WM_MAX_WINDOWS; i++) {
+		struct wm_window_info *o = &wins[i].info;
+		int t;
+
+		if (i == idx || !o->used)
+			continue;
+		if (spans_overlap(oy, h, o->y, (int)o->h)) {
+			/* our left side against its right, our right
+			 * side against its left */
+			t = *nx;
+			resist_edge(ox, w, &t, o->x + (int)o->w, -1, &bestx);
+			resist_edge(ox, w, &t, o->x, 1, &bestx);
+			if (t != *nx)
+				px = t;
+		}
+		if (spans_overlap(ox, w, o->x, (int)o->w)) {
+			t = *ny;
+			resist_edge(oy, h, &t, o->y + (int)o->h, -1, &besty);
+			resist_edge(oy, h, &t, o->y, 1, &besty);
+			if (t != *ny)
+				py = t;
+		}
+	}
+	*nx = px;
+	*ny = py;
 }
 
 /* The top-bar date/time text is also a button: it opens the Date & Time
@@ -1704,6 +1817,7 @@ static void handle_mouse(struct wm_status *st)
 						drag_my = pmy;
 						drag_dx = pmx - u->info.x;
 						drag_dy = pmy - u->info.y;
+						drag_moved = 0;
 					}
 				}
 			}
@@ -1733,7 +1847,7 @@ static void handle_mouse(struct wm_status *st)
 					(unsigned int)resize_id, u->info.x,
 					u->info.y, (unsigned int)nw,
 					(unsigned int)nh) == 0)
-					u->maximized = 0;
+					u->state = WIN_NORMAL;
 			}
 		} else {
 			resize_id = -1;
@@ -1743,25 +1857,36 @@ static void handle_mouse(struct wm_status *st)
 
 		if (u->info.used) {
 			int ox, oy, nx, ny;
-			int started = !u->maximized ||
-				iabs((int)mx - drag_mx) >= WM_DRAG_START ||
-				iabs((int)my - drag_my) >= WM_DRAG_START;
-
-			/* A maximized window is restored as soon as the drag
-			 * starts; keep the pointer's grab point relative to the
-			 * restored geometry.  A click that does not move must
-			 * leave it maximized. */
-			if (u->maximized && started &&
-			    restore_window(u, (unsigned int)drag_id) == 0) {
-				drag_dx = drag_mx - u->info.x;
-				drag_dy = drag_my - u->info.y;
+			if (iabs((int)mx - drag_mx) >= WM_DRAG_START ||
+			    iabs((int)my - drag_my) >= WM_DRAG_START)
+				drag_moved = 1;
+
+			/* A maximized or snapped window is restored as soon as
+			 * the drag starts.  Keep the grab point at the same
+			 * fraction of the title bar, so the restored (narrower)
+			 * window stays under the pointer.  A click that does
+			 * not move must leave it in place. */
+			if (u->state != WIN_NORMAL && drag_moved) {
+				unsigned int ow = u->info.w;
+				int gx = drag_mx - u->info.x;
+				int gy = drag_my - u->info.y;
+
+				if (restore_window(u,
+				    (unsigned int)drag_id) == 0) {
+					if (ow)
+						gx = gx * (int)u->info.w /
+						     (int)ow;
+					drag_dx = gx;
+					drag_dy = gy;
+				}
 			}
-			if (!u->maximized) {
+			if (u->state == WIN_NORMAL) {
 				ox = u->info.x;
 				oy = u->info.y;
 				nx = (int)mx - drag_dx;
 				ny = (int)my - drag_dy;
 
+				resist_geom(u, drag_id, &nx, &ny);
 				clamp_geom(u, &nx, &ny);
 				if (nx != ox || ny != oy) {
 					struct wm_present_info present;
@@ -1799,6 +1924,23 @@ static void handle_mouse(struct wm_status *st)
 			drag_id = -1;
 		}
 	} else if (!down && was) {
+		/* Dropping a window with the pointer on the left/right screen
+		 * edge snaps it to that half; on the top edge it maximizes. */
+		if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&
+		    wins[drag_id].state == WIN_NORMAL) {
+			struct uwin *u = &wins[drag_id];
+			int st = WIN_NORMAL;
+
+			if ((int)mx < WM_SNAP_EDGE)
+				st = WIN_SNAP_LEFT;
+			else if ((int)mx >= (int)fbw - WM_SNAP_EDGE)
+				st = WIN_SNAP_RIGHT;
+			else if ((int)my < WM_SNAP_EDGE)
+				st = WIN_MAXIMIZED;
+			if (st != WIN_NORMAL &&
+			    place_window(u, (unsigned int)drag_id, st) != 0)
+				bar_notice("resize failed");
+		}
 		drag_id = -1;
 		resize_id = -1;
 	}