Răsfoiți Sursa

Lay out the Cloudron dashboard: CSS variable scoping, % heights, alignment

The logged-in dashboard rendered its sidebar full-width and centred, with
the app grid pushed below it.  Engine fixes, all general:

- Custom properties: :root/html/body/*/:host definitions win; a
  component-level definition applies only while no other rule gives the
  name a different value, else var() uses its fallback.  Last-wins made
  Font Awesome's --fa-width 100% for every icon, so each menu entry
  measured ~1100 px and the sidebar took the whole row.
- Percentage heights resolve against the containing block's definite
  height (viewport for html/body), as a minimum since inner boxes do not
  scroll; they used to resolve against the width.
- Inline-block and flex rows restore the line alignment, so a centred
  .fa-fw icon no longer centres the label after it.
- Flex-item form controls get a default width; placeholders do not
  scroll; align-items:center centres a flex column's lines.
- offsetWidth/clientWidth fall back to the viewport before layout, and
  window gets a resize event after the first layout and on width changes
  (the app grid measured 0 on mount and chose the phone layout).
Johannes Zellner 1 săptămână în urmă
părinte
comite
40be0da0eb
7 a modificat fișierele cu 185 adăugiri și 11 ștergeri
  1. 18 2
      AGENTS.md
  2. 78 2
      browser.c
  3. 71 5
      browser_html.c
  4. 3 0
      browser_html.h
  5. 0 0
      docs/userland.html
  6. 12 2
      js_browser.c
  7. 3 0
      js_browser.h

+ 18 - 2
AGENTS.md

@@ -1423,7 +1423,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   the Cloudron dashboard would otherwise reload on every menu click).
   Clicks on plain elements with listeners (`div role="button"`) reach the
   innermost laid-out box under the pointer.  `<select>` exposes `options`
-  and `multiple`, and an `<option>` without `value` reports its text.  The
+  and `multiple`, and an `<option>` without `value` reports its text.
+  `offsetWidth`/`clientWidth` of an element without a laid-out box report
+  the viewport width (Vue apps measure on mount and picked a phone layout
+  for 0), and `window` gets a `resize` event after the first layout of a
+  scripted page and whenever the page width changes.  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
   Cloudron dashboard (`my.<domain>`: a Vite/Vue 3 app, 12 modules / 3.5 MB)
   boots, builds its PKCE request, and its OIDC login form renders, submits
   through `fetch()` with the interaction cookie and shows the server's
@@ -2211,7 +2218,16 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     of descendant content, gradients/box-shadow/transitions (a gradient
     background falls back to its last solid colour), or CSS `color-mix`.
     A non-uniform `background-size` uses the width scale for both axes.
-    CSS custom properties (`var()`), flex rows with wrap/grow/gap/justify/
+    CSS custom properties (`var()`: one document-wide table, expanded when
+    a sheet is parsed; definitions on `:root`/`html`/`body`/`*`/`:host`
+    win, a component-level definition applies only while no other rule
+    gives the name a different value, otherwise `var()` uses its fallback
+    -- Font Awesome's competing `--fa-width` values made every icon 100%
+    wide), percentage heights (against the containing block's definite
+    height, `lay.cb_h`, with the viewport for `html`/`body`; used as a
+    minimum since there are no scrollable inner boxes, and auto when the
+    containing height is indefinite -- they used to resolve against the
+    width), flex rows with wrap/grow/gap/justify/
     align, two-track and `repeat(auto-fit, minmax(...))` grids, inline-block
     runs, basic proportional table columns, table rowspan/colspan/percentage
     columns and inline SVG (filled/stroked geometry, `use`/`symbol`,

+ 78 - 2
browser.c

@@ -170,6 +170,9 @@ static unsigned int pending_post_len;
 /* The pending navigation is a POST even when its body is empty (a form
  * without fields, e.g. the OIDC consent auto-submit). */
 static int pending_post_is;
+/* window "resize" bookkeeping: the page width the last event was for. */
+static int resize_sent_w = -1;
+static int resize_pending;
 static unsigned int pending_action;
 static int cancel_fetch;
 static int quit;
@@ -182,6 +185,8 @@ static int page_cache_scroll;
 static unsigned int page_cache_nnode;
 static int page_cache_x, page_cache_y, page_cache_w, page_cache_h;
 static int layout_valid, layout_left, layout_right;
+/* Height of the page viewport: what html/body height:100% resolves to. */
+static int layout_view_h;
 static unsigned int layout_nnode;
 static int layout_content_h;
 
@@ -218,6 +223,8 @@ struct lay {
 	int line_left, line_right;
 	int parent_top;
 	int cb_left, cb_right, cb_top;  /* containing block for absolute boxes */
+	int cb_h;       /* definite content height of the containing block for
+			 * percentage heights; 0 = indefinite (auto) */
 	int run_x;
 	int align;
 	int depth;
@@ -1804,6 +1811,8 @@ static void run_page_scripts(void)
 		js_host_status("JavaScript: out of memory", 1);
 		return;
 	}
+	resize_sent_w = -1;
+	resize_pending = 0;
 	prefetch_module_preloads();
 
 	for (i = 0; i < doc.nscripts; i++) {
@@ -5482,7 +5491,7 @@ static void flush_line(struct lay *l)
 						   l->ui->theme.border);
 					/* A value wider than the field shows its
 					 * tail (where typing happens). */
-					if (editable && w > 12) {
+					if (editable && fs && fs->len && w > 12) {
 						int tw = (int)run_text_width(l->ui,
 							r, txt, tl);
 
@@ -7117,6 +7126,7 @@ static void layout_block(struct lay *l, unsigned int node,
 			 unsigned short link)
 {
 	int old_left = l->left, old_right = l->right, old_align = l->align;
+	int parent_cb_h = l->cb_h;
 	int saved_parent_top = l->parent_top;
 	int saved_cb_left = l->cb_left, saved_cb_right = l->cb_right;
 	int saved_cb_top = l->cb_top;
@@ -7496,6 +7506,21 @@ static void layout_block(struct lay *l, unsigned int node,
 					  st->color);
 			}
 		}
+		/* This box's definite content height, for its children's
+		 * percentage heights. */
+		if (doc.nodes[node].tag == BR_TAG_HTML ||
+		    doc.nodes[node].tag == BR_TAG_BODY)
+			l->cb_h = layout_view_h;
+		else if (st->height_kind == 1)
+			l->cb_h = (int)st->height -
+				  (st->box_sizing ? deco_h : 0);
+		else if (st->height_kind == 2 && parent_cb_h > 0)
+			l->cb_h = parent_cb_h * (int)st->height / 100 -
+				  (st->box_sizing ? deco_h : 0);
+		else
+			l->cb_h = 0;
+		if (l->cb_h < 0)
+			l->cb_h = 0;
 		if (doc.nodes[node].tag == BR_TAG_TR)
 			layout_table_row(l, node, st, depth + 1, link);
 		else if (doc.nodes[node].tag == BR_TAG_TABLE &&
@@ -7509,8 +7534,19 @@ static void layout_block(struct lay *l, unsigned int node,
 		else if (st->display == BR_DISPLAY_FLEX && !st->flex_col &&
 			 flex_wants_row(node, st))
 			layout_flex_children(l, node, st, depth + 1, link);
-		else
+		else if (st->display == BR_DISPLAY_FLEX && st->flex_col &&
+			 st->align_items == 1) {
+			/* align-items: center in a column: the items' lines
+			 * (an app card's icon and label) are centred; a child
+			 * with its own text-align keeps it. */
+			struct br_style cst = *st;
+
+			cst.align = BR_ALIGN_CENTER;
+			l->align = BR_ALIGN_CENTER;
+			layout_children(l, node, &cst, depth + 1, link);
+		} else
 			layout_children(l, node, st, depth + 1, link);
+		l->cb_h = parent_cb_h;
 	}
 	flush_line(l);
 	if (clip_pushed)
@@ -7522,6 +7558,21 @@ static void layout_block(struct lay *l, unsigned int node,
 	}
 	if (doc.nodes[node].tag == BR_TAG_IMG) {
 		content_h = img_dh;
+	} else if (st->height_kind == 2 &&
+		   doc.nodes[node].tag != BR_TAG_HTML &&
+		   doc.nodes[node].tag != BR_TAG_BODY) {
+		/* A percentage of the containing block's definite height, or
+		 * auto when it has none (it used to resolve against the
+		 * width).  The result is a minimum: without scrollable inner
+		 * boxes, an app shell's height:100%; overflow:hidden column
+		 * would otherwise clip everything past the first screen. */
+		int auto_h = l->y - (top + bt + pt);
+
+		content_h = parent_cb_h > 0 ?
+			    parent_cb_h * (int)st->height / 100 -
+			    (st->box_sizing ? deco_h : 0) : 0;
+		if (content_h < auto_h)
+			content_h = auto_h;
 	} else if (st->height_kind &&
 	    !((doc.nodes[node].tag == BR_TAG_HTML ||
 	       doc.nodes[node].tag == BR_TAG_BODY) &&
@@ -7932,6 +7983,11 @@ static int flex_item_width(struct lay *l, unsigned int n,
 			w = (int)nd->width_attr;
 		else
 			w = 64;
+	} else if (nd->tag == BR_TAG_INPUT || nd->tag == BR_TAG_SELECT ||
+		   nd->tag == BR_TAG_TEXTAREA) {
+		/* A control has no content to measure: its default size
+		 * (a search field in a filter bar). */
+		w = nd->tag == BR_TAG_TEXTAREA ? 180 : 150;
 	} else {
 		w = intrinsic_width(l->ui, n, cs, 0, avail);
 	}
@@ -7960,6 +8016,10 @@ static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
 	int row_left = l->left, row_right = l->right;
 	int avail = row_right - row_left;
 	int x = row_left, y = l->y, row_h = 0, any = 0;
+	/* Each box sets l->align from its own style; the text after the row
+	 * must keep the container's (a centred .fa-fw icon centred the menu
+	 * label that followed it). */
+	unsigned char row_align = l->align;
 
 	flush_line(l);
 	y = l->y;
@@ -8026,6 +8086,7 @@ static unsigned int layout_inline_blocks(struct lay *l, unsigned int first,
 		y += row_h;
 	l->left = row_left;
 	l->right = row_right;
+	l->align = row_align;
 	l->y = y;
 	l->line_x = l->left;
 	l->run_x = l->left;
@@ -8052,6 +8113,7 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 	int row_left = l->left, row_right = l->right;
 	int avail = row_right - row_left;
 	int idx, row_top;
+	unsigned char row_align = l->align;
 
 	if (avail < 160 || depth > 48) {
 		layout_children(l, parent, pst, depth, link);
@@ -8202,6 +8264,7 @@ static void layout_flex_children(struct lay *l, unsigned int parent,
 	}
 	l->left = row_left;
 	l->right = row_right;
+	l->align = row_align;
 	l->y = row_top;
 	l->line_x = l->left;
 	l->run_x = l->left;
@@ -9420,6 +9483,7 @@ static int measure_page(int left, int right)
 	l->line_y = 0;
 	l->line_h = 0;
 	l->align = BR_ALIGN_LEFT;
+	l->cb_h = layout_view_h;
 	l->float_active = 0;
 	l->parent_top = 0;
 	l->cb_left = left;
@@ -9477,6 +9541,7 @@ static void render_page(int left, int right, int y0)
 	l->line_y = y0;
 	l->line_h = 0;
 	l->align = BR_ALIGN_LEFT;
+	l->cb_h = layout_view_h;
 	l->float_active = 0;
 	l->parent_top = 0;
 	l->cb_left = left;
@@ -9698,11 +9763,17 @@ static void draw_page(struct ngui_rect page)
 	right = page.x + page.w - 16;
 	if (!layout_valid || left != layout_left || right != layout_right ||
 	    layout_nnode != doc.nnode) {
+		layout_view_h = page.h - 8;
 		layout_content_h = measure_page(left, right);
 		layout_valid = 1;
 		layout_left = left;
 		layout_right = right;
 		layout_nnode = doc.nnode;
+		/* Scripts that measured before any layout (offsetWidth on
+		 * mount) get a resize event once real boxes exist, and again
+		 * whenever the page width changes. */
+		if (js_browser_active() && right - left != resize_sent_w)
+			resize_pending = 1;
 	}
 	ch = layout_content_h;
 	page_scroll.content_h = ch;
@@ -10449,6 +10520,11 @@ void _start(const char *arg)
 			preload_active = 1;
 		}
 		js_browser_tick();
+		if (resize_pending && !pending_action) {
+			resize_pending = 0;
+			resize_sent_w = layout_right - layout_left;
+			js_browser_window_event("resize");
+		}
 		persist_tick();
 		if (pending_action)
 			continue;

+ 71 - 5
browser_html.c

@@ -594,18 +594,42 @@ static unsigned int css_var_find(struct br_doc *d, const char *name,
 	}
 }
 
+#define BR_VAR_ROOT     1
+#define BR_VAR_CONFLICT 2
+
+/* Custom properties live in one document-wide table (var() is expanded
+ * when a sheet is parsed, not per element).  Definitions on :root, html,
+ * body or * are the theme and always apply, last one winning.  A definition
+ * on any other selector is a local override: it is used while it is the
+ * only value seen for that name, and a second, different local value marks
+ * the name ambiguous so var() falls back.  Last-wins across components was
+ * wrong: Font Awesome sets --fa-width to 100% on .fa-stack-1x and 1.25em on
+ * .fa-fw, and every icon became 100% wide. */
 static void css_var_set(struct br_doc *d, const char *name,
-			unsigned int nlen, const char *val, unsigned int vlen)
+			unsigned int nlen, const char *val, unsigned int vlen,
+			int root)
 {
 	unsigned int i, noff, voff;
 
 	if (nlen < 3 || name[0] != '-' || name[1] != '-' || !vlen)
 		return;
+	i = css_var_find(d, name, nlen);
+	if (i < BR_MAX_VARS && !root) {
+		const char *old = attr_ptr(d, d->var_val[i]);
+		unsigned int k = 0;
+
+		if (d->var_flags[i] & BR_VAR_ROOT)
+			return;
+		while (k < vlen && old[k] == val[k])
+			k++;
+		if (!(k == vlen && old[k] == '\0'))
+			d->var_flags[i] |= BR_VAR_CONFLICT;
+		return;
+	}
 	noff = store_raw(d, name, nlen, 63);
 	voff = store_raw(d, val, vlen, 199);
 	if (!noff || !voff)
 		return;
-	i = css_var_find(d, name, nlen);
 	if (i >= BR_MAX_VARS) {
 		unsigned int h;
 
@@ -617,8 +641,45 @@ static void css_var_set(struct br_doc *d, const char *name,
 		while (d->var_slot[h])
 			h = (h + 1) & (VAR_HASH_SIZE - 1);
 		d->var_slot[h] = (unsigned short)(i + 1);
+		d->var_flags[i] = 0;
 	}
 	d->var_val[i] = voff;
+	if (root)
+		d->var_flags[i] = BR_VAR_ROOT;
+}
+
+/* 1 when every selector of the list targets the document root (the theme
+ * scope for custom properties): :root, html, body, * or :host, optionally
+ * with classes/attributes but no combinator. */
+static int css_root_selector(const char *s, unsigned int len)
+{
+	unsigned int i = 0;
+
+	while (i < len) {
+		unsigned int st, en;
+
+		while (i < len && (is_space(s[i]) || s[i] == ','))
+			i++;
+		if (i >= len)
+			break;
+		st = i;
+		while (i < len && s[i] != ',')
+			i++;
+		en = i;
+		while (en > st && is_space(s[en - 1]))
+			en--;
+		if (!((en - st >= 5 && ci_eq_n(s + st, ":root", 5)) ||
+		      (en - st >= 5 && ci_eq_n(s + st, ":host", 5)) ||
+		      (en - st >= 4 && ci_eq_n(s + st, "html", 4)) ||
+		      (en - st >= 4 && ci_eq_n(s + st, "body", 4)) ||
+		      s[st] == '*'))
+			return 0;
+		for (; st < en; st++)
+			if (is_space(s[st]) || s[st] == '>' || s[st] == '+' ||
+			    s[st] == '~')
+				return 0;
+	}
+	return 1;
 }
 
 /* Find the end of a declaration value: the next ';' outside quotes and
@@ -691,7 +752,7 @@ static int css_dark_selector(const char *s, unsigned int len)
 
 /* Collect --custom-property definitions from one declaration block. */
 static void css_collect_block(struct br_doc *d, const char *s,
-			      unsigned int len)
+			      unsigned int len, int root)
 {
 	unsigned int i = 0;
 
@@ -734,7 +795,7 @@ static void css_collect_block(struct br_doc *d, const char *s,
 		if (pend - pstart >= 3 && s[pstart] == '-' &&
 		    s[pstart + 1] == '-')
 			css_var_set(d, s + pstart, pend - pstart,
-				    s + vstart, vend - vstart);
+				    s + vstart, vend - vstart, root);
 	}
 }
 
@@ -3683,6 +3744,9 @@ static unsigned int css_expand_vars(struct br_doc *d, const char *s,
 		if (nlen >= 3 && s[inner] == '-' && s[inner + 1] == '-') {
 			unsigned int vi = css_var_find(d, s + inner, nlen);
 
+			if (vi < BR_MAX_VARS &&
+			    (d->var_flags[vi] & BR_VAR_CONFLICT))
+				vi = BR_MAX_VARS;
 			if (vi < BR_MAX_VARS) {
 				const char *v = attr_ptr(d, d->var_val[vi]);
 				unsigned int vn = css_expand_vars(d, v, s_len(v),
@@ -6058,7 +6122,9 @@ static void parse_css(struct br_doc *d, const char *s, unsigned int len)
 		if (sstart == send)
 			continue;
 		if (!css_dark_selector(s + sstart, send - sstart))
-			css_collect_block(d, s + bstart, bend - bstart);
+			css_collect_block(d, s + bstart, bend - bstart,
+					  css_root_selector(s + sstart,
+							    send - sstart));
 		if (css_collect_only)
 			continue;
 		parse_decls(d, s + bstart, bend - bstart, decls, &nd);

+ 3 - 0
browser_html.h

@@ -286,6 +286,9 @@ struct br_doc {
 	 * single table: the last definition wins, which matches :root. */
 	unsigned int var_name[BR_MAX_VARS];
 	unsigned int var_val[BR_MAX_VARS];
+	/* BR_VAR_ROOT: set by a :root, html, body or * rule; BR_VAR_CONFLICT: two
+	 * component rules disagree, so var() uses its fallback. */
+	unsigned char var_flags[BR_MAX_VARS];
 	unsigned short var_slot[1024];
 	unsigned int nvars;
 	/* Rule index: rightmost class/id/tag -> linked list of rule indices. */

Fișier diff suprimat deoarece este prea mare
+ 0 - 0
docs/userland.html


+ 12 - 2
js_browser.c

@@ -2115,8 +2115,12 @@ static int node_has(js_State *state, void *p, const char *name)
 	    !strcmp(name, "scrollWidth") || !strcmp(name, "scrollHeight")) {
 		int x = 0, y = 0, w = 0, h = 0;
 
-		if (host.rect)
-			host.rect(node, &x, &y, &w, &h);
+		/* Before the first layout an element has no box; a block's
+		 * width is then best guessed as the viewport (Vue apps measure
+		 * offsetWidth on mount and pick a phone layout for 0).  A
+		 * resize event after layout lets them measure again. */
+		if (!host.rect || !host.rect(node, &x, &y, &w, &h))
+			w = n->type == BR_NODE_ELEM ? (int)host.width : 0;
 		js_pushnumber(state, (!strcmp(name, "offsetHeight") ||
 				      !strcmp(name, "clientHeight") ||
 				      !strcmp(name, "scrollHeight")) ? h : w);
@@ -3277,6 +3281,12 @@ static void report_top(js_State *state)
 	js_pop(state, 1);
 }
 
+void js_browser_window_event(const char *type)
+{
+	if (J && D)
+		js_browser_event(JS_TARGET_WINDOW, type, 0, 0);
+}
+
 void js_browser_hashchange(void)
 {
 	hash_pending = 1;

+ 3 - 0
js_browser.h

@@ -76,6 +76,9 @@ int js_browser_run_module(const char *source, const char *url);
  * a click on <a href="#/apps">); it fires from the next tick. */
 void js_browser_hashchange(void);
 
+/* Dispatch a plain event (e.g. "resize") on window now. */
+void js_browser_window_event(const char *type);
+
 /* Run due setTimeout/setInterval callbacks; call once per UI loop. */
 void js_browser_tick(void);
 

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff