From e609f24c87275fbc5ba0784fa76d884edc9d1344 Mon Sep 17 00:00:00 2001 From: CRT Date: Wed, 30 Sep 2026 15:15:09 +0000 Subject: [PATCH] App menu: the titlebar logo opens it too, hung under the logo on its side of the window The logo at the start of the titlebar is now a button (aria-haspopup, keyboard reachable, tooltip) and a second trigger of the same menu: same component and model, only the anchor differs (`openAppMenu({ anchor: 'logo' })`). A submenu opens toward the middle of the window, else on the other side, else over the menu (`submenuLeft`, tested). A click on the logo never starts a window drag; a press that moves more than 5px does (the window's `startDragging`), so the logo still moves the window like the titlebar around it. A right-click shows the window's system menu. Clear of the macOS traffic lights: the logo sits after the titlebar's 78px inset and the menu hangs below it. The button at the end and the account menu's row stay. --- packages/create-waybetter/src/app.js | 1 + packages/shell/src/frame/AppMenu.tsx | 94 ++++++++++++++++++----- packages/shell/src/frame/Titlebar.tsx | 7 +- packages/shell/src/frame/appMenu.css | 14 +++- packages/shell/src/frame/menuModel.ts | 18 +++++ packages/shell/src/frame/shell.css | 3 +- packages/shell/src/frame/windowBinding.ts | 2 +- packages/shell/src/windowControls.ts | 4 + packages/shell/tests/menu.test.ts | 18 ++++- 9 files changed, 133 insertions(+), 28 deletions(-) diff --git a/packages/create-waybetter/src/app.js b/packages/create-waybetter/src/app.js index c043773..d23945d 100644 --- a/packages/create-waybetter/src/app.js +++ b/packages/create-waybetter/src/app.js @@ -1065,6 +1065,7 @@ function tauriTypes() { ' toggleMaximize(): Promise;', ' close(): Promise;', " startResizeDragging(direction: 'North' | 'South' | 'East' | 'West' | 'NorthEast' | 'NorthWest' | 'SouthEast' | 'SouthWest'): Promise;", + ' startDragging(): Promise;', ' isMaximized(): Promise;', ' onResized(handler: () => void): Promise<() => void>;', ' setBackgroundColor(color: [number, number, number, number]): Promise;', diff --git a/packages/shell/src/frame/AppMenu.tsx b/packages/shell/src/frame/AppMenu.tsx index 9c43d11..a12e184 100644 --- a/packages/shell/src/frame/AppMenu.tsx +++ b/packages/shell/src/frame/AppMenu.tsx @@ -1,7 +1,8 @@ // The app menu: the titlebar's "Settings and more" button, at the far end of the titlebar cluster // (before the caption buttons on Windows and Linux; macOS has none there). It opens on click, on -// Alt+F and F10 (Shell.tsx; a focused tab forwards the keys), from the command palette and from the -// last row of the account menu. Its rows are the app's (`ShellSite.menu`, menuModel.ts): built-in +// Alt+F and F10 (Shell.tsx; a focused tab forwards the keys), from the command palette, from the +// last row of the account menu, and from the app's logo at the start of the titlebar (the same +// menu, hung under the logo on its side of the window). Its rows are the app's (`ShellSite.menu`, menuModel.ts): built-in // ones by name, each shown only when the app serves what it needs, and the app's own. // // Like every shell menu it is an overlay: while open the shell is raised above the tab webviews @@ -19,19 +20,19 @@ import { useDev, usePaneTick, useT } from './hooks.ts'; import { formatNumber } from './i18n.ts'; import { Icon, type IconName } from './icons.tsx'; import { - DEFAULT_MENU, ZOOM_MAX, ZOOM_MIN, findSpec, joinGroups, languageChoice, languageRows, menuText, moveStop, planMenu, shortcutText, specRows, submenuKeys, typeahead, + DEFAULT_MENU, ZOOM_MAX, ZOOM_MIN, findSpec, joinGroups, languageChoice, languageRows, menuText, moveStop, planMenu, shortcutText, specRows, submenuKeys, submenuLeft, typeahead, type BuiltinMenuItem, type MenuEnv, type MenuItemRow, type MenuRow, } from './menuModel.ts'; import { hasFeature, site } from './site.ts'; import { isWeb, setOverlay, shell, type TabParams } from './state.ts'; import { openWorkspaceSave } from './TitlebarExtras.tsx'; -import { os } from './windowBinding.ts'; +import { os, startDrag, systemMenu } from './windowBinding.ts'; import { loadWorkspaces, restoreWorkspace, workspaceList, workspaces } from './workspaces.ts'; import { readZoom, stepZoom, zoomOf, zoomTarget } from './zoomStore.ts'; import './appMenu.css'; -/** How the menu is asked to open: at a submenu, with the first row focused (keyboard), or closing if it is open. */ -export type AppMenuRequest = { submenu?: string; keyboard?: boolean; toggle?: boolean }; +/** How the menu is asked to open: under the logo instead of its button, at a submenu, with the first row focused (keyboard), or closing if it is open. */ +export type AppMenuRequest = { anchor?: 'button' | 'logo'; submenu?: string; keyboard?: boolean; toggle?: boolean }; /** Opens the app menu (Alt+F / F10, the palette, the account menu's last row). */ export const openAppMenu = (request: AppMenuRequest = {}) => window.dispatchEvent(new CustomEvent('bt-app-menu', { detail: request })); @@ -44,6 +45,16 @@ const getError = (error: unknown) => (error instanceof Error ? error.message : S const percent = (factor: number) => formatNumber(factor, { style: 'percent', maximumFractionDigits: 0 }); type Focus = { level: number; id?: string; edge?: 'first' | 'last' | 'menu' }; +/** Where the menu hangs: under its button (from the window's end edge) or under the logo (from its start edge). */ +type Anchor = { at: 'button' | 'logo'; top: number; left?: number; right?: number }; + +/** Which trigger the open menu hangs from (null: closed), for the triggers' `aria-expanded`. */ +let openAt: Anchor['at'] | null = null; +function noteOpen(at: Anchor['at'] | null) { + if (openAt === at) return; + openAt = at; + window.dispatchEvent(new CustomEvent('bt-app-menu-state')); +} /** The rows of one level a key can stop on: enabled items; the zoom row counts once. */ function stopsOf(menu: HTMLElement): HTMLElement[] { @@ -62,7 +73,7 @@ export function AppMenu() { const menu = config.menu ?? {}; const [open, setOpen] = useState(false); const [path, setPath] = useState([]); - const [anchor, setAnchor] = useState({ top: 50, right: 8 }); + const [anchor, setAnchor] = useState({ at: 'button', top: 50, right: 8 }); const [mode, setMode] = useState(null); const [asking, setAsking] = useState<{ to: string; changes: number } | null>(null); const [version, setVersion] = useState(''); @@ -89,8 +100,12 @@ export function AppMenu() { }, []); function show(request: AppMenuRequest = {}) { - const rect = button.current?.getBoundingClientRect(); - if (rect) setAnchor({ top: Math.round(rect.bottom + 6), right: Math.max(8, Math.round(window.innerWidth - rect.right)) }); + const logo = request.anchor === 'logo' ? document.querySelector('.bt-titlebar-logo') : null; + const rect = (logo ?? button.current)?.getBoundingClientRect(); + if (rect) { + const top = Math.round(rect.bottom + 6); + setAnchor(logo ? { at: 'logo', top, left: Math.max(8, Math.round(rect.left - 3)) } : { at: 'button', top, right: Math.max(8, Math.round(window.innerWidth - rect.right)) }); + } const active = document.activeElement as HTMLElement | null; before.current = active && active !== document.body && document.hasFocus() ? active : null; // Keys pressed in a tab open the menu: the shell takes the keyboard. @@ -121,7 +136,8 @@ export function AppMenu() { useEffect(() => { const onRequest = (event: Event) => { const request = (event as CustomEvent).detail ?? {}; - if (live.current.open && request.toggle) close('escape'); else show(request); + // Asked again from the other trigger, the open menu moves there instead of closing. + if (live.current.open && request.toggle && (request.anchor ?? 'button') === (openAt ?? 'button')) close('escape'); else show(request); }; window.addEventListener('bt-app-menu', onRequest); return () => window.removeEventListener('bt-app-menu', onRequest); @@ -131,6 +147,7 @@ export function AppMenu() { if (!open) return; // Native tab webviews would cover the menu. setOverlay(1); + noteOpen(anchor.at); const away = () => close(); // Keys while nothing in the menu has focus (a click on its padding): Esc closes, arrows enter it. const onKey = (event: KeyboardEvent) => { @@ -145,8 +162,8 @@ export function AppMenu() { window.addEventListener('blur', away); window.addEventListener('resize', away); window.addEventListener('keydown', onKey); - return () => { setOverlay(-1); window.removeEventListener('blur', away); window.removeEventListener('resize', away); window.removeEventListener('keydown', onKey); }; - }, [open]); + return () => { setOverlay(-1); noteOpen(null); window.removeEventListener('blur', away); window.removeEventListener('resize', away); window.removeEventListener('keydown', onKey); }; + }, [open, anchor.at]); const layer = (level: number) => layers.current?.querySelector(`[data-level="${level}"]`) ?? null; @@ -162,10 +179,9 @@ export function AppMenu() { const box = parent.getBoundingClientRect(); el.style.maxHeight = `${window.innerHeight - edge * 2}px`; const { width, height } = el.getBoundingClientRect(); - // The menu hangs at the window's end edge: a submenu opens on its other side, else beside it, else over it. - const left = box.left - width + 3 >= edge ? box.left - width + 3 - : box.right + width - 3 <= window.innerWidth - edge ? box.right - 3 - : Math.max(edge, Math.min(box.left - width + 3, window.innerWidth - width - edge)); + const left = submenuLeft(box, width, window.innerWidth, edge); + // It slides in from the side it hangs on. + el.style.setProperty('--bt-app-sub-from', left < box.left ? '6px' : '-6px'); const top = Math.max(edge, Math.min((row?.getBoundingClientRect().top ?? box.top) - 6, window.innerHeight - height - edge)); el.style.left = `${Math.round(left)}px`; el.style.top = `${Math.round(top)}px`; @@ -448,16 +464,16 @@ export function AppMenu() { // An error belongs to the level it happened on: the deepest one open. if (error) levels[levels.length - 1]!.rows = [...levels[levels.length - 1]!.rows, { kind: 'note', id: 'error', title: '', text: error, tone: 'error' }]; return levels.map(({ rows, label }, level) =>
+ className={level === 0 ? `bt-menu bt-app-menu bt-pop${anchor.at === 'logo' ? '' : ' bt-pop--right'}` : 'bt-menu bt-app-menu bt-app-sub'} + style={level === 0 ? { top: anchor.top, left: anchor.left, right: anchor.right, maxHeight: `calc(100vh - ${anchor.top + 8}px)` } : undefined}> {rows.map((row, index) => renderRow(row, level, index))}
); } const label = menuText('label'); return <> - {open && createPortal(
close()} onContextMenu={event => { event.preventDefault(); close(); }}> @@ -467,3 +483,41 @@ export function AppMenu() {
, document.body)} ; } + +/** + * The app's logo at the start of the titlebar: a second trigger of the same menu, hung under the + * logo. A click opens it; a press that turns into a drag moves the window, as the titlebar around + * it does; a right-click shows the window's system menu, where a titlebar icon has always had it. + */ +export function AppMenuLogo() { + useT(); + const [expanded, setExpanded] = useState(openAt === 'logo'); + const press = useRef<{ x: number; y: number; dragged: boolean } | null>(null); + useEffect(() => { + const update = () => setExpanded(openAt === 'logo'); + window.addEventListener('bt-app-menu-state', update); + return () => window.removeEventListener('bt-app-menu-state', update); + }, []); + const { product, logo } = site(); + const label = menuText('label'); + return ; +} diff --git a/packages/shell/src/frame/Titlebar.tsx b/packages/shell/src/frame/Titlebar.tsx index d8636ab..b35c264 100644 --- a/packages/shell/src/frame/Titlebar.tsx +++ b/packages/shell/src/frame/Titlebar.tsx @@ -2,7 +2,7 @@ import { useEffect, type CSSProperties, type MouseEvent as ReactMouseEvent } fro import { RESIZE_BORDER } from '../chrome.ts'; import { chipFor } from '../updateModel.ts'; import { AccountSwitcher } from './AccountSwitcher.tsx'; -import { AppMenu } from './AppMenu.tsx'; +import { AppMenu, AppMenuLogo } from './AppMenu.tsx'; import { invoke } from './bridge.ts'; import { useT, useUpdateStatus } from './hooks.ts'; import { tx } from './i18n.ts'; @@ -23,12 +23,13 @@ export function titlebarContextMenu(event: ReactMouseEvent | MouseEvent) { /** * The 44px titlebar row. It sits under the dockview root: the top groups' tab headers are - * transparent and let clicks through to the drag layer, the logo and the caption buttons. + * transparent and let clicks through to the drag layer, the logo and the caption buttons. The logo + * is the app menu's second trigger (AppMenu.tsx); everything else that is empty drags the window. */ export function Titlebar({ maximized }: { maximized: boolean }) { return
- +
diff --git a/packages/shell/src/frame/appMenu.css b/packages/shell/src/frame/appMenu.css index 3515fe5..62b57df 100644 --- a/packages/shell/src/frame/appMenu.css +++ b/packages/shell/src/frame/appMenu.css @@ -13,6 +13,18 @@ .bt-app-button:focus-visible { outline: 0; box-shadow: var(--bt-ring); } .bt-shell .bt-app-button svg { width: 16px; height: 16px; fill: currentColor; stroke: none; } +/* The logo at the start of the titlebar: the menu's second trigger (a 30px target around the 20px + mark; with its margins it takes the 50px the tab strip leaves in --bt-tb-left). */ +.bt-titlebar-logo { + align-self: center; display: inline-grid; place-items: center; width: 30px; height: 30px; flex: 0 0 30px; margin: 0 9px 0 11px; padding: 0; box-sizing: border-box; /* rtl-ok: titlebar */ + border: 1px solid transparent; border-radius: 9px; background: transparent; cursor: pointer; -webkit-user-drag: none; + transition: background-color var(--bt-t1) var(--bt-ease), border-color var(--bt-t1) var(--bt-ease); +} +.bt-titlebar-logo img { display: block; width: 20px; height: 20px; pointer-events: none; -webkit-user-drag: none; } +.bt-titlebar-logo:hover { background: var(--bt-hover); border-color: var(--bt-line-soft); } +.bt-titlebar-logo[aria-expanded=true] { background: var(--bt-hover); border-color: rgb(var(--bt-accent-rgb) / .5); } +.bt-titlebar-logo:focus-visible { outline: 0; box-shadow: var(--bt-ring); } + /* The menu and its submenus. `display: contents`: the layers are positioned on their own. */ .bt-app-layers { display: contents; } .bt-menu.bt-app-menu { @@ -24,7 +36,7 @@ width: max-content; min-width: 224px; max-width: min(320px, calc(100vw - 16px)); z-index: 1; animation: bt-app-sub-in var(--bt-t1) var(--bt-ease-out); } -@keyframes bt-app-sub-in { from { opacity: 0; transform: translateX(6px); } } +@keyframes bt-app-sub-in { from { opacity: 0; transform: translateX(var(--bt-app-sub-from, 6px)); } } .bt-app-menu hr { flex: none; width: auto; align-self: stretch; } /* Rows: one highlight, wherever it comes from (the pointer moves the focus, as the arrows do); diff --git a/packages/shell/src/frame/menuModel.ts b/packages/shell/src/frame/menuModel.ts index d56ce06..fd52fc5 100644 --- a/packages/shell/src/frame/menuModel.ts +++ b/packages/shell/src/frame/menuModel.ts @@ -305,6 +305,24 @@ export function submenuKeys(rtl: boolean): { open: string; close: string } { return rtl ? { open: 'ArrowLeft', close: 'ArrowRight' } : { open: 'ArrowRight', close: 'ArrowLeft' }; } +/** + * The left edge of a submenu `width` wide beside its menu (`parent`), in a window `viewport` wide. + * The menu hangs at one edge of the window (under its button at the end, under the logo at the + * start): the submenu opens toward the middle, else on the other side, else over the menu, and + * always stays `edge` pixels inside the window. + */ +export function submenuLeft(parent: { left: number; right: number }, width: number, viewport: number, edge = 8): number { + const overlap = 3; + const before = parent.left - width + overlap; + const after = parent.right - overlap; + const fits = (x: number) => x >= edge && x + width <= viewport - edge; + const first = (parent.left + parent.right) / 2 > viewport / 2 ? before : after; + const second = first === before ? after : before; + if (fits(first)) return first; + if (fits(second)) return second; + return Math.max(edge, Math.min(first, viewport - width - edge)); +} + /** A shortcut as the platform writes it: `Ctrl+Shift+T` on Windows and Linux, `⇧⌘T` on macOS. */ export function shortcutText(shortcut: string, mac: boolean): string { const parts = shortcut.split('+').map(part => part.trim()).filter(Boolean); diff --git a/packages/shell/src/frame/shell.css b/packages/shell/src/frame/shell.css index e28e7ec..df53df7 100644 --- a/packages/shell/src/frame/shell.css +++ b/packages/shell/src/frame/shell.css @@ -91,12 +91,11 @@ html, body, #root { height: 100%; margin: 0; overflow: hidden; background: var(- pointer events only on its own controls; everything else falls through to the tabs and their drag region. With no tabs the titlebar itself is the drag region again. */ .bt-titlebar { z-index: 2; pointer-events: none; } -.bt-titlebar > :not(.bt-titlebar-drag):not(.bt-titlebar-spacer):not(.bt-titlebar-logo) { pointer-events: auto; } +.bt-titlebar > :not(.bt-titlebar-drag):not(.bt-titlebar-spacer) { pointer-events: auto; } .bt-shell--empty .bt-titlebar > * { pointer-events: auto; } /* Above the dock, so it must not paint: the tab strips hosted in the titlebar row show through it. */ .bt-shell:not(.bt-shell--empty) .bt-titlebar { background: transparent; } html[data-os=macos] .bt-titlebar { padding-left: 78px; } /* rtl-ok: traffic lights; the titlebar stays LTR */ -.bt-titlebar-logo { width: 20px; height: 20px; margin: 12px 14px 12px 16px; flex: 0 0 20px; -webkit-user-drag: none; } .bt-titlebar-spacer { flex: 1; min-width: 48px; } .bt-captions { display: flex; } .bt-caption { width: 46px; height: 44px; display: grid; place-items: center; padding: 0; background: transparent; border: 0; color: var(--bt-text-2); cursor: default; transition: background-color var(--bt-t1) var(--bt-ease), color var(--bt-t1) var(--bt-ease); } diff --git a/packages/shell/src/frame/windowBinding.ts b/packages/shell/src/frame/windowBinding.ts index a98e760..1851dcc 100644 --- a/packages/shell/src/frame/windowBinding.ts +++ b/packages/shell/src/frame/windowBinding.ts @@ -6,5 +6,5 @@ import { currentWindow, invoke } from './bridge.ts'; export { frameless, isTauri, os, type ResizeDirection } from '../windowControls.ts'; -export const { minimize, toggleMaximize, close, startResize, onMaximizedChange, systemMenu } = +export const { minimize, toggleMaximize, close, startResize, startDrag, onMaximizedChange, systemMenu } = createWindowControls((command, args) => invoke(command, args), currentWindow); diff --git a/packages/shell/src/windowControls.ts b/packages/shell/src/windowControls.ts index b8fde8e..02ffa05 100644 --- a/packages/shell/src/windowControls.ts +++ b/packages/shell/src/windowControls.ts @@ -9,6 +9,8 @@ export type NativeWindow = { toggleMaximize(): Promise; close(): Promise; startResizeDragging(direction: ResizeDirection): Promise; + /** Moves the window with the pointer (the titlebar logo, once a press on it becomes a drag). */ + startDragging?(): Promise; isMaximized(): Promise; onResized(handler: () => void): Promise<() => void>; }; @@ -25,6 +27,8 @@ export function createWindowControls(invoke: Invoke, currentWindow: () => Native toggleMaximize: () => currentWindow().toggleMaximize().catch(() => {}), close: () => currentWindow().close().catch(() => {}), startResize: (direction: ResizeDirection) => currentWindow().startResizeDragging(direction).catch(() => {}), + /** Starts a native window move while the pointer is down (nothing where the app's window lacks it, or in a preview). */ + startDrag: () => { try { void currentWindow().startDragging?.().catch(() => {}); } catch { /* No native window. */ } }, onMaximizedChange: (cb: (maximized: boolean) => void) => { const read = () => { currentWindow().isMaximized().then(cb).catch(() => {}); }; read(); diff --git a/packages/shell/tests/menu.test.ts b/packages/shell/tests/menu.test.ts index ab94001..5a73dff 100644 --- a/packages/shell/tests/menu.test.ts +++ b/packages/shell/tests/menu.test.ts @@ -9,7 +9,7 @@ import { fileURLToPath } from 'node:url'; import { setShellI18n, type ShellI18n } from '../src/frame/i18n.ts'; import { DEFAULT_MENU, MENU_BUILTINS, MENU_MESSAGES, ZOOM_MAX, ZOOM_MIN, ZOOM_STEPS, builtinAvailable, findSpec, joinGroups, languageChoice, languageRows, menuText, menuWith, - menuWithout, moveStop, planMenu, shortcutText, specRows, submenuKeys, typeahead, zoomPercent, zoomStep, + menuWithout, moveStop, planMenu, shortcutText, specRows, submenuKeys, submenuLeft, typeahead, zoomPercent, zoomStep, type MenuEnv, type MenuGroups, type MenuItemRow, type MenuItemSpec, type MenuLanguage, type MenuRow, } from '../src/frame/menuModel.ts'; import { buildItems } from '../src/frame/paletteModel.ts'; @@ -107,6 +107,22 @@ test('arrows wrap, Home and End jump, and with nothing focused an arrow enters a assert.deepEqual(submenuKeys(true), { open: 'ArrowLeft', close: 'ArrowRight' }); }); +test('a submenu opens toward the middle of the window and never leaves it', () => { + // Under the button at the window's end: to the left of the menu. + assert.equal(submenuLeft({ left: 990, right: 1294 }, 250, 1440), 990 - 250 + 3); + // Under the logo at the window's start: to the right of the menu. + assert.equal(submenuLeft({ left: 8, right: 312 }, 250, 1440), 312 - 3); + // No room toward the middle: the other side. + assert.equal(submenuLeft({ left: 100, right: 404 }, 250, 700), 404 - 3); + // No room on either side (a narrow window): over the menu, inside the window. + assert.equal(submenuLeft({ left: 11, right: 315 }, 250, 420), 162); + assert.equal(submenuLeft({ left: 108, right: 412 }, 250, 420), 8); + for (const [left, width, viewport] of [[0, 300, 320], [40, 310, 330], [500, 260, 1000], [900, 320, 1000]] as const) { + const at = submenuLeft({ left, right: left + 304 }, width, viewport); + assert.ok(at >= 8 && (at + width <= viewport - 8 || at === 8), `${left}/${width}/${viewport}: ${at}`); + } +}); + test('type-ahead finds the next row that starts with what was typed', () => { const labels = ['New tab', 'Split right', 'Split down', 'Language', 'Theme', 'Settings…', 'Écran', 'İstanbul']; assert.equal(typeahead(labels, -1, 's'), 1);