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
7.0 KiB141 linesraw
1/**
2 * Where an anchored panel (a menu, a popover) goes, in a page that may be scaled (#4653).
3 *
4 * "Interface size" in Settings scales the whole page with the CSS `zoom` property on the root. In Chromium (WebView2) and the engines
5 * that follow the standard, `getBoundingClientRect()` and `window.innerWidth` then report what is on the screen (visual pixels), but the
6 * `left`, `top`, `width` and `max-height` written on an element are in the element's own zoomed pixels: 100 written is 125 on the screen
7 * at 125%. A menu placed from the anchor's rectangle and written back unchanged therefore lands (scale - 1) of its distance too far
8 * right and down, which at the bottom-left of the window (the sidebar's "More" item) is off the screen: the click "does nothing".
9 *
10 * Everything here works in visual pixels and divides by the scale only at the end, when the numbers become CSS values.
11 * `measureScale()` reads the scale from the page itself (a probe element), so it is right whichever way an engine counts.
12 */
13
14export interface Box { left: number; top: number; right: number; bottom: number }
15export interface Size { width: number; height: number }
16
17export interface Placement {
18    /** CSS values for `position: fixed` (the element's own pixels). */
19    left: number;
20    top: number;
21    maxWidth: number;
22    maxHeight: number;
23    /** True when the panel sits above its anchor. */
24    above: boolean;
25    /** Where the panel is on the screen (visual pixels): for checks. */
26    visual: Box;
27}
28
29/** The least room (screen pixels) on one side of the anchor that is worth shortening the panel to. */
30const MIN_ROOM = 120;
31
32const finite = (n: number, fallback: number) => (Number.isFinite(n) ? n : fallback);
33
34/** A scale that makes sense: engines and tests may hand over 0, NaN or nonsense. */
35export function sane(scale: number): number {
36    return Number.isFinite(scale) && scale >= 0.25 && scale <= 4 ? scale : 1;
37}
38
39export interface PlaceOptions {
40    anchor: Box;
41    /** The panel's size on the screen when it has all the room it wants. */
42    size: Size;
43    viewport: Size;
44    /** Visual pixels per CSS pixel of the panel (1.25 at "Interface size" 125%). */
45    scale: number;
46    /** Air between the screen edge and the panel. */
47    margin?: number;
48    /** Air between the anchor and the panel when it opens below / above. */
49    gapBelow?: number;
50    gapAbove?: number;
51    /** Open to the right of the anchor, top aligned (a tooltip beside a sidebar row), instead of below / above. */
52    side?: boolean;
53}
54
55/**
56 * Below the anchor when it fits, otherwise above it, otherwise on the side with more room, shortened to fit (the panel scrolls).
57 * Left aligned with the anchor and kept inside the screen. Works for any viewport and scale.
58 */
59export function placeAnchored(o: PlaceOptions): Placement {
60    const scale = sane(o.scale);
61    const m = o.margin ?? 12;
62    const vw = Math.max(0, finite(o.viewport.width, 0)), vh = Math.max(0, finite(o.viewport.height, 0));
63    const maxW = Math.max(0, vw - 2 * m);
64    const width = Math.min(Math.max(0, finite(o.size.width, 0)), maxW);
65    let left: number, top: number, above = false, maxH: number;
66    if (o.side) {
67        left = Math.min(o.anchor.right + (o.gapBelow ?? 12), Math.max(m, vw - width - m));
68        maxH = Math.max(0, vh - 2 * m);
69        const height = Math.min(Math.max(0, finite(o.size.height, 0)), maxH);
70        top = Math.max(m, Math.min(o.anchor.top, vh - height - m));
71    }
72    else {
73        left = Math.max(m, Math.min(o.anchor.left, vw - width - m));
74        const gapBelow = o.gapBelow ?? 8, gapAbove = o.gapAbove ?? 6;
75        const height = Math.max(0, finite(o.size.height, 0));
76        const roomBelow = vh - (o.anchor.bottom + gapBelow) - m;
77        const roomAbove = o.anchor.top - gapAbove - m;
78        above = height > roomBelow && roomAbove > roomBelow;
79        const room = Math.max(0, above ? roomAbove : roomBelow);
80        if (room < Math.min(height, MIN_ROOM)) {
81            // a short window: neither side has room worth using, so the panel covers part of its anchor rather than shrinking to nothing
82            above = false;
83            maxH = Math.max(0, vh - 2 * m);
84            const shown = Math.min(height, maxH);
85            top = Math.max(m, Math.min(o.anchor.top, vh - shown - m));
86        }
87        else {
88            maxH = room;
89            const shown = Math.min(height, room);
90            top = above ? o.anchor.top - gapAbove - shown : o.anchor.bottom + gapBelow;
91        }
92    }
93    const heightShown = Math.min(Math.max(0, finite(o.size.height, 0)), maxH);
94    return {
95        left: left / scale,
96        top: top / scale,
97        maxWidth: maxW / scale,
98        maxHeight: maxH / scale,
99        above,
100        visual: { left, top, right: left + width, bottom: top + heightShown },
101    };
102}
103
104/** The scale and the window's size, both in the units `getBoundingClientRect()` uses on this engine. */
105export interface Frame { scale: number; viewport: Size }
106
107/**
108 * Visual pixels per CSS pixel where `host` lives, and the window's size in the same units as the rectangles, found by measuring: a probe 100 CSS
109 * pixels wide and a fixed box over the whole window are put in, read and taken out.
110 *
111 * Chromium before 128 (WebView2 122 on a Windows 11 that was never updated: QAGUI-STRIX 11-09) does not follow the standard: under a zoomed root it
112 * reports rectangles in the page's own pixels while `window.innerWidth` stays in screen pixels. The probe then says 1 (right: the rectangles need no
113 * dividing there), but a window size taken from `innerWidth` is the zoom too large and the room below an anchor at the foot of the window looks
114 * big enough: the menu went below the sidebar's More item, off the window, at 125% and up. The fixed box is measured like every other rectangle, so
115 * the anchor and the window are always in the same units, whichever way the engine counts.
116 */
117export function measureFrame(host: HTMLElement = document.body): Frame {
118    const fallback = (): Size => (typeof window === 'undefined' ? { width: 0, height: 0 } : { width: window.innerWidth, height: window.innerHeight });
119    try {
120        const probe = document.createElement('div');
121        probe.style.cssText = 'position:fixed;left:0;top:0;width:100px;height:1px;visibility:hidden;pointer-events:none;';
122        const frame = document.createElement('div');
123        frame.style.cssText = 'position:fixed;left:0;top:0;right:0;bottom:0;visibility:hidden;pointer-events:none;';
124        host.appendChild(probe);
125        host.appendChild(frame);
126        const scale = probe.getBoundingClientRect().width / 100;
127        const r = frame.getBoundingClientRect();
128        probe.remove();
129        frame.remove();
130        const viewport = r.width > 0 && r.height > 0 ? { width: r.width, height: r.height } : fallback();
131        return { scale: sane(scale), viewport };
132    }
133    catch {
134        return { scale: 1, viewport: fallback() };
135    }
136}
137
138/** Visual pixels per CSS pixel where `host` lives (see `measureFrame`). */
139export function measureScale(host: HTMLElement = document.body): number {
140    return measureFrame(host).scale;
141}