|
|
@@ -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;
|
|
|
}
|