okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main (87dfca0e7) into f-own-int: the 0.2.14 app branch, fixtures and CI fixes, the Qwen4-readiness tools, the shape-engine build scriptsffb3d9b31

main
13.8 KiB198 linesraw
1// #4653: the sidebar "More" menu did nothing at "Interface size" 125% on Windows. The page is scaled with CSS `zoom`; WebView2 reports the anchor
2// in screen pixels but writes left/top in the page's own pixels, so the menu landed (scale - 1) of its distance too far right and down: off the
3// screen at the bottom-left. The placement is now a pure function in screen pixels, divided by the scale at the end.
4// Real engine check (Edge 156 = the WebView2 runtime): phaseA-tests/round0213/guidata-shell/more-menu.mjs (14 of 18 cases broken before, 0 after).
5import assert from 'node:assert/strict';
6import test from 'node:test';
7import { readFileSync } from 'node:fs';
8import { resolve } from 'node:path';
9import { transform } from 'esbuild';
10
11const root = resolve(import.meta.dirname, '..');
12const { code } = await transform(readFileSync(resolve(root, 'src/lib/overlay-place.ts'), 'utf8'), { loader: 'ts', format: 'esm' });
13const { placeAnchored, measureScale, measureFrame, sane } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
14
15const VIEWPORTS = [[1920, 1080], [1536, 864], [1280, 720], [1024, 700], [900, 600], [640, 480], [360, 640]];
16const SCALES = [0.9, 1, 1.1, 1.25, 1.5, 1.75, 2];   // the "Interface size" choices (store.ts)
17const box = (left, top, width, height) => ({ left, top, right: left + width, bottom: top + height });
18
19/** The way the menu was placed before: the anchor's rectangle written back as CSS values (FloatingMenu.tsx at a3a10b90a). */
20function before(anchor, size, vp, scale) {
21  const left = Math.max(12, Math.min(anchor.left, vp.width - Math.min(size.width, vp.width - 24) - 12));
22  const below = anchor.bottom + 8;
23  const top = below + size.height + 12 <= vp.height ? below : Math.max(12, anchor.top - size.height - 6);
24  return { left: left * scale, top: top * scale, bottom: top * scale + size.height };   // what the person sees: CSS value times the scale
25}
26
27test('the old placement is off the screen at the bottom-left once the page is scaled (the bug)', () => {
28  const vp = { width: 1920, height: 1080 };
29  const anchor = box(15, 1020, 232, 36);               // the More item at the foot of the sidebar, seen at 125%
30  const seen = before(anchor, { width: 232 * 1.25, height: 107 }, vp, 1.25);
31  assert.ok(seen.bottom > vp.height, `the menu's bottom edge is at ${seen.bottom} of ${vp.height}`);
32  assert.equal(before(box(12, 1020, 232, 36), { width: 232, height: 86 }, vp, 1).bottom <= vp.height, true, 'and fine at 100%');
33});
34
35test('everywhere, at every scale, the panel is on the screen and its CSS values are the screen values divided by the scale', () => {
36  for (const [w, h] of VIEWPORTS) for (const scale of SCALES) {
37    const vp = { width: w, height: h };
38    const anchors = [box(12, h - 60, 232, 40), box(12, 8, 232, 40), box(w - 260, h / 2, 40, 40), box(w / 2 - 20, h / 2 - 20, 40, 40), box(w - 30, h - 40, 24, 24)];
39    for (const anchor of anchors) for (const menuH of [60, 107, 320, 2000]) {
40      const size = { width: 232 * scale, height: menuH * scale };
41      const p = placeAnchored({ anchor, size, viewport: vp, scale });
42      const tag = `${w}x${h} scale ${scale} anchor ${JSON.stringify(anchor)} menu ${menuH}`;
43      // CSS value x scale is where it really is
44      assert.ok(Math.abs(p.left * scale - p.visual.left) < 1e-6, tag);
45      assert.ok(Math.abs(p.top * scale - p.visual.top) < 1e-6, tag);
46      // inside the screen with the margin (the panel shrinks and scrolls when it is taller than the room)
47      assert.ok(p.visual.left >= 12 - 1e-6 && p.visual.top >= 12 - 1e-6, tag + ' top-left ' + JSON.stringify(p.visual));
48      assert.ok(p.visual.right <= w - 12 + 1e-6 || w < 24 + 232 * scale, tag + ' right ' + JSON.stringify(p.visual));
49      assert.ok(p.visual.bottom <= h - 12 + 1e-6, tag + ' bottom ' + JSON.stringify(p.visual));
50      // a limit it can be shortened to, in CSS pixels, that keeps it inside
51      assert.ok(p.maxHeight * scale <= h - 24 + 1e-6, tag);
52      assert.ok(p.maxWidth * scale <= w - 24 + 1e-6, tag);
53    }
54  }
55});
56
57test('the More menu (bottom-left of the window) opens above the item, whole, at every scale of the 1080p window', () => {
58  for (const scale of SCALES) {
59    const vp = { width: 1920, height: 1080 };
60    const anchor = box(12 * scale, 1080 - 12 - 40 * scale, 232 * scale, 40 * scale);
61    const p = placeAnchored({ anchor, size: { width: 232 * scale, height: 86 * scale }, viewport: vp, scale });
62    assert.equal(p.above, true, 'scale ' + scale);
63    assert.ok(p.visual.bottom <= anchor.top, 'does not cover the item');
64    assert.ok(Math.abs(p.visual.bottom - p.visual.top - 86 * scale) < 1e-6, 'its whole height is shown');
65  }
66});
67
68test('below the anchor when it fits; above when only there it fits; the side with more room, shortened, when neither', () => {
69  const vp = { width: 1000, height: 700 };
70  const mid = placeAnchored({ anchor: box(100, 100, 100, 30), size: { width: 200, height: 150 }, viewport: vp, scale: 1 });
71  assert.equal(mid.above, false); assert.equal(mid.top, 138);
72  const low = placeAnchored({ anchor: box(100, 640, 100, 30), size: { width: 200, height: 150 }, viewport: vp, scale: 1 });
73  assert.equal(low.above, true); assert.equal(low.top, 640 - 6 - 150);
74  const tall = placeAnchored({ anchor: box(100, 300, 100, 30), size: { width: 200, height: 900 }, viewport: vp, scale: 1 });
75  assert.ok(tall.maxHeight <= 700 - 24 && tall.maxHeight > 250, 'shortened to the larger side: ' + tall.maxHeight);
76  assert.ok(tall.visual.top >= 12 && tall.visual.bottom <= 688);
77});
78
79test('a short window with no room on either side: the panel covers part of its anchor instead of shrinking to nothing', () => {
80  const p = placeAnchored({ anchor: box(100, 80, 100, 40), size: { width: 200, height: 150 }, viewport: { width: 1000, height: 200 }, scale: 1 });
81  assert.equal(p.above, false);
82  assert.ok(p.maxHeight >= 150, 'its whole height is allowed: ' + p.maxHeight);
83  assert.ok(p.visual.top >= 12 && p.visual.bottom <= 188, JSON.stringify(p.visual));
84});
85
86test('left aligned with the anchor, pushed back inside when it would run off the right edge', () => {
87  const p = placeAnchored({ anchor: box(900, 100, 80, 30), size: { width: 232, height: 100 }, viewport: { width: 1000, height: 700 }, scale: 1 });
88  assert.equal(p.visual.right, 1000 - 12);
89  const q = placeAnchored({ anchor: box(300, 100, 80, 30), size: { width: 232, height: 100 }, viewport: { width: 1000, height: 700 }, scale: 1 });
90  assert.equal(q.left, 300);
91});
92
93test('a tooltip beside a sidebar row opens to the right, top aligned, and is kept inside', () => {
94  const p = placeAnchored({ anchor: box(10, 400, 240, 32), size: { width: 272 * 1.25, height: 80 * 1.25 }, viewport: { width: 1920, height: 1080 }, scale: 1.25, side: true, gapBelow: 12 });
95  assert.equal(p.visual.left, 262);
96  assert.equal(p.visual.top, 400);
97  const low = placeAnchored({ anchor: box(10, 1060, 240, 32), size: { width: 272, height: 100 }, viewport: { width: 1920, height: 1080 }, scale: 1, side: true, gapBelow: 12 });
98  assert.equal(low.visual.bottom, 1080 - 12);
99});
100
101test('nonsense numbers never throw and never put the panel outside the screen', () => {
102  for (const scale of [0, NaN, -1, Infinity, 100, 0.01]) {
103    assert.equal(sane(scale), 1);
104    const p = placeAnchored({ anchor: box(0, 0, 0, 0), size: { width: 100, height: 100 }, viewport: { width: 800, height: 600 }, scale });
105    assert.ok(Number.isFinite(p.left) && Number.isFinite(p.top));
106  }
107  const p = placeAnchored({ anchor: box(5, 5, 5, 5), size: { width: NaN, height: NaN }, viewport: { width: NaN, height: 0 }, scale: 1 });
108  assert.ok(Number.isFinite(p.left) && Number.isFinite(p.top) && Number.isFinite(p.maxHeight));
109});
110
111test('the scale is measured from the page itself, and a failed measure is 1', () => {
112  const doc = (width) => ({ createElement: () => ({ style: {}, getBoundingClientRect: () => ({ width }), remove() {} }) });
113  const host = { appendChild() {} };
114  globalThis.document = doc(125);
115  assert.equal(measureScale(host), 1.25);
116  globalThis.document = doc(100);
117  assert.equal(measureScale(host), 1);
118  globalThis.document = doc(0);
119  assert.equal(measureScale(host), 1);
120  globalThis.document = { createElement: () => { throw new Error('no DOM'); } };
121  assert.equal(measureScale(host), 1);
122});
123
124test('every anchored panel uses the scale-aware numbers', () => {
125  const read = f => readFileSync(resolve(root, 'src', f), 'utf8');
126  assert.match(read('components/FloatingMenu.tsx'), /placeAnchored\(/);
127  for (const f of ['components/FloatingMenu.tsx', 'components/Popover.tsx', 'components/ui.tsx', 'components/VersionMemoryTooltip.tsx', 'app/Sidebar.tsx', 'screens/Chat.tsx']) {
128    assert.match(read(f), /measureFrame\(\)/, f);
129    // the window's size comes from the same measure as the rectangles (WebView2 122 counts them differently: QAGUI-STRIX 11-09)
130    assert.doesNotMatch(read(f), /window\.inner(Width|Height) - /, f);
131  }
132  assert.doesNotMatch(read('components/FloatingMenu.tsx'), /calc\(100dvh - \$\{placed\.top/, 'the viewport-unit max height is gone');
133});
134
135test('a Select list: as tall as it needs when the window has the room, else all the room above or below, never off the top (owner report, 11-04)', () => {
136  // the owner's case: a short window, a long list, the button in the lower half; the old list was capped at 320 px and opened upward by a
137  // transform, so at 150% it started above the window
138  for (const scale of SCALES) for (const [w, h] of VIEWPORTS) for (const rows of [3, 9, 40, 118]) {
139    const natural = (rows * 38 + 12) * scale;
140    for (const top of [h * 0.2, h * 0.5, h * 0.8]) {
141      const anchor = box(w * 0.6, top, 240, 34 * scale);
142      const p = placeAnchored({ anchor, size: { width: 260 * scale, height: natural }, viewport: { width: w, height: h }, scale, gapBelow: 6, gapAbove: 6 });
143      const tag = `${w}x${h} scale ${scale} rows ${rows} top ${top}`;
144      assert.ok(p.visual.top >= 12 - 1e-6 && p.visual.bottom <= h - 12 + 1e-6, tag + ' ' + JSON.stringify(p.visual));
145      const roomBelow = h - anchor.bottom - 6 - 12, roomAbove = anchor.top - 6 - 12;
146      if (natural <= Math.max(roomBelow, roomAbove) && Math.max(roomBelow, roomAbove) >= 120)
147        assert.ok(Math.abs((p.visual.bottom - p.visual.top) - natural) < 1e-6 || p.maxHeight * scale >= natural - 1e-6, tag + ': it fits, so all of it is shown');
148      else if (Math.max(roomBelow, roomAbove) >= 120)
149        assert.ok(p.maxHeight * scale >= Math.max(roomBelow, roomAbove) - 1e-6, tag + ': too long, so it uses all the room');
150    }
151  }
152});
153
154test('the Select list is placed by the shared function, with no fixed cap and no upward transform', () => {
155  const ui = readFileSync(resolve(root, 'src/components/ui.tsx'), 'utf8');
156  assert.match(ui, /placeAnchored\(\{ anchor: r, size: \{ width, height: natural \}/);
157  assert.match(ui, /maxHeight: placed\.maxHeight/);
158  assert.doesNotMatch(ui, /data-up=\{placed\.up/);
159  assert.doesNotMatch(ui, /Math\.min\(320, options\.length/);
160});
161
162// QAGUI-STRIX 11-09: on WebView2 122 (Chromium before 128) the More menu opened off the window at 125% and up. That engine reports rectangles in the
163// page's own pixels under a zoomed root (a 100 px probe measures 100) while innerWidth/innerHeight stay screen pixels; the current engine reports both
164// in screen pixels. A fake page of each kind: the window's size is read from a fixed box measured like any rectangle, so it matches the anchor.
165// Real engine check: Edge with --disable-blink-features=StandardizedBrowserZoom behaves as the old one (phaseA-tests/par/appwin/menus.mjs).
166function fakeEngine({ zoom, standard, inner }) {
167  const unit = standard ? zoom : 1;   // screen pixels per unit the rectangles are reported in
168  globalThis.document = { createElement: () => { const el = { style: { cssText: '' }, remove() {} };
169    el.getBoundingClientRect = () => el.style.cssText.includes('width:100px') ? { width: 100 * unit, height: unit }
170      : { width: standard ? inner.width : inner.width / zoom, height: standard ? inner.height : inner.height / zoom };
171    return el; } };
172  globalThis.window = { innerWidth: inner.width, innerHeight: inner.height };
173  return { toScreen: css => css * zoom, rect: screen => standard ? screen : screen / zoom };
174}
175
176test('WebView2 122 and the current engine: the More menu at the foot of the sidebar is inside the window at every Interface size', () => {
177  const host = { appendChild() {} };
178  try {
179    for (const standard of [true, false]) for (const [w, h] of [[1440, 920], [1024, 700], [1920, 1080]]) for (const zoom of [0.9, 1, 1.25, 1.5, 1.75]) {
180      const eng = fakeEngine({ zoom, standard, inner: { width: w, height: h } });
181      const { scale, viewport } = measureFrame(host);
182      // the More item on the screen: 12 px from the left and the bottom, 40 CSS px tall, 232 wide; the menu is 107 CSS px tall
183      const item = { left: 12 * zoom, top: h - 52 * zoom, right: 244 * zoom, bottom: h - 12 * zoom };
184      const anchor = { left: eng.rect(item.left), top: eng.rect(item.top), right: eng.rect(item.right), bottom: eng.rect(item.bottom) };
185      const p = placeAnchored({ anchor, size: { width: 232 * scale, height: 107 * scale }, viewport, scale });
186      const top = eng.toScreen(p.top), bottom = top + Math.min(107, p.maxHeight) * zoom;
187      const tag = `${standard ? 'current' : 'WebView2 122'} ${w}x${h} at ${Math.round(zoom * 100)}%`;
188      assert.ok(top >= 0 && bottom <= h, `${tag}: the menu is painted at ${top.toFixed(0)}..${bottom.toFixed(0)} in a ${h} px window`);
189      assert.ok(bottom <= item.top + 1e-6, `${tag}: it does not cover the item`);
190      // the window's size taken from innerWidth/innerHeight (before) puts it off the window on the old engine at 125% and up: the bug
191      if (!standard && zoom >= 1.25 && w === 1440) {
192        const old = placeAnchored({ anchor, size: { width: 232 * scale, height: 107 * scale }, viewport: { width: w, height: h }, scale });
193        assert.ok(eng.toScreen(old.top) + 107 * zoom > h, `${tag}: the old measure was off the window`);
194      }
195    }
196  } finally { delete globalThis.document; delete globalThis.window; }
197});
198