Explorar o código

wm: soft drop shadow for normal windows

Johannes Zellner hai 12 horas
pai
achega
1123caf1a4
Modificáronse 5 ficheiros con 338 adicións e 46 borrados
  1. 10 1
      AGENTS.md
  2. 10 1
      docs/graphics.html
  3. 192 44
      user/apps/wm/ucmd_wm.c
  4. 119 0
      user/lib/uwm/uwm_lib.c
  5. 7 0
      user/lib/uwm/uwm_lib.h

+ 10 - 1
AGENTS.md

@@ -551,7 +551,16 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   edge, and the close control is the Adwaita `window-close` icon in the top
   right (3 px from the edge, no red button fill); it is dimmed while idle,
   turns white while the pointer is over it and shows the hand cursor.  A
-  hover change repaints only the two affected button rectangles.  A resize
+  hover change repaints only the two affected button rectangles.  Every
+  normal window casts a soft 10 px (`WM_SHADOW_SPREAD`) drop shadow through
+  `usurf_window_shadow()` (`uwm_lib.c`): the ring darkens with a rounded
+  distance falloff, so its corners fade in a curve.  Shadows are painted
+  after the windows, back to front, so a front
+  window's shadow falls on the windows behind it and is clipped by every
+  window in front; popovers stay flat.  Geometry damage expands by the
+  spread, and a focused drag copies the window block but repaints the old
+  and new ring strips with `draw_rect()` because the ring's background is
+  stationary and must not be block-copied.  A resize
   band (`WM_RESIZE_IN` 1 px inside,
   `WM_RESIZE_OUT` 4 px outside every outer edge) is the edge/corner resize
   grab area; corner squares reach `WM_CORNER_IN` (12 px) along both edges.

+ 10 - 1
docs/graphics.html

@@ -161,7 +161,16 @@
 			inset 8 px from the left; the close control is the Adwaita
 			<code>window-close</code> symbol at the right edge with no red
 			button fill, dimmed while idle and brightened on hover, and it
-			shows the hand cursor. The desktop starts as a flat colour;
+			shows the hand cursor. Every normal window also casts a 10 px
+			soft drop shadow: <code>usurf_window_shadow()</code> darkens
+			the ring outside the window edge with a rounded distance
+			falloff, so the corners fade in a curve.
+			Shadows are painted after the windows, back to front, so a
+			front window's shadow falls on the windows behind it and is
+			clipped by every window in front; popovers stay flat. Geometry
+			damage grows by the shadow spread, while a title-bar drag
+			repaints only the old and new ring strips around the copied
+			window block. The desktop starts as a flat colour;
 			<code>view</code>'s
 			<em>Background</em> button stores a pre-scaled RGB565 file
 			(<code>/data/WALLPAPR.BIN</code> on an installed board, else

+ 192 - 44
user/apps/wm/ucmd_wm.c

@@ -25,6 +25,10 @@
 #define WM_CLOSE_INSET    3
 #define WM_CLOSE_ICON_PX  16
 
+/* Soft drop shadow around normal windows: this many pixels outside every
+ * edge.  Geometry damage expands by it and the shadow pass paints the ring. */
+#define WM_SHADOW_SPREAD  10
+
 struct uwin {
 	struct wm_window_info info;
 	struct wm_map_info map;
@@ -965,6 +969,83 @@ static unsigned int rect_sub(struct orect *out, const struct orect *r,
 	return n;
 }
 
+/* cur = cur minus cut, using nxt as scratch.  Returns the new rectangle
+ * count; 0 also means the region is empty or the scratch list overflowed. */
+static unsigned int region_sub(struct orect *cur, unsigned int ncur,
+			       struct orect *nxt, const struct orect *cut)
+{
+	unsigned int k, n = 0;
+
+	for (k = 0; k < ncur; k++) {
+		if (n + 4 > OCC_MAX)
+			return 0;
+		n += rect_sub(&nxt[n], &cur[k], cut);
+	}
+	for (k = 0; k < n; k++)
+		cur[k] = nxt[k];
+	return n;
+}
+
+/* Soft shadows are painted after the windows, back to front, so a front
+ * window's shadow falls on the windows behind it while every window and
+ * every window in front of it occludes that shadow.  Only the damage
+ * rectangle is touched. */
+static void draw_shadows(const struct orect *dmg)
+{
+	static struct orect cur[OCC_MAX], nxt[OCC_MAX];
+	int order[WM_MAX_WINDOWS];
+	unsigned int n = build_order(order), i, j, ncur;
+
+	for (i = n; i-- > 0;) {
+		struct uwin *u = &wins[order[i]];
+		struct orect own, fr;
+		int x0, y0, x1, y1;
+
+		if (!u->info.used || u->info.popover)
+			continue;
+		x0 = u->info.x - WM_SHADOW_SPREAD;
+		y0 = u->info.y - WM_SHADOW_SPREAD;
+		x1 = u->info.x + (int)u->info.w + WM_SHADOW_SPREAD;
+		y1 = u->info.y + (int)u->info.h + WM_SHADOW_SPREAD;
+		if (x0 < dmg->x) x0 = dmg->x;
+		if (y0 < dmg->y) y0 = dmg->y;
+		if (x1 > dmg->x + (int)dmg->w) x1 = dmg->x + (int)dmg->w;
+		if (y1 > dmg->y + (int)dmg->h) y1 = dmg->y + (int)dmg->h;
+		if (y0 < WM_BAR_H) y0 = WM_BAR_H;
+		if (x1 <= x0 || y1 <= y0)
+			continue;
+		cur[0].x = x0;
+		cur[0].y = y0;
+		cur[0].w = (unsigned int)(x1 - x0);
+		cur[0].h = (unsigned int)(y1 - y0);
+		ncur = 1;
+		own.x = u->info.x;
+		own.y = u->info.y;
+		own.w = u->info.w;
+		own.h = u->info.h;
+		ncur = region_sub(cur, ncur, nxt, &own);
+		for (j = 0; j < i && ncur; j++) {
+			struct uwin *f = &wins[order[j]];
+
+			if (!f->info.used)
+				continue;
+			fr.x = f->info.x;
+			fr.y = f->info.y;
+			fr.w = f->info.w;
+			fr.h = f->info.h;
+			ncur = region_sub(cur, ncur, nxt, &fr);
+		}
+		if (!ncur)
+			continue;
+		for (j = 0; j < ncur; j++)
+			usurf_window_shadow(&screen, u->info.x, u->info.y,
+					    (int)u->info.w, (int)u->info.h,
+					    WM_SHADOW_SPREAD,
+					    cur[j].x, cur[j].y,
+					    cur[j].w, cur[j].h);
+	}
+}
+
 static void wall_build_maps(void)
 {
 	unsigned int i;
@@ -1130,6 +1211,15 @@ static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
 		    y < wins[i].info.y + (int)wins[i].info.h &&
 		    y + (int)h > wins[i].info.y)
 			draw_window_clip(i, x, y, w, h);
+	{
+		struct orect dmg;
+
+		dmg.x = x;
+		dmg.y = y;
+		dmg.w = w;
+		dmg.h = h;
+		draw_shadows(&dmg);
+	}
 }
 
 static void draw_rect(int x, int y, unsigned int w, unsigned int h)
@@ -1201,8 +1291,11 @@ static void draw_rect(int x, int y, unsigned int w, unsigned int h)
 			draw_window_clip((unsigned int)order[i], cur[j].x,
 					 cur[j].y, cur[j].w, cur[j].h);
 	}
-	if (overflow)
+	if (overflow) {
 		draw_rect_naive(dmg.x, dmg.y, dmg.w, dmg.h);
+		return;
+	}
+	draw_shadows(&dmg);
 }
 
 /* Invisible resize border: edge bits for a point in the band around the
@@ -1376,6 +1469,26 @@ static void add_rect(struct wm_present_info *p, int x, int y,
 	}
 }
 
+/* The rectangle a window occupies including its drop shadow, clipped below
+ * the top bar. */
+static void window_shadow_rect(struct orect *r, int x, int y,
+			       unsigned int w, unsigned int h)
+{
+	r->x = x - WM_SHADOW_SPREAD;
+	r->y = y - WM_SHADOW_SPREAD;
+	r->w = w + 2 * WM_SHADOW_SPREAD;
+	r->h = h + 2 * WM_SHADOW_SPREAD;
+	if (r->y < WM_BAR_H) {
+		unsigned int cut = (unsigned int)(WM_BAR_H - r->y);
+
+		if (cut >= r->h)
+			r->h = 0;
+		else
+			r->h -= cut;
+		r->y = WM_BAR_H;
+	}
+}
+
 /* Damage rectangle covering one window's close button, for a hover change. */
 static void close_add_damage(struct wm_present_info *p, int idx)
 {
@@ -1509,16 +1622,18 @@ static int shadow_overlay_covers(int x, int y, int w, int h)
 	return 0;
 }
 
-/* A focused window is topmost, so the shadow contains exactly its pixels in
- * the old rectangle, unless the dock or the brightness OSD covers part of
- * it.  Move that block and repaint only the old part the new rectangle does
- * not cover.  When either rectangle is partly off-screen, or an overlay hid
- * part of the source, the shadow has no valid pixels there, so repaint the
- * new rect instead of copying overlay pixels into the window. */
+/* A focused window is topmost, so the screen shadow contains exactly its
+ * pixels in the old rectangle, unless the dock or the brightness OSD covers
+ * part of it.  Copy that block, repaint the old ring the new window does not
+ * cover, and repaint the new shadow ring (its background is stationary and
+ * may differ from the old position, so the ring is never block-copied).
+ * When either window rectangle is partly off-screen, or an overlay hid part
+ * of the source, repaint the whole new shadow rect instead of copying
+ * overlay pixels into the window. */
 static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
 {
 	struct wm_present_info present;
-	struct orect oldr, newr, pieces[4];
+	struct orect old_sh, new_sh, new_win, pieces[4];
 	unsigned int np, k;
 	int w = (int)u->info.w, h = (int)u->info.h;
 	int old_on = ox >= 0 && oy >= 0 &&
@@ -1527,34 +1642,47 @@ static void move_window_pixels(struct uwin *u, int ox, int oy, int nx, int ny)
 	int new_on = nx >= 0 && ny >= 0 &&
 		     nx + w <= (int)fbw && ny + h <= (int)fbh;
 
+	window_shadow_rect(&old_sh, ox, oy, u->info.w, u->info.h);
+	window_shadow_rect(&new_sh, nx, ny, u->info.w, u->info.h);
+	new_win.x = nx;
+	new_win.y = ny;
+	new_win.w = u->info.w;
+	new_win.h = u->info.h;
 	if (u_wm_begin() != 0)
 		return;
 	remap_window_contents();
 	if (old_on && new_on)
 		usurf_copy(&screen, nx, ny, ox, oy, u->info.w, u->info.h);
-	else
-		draw_rect(nx, ny, u->info.w, u->info.h);
-	oldr.x = ox;
-	oldr.y = oy;
-	oldr.w = u->info.w;
-	oldr.h = u->info.h;
-	newr.x = nx;
-	newr.y = ny;
-	newr.w = u->info.w;
-	newr.h = u->info.h;
-	np = rect_sub(pieces, &oldr, &newr);
+	/* Everything the old shadow rect covered outside the new window and
+	 * its shadow.  The new ring is repainted below, so cutting it out here
+	 * also keeps its falloff from being darkened twice. */
+	np = rect_sub(pieces, &old_sh, &new_sh);
 	for (k = 0; k < np; k++)
 		draw_rect(pieces[k].x, pieces[k].y, pieces[k].w, pieces[k].h);
+	if (old_on && new_on) {
+		/* Only the ring around the new position; its interior is the
+		 * copied window block. */
+		np = rect_sub(pieces, &new_sh, &new_win);
+		for (k = 0; k < np; k++)
+			draw_rect(pieces[k].x, pieces[k].y,
+				  pieces[k].w, pieces[k].h);
+	} else {
+		draw_rect(new_sh.x, new_sh.y, new_sh.w, new_sh.h);
+	}
 	present.count = 0;
 	present.immediate = 1;
-	/* Present the union of the old and new rectangles as one rect.
-	 * The shadow is correct for all of it, and one rect cannot be cut
-	 * off by the kernel's four-entry present list. */
+	/* Present the union of the old and new shadow rectangles as one
+	 * rect.  The screen shadow is correct for all of it, and one rect
+	 * cannot be cut off by the kernel's four-entry present list. */
 	{
-		int ux0 = ox < nx ? ox : nx;
-		int uy0 = oy < ny ? oy : ny;
-		int ux1 = (ox > nx ? ox : nx) + (int)u->info.w;
-		int uy1 = (oy > ny ? oy : ny) + (int)u->info.h;
+		int ux0 = old_sh.x < new_sh.x ? old_sh.x : new_sh.x;
+		int uy0 = old_sh.y < new_sh.y ? old_sh.y : new_sh.y;
+		int ox1 = old_sh.x + (int)old_sh.w;
+		int oy1 = old_sh.y + (int)old_sh.h;
+		int nx1 = new_sh.x + (int)new_sh.w;
+		int ny1 = new_sh.y + (int)new_sh.h;
+		int ux1 = ox1 > nx1 ? ox1 : nx1;
+		int uy1 = oy1 > ny1 ? oy1 : ny1;
 
 		add_rect(&present, ux0, uy0, (unsigned int)(ux1 - ux0),
 			 (unsigned int)(uy1 - uy0));
@@ -1568,22 +1696,29 @@ static int iabs(int v)
 	return v < 0 ? -v : v;
 }
 
-/* Repaint the old and new rectangles of a geometry change. */
+/* Repaint the old and new rectangles of a geometry change.  The old rect
+ * is cut by the new one and the new rect drawn last, so a pixel in the
+ * overlap gets its window and shadow exactly once. */
 static void redraw_geom(struct uwin *u, int ox, int oy,
 			unsigned int ow, unsigned int oh)
 {
 	struct wm_present_info present;
-	unsigned int i;
+	struct orect oldr, newr, pieces[4];
+	unsigned int i, np;
 
+	window_shadow_rect(&oldr, ox, oy, ow, oh);
+	window_shadow_rect(&newr, u->info.x, u->info.y, u->info.w, u->info.h);
 	present.count = 0;
 	present.immediate = 1;
-	add_rect(&present, ox, oy, ow, oh);
-	add_rect(&present, u->info.x, u->info.y, u->info.w, u->info.h);
+	add_rect(&present, oldr.x, oldr.y, oldr.w, oldr.h);
+	add_rect(&present, newr.x, newr.y, newr.w, newr.h);
 	if (u_wm_begin() == 0) {
 		remap_window_contents();
-		for (i = 0; i < present.count; i++)
-			draw_rect(present.rect[i].x, present.rect[i].y,
-				  present.rect[i].w, present.rect[i].h);
+		np = rect_sub(pieces, &oldr, &newr);
+		for (i = 0; i < np; i++)
+			draw_rect(pieces[i].x, pieces[i].y,
+				  pieces[i].w, pieces[i].h);
+		draw_rect(newr.x, newr.y, newr.w, newr.h);
 		wm_overlays(&present);
 		u_wm_present(&present);
 	}
@@ -3241,7 +3376,8 @@ static void handle_mouse(struct wm_status *st)
 				clamp_geom(u, &nx, &ny);
 				if (nx != ox || ny != oy) {
 					struct wm_present_info present;
-					unsigned int i;
+					struct orect oldr, newr, pieces[4];
+					unsigned int i, np;
 
 					u_wm_set_geom((unsigned int)drag_id,
 						      nx, ny);
@@ -3251,21 +3387,33 @@ static void handle_mouse(struct wm_status *st)
 						move_window_pixels(u, ox, oy,
 								   nx, ny);
 					} else {
+						window_shadow_rect(&oldr, ox, oy,
+								   u->info.w,
+								   u->info.h);
+						window_shadow_rect(&newr, nx, ny,
+								   u->info.w,
+								   u->info.h);
 						present.count = 0;
 						present.immediate = 1;
-						add_rect(&present, ox, oy,
-							 u->info.w, u->info.h);
-						add_rect(&present, nx, ny,
-							 u->info.w, u->info.h);
+						add_rect(&present, oldr.x,
+							 oldr.y, oldr.w,
+							 oldr.h);
+						add_rect(&present, newr.x,
+							 newr.y, newr.w,
+							 newr.h);
 						if (u_wm_begin() == 0) {
 							remap_window_contents();
-							for (i = 0;
-							     i < present.count; i++)
+							np = rect_sub(pieces,
+								      &oldr,
+								      &newr);
+							for (i = 0; i < np; i++)
 								draw_rect(
-								  present.rect[i].x,
-								  present.rect[i].y,
-								  present.rect[i].w,
-								  present.rect[i].h);
+								  pieces[i].x,
+								  pieces[i].y,
+								  pieces[i].w,
+								  pieces[i].h);
+							draw_rect(newr.x, newr.y,
+								  newr.w, newr.h);
 							wm_overlays(&present);
 							u_wm_present(&present);
 						}

+ 119 - 0
user/lib/uwm/uwm_lib.c

@@ -155,6 +155,125 @@ int usurf_icon(struct usurf *s, int x, int y, enum uwm_icon icon,
 	return usurf_icon_clip(s, x, y, icon, rgb, 0, 0, s->w, s->h);
 }
 
+/* ---- Window drop shadow ------------------------------------------------ */
+#define SHADOW_FALLOFF_N 11
+static const unsigned char shadow_falloff[SHADOW_FALLOFF_N] = {
+	0, 120, 110, 100, 90, 80, 69, 58, 46, 33, 20
+};
+
+/* Darken one band outside (wx,wy)-(wx1,wy1).  The alpha comes from the
+ * rounded distance to the window edge (max + 0.4 * min), so the four bands
+ * together form a soft ring whose corners fade in a curve instead of a
+ * square corner, without touching the window interior. */
+static void shadow_band(struct usurf *s, int x0, int y0, int x1, int y1,
+			int wx, int wy, int wx1, int wy1, int spread,
+			int bytes)
+{
+	int x, y;
+
+	for (y = y0; y < y1; y++) {
+		unsigned char *row = s->px + (unsigned long)y * s->pitch;
+		int dy = 0;
+
+		if (y < wy)
+			dy = wy - y;
+		else if (y >= wy1)
+			dy = y - wy1 + 1;
+		for (x = x0; x < x1; x++) {
+			int dx = 0, d, lo, hi;
+			unsigned int a, ia;
+			unsigned char *p;
+
+			if (x < wx)
+				dx = wx - x;
+			else if (x >= wx1)
+				dx = x - wx1 + 1;
+			lo = dx < dy ? dx : dy;
+			hi = dx < dy ? dy : dx;
+			d = hi + ((lo * 13) >> 5);
+			if (d <= 0 || d > spread)
+				continue;
+			if (d >= SHADOW_FALLOFF_N)
+				d = SHADOW_FALLOFF_N - 1;
+			a = shadow_falloff[d];
+			if (!a)
+				continue;
+			ia = 256 - a;
+			usurf_pixels++;
+			if (s->bpp == 16) {
+				unsigned short *q = (unsigned short *)row + x;
+				unsigned int v = *q;
+				unsigned int r = ((((v >> 11) & 0x1F) * ia)
+						  + 128) >> 8;
+				unsigned int g = ((((v >> 5) & 0x3F) * ia)
+						  + 128) >> 8;
+				unsigned int b = (((v & 0x1F) * ia)
+						  + 128) >> 8;
+
+				*q = (unsigned short)((r << 11) | (g << 5) | b);
+			} else {
+				p = row + x * bytes;
+				p[0] = (unsigned char)((p[0] * ia + 128) >> 8);
+				p[1] = (unsigned char)((p[1] * ia + 128) >> 8);
+				p[2] = (unsigned char)((p[2] * ia + 128) >> 8);
+				if (s->bpp == 32)
+					p[3] = 0xFF;
+			}
+		}
+	}
+}
+
+/* A soft drop shadow around a window rectangle: `spread` pixels outward,
+ * strongest next to the window.  The window interior is never touched and
+ * the whole ring is clipped to (cx,cy,cw,ch); callers pass the current
+ * damage rectangle so a partial repaint only darkens pixels it owns. */
+void usurf_window_shadow(struct usurf *s, int wx, int wy, int ww, int wh,
+			 int spread, int cx, int cy,
+			 unsigned int cw, unsigned int ch)
+{
+	int wx1 = wx + ww, wy1 = wy + wh;
+	int x0, y0, x1, y1, ix0, iy0, ix1, iy1;
+	int bytes;
+
+	if (!s || !s->px || ww <= 0 || wh <= 0 || spread <= 0)
+		return;
+	if (s->bpp != 16 && s->bpp != 24 && s->bpp != 32)
+		return;
+	bytes = (int)(s->bpp / 8);
+	x0 = wx - spread;
+	y0 = wy - spread;
+	x1 = wx1 + spread;
+	y1 = wy1 + spread;
+	ix0 = cx;
+	iy0 = cy;
+	ix1 = cx + (int)cw;
+	iy1 = cy + (int)ch;
+	if (ix0 < 0) ix0 = 0;
+	if (iy0 < 0) iy0 = 0;
+	if (ix1 > (int)s->w) ix1 = (int)s->w;
+	if (iy1 > (int)s->h) iy1 = (int)s->h;
+	if (x0 < ix0) x0 = ix0;
+	if (y0 < iy0) y0 = iy0;
+	if (x1 > ix1) x1 = ix1;
+	if (y1 > iy1) y1 = iy1;
+	if (x1 <= x0 || y1 <= y0)
+		return;
+	/* Corners belong to the top and bottom bands. */
+	shadow_band(s, x0, y0, x1, wy < y1 ? wy : y1,
+		    wx, wy, wx1, wy1, spread, bytes);
+	shadow_band(s, x0, wy1 > y0 ? wy1 : y0, x1, y1,
+		    wx, wy, wx1, wy1, spread, bytes);
+	{
+		int by0 = wy > y0 ? wy : y0;
+		int by1 = wy1 < y1 ? wy1 : y1;
+
+		shadow_band(s, x0, by0, wx < x1 ? wx : x1, by1,
+			    wx, wy, wx1, wy1, spread, bytes);
+		shadow_band(s, wx1 > x0 ? wx1 : x0, by0, x1, by1,
+			    wx, wy, wx1, wy1, spread, bytes);
+	}
+}
+
 /* ---- Full-colour app icons -------------------------------------------- */
 #define UWM_APP_BYTES (APP_ICON_PX * APP_ICON_PX * 3u)
 

+ 7 - 0
user/lib/uwm/uwm_lib.h

@@ -66,6 +66,13 @@ int usurf_icon_clip(struct usurf *s, int x, int y, enum uwm_icon icon,
 		    unsigned int rgb, int cx, int cy,
 		    unsigned int cw, unsigned int ch);
 
+/* Soft drop shadow around a window rectangle: `spread` pixels outward,
+ * darkest next to the edge, clipped to (cx,cy,cw,ch).  The window interior
+ * is not modified. */
+void usurf_window_shadow(struct usurf *s, int wx, int wy, int ww, int wh,
+			 int spread, int cx, int cy,
+			 unsigned int cw, unsigned int ch);
+
 /* Draw one full-colour app icon from /ramfs/icons/apps96.icn, scaled to
  * size x size and centred at (x, y).  Returns 0 when the bundle (or that
  * id) is not available, so the caller can fall back to a letter tile. */