From 79ea7858caeaa9922218d5f208945f9a44d7e2b7 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 25 Oct 2026 16:17:04 -0700 Subject: [PATCH] Fix double-dimming when app menu submenus are open --- app/ui/layers.ts | 13 ++++++++++++- app/ui/shell/in-process-window.ts | 9 +++++---- app/ui/window-menu.ts | 10 ++++++++++ tests/menu-display-list.test.cjs | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/app/ui/layers.ts b/app/ui/layers.ts index 4101810c..1553216d 100644 --- a/app/ui/layers.ts +++ b/app/ui/layers.ts @@ -102,6 +102,15 @@ export interface Layer { readonly depth?: number; paintParts?(): Plane[]; readonly paintOverBase?: boolean; + /** + * True when this layer stands in for `below`, a stacked layer directly + * beneath it (or beneath another layer it stands in for): when this layer + * calls paintBelow, the stack skips `below` and paints what is under it + * instead. A submenu replacing the menu it opened from uses this, so the + * hidden menu costs no plane and its dimUnderneath doesn't dim the content + * a second time; popping the submenu still returns to it. + */ + replacesLayerBelow?(below: Layer): boolean; /** * Dim everything painted beneath this layer: a 0..1 brightness factor * applied to the pixels and glyphs of every plane below it in the stack @@ -326,7 +335,9 @@ export class LayerStack { return [ownPlane]; } const dim = layer.dimUnderneath || 1; - const below = this.paintLayer(layer.paintOverBase ? 0 : index - 1, dimSoFar * dim, raw); + let belowIndex = layer.paintOverBase ? 0 : index - 1; + while (belowIndex > 0 && layer.replacesLayerBelow?.(this.layers[belowIndex]!)) belowIndex--; + const below = this.paintLayer(belowIndex, dimSoFar * dim, raw); return [...(!raw && dim < 1 ? dimPlanes(below, dim) : below), ownPlane]; } } diff --git a/app/ui/shell/in-process-window.ts b/app/ui/shell/in-process-window.ts index dddb31f4..d51e1f5f 100644 --- a/app/ui/shell/in-process-window.ts +++ b/app/ui/shell/in-process-window.ts @@ -192,12 +192,13 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces drawIcon: options.drawIcon ?? windowIcon(options.icon, options.iconLetter), handleInput: async (event, frameId) => { // The shell opened its system menu over this window; close our own - // context menu so the two never stack. Never forwarded to app layers. + // context menu (and any window submenus open from it) so the two never + // stack. Never forwarded to app layers. if (event.type === "system-menu-opened") { options.onSystemMenuOpened?.(); - if (stack.popIfTop((layer) => layer instanceof WindowMenuLayer)) { - requestRender(); - } + let closedMenu = false; + while (stack.popIfTop((layer) => layer instanceof WindowMenuLayer)) closedMenu = true; + if (closedMenu) requestRender(); return; } // The default tap-then-hold response: the window menu (or the system diff --git a/app/ui/window-menu.ts b/app/ui/window-menu.ts index e8b4e757..07aab3a6 100644 --- a/app/ui/window-menu.ts +++ b/app/ui/window-menu.ts @@ -33,6 +33,16 @@ export class WindowMenuLayer extends MenuLayer { ? { ...WINDOW_MENU_LAYOUT, footer: gestureHints([[GESTURE_SHORT_THEN_LONG_PRESS, "system menu"]]) } : WINDOW_MENU_LAYOUT); } + + /** + * A window submenu (Settings, Spotify, ...) opens in place of the window + * menu it came from, which it would mostly cover anyway: the window content + * shows beneath it, dimmed once, rather than the parent menu dimming it a + * second time and costing a plane of its own. + */ + replacesLayerBelow(below: Layer): boolean { + return below instanceof WindowMenuLayer; + } } export type WindowMenuOptions = { diff --git a/tests/menu-display-list.test.cjs b/tests/menu-display-list.test.cjs index ee328f2d..35984ee7 100644 --- a/tests/menu-display-list.test.cjs +++ b/tests/menu-display-list.test.cjs @@ -64,6 +64,22 @@ test('context menu surface and selected row both use +4 without changing the app assert.deepEqual(closed.image.pixels,base.pixels); assert.deepEqual(wire.presentationRecords(prepareFrameDraws(closed.draws)),[]); }); +test('a submenu that replaces its parent menu skips the parent plane and dims the content once',()=>{ + const base=new graphics.GrayImage(100,90,200); + const stack=new LayerStack({paint:()=>base},noopLayerActions,{width:100,height:90}); + const layout={x:10,y:8,width:80,minHeight:70,maxHeight:70,dimUnderneath:0.5}; + class SubmenuLayer extends MenuLayer { replacesLayerBelow(below) { return below instanceof MenuLayer; } } + const parent=new MenuLayer('PARENT',[{label:'SUB',onSelect(){}}],layout); + stack.push(parent); + stack.push(new SubmenuLayer('CHILD',[{label:'ITEM',onSelect(){}}],layout)); + stack.push(new SubmenuLayer('GRANDCHILD',[{label:'ITEM',onSelect(){}}],layout)); + const painted=stack.paint(); + assert.equal(painted.length,2); + assert.equal(painted[0].image.pixels[0],100); + assert.equal(stack.baseDim(),0.5); + stack.pop();stack.pop(); + assert.equal(stack.paint().length,2); +}); test('text page paints at the stack size, not the lens size',()=>{ // An app window's stack is its viewport; a lens-sized plane is rejected by // the compositor as larger than the window surface.