Просмотр исходного кода

browser: reliable tooltips, opacity, transitions and modal stacking

- Forced reflows are capped at 8 per main-loop pass instead of 40 ms per
  100 ms: on slow boards every reflow was refused and Bootstrap placed
  its tooltip from stale boxes.
- opacity is a real alpha; translucent absolute/fixed boxes are blended
  over what lies beneath them (group opacity).
- transition / transition-* animate opacity and translate() (percentages
  of the own box) from the value on screen, with easing, and dispatch
  transitionend; the start state comes from the last paint, so a forced
  reflow before adding a class works as in browsers.
- A fixed box pinned by top and bottom spans the viewport (modal
  backdrops).
- Painted absolute/fixed boxes with a background or z-index occlude the
  content beneath them for clicks and hover; pointer-events: none is
  parsed.
- Animations of elements no longer painted are dropped.

Bootstrap's modal on cloudron.io now fades its backdrop in, slides the
dialog down and closes on a backdrop click.
Johannes Zellner 1 день назад
Родитель
Сommit
0c0473ea82
5 измененных файлов с 666 добавлено и 46 удалено
  1. 39 3
      AGENTS.md
  2. 0 0
      docs/userland.html
  3. 395 25
      user/apps/browser/browser.c
  4. 217 18
      user/apps/browser/browser_html.c
  5. 15 0
      user/apps/browser/browser_html.h

+ 39 - 3
AGENTS.md

@@ -2114,8 +2114,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   scripted page and whenever the page width changes.  A geometry read
   after a DOM change (`offsetWidth`, `getBoundingClientRect`, ...) runs a
   synchronous layout first (`js_force_layout()`: measure + render into the
-  page back buffer, at most 40 ms per 100 ms), like a browser's forced
-  reflow; `documentElement.clientWidth/Height` are the viewport.
+  page back buffer), like a browser's forced reflow.  The guard is a count
+  (`FORCE_LAYOUT_PER_PASS`, 8 per main-loop pass), not a time budget: on
+  the ION one cloudron.io re-layout takes longer than any sensible budget,
+  and a refused reflow left Bootstrap placing its tooltip from stale boxes.
+  `documentElement.clientWidth/Height` are the viewport.
   **Hover**: each frame the element under the pointer (`hover_target()`:
   the innermost box, or the link/control hit inside it) is compared with
   the last one, and `page_hover_events()` dispatches `mouseout`/
@@ -2128,7 +2131,33 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   An absolute/fixed box with an auto width and one horizontal offset is
   shrink-to-fit, absolute/fixed children add nothing to a parent's
   shrink-to-fit width and do not end a float row, and `<a href="">` is a
-  link to the page itself.  The logged-in
+  link to the page itself.  A fixed box pinned by `top` and `bottom` with
+  an auto height spans the viewport between them (a modal backdrop).
+  **Opacity and transitions**: `opacity` is a 0..255 alpha
+  (`br_style.opacity`; `opacity_hidden` is alpha 0).  A translucent
+  absolute/fixed box gets group opacity: the deferred painter saves the
+  page area (`alpha_begin()`), paints the box and blends it back over its
+  layout box (`alpha_end()`); in-flow elements still paint opaque.
+  `transition`/`transition-property|duration|timing-function` reduce to a
+  mask of opacity/transform, one duration and an easing (`tr_mask`,
+  `tr_ms`, `tr_ease`).  The render pass remembers each element's last
+  target opacity and `translate()` offset (`pstate[]`; `translate()` takes
+  percentages of the own box, `tx_pct`/`ty_pct`); a changed target on an
+  element with a matching transition starts an animation from the value on
+  screen (`anim_value()`, up to 32 at once), the page re-renders every
+  pass while one runs, and `transitionend` is dispatched when it ends
+  (`anim_after_frame()`); animations of elements no longer painted are
+  dropped.  The start value is whatever the previous paint saw, which is
+  why a forced reflow is a paint pass: Bootstrap reads `offsetWidth` after
+  showing the modal and before adding `.in`, so its backdrop fades in and
+  the dialog slides down.  **Stacking for input**: each painted
+  absolute/fixed box with a background or a `z-index` (and without
+  `pointer-events: none`, parsed and inherited as `pe_none`) adds an
+  occluder hit below its own link hits (`hit_add_occluder()`): links
+  painted under it stop matching (`find_hit_ex()`), and the box search for
+  clicks/hover is limited to the covering box's subtree, so a click on a
+  modal's backdrop reaches `.modal` (Bootstrap closes it) instead of the
+  page behind.  The logged-in
   dashboard (sidebar with expandable groups, header, app grid, hash-routed
   views) was verified in QEMU against a local stand-in that proxies the
   public dashboard assets and mocks `/api/v1` and the OIDC endpoints.  The
@@ -3112,6 +3141,13 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     gives the name a different value, otherwise `var()` uses its fallback
     -- Font Awesome's competing `--fa-width` values made every icon 100%
     wide; rules touching such names are re-expanded per element against
+    Transitions animate only `opacity` (painted only on absolute/fixed
+    boxes; in-flow translucency paints opaque) and `translate()`; colours,
+    sizes, `scale`/`rotate`, `@keyframes` animations, per-property
+    durations/delays in one `transition` list and `transitioncancel`/
+    `transitionstart` are not supported, and every animation frame is a
+    full page re-render (~25 ms on the host, several times that on the
+    ION, so slides are choppy there).
     that element's own matched rules, but values are not inherited from
     ancestors' rules), percentage heights (against the containing block's definite
     height, `lay.cb_h`, with the viewport for `html`/`body`; used as a

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
docs/userland.html


+ 395 - 25
user/apps/browser/browser.c

@@ -363,6 +363,10 @@ struct lay {
 struct hit {
 	int x, y, w, h;
 	unsigned short node;
+	/* A painted absolute/fixed box (a modal, its backdrop, a fixed
+	 * header): it hides the hits painted before it.  Inserted below the
+	 * box's own hits, so its links still win. */
+	unsigned char occl;
 };
 
 /* Live form state; the DOM attributes stay the initial/default value. */
@@ -392,6 +396,8 @@ static int s_eq(const char *a, const char *b);
 static void form_key(int k);
 static void form_click(unsigned int node);
 static void style_for_node(unsigned int node, struct br_style *out);
+static void anim_reset(void);
+static void anim_after_frame(void);
 static int form_author_styled(const struct br_style *st);
 static int css_radius(const struct br_style *st, int corner, int w, int h);
 static void form_paint_author(struct ngui *ui, struct ngui_rect box,
@@ -732,6 +738,7 @@ static int heap_start(void)
 
 static void heap_clear(void)
 {
+	anim_reset();           /* also drops the page-heap alpha buffer */
 	memset(font_faces, 0, sizeof(font_faces));
 	memset(font_pick, 0, sizeof(font_pick));
 	ngui_font_detach(&ui);
@@ -1218,7 +1225,12 @@ static int js_host_computed(unsigned int node, const char *property,
 		static const char *cl[] = { "none", "left", "right", "both" };
 		snprintf(buf, cap, "%s", cl[st.clear < 4 ? st.clear : 0]);
 	} else if (!strcmp(name, "opacity")) {
-		snprintf(buf, cap, "%s", st.opacity_hidden ? "0" : "1");
+		if (st.opacity >= 255)
+			snprintf(buf, cap, "1");
+		else
+			snprintf(buf, cap, "0.%02d",
+				 (st.opacity * 100 + 127) / 255 > 99 ? 99 :
+				 (st.opacity * 100 + 127) / 255);
 	} else if (!strcmp(name, "visibility")) {
 		snprintf(buf, cap, "%s", st.vis_hidden ? "hidden" : "visible");
 	} else if (!strcmp(name, "overflow")) {
@@ -8012,6 +8024,7 @@ static void flush_line(struct lay *l)
 						h->w = w;
 						h->h = hh;
 						h->node = (unsigned short)target;
+						h->occl = 0;
 						nhits++;
 					}
 				}
@@ -9935,6 +9948,244 @@ static void draw_image_block(struct lay *l, unsigned int node, int x, int y,
 static int intrinsic_width(struct ngui *ui, unsigned int node,
 			   const struct br_style *pst, int depth, int base);
 
+/* ---- CSS transitions (opacity, transform translate) ----
+ *
+ * The cascade only knows the current computed value, so the paint pass
+ * remembers each element's last target value (pstate[]).  When a later
+ * paint sees a different target and the element has a transition for that
+ * property, an animation from the value on screen to the new target starts
+ * and the page is repainted every pass until it ends; then `transitionend`
+ * is dispatched.  The start state is whatever the previous paint saw: a
+ * script that changes a class right after display (Bootstrap adds `.in`)
+ * forces a reflow first (offsetWidth), and js_force_layout() is a paint
+ * pass, so that state is recorded like in a browser.  Only the render
+ * pass records (render_pass); the measure pass does not. */
+#define ANIM_MAX  32
+#define ANIM_OP   0
+#define ANIM_TX   1
+#define ANIM_TY   2
+
+struct anim {
+	unsigned short node;
+	unsigned char prop, ease;
+	short from, to;
+	unsigned int t0, dur;
+	unsigned int seq;       /* render_seq of the last paint that saw it */
+};
+
+struct paint_state {
+	short tx, ty;
+	unsigned char op;
+	unsigned char seen;     /* bit per ANIM_* prop */
+};
+
+static struct paint_state pstate[BR_MAX_NODES];
+static struct anim anims[ANIM_MAX];
+static int nanims;
+static int render_pass;
+static unsigned int render_seq;
+static unsigned short anim_ended[ANIM_MAX];
+static int nanim_ended;
+
+
+
+static int anim_ease(int ease, int p)      /* p and result 0..1024 */
+{
+	int q = 1024 - p;
+
+	switch (ease) {
+	case BR_EASE_LINEAR:
+		return p;
+	case BR_EASE_IN:
+		return (int)((long)p * p / 1024);
+	case BR_EASE_OUT:
+		return 1024 - (int)((long)q * q / 1024 * q / 1024);
+	default:                /* ease, ease-in-out: smoothstep */
+		return (int)((long)p * p / 1024 * (3072 - 2 * p) / 1024);
+	}
+}
+
+static int anim_find(unsigned int node, int prop)
+{
+	int i;
+
+	for (i = 0; i < nanims; i++)
+		if (anims[i].node == node && anims[i].prop == prop)
+			return i;
+	return -1;
+}
+
+static int anim_now(const struct anim *a, unsigned int now)
+{
+	unsigned int dt = now - a->t0;
+	int p;
+
+	if (dt >= a->dur)
+		return a->to;
+	p = anim_ease(a->ease, (int)(dt * 1024 / a->dur));
+	return a->from + (int)((long)(a->to - a->from) * p / 1024);
+}
+
+static int anim_has(unsigned int node)
+{
+	int i;
+
+	for (i = 0; i < nanims; i++)
+		if (anims[i].node == node)
+			return 1;
+	return 0;
+}
+
+/* The value to paint for a property whose computed target is `target`. */
+static int anim_value(unsigned int node, int prop, int target,
+		      const struct br_style *st)
+{
+	struct paint_state *ps;
+	unsigned int now = u_ticks_ms();
+	int i, prev, bit = 1 << prop;
+	int tr = prop == ANIM_OP ? (st->tr_mask & BR_TR_OPACITY) :
+				   (st->tr_mask & BR_TR_TRANSFORM);
+
+	if (!render_pass || node >= BR_MAX_NODES)
+		return target;
+	ps = &pstate[node];
+	prev = prop == ANIM_OP ? ps->op : prop == ANIM_TX ? ps->tx : ps->ty;
+	i = anim_find(node, prop);
+	if ((ps->seen & bit) && prev != target) {
+		if (tr && st->tr_ms) {
+			int from = i >= 0 ? anim_now(&anims[i], now) : prev;
+
+			if (i < 0 && nanims < ANIM_MAX)
+				i = nanims++;
+			if (i >= 0) {
+				anims[i].node = (unsigned short)node;
+				anims[i].prop = (unsigned char)prop;
+				anims[i].ease = st->tr_ease;
+				anims[i].from = (short)from;
+				anims[i].to = (short)target;
+				anims[i].t0 = now;
+				anims[i].dur = st->tr_ms;
+				anims[i].seq = render_seq;
+			}
+		} else if (i >= 0) {
+			anims[i] = anims[--nanims];
+			i = -1;
+		}
+	}
+	ps->seen |= (unsigned char)bit;
+	if (prop == ANIM_OP)
+		ps->op = (unsigned char)target;
+	else if (prop == ANIM_TX)
+		ps->tx = (short)target;
+	else
+		ps->ty = (short)target;
+	if (i < 0)
+		return target;
+	anims[i].seq = render_seq;
+	if (now - anims[i].t0 >= anims[i].dur) {
+		if (nanim_ended < ANIM_MAX)
+			anim_ended[nanim_ended++] = anims[i].node;
+		anims[i] = anims[--nanims];
+		return target;
+	}
+	return anim_now(&anims[i], now);
+}
+
+/* After the frame: running animations need another paint, and finished
+ * ones get their `transitionend` (Bootstrap also has a timer fallback). */
+static void anim_after_frame(void)
+{
+	static unsigned int checked_seq;
+	int i, j, n = nanim_ended;
+
+	/* An element removed or hidden mid-transition is no longer painted;
+	 * its animation would otherwise keep every frame re-rendering. */
+	if (render_seq != checked_seq) {
+		for (i = 0; i < nanims; )
+			if (anims[i].seq != render_seq)
+				anims[i] = anims[--nanims];
+			else
+				i++;
+		checked_seq = render_seq;
+	}
+	if (nanims) {
+		page_cache_valid = 0;
+		ngui_invalidate(&ui);
+	}
+	nanim_ended = 0;
+	if (!js_browser_active())
+		return;
+	for (i = 0; i < n; i++) {
+		for (j = 0; j < i; j++)
+			if (anim_ended[j] == anim_ended[i])
+				break;
+		if (j == i && anim_ended[i] < doc.nnode)
+			js_browser_event(anim_ended[i], "transitionend", 1, 0);
+	}
+}
+
+/* Group opacity for a translucent positioned box: the page area under it
+ * is saved before the box paints and blended back afterwards. */
+static unsigned short *alpha_snap;
+static unsigned int alpha_snap_px;
+
+static void anim_reset(void)
+{
+	memset(pstate, 0, sizeof(pstate));
+	nanims = 0;
+	nanim_ended = 0;
+	alpha_snap = 0;         /* lived in the page heap that is being reset */
+	alpha_snap_px = 0;
+}
+
+static int alpha_begin(struct ngui_rect r)
+{
+	unsigned int n = (unsigned int)r.w * (unsigned int)r.h;
+	int y;
+
+	if (r.w <= 0 || r.h <= 0)
+		return 0;
+	if (n > alpha_snap_px) {
+		free(alpha_snap);
+		alpha_snap = (unsigned short *)malloc(n * 2u);
+		alpha_snap_px = alpha_snap ? n : 0;
+		if (!alpha_snap)
+			return 0;
+	}
+	for (y = 0; y < r.h; y++)
+		memcpy(alpha_snap + y * r.w, ui.px + (r.y + y) * ui.w + r.x,
+		       (unsigned int)r.w * 2u);
+	return 1;
+}
+
+static void alpha_end(struct ngui_rect r, struct ngui_rect box, int a)
+{
+	int x, y, x0, x1, y0, y1;
+
+	x0 = box.x > r.x ? box.x : r.x;
+	y0 = box.y > r.y ? box.y : r.y;
+	x1 = box.x + box.w < r.x + r.w ? box.x + box.w : r.x + r.w;
+	y1 = box.y + box.h < r.y + r.h ? box.y + box.h : r.y + r.h;
+	for (y = y0; y < y1; y++) {
+		unsigned short *d = ui.px + y * ui.w;
+		const unsigned short *o = alpha_snap + (y - r.y) * r.w - r.x;
+
+		for (x = x0; x < x1; x++) {
+			unsigned int n = d[x], b = o[x];
+			unsigned int rr, gg, bb;
+
+			if (n == b)
+				continue;
+			rr = (((n >> 11) & 31) * a + ((b >> 11) & 31) *
+			      (255 - a)) / 255;
+			gg = (((n >> 5) & 63) * a + ((b >> 5) & 63) *
+			      (255 - a)) / 255;
+			bb = ((n & 31) * a + (b & 31) * (255 - a)) / 255;
+			d[x] = (unsigned short)(rr << 11 | gg << 5 | bb);
+		}
+	}
+}
+
 static void layout_block(struct lay *l, unsigned int node,
 			 const struct br_style *st, int depth,
 			 unsigned short link)
@@ -10078,9 +10329,25 @@ static void layout_block(struct lay *l, unsigned int node,
 		rel_dy = st->top_set ? st->top :
 			 (st->bottom_set ? -st->bottom : 0);
 	}
-	if (st->transform_set) {
-		rel_dx += st->tx;
-		rel_dy += st->ty;
+	if (st->transform_set || (st->tr_mask & BR_TR_TRANSFORM) ||
+	    (render_pass && anim_has(node))) {
+		int tx = st->tx, ty = st->ty;
+
+		/* translate(0, -25%): a percentage of the element's own
+		 * box (the measure pass's height, the flow width). */
+		if (st->tx_pct)
+			tx = (old_right - old_left) * st->tx / 100;
+		if (st->ty_pct)
+			ty = node < BR_MAX_NODES ? block_h[node] * st->ty / 100
+						 : 0;
+		if (!st->transform_set)
+			tx = ty = 0;
+		tx = anim_value(node, ANIM_TX, tx, st);
+		ty = anim_value(node, ANIM_TY, ty, st);
+		rel_dx += tx;
+		rel_dy += ty;
+		if (tx || ty)
+			shifted = 1;
 	}
 	if ((doc.nodes[node].tag == BR_TAG_INPUT ||
 	     doc.nodes[node].tag == BR_TAG_TEXTAREA ||
@@ -10485,6 +10752,16 @@ static void layout_block(struct lay *l, unsigned int node,
 	if (!st->height_kind && node < BR_MAX_NODES &&
 	    content_h + deco_h < flex_stretch_h[node])
 		content_h = flex_stretch_h[node] - deco_h;
+	/* A fixed box pinned by both top and bottom with an auto height
+	 * spans the viewport between them (Bootstrap's .modal-backdrop is
+	 * an empty `position: fixed; top: 0; bottom: 0` box). */
+	if (!st->height_kind && st->pos == 2 && st->top_set &&
+	    st->bottom_set) {
+		int fh = page_rect.h - st->top - st->bottom - deco_h;
+
+		if (content_h < fh)
+			content_h = fh;
+	}
 	if (st->max_height_kind) {
 		int mh = st->max_height_kind == 3 ?
 			 cbw * st->max_height_calc_pct / 100 +
@@ -13110,6 +13387,39 @@ static void sort_deferred(void)
 	}
 }
 
+static void hit_add_occluder(unsigned int node, int at)
+{
+	const struct br_layout_box *b;
+	struct hit h;
+	int x0, y0, x1, y1;
+
+	if (node >= BR_MAX_NODES || nhits >= HIT_MAX || at > nhits)
+		return;
+	b = &layout_boxes[node];
+	if (!b->valid)
+		return;
+	x0 = b->x > page_rect.x ? b->x : page_rect.x;
+	y0 = b->y > page_rect.y ? b->y : page_rect.y;
+	x1 = b->x + b->w;
+	y1 = b->y + b->h;
+	if (x1 > page_rect.x + page_rect.w - 12)
+		x1 = page_rect.x + page_rect.w - 12;
+	if (y1 > page_rect.y + page_rect.h)
+		y1 = page_rect.y + page_rect.h;
+	if (x1 <= x0 || y1 <= y0)
+		return;
+	h.x = x0;
+	h.y = y0;
+	h.w = x1 - x0;
+	h.h = y1 - y0;
+	h.node = (unsigned short)node;
+	h.occl = 1;
+	memmove(&hits[at + 1], &hits[at],
+		(unsigned int)(nhits - at) * sizeof(hits[0]));
+	hits[at] = h;
+	nhits++;
+}
+
 static void render_page(int left, int right, int y0)
 {
 	struct lay *l = &layout_state;
@@ -13139,6 +13449,8 @@ static void render_page(int left, int right, int y0)
 	l->space_pending = 0;
 	nhits = 0;
 	ndeferred = 0;
+	render_pass = 1;
+	render_seq++;
 	base = root_style();
 	layout_children(l, doc.root, &base, 0, 0);
 	sort_deferred();
@@ -13147,15 +13459,22 @@ static void render_page(int left, int right, int y0)
 
 		for (i = 0; i < ndeferred; i++) {
 			struct br_style st;
-			int hide;
+			int hide, op, blend, hit0;
 
 			br_style_for(&doc, deferred[i].node, &base, &st);
 			if (st.display == BR_DISPLAY_NONE)
 				continue;
 			/* A transparent or visibility:hidden positioned box
 			 * still gets its boxes (scripts measure it) but no
-			 * paint and no hit rectangles. */
-			hide = st.opacity_hidden || st.vis_hidden;
+			 * paint and no hit rectangles.  A translucent one
+			 * (a 50% modal backdrop, a fading tooltip) is
+			 * blended over what is under it; the opacity may be
+			 * mid-transition. */
+			hit0 = nhits;
+			op = anim_value(deferred[i].node, ANIM_OP, st.opacity,
+					&st);
+			hide = op == 0 || st.vis_hidden;
+			blend = !hide && op < 255 && alpha_begin(page_rect);
 			l->draw = !hide;
 			l->left = deferred[i].flow_left;
 			l->right = deferred[i].flow_right;
@@ -13230,25 +13549,62 @@ static void render_page(int left, int right, int y0)
 				layout_block(l, deferred[i].node, &st, 0,
 					     deferred[i].link);
 			}
+			if (!hide && !st.pe_none &&
+			    (st.pos == 1 || st.pos == 2) &&
+			    (st.has_bg || st.bg_image || st.z_set))
+				hit_add_occluder(deferred[i].node, hit0);
+			if (blend) {
+				const struct br_layout_box *b =
+					&layout_boxes[deferred[i].node];
+				struct ngui_rect box = page_rect;
+
+				/* The box itself; descendants may overflow
+				 * it, so a full-page box is the safe case. */
+				if (deferred[i].node < BR_MAX_NODES &&
+				    b->valid) {
+					box.x = b->x;
+					box.y = b->y;
+					box.w = b->w;
+					box.h = b->h;
+				}
+				alpha_end(page_rect, box, op);
+			}
 		}
 	}
+	render_pass = 0;
 }
 
 /* ---- UI ---- */
 
-static int find_hit(int x, int y)
+/* The link/control under a point, topmost first.  An occluder above it
+ * returns 0 and its node in *occl (when given), so the caller can target
+ * the covering box instead of content painted underneath. */
+static int find_hit_ex(int x, int y, int *occl)
 {
 	int i;
 
+	if (occl)
+		*occl = -1;
 	for (i = nhits - 1; i >= 0; i--) {
 		struct hit *h = &hits[i];
 		if (x >= h->x && y >= h->y && x < h->x + h->w &&
-		    y < h->y + h->h)
+		    y < h->y + h->h) {
+			if (h->occl) {
+				if (occl)
+					*occl = (int)h->node;
+				return 0;
+			}
 			return (int)h->node;
+		}
 	}
 	return 0;
 }
 
+static int find_hit(int x, int y)
+{
+	return find_hit_ex(x, y, 0);
+}
+
 /* ---- URL-bar autocomplete ----
  *
  * While the address bar has the focus a dropdown lists up to SUG_MAX
@@ -13858,7 +14214,21 @@ static void draw_page(struct ngui_rect page)
  * boxes for nodes they just inserted.  Absolute/fixed boxes only get their
  * boxes in the painting pass, so this is a full render; the next frame
  * repaints the screen as usual.  Skipped while the page has never been
- * drawn (no viewport yet) or without the back buffer. */
+ * drawn (no viewport yet) or without the back buffer.
+ *
+ * The guard is a count per main-loop pass, not a time budget: a hover or
+ * click handler (Bootstrap's tooltip does ~6 write/read rounds) must get
+ * correct geometry even on the ION, where one re-layout of cloudron.io
+ * takes longer than any reasonable per-window budget; only a script that
+ * keeps writing and reading in one pass falls back to the last layout. */
+#define FORCE_LAYOUT_PER_PASS 8
+static int force_layout_count;
+
+static void js_force_layout_pass_reset(void)
+{
+	force_layout_count = 0;
+}
+
 static void js_force_layout(void)
 {
 	static int busy;
@@ -13866,23 +14236,12 @@ static void js_force_layout(void)
 	int saved_w = ui.w, saved_h = ui.h;
 	struct ngui_rect page = page_rect;
 
-	static unsigned int window_t0, spent;
-	unsigned int t0 = u_ticks_ms();
-
 	if (busy || !page_backbuf || pagebuf_w != ui.w ||
 	    pagebuf_h != ui.h || page.w < 40 || page.h < 40 ||
-	    layout_right <= layout_left)
-		return;
-	/* Budget: a script that alternates writes and reads (an animation
-	 * loop) would otherwise re-render the page on every read.  Past
-	 * 40 ms of forced layouts in a 100 ms window it reads the last
-	 * layout, as before. */
-	if (t0 - window_t0 >= 100) {
-		window_t0 = t0;
-		spent = 0;
-	}
-	if (spent >= 40)
+	    layout_right <= layout_left ||
+	    force_layout_count >= FORCE_LAYOUT_PER_PASS)
 		return;
+	force_layout_count++;
 	busy = 1;
 	ui.px = page_backbuf;
 	layout_view_h = page.h;
@@ -13897,7 +14256,6 @@ static void js_force_layout(void)
 	ui.h = saved_h;
 	page_cache_valid = 0;
 	ngui_invalidate(&ui);
-	spent += u_ticks_ms() - t0;
 	busy = 0;
 }
 
@@ -14806,10 +15164,17 @@ static int node_invisible(unsigned int n)
  * that contains it (the last one wins a tie, i.e. the later/deeper node).
  * Used to deliver clicks to elements that are neither links nor controls
  * (a Vue <div role="button">, a card with a click listener). */
+static int node_contains(unsigned int a, unsigned int n);
+
 static unsigned int find_box_hit(int x, int y)
 {
 	unsigned int i, best = 0, best_area = 0xFFFFFFFFu;
+	int root;
 
+	/* A covering positioned box (a modal) takes the point: only it and
+	 * its descendants are candidates, not the page painted under it. */
+	if (find_hit_ex(x, y, &root) || root < 0)
+		root = -1;
 	for (i = 1; i < doc.nnode && i < BR_MAX_NODES; i++) {
 		const struct br_layout_box *b = &layout_boxes[i];
 		unsigned int area;
@@ -14818,6 +15183,8 @@ static unsigned int find_box_hit(int x, int y)
 		    x < b->x || y < b->y || x >= b->x + b->w ||
 		    y >= b->y + b->h)
 			continue;
+		if (root >= 0 && !node_contains((unsigned int)root, i))
+			continue;
 		area = (unsigned int)b->w * (unsigned int)b->h;
 		if (area <= best_area && !node_invisible(i)) {
 			best = i;
@@ -15200,6 +15567,7 @@ void _start(const char *arg)
 			preload_node = 0;
 			preload_active = 1;
 		}
+		js_force_layout_pass_reset();
 		js_browser_tick();
 		if (resize_pending && !pending_action) {
 			resize_pending = 0;
@@ -15227,10 +15595,12 @@ void _start(const char *arg)
 				continue;
 			}
 		} else {
+			js_force_layout_pass_reset();
 			draw(&action);
 			handle_keys(&action);
 			handle_page_click(&action);
 			page_hover_events();
+			anim_after_frame();
 			ngui_end(&ui);
 		}
 		if (action & ACT_NAV) {

+ 217 - 18
user/apps/browser/browser_html.c

@@ -2541,6 +2541,8 @@ enum {
 	PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
 	PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
 	PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
+	PROP_TX_PCT, PROP_TY_PCT, PROP_TR_MASK, PROP_TR_MS, PROP_TR_EASE,
+	PROP_PE_NONE,
 	PROP_COUNT
 };
 
@@ -3760,6 +3762,163 @@ static int parse_fixed_100(const char *s, unsigned int len,
 	return 1;
 }
 
+/* One translate() argument: px (or rem/em/vw through parse_len), or a
+ * percentage of the element's own box, which only layout can resolve. */
+static int translate_arg(const char *v, unsigned int vl, unsigned int *off,
+			 int *pct)
+{
+	unsigned int q = *off;
+	signed short h100;
+
+	*pct = 0;
+	while (q < vl && is_space(v[q]))
+		q++;
+	if (parse_fixed_100(v, vl, &q, &h100) && q < vl && v[q] == '%') {
+		*off = q + 1;
+		*pct = 1;
+		return h100 / 100;
+	}
+	return parse_len(v, vl, off);
+}
+
+/* "0.3s" / ".15s" / "300ms" -> milliseconds; -1 when not a time. */
+static int parse_time_ms(const char *s, unsigned int len, unsigned int *pos)
+{
+	unsigned int i = *pos;
+	long v = 0, scale = 1000;
+	int digits = 0;
+
+	while (i < len && is_space(s[i]))
+		i++;
+	while (i < len && s[i] >= '0' && s[i] <= '9') {
+		v = v * 10 + (s[i] - '0');
+		digits = 1;
+		i++;
+	}
+	if (i < len && s[i] == '.') {
+		i++;
+		while (i < len && s[i] >= '0' && s[i] <= '9') {
+			if (scale > 1) {
+				v = v * 10 + (s[i] - '0');
+				scale /= 10;
+			}
+			digits = 1;
+			i++;
+		}
+	}
+	if (!digits)
+		return -1;
+	if (i + 1 < len && s[i] == 'm' && s[i + 1] == 's') {
+		*pos = i + 2;
+		v = v * scale / 1000;
+	} else if (i < len && s[i] == 's') {
+		*pos = i + 1;
+		v = v * scale;
+	} else {
+		return -1;
+	}
+	if (v > 10000)
+		v = 10000;
+	return (int)v;
+}
+
+static int ease_code(const char *s, unsigned int n)
+{
+	if (contains_ci_len(s, n, "ease-in-out"))
+		return BR_EASE_IN_OUT;
+	if (contains_ci_len(s, n, "ease-out"))
+		return BR_EASE_OUT;
+	if (contains_ci_len(s, n, "ease-in"))
+		return BR_EASE_IN;
+	if (contains_ci_len(s, n, "linear"))
+		return BR_EASE_LINEAR;
+	if (contains_ci_len(s, n, "cubic-bezier"))
+		return BR_EASE_OUT;
+	return BR_EASE_EASE;
+}
+
+static int tr_prop_mask(const char *s, unsigned int n)
+{
+	int m = 0;
+
+	if (contains_ci_len(s, n, "all"))
+		m |= BR_TR_OPACITY | BR_TR_TRANSFORM;
+	if (contains_ci_len(s, n, "opacity"))
+		m |= BR_TR_OPACITY;
+	if (contains_ci_len(s, n, "transform"))
+		m |= BR_TR_TRANSFORM;
+	return m;
+}
+
+/* `transition` (shorthand, comma list) and its longhands, reduced to the
+ * set of animated properties the engine supports (opacity, transform),
+ * one duration (the longest listed) and the first easing.  A shorthand
+ * item without a property name means `all`. */
+static void add_transition(struct br_decl *out, unsigned int *nout,
+			   const char *prop,
+			   const char *v, unsigned int vl, int important)
+{
+	unsigned int i = 0;
+	int mask = 0, ms = 0, ease = -1;
+
+	if (str_eq(prop, "transition-property")) {
+		decl_add(out, nout, PROP_TR_MASK, tr_prop_mask(v, vl),
+			 important);
+		return;
+	}
+	if (str_eq(prop, "transition-timing-function")) {
+		decl_add(out, nout, PROP_TR_EASE, ease_code(v, vl), important);
+		return;
+	}
+	while (i < vl) {
+		unsigned int a = i, e, q;
+		int t, item_ms = -1;
+
+		while (i < vl && v[i] != ',') {
+			if (v[i] == '(')
+				while (i < vl && v[i] != ')')
+					i++;
+			i++;
+		}
+		e = i < vl ? i : vl;
+		for (q = a; q < e; q++) {
+			unsigned int p = q;
+
+			if ((q == a || is_space(v[q - 1])) &&
+			    (t = parse_time_ms(v, e, &p)) >= 0) {
+				if (item_ms < 0)
+					item_ms = t;    /* the second time is the delay */
+				q = p;
+			}
+		}
+		if (str_eq(prop, "transition-duration")) {
+			if (item_ms > ms)
+				ms = item_ms;
+		} else if (item_ms > 0) {
+			int m = tr_prop_mask(v + a, e - a);
+
+			if (!m && !contains_ci_len(v + a, e - a, "none"))
+				m = BR_TR_OPACITY | BR_TR_TRANSFORM;
+			if (m) {
+				mask |= m;
+				if (item_ms > ms)
+					ms = item_ms;
+				if (ease < 0)
+					ease = ease_code(v + a, e - a);
+			}
+		}
+		i = e + 1;
+	}
+	if (str_eq(prop, "transition-duration")) {
+		decl_add(out, nout, PROP_TR_MS, ms, important);
+		return;
+	}
+	decl_add(out, nout, PROP_TR_MASK, mask, important);
+	decl_add(out, nout, PROP_TR_MS, ms, important);
+	decl_add(out, nout, PROP_TR_EASE, ease < 0 ? BR_EASE_EASE : ease,
+		 important);
+}
+
 static int find_fn(const char *v, unsigned int len, const char *name,
 		   unsigned int *arg)
 {
@@ -5819,6 +5978,10 @@ static void parse_decls(struct br_doc *d, const char *s,
 			decl_add(out, nout, PROP_UNDERLINE, u, important);
 			decl_add(out, nout, PROP_STRIKE, stv, important);
 			decl_add(out, nout, PROP_OVERLINE, ov, important);
+		} else if (str_eq(prop, "pointer-events")) {
+			decl_add(out, nout, PROP_PE_NONE,
+				 vend - vstart >= 4 &&
+				 ci_eq_n(s + vstart, "none", 4), important);
 		} else if (str_eq(prop, "cursor")) {
 			int c = parse_cursor(s + vstart, vend - vstart);
 
@@ -5846,21 +6009,25 @@ static void parse_decls(struct br_doc *d, const char *s,
 						"collapse");
 			decl_add(out, nout, PROP_VIS_HIDDEN, h, important);
 		} else if (str_eq(prop, "opacity")) {
-			unsigned int q;
-			int h = 1, any = 0;
-
-			for (q = vstart; q < vend; q++) {
-				if (s[q] >= '1' && s[q] <= '9') {
-					h = 0;
-					break;
-				}
-				if (s[q] >= '0' && s[q] <= '9')
-					any = 1;
+			/* Stored as transparency (255 - alpha), so a missing
+			 * declaration means opaque.  `0`, `.5`, `0.9`, `50%`;
+			 * anything unparsable (inherit, var leftovers) is 1. */
+			unsigned int q = vstart;
+			signed short h100;
+			int a = 255;
+
+			if (parse_fixed_100(s, vend, &q, &h100)) {
+				int pct = q < vend && s[q] == '%';
+				long v = pct ? h100 / 100 : h100;
+
+				if (v < 0)
+					v = 0;
+				if (v > 100)
+					v = 100;
+				a = (int)(v * 255 / 100);
 			}
-			if (!any || contains_ci_len(s + vstart, vend - vstart,
-						    "inherit"))
-				h = 0;
-			decl_add(out, nout, PROP_OPACITY_HIDDEN, h, important);
+			decl_add(out, nout, PROP_OPACITY_HIDDEN, 255 - a,
+				 important);
 		} else if (str_eq(prop, "overflow") ||
 			   str_eq(prop, "overflow-x") ||
 			   str_eq(prop, "overflow-y")) {
@@ -6103,7 +6270,7 @@ static void parse_decls(struct br_doc *d, const char *s,
 		} else if (str_eq(prop, "transform")) {
 			const char *v = s + vstart;
 			unsigned int vl = vend - vstart;
-			int tx = 0, ty = 0;
+			int tx = 0, ty = 0, txp = 0, typ = 0;
 			int is_x = contains_ci_len(v, vl, "translatex");
 			int is_y = contains_ci_len(v, vl, "translatey");
 			unsigned int off = 0, arg;
@@ -6114,13 +6281,15 @@ static void parse_decls(struct br_doc *d, const char *s,
 				if (off < vl)
 					off++;
 				if (!is_y)
-					tx = parse_len(v, vl, &off);
+					tx = translate_arg(v, vl, &off, &txp);
 				if (!is_x) {
 					while (off < vl &&
 					       (is_space(v[off]) || v[off] == ','))
 						off++;
-					ty = parse_len(v, vl, &off);
+					ty = translate_arg(v, vl, &off, &typ);
 				}
+				decl_add(out, nout, PROP_TX_PCT, txp, important);
+				decl_add(out, nout, PROP_TY_PCT, typ, important);
 				decl_add(out, nout, PROP_TRANSFORM_X, tx,
 					 important);
 				decl_add(out, nout, PROP_TRANSFORM_Y, ty,
@@ -6167,6 +6336,12 @@ static void parse_decls(struct br_doc *d, const char *s,
 						 important);
 				}
 			}
+		} else if (str_eq(prop, "transition") ||
+			   str_eq(prop, "transition-property") ||
+			   str_eq(prop, "transition-duration") ||
+			   str_eq(prop, "transition-timing-function")) {
+			add_transition(out, nout, prop, s + vstart,
+				       vend - vstart, important);
 		} else if (str_eq(prop, "transform-origin")) {
 			add_transform_origin(out, nout, s + vstart,
 					     vend - vstart, important);
@@ -8848,7 +9023,8 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 		st->vis_hidden = (unsigned char)(dcl->val != 0);
 		break;
 	case PROP_OPACITY_HIDDEN:
-		st->opacity_hidden = (unsigned char)(dcl->val != 0);
+		st->opacity = (unsigned char)(255 - (dcl->val & 255));
+		st->opacity_hidden = (unsigned char)(st->opacity == 0);
 		break;
 	case PROP_FILL_MODE:
 		st->fill_mode = (unsigned char)dcl->val;
@@ -9039,6 +9215,24 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 	case PROP_CURSOR:
 		st->cursor = (unsigned char)dcl->val;
 		break;
+	case PROP_PE_NONE:
+		st->pe_none = (unsigned char)(dcl->val != 0);
+		break;
+	case PROP_TX_PCT:
+		st->tx_pct = (unsigned char)(dcl->val != 0);
+		break;
+	case PROP_TY_PCT:
+		st->ty_pct = (unsigned char)(dcl->val != 0);
+		break;
+	case PROP_TR_MASK:
+		st->tr_mask = (unsigned char)dcl->val;
+		break;
+	case PROP_TR_MS:
+		st->tr_ms = (unsigned short)dcl->val;
+		break;
+	case PROP_TR_EASE:
+		st->tr_ease = (unsigned char)dcl->val;
+		break;
 	case PROP_FLOAT:
 		st->float_left = (unsigned char)dcl->val;
 		break;
@@ -9287,7 +9481,12 @@ void br_style_for(struct br_doc *d, unsigned int node,
 	out->z_index = 0;
 	out->z_set = 0;
 	out->tx = out->ty = 0;
+	out->tx_pct = out->ty_pct = 0;
 	out->transform_set = 0;
+	out->opacity = 255;
+	out->tr_mask = 0;
+	out->tr_ms = 0;
+	out->tr_ease = BR_EASE_EASE;
 	out->transform_scale = 0;
 	out->scale_x = out->scale_y = 100;
 	out->rotate_x100 = 0;

+ 15 - 0
user/apps/browser/browser_html.h

@@ -11,6 +11,15 @@
 #define BR_CURSOR_AUTO 0
 
 #define BR_MAX_NODES     16384
+
+/* CSS transitions the engine animates (br_style.tr_mask). */
+#define BR_TR_OPACITY   1
+#define BR_TR_TRANSFORM 2
+#define BR_EASE_LINEAR  0
+#define BR_EASE_EASE    1
+#define BR_EASE_IN      2
+#define BR_EASE_OUT     3
+#define BR_EASE_IN_OUT  4
 #define BR_TEXT_SIZE     524288
 #define BR_ATTR_SIZE     4194304
 #define BR_CSS_SIZE      262144  /* inline <style> text; google.com has ~94 KB */
@@ -163,7 +172,12 @@ struct br_style {
 	signed short z_index;
 	unsigned char z_set;
 	signed short tx, ty;
+	unsigned char tx_pct, ty_pct; /* tx/ty are percent of the own box */
 	unsigned char transform_set;
+	unsigned char opacity;      /* 0..255 (255 opaque); not inherited */
+	unsigned char tr_mask;      /* BR_TR_*: transitioned properties */
+	unsigned char tr_ease;      /* BR_EASE_* */
+	unsigned short tr_ms;       /* transition duration */
 	unsigned char transform_scale; /* scale()/rotate() paint hint */
 	signed short scale_x, scale_y; /* hundredths, 100 = 1.0 */
 	signed short rotate_x100;      /* hundredths of a degree */
@@ -174,6 +188,7 @@ struct br_style {
 	unsigned char list_item;    /* li marker is drawn */
 	unsigned char nowrap;       /* white-space: nowrap (inherited) */
 	unsigned char uppercase;    /* text-transform: uppercase (inherited) */
+	unsigned char pe_none;      /* pointer-events: none (inherited) */
 	unsigned char cursor;       /* CSS cursor (inherited): BR_CURSOR_AUTO or
 				       enum cursor_shape + 1 */
 	unsigned char border_w[4];  /* top, right, bottom, left */

Некоторые файлы не были показаны из-за большого количества измененных файлов