okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
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). */ 19left :number ; 20top :number ; 21maxWidth :number ; 22maxHeight :number ; 23/** True when the panel sits above its anchor. */ 24above :boolean ; 25/** Where the panel is on the screen (visual pixels): for checks. */ 26visual :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 { 36return Number . isFinite ( scale ) && scale >= 0.25 && scale <= 4 ?scale :1 ; 37} 38 39export interface PlaceOptions { 40anchor :Box ; 41/** The panel's size on the screen when it has all the room it wants. */ 42size :Size ; 43viewport :Size ; 44/** Visual pixels per CSS pixel of the panel (1.25 at "Interface size" 125%). */ 45scale :number ; 46/** Air between the screen edge and the panel. */ 47margin ?:number ; 48/** Air between the anchor and the panel when it opens below / above. */ 49gapBelow ?:number ; 50gapAbove ?:number ; 51/** Open to the right of the anchor, top aligned (a tooltip beside a sidebar row), instead of below / above. */ 52side ?: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 { 60const scale = sane ( o . scale ); 61const m = o . margin ?? 12 ; 62const vw = Math . max ( 0 , finite ( o . viewport . width , 0 )), vh = Math . max ( 0 , finite ( o . viewport . height , 0 )); 63const maxW = Math . max ( 0 , vw - 2 * m ); 64const width = Math . min ( Math . max ( 0 , finite ( o . size . width , 0 )), maxW ); 65let left :number , top :number , above = false , maxH :number ; 66if ( o . side ) { 67left = Math . min ( o . anchor . right + ( o . gapBelow ?? 12 ), Math . max ( m , vw - width - m )); 68maxH = Math . max ( 0 , vh - 2 * m ); 69const height = Math . min ( Math . max ( 0 , finite ( o . size . height , 0 )), maxH ); 70top = Math . max ( m , Math . min ( o . anchor . top , vh - height - m )); 71} 72else { 73left = Math . max ( m , Math . min ( o . anchor . left , vw - width - m )); 74const gapBelow = o . gapBelow ?? 8 , gapAbove = o . gapAbove ?? 6 ; 75const height = Math . max ( 0 , finite ( o . size . height , 0 )); 76const roomBelow = vh - ( o . anchor . bottom + gapBelow ) - m ; 77const roomAbove = o . anchor . top - gapAbove - m ; 78above = height > roomBelow && roomAbove > roomBelow ; 79const room = Math . max ( 0 , above ?roomAbove :roomBelow ); 80if ( 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 82above = false ; 83maxH = Math . max ( 0 , vh - 2 * m ); 84const shown = Math . min ( height , maxH ); 85top = Math . max ( m , Math . min ( o . anchor . top , vh - shown - m )); 86} 87else { 88maxH = room ; 89const shown = Math . min ( height , room ); 90top = above ?o . anchor . top - gapAbove - shown :o . anchor . bottom + gapBelow ; 91} 92} 93const heightShown = Math . min ( Math . max ( 0 , finite ( o . size . height , 0 )), maxH ); 94return { 95left :left / scale , 96top :top / scale , 97maxWidth :maxW / scale , 98maxHeight :maxH / scale , 99 above, 100visual :{ 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 { 118const fallback = () :Size => ( typeof window === 'undefined' ?{ width :0 , height :0 } :{ width :window . innerWidth , height :window . innerHeight }); 119try { 120const probe = document . createElement ( 'div' ); 121probe . style . cssText = 'position:fixed;left:0;top:0;width:100px;height:1px;visibility:hidden;pointer-events:none;' ; 122const frame = document . createElement ( 'div' ); 123frame . style . cssText = 'position:fixed;left:0;top:0;right:0;bottom:0;visibility:hidden;pointer-events:none;' ; 124host . appendChild ( probe ); 125host . appendChild ( frame ); 126const scale = probe . getBoundingClientRect (). width / 100 ; 127const r = frame . getBoundingClientRect (); 128probe . remove (); 129frame . remove (); 130const viewport = r . width > 0 && r . height > 0 ?{ width :r . width , height :r . height } :fallback (); 131return { scale :sane ( scale ), viewport}; 132} 133catch { 134return { 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 { 140return measureFrame ( host ). scale ; 141}