diff --git a/apps/desktop/electron/hud-input-policy.test.ts b/apps/desktop/electron/hud-input-policy.test.ts deleted file mode 100644 index 96ba97624a..0000000000 --- a/apps/desktop/electron/hud-input-policy.test.ts +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Unit tests for the HUD input policy. The split is the point: the solid - * fallback has to reach the X11 windows where the input region never comes - * back, and has to leave native Wayland (and macOS/Windows) on click-through. - */ - -import assert from 'node:assert/strict' - -import { test } from 'vitest' - -import { hudInputPolicy, hudUsesNativeDrag, hudUsesWorkspaceTransfer } from './hud-input-policy' - -const X11_SESSION = { DISPLAY: ':0', XDG_SESSION_TYPE: 'x11' } -const WAYLAND_SESSION = { WAYLAND_DISPLAY: 'wayland-0', XDG_SESSION_TYPE: 'wayland' } -const XWAYLAND_SESSION = { DISPLAY: ':0', WAYLAND_DISPLAY: 'wayland-0', XDG_SESSION_TYPE: 'wayland' } - -test('the click-through design is kept everywhere it works', () => { - for (const platform of ['darwin', 'win32']) { - assert.equal(hudInputPolicy(platform, {}, []), 'click-through') - assert.equal(hudInputPolicy(platform, X11_SESSION, []), 'click-through') - } -}) - -test('an X11 session gets the solid HUD', () => { - assert.equal(hudInputPolicy('linux', X11_SESSION, []), 'solid') -}) - -test('a Wayland session keeps click-through — Electron 20+ is a native Wayland client there', () => { - assert.equal(hudInputPolicy('linux', WAYLAND_SESSION, []), 'click-through') - assert.equal(hudInputPolicy('linux', XWAYLAND_SESSION, []), 'click-through') -}) - -test('asking for a native Wayland surface keeps the click-through path', () => { - for (const argv of [['--ozone-platform=wayland'], ['--ozone-platform-hint=wayland']]) { - assert.equal(hudInputPolicy('linux', WAYLAND_SESSION, argv), 'click-through') - } - - assert.equal( - hudInputPolicy('linux', { ...WAYLAND_SESSION, ELECTRON_OZONE_PLATFORM_HINT: 'wayland' }, []), - 'click-through' - ) -}) - -test('an auto hint follows the session', () => { - assert.equal(hudInputPolicy('linux', WAYLAND_SESSION, ['--ozone-platform-hint=auto']), 'click-through') - assert.equal(hudInputPolicy('linux', X11_SESSION, ['--ozone-platform-hint=auto']), 'solid') - assert.equal(hudInputPolicy('linux', XWAYLAND_SESSION, ['--ozone-platform-hint=auto']), 'click-through') -}) - -test('asking for X11 on a Wayland session gets the solid HUD', () => { - assert.equal(hudInputPolicy('linux', WAYLAND_SESSION, ['--ozone-platform=x11']), 'solid') - assert.equal(hudInputPolicy('linux', { ...WAYLAND_SESSION, ELECTRON_OZONE_PLATFORM_HINT: 'x11' }, []), 'solid') -}) - -test('the explicit switch beats the hint, and the last switch wins', () => { - assert.equal( - hudInputPolicy('linux', WAYLAND_SESSION, ['--ozone-platform-hint=auto', '--ozone-platform=x11']), - 'solid' - ) - assert.equal( - hudInputPolicy('linux', X11_SESSION, ['--ozone-platform=x11', '--ozone-platform=wayland']), - 'click-through' - ) -}) - -test('a backend nobody recognises follows the session, not a silent X11 default', () => { - assert.equal(hudInputPolicy('linux', X11_SESSION, ['--ozone-platform=headless']), 'solid') - assert.equal(hudInputPolicy('linux', WAYLAND_SESSION, ['--ozone-platform=headless']), 'click-through') - assert.equal(hudInputPolicy('linux', {}, []), 'solid') -}) - -test('only a native Wayland HUD gets a compositor drag region', () => { - assert.equal(hudUsesNativeDrag('darwin', WAYLAND_SESSION, []), false) - assert.equal(hudUsesNativeDrag('win32', WAYLAND_SESSION, []), false) - assert.equal(hudUsesNativeDrag('linux', X11_SESSION, []), false) - assert.equal(hudUsesNativeDrag('linux', WAYLAND_SESSION, []), true) - assert.equal(hudUsesNativeDrag('linux', WAYLAND_SESSION, ['--ozone-platform=x11']), false) - assert.equal(hudUsesNativeDrag('linux', X11_SESSION, ['--ozone-platform=wayland']), true) -}) - -test('only a renderer-driven Linux HUD needs temporary all-workspace visibility', () => { - assert.equal(hudUsesWorkspaceTransfer('darwin', X11_SESSION, []), false) - assert.equal(hudUsesWorkspaceTransfer('win32', X11_SESSION, []), false) - assert.equal(hudUsesWorkspaceTransfer('linux', X11_SESSION, []), true) - assert.equal(hudUsesWorkspaceTransfer('linux', WAYLAND_SESSION, []), false) - assert.equal(hudUsesWorkspaceTransfer('linux', WAYLAND_SESSION, ['--ozone-platform=x11']), true) - assert.equal(hudUsesWorkspaceTransfer('linux', X11_SESSION, ['--ozone-platform=wayland']), false) -}) diff --git a/apps/desktop/electron/hud-input-policy.ts b/apps/desktop/electron/hud-input-policy.ts deleted file mode 100644 index ddc2fb13a1..0000000000 --- a/apps/desktop/electron/hud-input-policy.ts +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Which input model the HUD can safely use on this system. - * - * The HUD's default is per-element click-through: the window ignores the mouse - * and turns solid only where the renderer's hit test finds a control under the - * cursor. That design rests on `setIgnoreMouseEvents(false)` actually handing - * the input region back. On X11 it does not — once a window has ignored the - * mouse the X server keeps hit-testing straight through it, and no later call - * restores it. Ignore is a one-way door there, so the only policy that works - * is never to walk through it: keep the HUD a normal solid window. - * - * That trade is a real loss — a solid HUD swallows clicks in the faded band - * above the composer — so it is scoped to the Ozone backend where restore is - * known to be broken, not to every Linux desktop. - * - * The windowing backend, not the login session type, is what decides this. - * Electron 20+ defaults `ozone-platform-hint` to `auto`, which prefers a - * native Wayland surface on a Wayland session. That is why HUD drag via - * `setBounds` is a no-op for those users (#82851): they are already a Wayland - * client. Native Wayland keeps click-through (main's cursor poll re-arms it). - * X11 and explicit XWayland (`--ozone-platform=x11` / `ozone_platform_hint: - * x11`) take the solid path. - */ - -export type HudInputPolicy = 'click-through' | 'solid' - -type Backend = 'wayland' | 'x11' - -/** - * The Ozone platform this process was asked for, or null when nothing asked. - * - * `--ozone-platform` names a backend outright and `--ozone-platform-hint` - * (equivalently `ELECTRON_OZONE_PLATFORM_HINT`) asks for one, with `auto` - * meaning "Wayland if the session is Wayland". The explicit switch wins over - * the hint, and a repeated switch resolves last-one-wins, matching Chromium. - */ -function requestedOzonePlatform(env: NodeJS.ProcessEnv, argv: readonly string[]): null | string { - let explicit: null | string = null - let hint: null | string = null - - for (const arg of argv) { - const match = /^--ozone-platform(-hint)?=(.+)$/.exec(arg) - - if (!match) { - continue - } - - if (match[1]) { - hint = match[2].toLowerCase() - } else { - explicit = match[2].toLowerCase() - } - } - - return explicit ?? hint ?? env.ELECTRON_OZONE_PLATFORM_HINT?.toLowerCase() ?? null -} - -function sessionIsWayland(env: NodeJS.ProcessEnv): boolean { - return env.XDG_SESSION_TYPE === 'wayland' || (Boolean(env.WAYLAND_DISPLAY) && !env.DISPLAY) -} - -/** - * The backend Electron will actually use on Linux. - * - * Unset / `auto` follows the session: Wayland sessions get a native Wayland - * surface (Electron 20+ default), X11 sessions stay on X11. An explicit - * `--ozone-platform=x11` (or `desktop.ozone_platform_hint: x11`) is the - * COSMIC always-on-top escape hatch and lands on the solid HUD path. - */ -function linuxBackend(env: NodeJS.ProcessEnv, argv: readonly string[]): Backend { - const requested = requestedOzonePlatform(env, argv) - - if (requested === 'x11') { - return 'x11' - } - - if (requested === 'wayland') { - return 'wayland' - } - - return sessionIsWayland(env) ? 'wayland' : 'x11' -} - -/** - * The input model the HUD should use. - * - * macOS and Windows keep the click-through design: `setIgnoreMouseEvents(true, - * { forward: true })` is `@platform darwin,win32`, so the renderer goes on - * seeing the cursor while the window ignores it and can re-arm whenever the - * pointer comes back to the bar. - */ -export function hudInputPolicy(platform: string, env: NodeJS.ProcessEnv, argv: readonly string[]): HudInputPolicy { - if (platform !== 'linux') { - return 'click-through' - } - - return linuxBackend(env, argv) === 'x11' ? 'solid' : 'click-through' -} - -/** - * Whether the renderer needs a native compositor drag region for the HUD. - * - * Native Wayland cannot honour app-driven window positions, so it needs - * `-webkit-app-region: drag`. X11 can use the renderer's `moveBy` path and - * must not install that region: it consumes the pointer stream needed by the - * renderer's immediate Ctrl+primary-button drag. - */ -export function hudUsesNativeDrag(platform: string, env: NodeJS.ProcessEnv, argv: readonly string[]): boolean { - return platform === 'linux' && hudInputPolicy(platform, env, argv) === 'click-through' -} - -/** - * Whether a renderer-driven HUD move needs the X11 workspace-transfer bridge. - * - * While the pointer is grabbed the window is made temporarily visible on all - * desktops. Releasing the grab clears that flag; Chromium's X11 backend then - * reads `_NET_CURRENT_DESKTOP` and sends `_NET_WM_DESKTOP` for that desktop, - * which gives KWin the same "hold window + switch desktop" behaviour as a - * native titlebar drag. Native Wayland already delegates movement to the - * compositor and must stay out of this path. - */ -export function hudUsesWorkspaceTransfer(platform: string, env: NodeJS.ProcessEnv, argv: readonly string[]): boolean { - return platform === 'linux' && hudInputPolicy(platform, env, argv) === 'solid' -} diff --git a/apps/desktop/electron/hud-ipc.ts b/apps/desktop/electron/hud-ipc.ts index 703ee212a2..dc9066b239 100644 --- a/apps/desktop/electron/hud-ipc.ts +++ b/apps/desktop/electron/hud-ipc.ts @@ -5,9 +5,13 @@ import { type BrowserWindow, ipcMain } from 'electron' import { normalizeHudResizeBounds } from './hud-geometry' -import { hudInputPolicy, hudUsesNativeDrag, hudUsesWorkspaceTransfer } from './hud-input-policy' +import { hudWindowingView, resolveHudWindowing } from './hud-windowing' import { hudFrostFor, type TranslucencyState } from './translucency' +function hudWindowing() { + return resolveHudWindowing(process.platform, process.env, process.argv) +} + export interface HudIpcDeps { isMac: boolean /** Main's authoritative translucency state (Settings → Appearance). */ @@ -32,7 +36,11 @@ export function registerHudIpc({ // Chromium drag region that steals modifier-drag gestures from the WM. // Main answers because it owns the actual Ozone backend selection. ipcMain.on('hermes:hud:native-drag', event => { - event.returnValue = hudUsesNativeDrag(process.platform, process.env, process.argv) + event.returnValue = hudWindowing().move === 'native-drag' + }) + + ipcMain.on('hermes:hud:windowing', event => { + event.returnValue = hudWindowingView(hudWindowing()) }) // X11/KWin window transfer: a renderer-driven grab is temporarily sticky so @@ -48,7 +56,7 @@ export function registerHudIpc({ !hudWindow || hudWindow.isDestroyed() || event.sender !== hudWindow.webContents || - !hudUsesWorkspaceTransfer(process.platform, process.env, process.argv) + !hudWindowing().workspaceTransfer ) { return } @@ -155,7 +163,7 @@ export function registerHudIpc({ // cannot restore the input region afterwards. Veto the request there so // the HUD stays a normal solid window. Native Wayland and macOS/Windows // keep the per-element path. - if (Boolean(ignore) && hudInputPolicy(process.platform, process.env, process.argv) === 'solid') { + if (Boolean(ignore) && !hudWindowing().ignoreMouse) { return } @@ -180,6 +188,10 @@ export function registerHudIpc({ const [x, y] = hudWindow.getPosition() + if (!hudWindowing().clientPlacement) { + return + } + // setBounds — NOT setPosition: on Windows, a transparent frameless window // silently grows ~1px per setPosition call (worse at >100% DPI). The renderer // snapshots outerWidth/outerHeight when the composer drag arms and re-pins diff --git a/apps/desktop/electron/hud-overlay.test.ts b/apps/desktop/electron/hud-overlay.test.ts new file mode 100644 index 0000000000..0a6eec281b --- /dev/null +++ b/apps/desktop/electron/hud-overlay.test.ts @@ -0,0 +1,39 @@ +import assert from 'node:assert/strict' + +import { test } from 'vitest' + +import { applyHudElectronOverlay } from './hud-overlay' + +test('macOS uses the floating panel level and all-spaces visibility', () => { + const calls: string[] = [] + const win = { + setAlwaysOnTop(flag: boolean, level?: string) { + calls.push(`alwaysOnTop:${flag}:${level}`) + }, + setVisibleOnAllWorkspaces(visible: boolean, options?: { visibleOnFullScreen?: boolean }) { + calls.push(`allWorkspaces:${visible}:${options?.visibleOnFullScreen === true}`) + } + } + + applyHudElectronOverlay(win, 'darwin') + + assert.deepEqual(calls, ['alwaysOnTop:true:floating', 'allWorkspaces:true:true']) +}) + +test('Linux and Windows only set the screen-saver always-on-top level', () => { + for (const platform of ['linux', 'win32']) { + const calls: string[] = [] + const win = { + setAlwaysOnTop(flag: boolean, level?: string) { + calls.push(`alwaysOnTop:${flag}:${level}`) + }, + setVisibleOnAllWorkspaces() { + calls.push('allWorkspaces') + } + } + + applyHudElectronOverlay(win, platform) + + assert.deepEqual(calls, ['alwaysOnTop:true:screen-saver']) + } +}) diff --git a/apps/desktop/electron/hud-overlay.ts b/apps/desktop/electron/hud-overlay.ts new file mode 100644 index 0000000000..b43fad61bd --- /dev/null +++ b/apps/desktop/electron/hud-overlay.ts @@ -0,0 +1,41 @@ +/** + * HUD overlay adapters. + * + * Electron `alwaysOnTop` is the generic ask. Some compositors ignore it + * (Hyprland tiles the toplevel; COSMIC drops z-order). Each adapter speaks + * that compositor's dialect; `promoteHudOverlay` is the one call site. + */ + +import { promoteHudOnHyprland } from './hud-hyprland' + +export interface HudElectronOverlayWindow { + setAlwaysOnTop(flag: boolean, level?: string): void + setVisibleOnAllWorkspaces?( + visible: boolean, + options?: { skipTransformProcessType?: boolean; visibleOnFullScreen?: boolean } + ): void +} + +/** Chrome Electron itself can honour. Compositor IPC is `promoteHudOverlay`. */ +export function applyHudElectronOverlay(win: HudElectronOverlayWindow, platform: string): void { + win.setAlwaysOnTop(true, platform === 'darwin' ? 'floating' : 'screen-saver') + + if (platform !== 'darwin') { + return + } + + try { + win.setVisibleOnAllWorkspaces?.(true, { visibleOnFullScreen: true, skipTransformProcessType: true }) + } catch { + // Not supported everywhere — best effort. + } +} + +/** + * Ask the running compositor to treat the HUD as an overlay. Hyprland is the + * first adapter (float + pin). Sway/niri hang off this same function later. + * Returns true when an adapter applied; false is "nothing to do / not that WM". + */ +export async function promoteHudOverlay(options: { title: string }): Promise { + return promoteHudOnHyprland(options) +} diff --git a/apps/desktop/electron/hud-windowing.test.ts b/apps/desktop/electron/hud-windowing.test.ts new file mode 100644 index 0000000000..5d353be6c6 --- /dev/null +++ b/apps/desktop/electron/hud-windowing.test.ts @@ -0,0 +1,128 @@ +/** + * Unit tests for the HUD windowing profile. Ozone parsing and the capability + * matrix live here: one profile, every operation reads the same flags. + */ + +import assert from 'node:assert/strict' + +import { test } from 'vitest' + +import { hudWindowingView, linuxOzoneBackend, resolveHudWindowing } from './hud-windowing' + +const X11_SESSION = { DISPLAY: ':0', XDG_SESSION_TYPE: 'x11' } +const WAYLAND_SESSION = { WAYLAND_DISPLAY: 'wayland-0', XDG_SESSION_TYPE: 'wayland' } +const XWAYLAND_SESSION = { DISPLAY: ':0', WAYLAND_DISPLAY: 'wayland-0', XDG_SESSION_TYPE: 'wayland' } + +const input = (platform: string, env: NodeJS.ProcessEnv, argv: readonly string[] = []) => + resolveHudWindowing(platform, env, argv).input + +test('macOS and Windows share the click-through + renderer-drag profile', () => { + for (const platform of ['darwin', 'win32'] as const) { + const windowing = resolveHudWindowing(platform, WAYLAND_SESSION, ['--ozone-platform=wayland']) + + assert.equal(windowing.backend, platform === 'darwin' ? 'cocoa' : 'win32') + assert.equal(windowing.input, 'click-through') + assert.equal(windowing.move, 'renderer') + assert.equal(windowing.clientPlacement, true) + assert.equal(windowing.controlDrag, false) + assert.equal(windowing.workspaceTransfer, false) + assert.equal(windowing.cursorFeed, false) + assert.equal(windowing.ignoreMouse, true) + } +}) + +test('Linux X11 is solid, renderer-drag, and can place the window', () => { + const windowing = resolveHudWindowing('linux', X11_SESSION, []) + + assert.equal(windowing.backend, 'x11') + assert.equal(windowing.input, 'solid') + assert.equal(windowing.move, 'renderer') + assert.equal(windowing.clientPlacement, true) + assert.equal(windowing.controlDrag, true) + assert.equal(windowing.workspaceTransfer, true) + assert.equal(windowing.cursorFeed, false) + assert.equal(windowing.ignoreMouse, false) +}) + +test('native Wayland cannot place the window and needs compositor drag + cursor feed', () => { + const windowing = resolveHudWindowing('linux', WAYLAND_SESSION, []) + + assert.equal(windowing.backend, 'wayland') + assert.equal(windowing.input, 'click-through') + assert.equal(windowing.move, 'native-drag') + assert.equal(windowing.clientPlacement, false) + assert.equal(windowing.controlDrag, false) + assert.equal(windowing.workspaceTransfer, false) + assert.equal(windowing.cursorFeed, true) + assert.equal(windowing.ignoreMouse, true) +}) + +test('a Wayland session with DISPLAY set is still a native Wayland client under Electron 20+', () => { + assert.equal(input('linux', XWAYLAND_SESSION), 'click-through') +}) + +test('asking for a native Wayland surface keeps the click-through path', () => { + for (const argv of [['--ozone-platform=wayland'], ['--ozone-platform-hint=wayland']]) { + assert.equal(input('linux', WAYLAND_SESSION, argv), 'click-through') + } + + assert.equal( + input('linux', { ...WAYLAND_SESSION, ELECTRON_OZONE_PLATFORM_HINT: 'wayland' }), + 'click-through' + ) +}) + +test('an auto hint follows the session', () => { + assert.equal(input('linux', WAYLAND_SESSION, ['--ozone-platform-hint=auto']), 'click-through') + assert.equal(input('linux', X11_SESSION, ['--ozone-platform-hint=auto']), 'solid') + assert.equal(input('linux', XWAYLAND_SESSION, ['--ozone-platform-hint=auto']), 'click-through') +}) + +test('asking for X11 on a Wayland session takes the solid / renderer-drag profile', () => { + const windowing = resolveHudWindowing('linux', WAYLAND_SESSION, ['--ozone-platform=x11']) + + assert.equal(windowing.backend, 'x11') + assert.equal(windowing.input, 'solid') + assert.equal(windowing.move, 'renderer') + assert.equal(windowing.clientPlacement, true) + assert.equal( + input('linux', { ...WAYLAND_SESSION, ELECTRON_OZONE_PLATFORM_HINT: 'x11' }), + 'solid' + ) +}) + +test('the explicit switch beats the hint, and the last switch wins', () => { + assert.equal(input('linux', WAYLAND_SESSION, ['--ozone-platform-hint=auto', '--ozone-platform=x11']), 'solid') + assert.equal(input('linux', X11_SESSION, ['--ozone-platform=x11', '--ozone-platform=wayland']), 'click-through') +}) + +test('a backend nobody recognises follows the session, not a silent X11 default', () => { + assert.equal(input('linux', X11_SESSION, ['--ozone-platform=headless']), 'solid') + assert.equal(input('linux', WAYLAND_SESSION, ['--ozone-platform=headless']), 'click-through') + assert.equal(input('linux', {}, []), 'solid') +}) + +test('linuxOzoneBackend is the session/argv normalizer', () => { + assert.equal(linuxOzoneBackend(X11_SESSION, []), 'x11') + assert.equal(linuxOzoneBackend(WAYLAND_SESSION, []), 'wayland') + assert.equal(linuxOzoneBackend(WAYLAND_SESSION, ['--ozone-platform=x11']), 'x11') + assert.equal(linuxOzoneBackend(X11_SESSION, ['--ozone-platform-hint=auto']), 'x11') +}) + +test('the renderer view is a boolean slice of the profile', () => { + const wayland = hudWindowingView(resolveHudWindowing('linux', WAYLAND_SESSION, [])) + const x11 = hudWindowingView(resolveHudWindowing('linux', X11_SESSION, [])) + + assert.deepEqual(wayland, { + clientPlacement: false, + controlDrag: false, + nativeDrag: true, + workspaceTransfer: false + }) + assert.deepEqual(x11, { + clientPlacement: true, + controlDrag: true, + nativeDrag: false, + workspaceTransfer: true + }) +}) diff --git a/apps/desktop/electron/hud-windowing.ts b/apps/desktop/electron/hud-windowing.ts new file mode 100644 index 0000000000..a834bcf2f8 --- /dev/null +++ b/apps/desktop/electron/hud-windowing.ts @@ -0,0 +1,137 @@ +/** + * HUD windowing profile — one object for every operation that used to + * special-case OS / Ozone / compositor. + * + * The Ozone backend (not the login session) is the normalizer: Electron 20+ + * prefers a native Wayland surface on a Wayland session, so `setBounds` + * position is a no-op there and ignore-mouse restore works. X11 is the + * inverse. macOS and Windows keep the click-through + renderer-drag design. + * + * Capabilities are derived once. Callers ask the profile, they do not + * re-derive "linux && click-through". + */ + +export type HudOzoneBackend = 'wayland' | 'x11' +export type HudWindowingBackend = 'cocoa' | 'win32' | HudOzoneBackend +export type HudInput = 'click-through' | 'solid' +export type HudMove = 'native-drag' | 'renderer' + +export interface HudWindowing { + backend: HudWindowingBackend + /** Whether the window may ignore the mouse (restore is known to work). */ + ignoreMouse: boolean + input: HudInput + move: HudMove + /** Client can set global x/y. Native Wayland cannot. */ + clientPlacement: boolean + /** Immediate Ctrl+primary-button grab (X11 renderer drag). */ + controlDrag: boolean + /** Temporary all-workspaces during a renderer grab (X11/KWin). */ + workspaceTransfer: boolean + /** Main must poll the cursor; `{ forward: true }` is unavailable. */ + cursorFeed: boolean +} + +/** Slice the renderer is allowed to see. */ +export interface HudWindowingView { + clientPlacement: boolean + controlDrag: boolean + nativeDrag: boolean + workspaceTransfer: boolean +} + +function requestedOzonePlatform(env: NodeJS.ProcessEnv, argv: readonly string[]): null | string { + let explicit: null | string = null + let hint: null | string = null + + for (const arg of argv) { + const match = /^--ozone-platform(-hint)?=(.+)$/.exec(arg) + + if (!match) { + continue + } + + if (match[1]) { + hint = match[2].toLowerCase() + } else { + explicit = match[2].toLowerCase() + } + } + + return explicit ?? hint ?? env.ELECTRON_OZONE_PLATFORM_HINT?.toLowerCase() ?? null +} + +function sessionIsWayland(env: NodeJS.ProcessEnv): boolean { + return env.XDG_SESSION_TYPE === 'wayland' || (Boolean(env.WAYLAND_DISPLAY) && !env.DISPLAY) +} + +/** + * The Ozone platform Electron will actually use on Linux. + * + * Unset / `auto` follows the session. An explicit `--ozone-platform=x11` + * (or `desktop.ozone_platform_hint: x11`) is the COSMIC always-on-top escape + * hatch and lands on the solid / renderer-drag path. + */ +export function linuxOzoneBackend(env: NodeJS.ProcessEnv, argv: readonly string[]): HudOzoneBackend { + const requested = requestedOzonePlatform(env, argv) + + if (requested === 'x11') { + return 'x11' + } + + if (requested === 'wayland') { + return 'wayland' + } + + return sessionIsWayland(env) ? 'wayland' : 'x11' +} + +function desktopWindowing(backend: 'cocoa' | 'win32'): HudWindowing { + return { + backend, + ignoreMouse: true, + input: 'click-through', + move: 'renderer', + clientPlacement: true, + controlDrag: false, + workspaceTransfer: false, + cursorFeed: false + } +} + +export function resolveHudWindowing( + platform: string, + env: NodeJS.ProcessEnv, + argv: readonly string[] +): HudWindowing { + if (platform === 'darwin') { + return desktopWindowing('cocoa') + } + + if (platform === 'win32') { + return desktopWindowing('win32') + } + + const backend = linuxOzoneBackend(env, argv) + const wayland = backend === 'wayland' + + return { + backend, + ignoreMouse: wayland, + input: wayland ? 'click-through' : 'solid', + move: wayland ? 'native-drag' : 'renderer', + clientPlacement: !wayland, + controlDrag: !wayland, + workspaceTransfer: !wayland, + cursorFeed: wayland + } +} + +export function hudWindowingView(windowing: HudWindowing): HudWindowingView { + return { + clientPlacement: windowing.clientPlacement, + controlDrag: windowing.controlDrag, + nativeDrag: windowing.move === 'native-drag', + workspaceTransfer: windowing.workspaceTransfer + } +} diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index 3bd334f4f2..2786d9fe2f 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -205,8 +205,8 @@ import { import { cursorPointInWindow } from './hud-cursor' import { startHudGameOverlayWatch } from './hud-game-overlay' import { applyHudResetBounds, defaultHudBounds } from './hud-geometry' -import { promoteHudOnHyprland } from './hud-hyprland' -import { hudInputPolicy } from './hud-input-policy' +import { applyHudElectronOverlay, promoteHudOverlay } from './hud-overlay' +import { resolveHudWindowing } from './hud-windowing' import { registerHudIpc } from './hud-ipc' import { snapHudBounds } from './hud-snap' import { createHudSnapShortcut } from './hud-snap-shortcut' @@ -11708,8 +11708,12 @@ const HUD_CURSOR_POLL_MS = 60 // Snap-to-pointer — global ⌘⇧G while the HUD is open (tap, not hold). const HUD_SNAP_ANCHOR_Y = 48 +function hudWindowing() { + return resolveHudWindowing(process.platform, process.env, process.argv) +} + function applyHudSnapToPointer() { - if (!hudWindow || hudWindow.isDestroyed()) { + if (!hudWindow || hudWindow.isDestroyed() || !hudWindowing().clientPlacement) { return } @@ -11763,20 +11767,15 @@ function registerHudSnapShortcut() { * the main process. */ function startHudCursorFeed(win: BrowserWindow) { - if (process.platform !== 'linux') { - return - } + const windowing = hudWindowing() - // On an X11 window `setIgnoreMouseEvents(false)` does not restore the input - // region once the window has ignored the mouse. Ignore is a one-way door - // there, so the HUD is held solid for its whole life (the companion veto - // is in the hermes:hud:ignore-mouse handler). Native Wayland keeps the - // poll — that is what re-arms click-through when the pointer returns. - if (hudInputPolicy(process.platform, process.env, process.argv) === 'solid') { - try { - win.setIgnoreMouseEvents(false) - } catch { - // best effort + if (!windowing.cursorFeed) { + if (!windowing.ignoreMouse) { + try { + win.setIgnoreMouseEvents(false) + } catch { + // best effort + } } return @@ -11946,17 +11945,9 @@ function spawnHudWindow(sessionId, profile) { webPreferences: chatWindowWebPreferences(PRELOAD_PATH) }) - win.setAlwaysOnTop(true, IS_MAC ? 'floating' : 'screen-saver') + applyHudElectronOverlay(win, process.platform) win.setHiddenInMissionControl?.(true) - if (IS_MAC) { - try { - win.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true, skipTransformProcessType: true }) - } catch { - // Not supported everywhere — best effort. - } - } - // Linux intentionally starts on ONE virtual desktop. During a renderer // grab, hermes:hud:workspace-transfer temporarily makes the X11 window // sticky; releasing it assigns the HUD to KDE's then-current desktop. @@ -11985,11 +11976,9 @@ function spawnHudWindow(sessionId, profile) { mainWindow.hide() } - // Hyprland tiles new toplevels. alwaysOnTop is compositor-owned on - // native Wayland, and xdg_toplevel.move is ignored on a tiled window, - // so the HUD never becomes an overlay and cannot be dragged. Same - // socket as read_window_below; no-op everywhere else. - void promoteHudOnHyprland({ title: HUD_WINDOW_TITLE }) + // Compositor overlay adapters (Hyprland float+pin today). Electron + // alwaysOnTop is already set; this is the dialect some WMs actually hear. + void promoteHudOverlay({ title: HUD_WINDOW_TITLE }) } }) diff --git a/apps/desktop/electron/preload.ts b/apps/desktop/electron/preload.ts index 8e8f9d4b1d..c438221032 100644 --- a/apps/desktop/electron/preload.ts +++ b/apps/desktop/electron/preload.ts @@ -8,7 +8,8 @@ import { contextBridge, ipcRenderer, webFrame, webUtils } from 'electron' // "Desktop IPC bridge is unavailable"). No reply means no glass, which degrades // to an ordinary opaque window rather than a page thinned over nothing. const translucencySupport = ipcRenderer.sendSync('hermes:translucency:support') -const hudNativeDrag = ipcRenderer.sendSync('hermes:hud:native-drag') === true +const hudWindowing = ipcRenderer.sendSync('hermes:hud:windowing') +const hudNativeDrag = hudWindowing?.nativeDrag === true contextBridge.exposeInMainWorld('hermesDesktop', { glassSupported: translucencySupport?.glass === true, @@ -72,6 +73,12 @@ contextBridge.exposeInMainWorld('hermesDesktop', { // window; `onChanged` keeps every window's toggle truthful. hud: { nativeDrag: hudNativeDrag, + windowing: { + clientPlacement: hudWindowing?.clientPlacement !== false, + controlDrag: hudWindowing?.controlDrag === true, + nativeDrag: hudNativeDrag, + workspaceTransfer: hudWindowing?.workspaceTransfer === true + }, open: request => ipcRenderer.invoke('hermes:hud:open', request), close: () => ipcRenderer.invoke('hermes:hud:close'), setIgnoreMouse: ignore => ipcRenderer.send('hermes:hud:ignore-mouse', ignore), diff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsx index 4906ff4e3f..dff9371f54 100644 --- a/apps/desktop/src/app/chat/composer/index.tsx +++ b/apps/desktop/src/app/chat/composer/index.tsx @@ -12,7 +12,6 @@ import { PR_COMMENT_URL_RE } from '@/lib/chat-runtime' import { sanitizeComposerInput } from '@/lib/composer-input-sanitize' import { DATA_IMAGE_URL_RE } from '@/lib/embedded-images' import { triggerHaptic } from '@/lib/haptics' -import { isLinuxPlatform } from '@/lib/platform' import { cn } from '@/lib/utils' import { interceptsTypedVoiceStop } from '@/lib/voice-stop-word' import { sessionCompacting } from '@/store/compaction' @@ -108,12 +107,12 @@ export function ChatBar({ onTranscribeAudio }: ChatBarProps) { const hudMode = useStore($hudMode) - const hudNativeDrag = hudMode && window.hermesDesktop?.hud?.nativeDrag === true - const hudX11Drag = hudMode && isLinuxPlatform() && !hudNativeDrag + const hudWindowing = window.hermesDesktop?.hud?.windowing + const hudNativeDrag = hudMode && hudWindowing?.nativeDrag === true const { grabbing: hudGrabbing, onPointerDown: onHudDragPointerDown } = useHudComposerDrag(hudMode && !hudNativeDrag, { - controlDrag: hudX11Drag, - workspaceTransfer: hudX11Drag + controlDrag: hudWindowing?.controlDrag === true, + workspaceTransfer: hudWindowing?.workspaceTransfer === true }) // Typed stop phrase during an active voice conversation ends it — same diff --git a/apps/desktop/src/app/hud/hud-shell.tsx b/apps/desktop/src/app/hud/hud-shell.tsx index d3d6e580ff..b1743ea0d7 100644 --- a/apps/desktop/src/app/hud/hud-shell.tsx +++ b/apps/desktop/src/app/hud/hud-shell.tsx @@ -377,7 +377,7 @@ export function HudShell() { // growth bug); the handle is the one sanctioned way to change size, driving // the same flip-resizable-for-the-call pattern the pet overlay uses. const { resizing: hudResizing, onPointerDown: onHudResizePointerDown } = useHudResizeHandle() - const resizeDirections = hudResizeDirections(window.hermesDesktop?.hud?.nativeDrag === true) + const resizeDirections = hudResizeDirections(window.hermesDesktop?.hud?.windowing?.clientPlacement !== false) // Force the HOST layers transparent. index.html's pre-paint script writes an // opaque themed background onto as an INLINE style (the anti-white- diff --git a/apps/desktop/src/app/hud/resize-handle.test.ts b/apps/desktop/src/app/hud/resize-handle.test.ts index 7d1ffd1390..2621d5fd0e 100644 --- a/apps/desktop/src/app/hud/resize-handle.test.ts +++ b/apps/desktop/src/app/hud/resize-handle.test.ts @@ -62,11 +62,11 @@ describe('hudResizeBounds', () => { describe('hudResizeDirections', () => { it('keeps every edge and corner on platforms with global window positioning', () => { - expect(hudResizeDirections(false)).toBe(HUD_RESIZE_DIRECTIONS) + expect(hudResizeDirections(true)).toBe(HUD_RESIZE_DIRECTIONS) }) - it('exposes only position-preserving handles on native Wayland', () => { - expect(hudResizeDirections(true)).toEqual(['e', 'se', 's']) + it('exposes only position-preserving handles when the client cannot place the window', () => { + expect(hudResizeDirections(false)).toEqual(['e', 'se', 's']) }) }) diff --git a/apps/desktop/src/app/hud/resize-handle.ts b/apps/desktop/src/app/hud/resize-handle.ts index 408ccc2a74..3aa0a37069 100644 --- a/apps/desktop/src/app/hud/resize-handle.ts +++ b/apps/desktop/src/app/hud/resize-handle.ts @@ -10,10 +10,10 @@ export type HudResizeDirection = (typeof HUD_RESIZE_DIRECTIONS)[number] const HUD_WAYLAND_RESIZE_DIRECTIONS = ['e', 'se', 's'] as const satisfies readonly HudResizeDirection[] -/** Native Wayland cannot read or change a top-level window's global x/y, so - * only edges that preserve the existing top-left position are truthful. */ -export function hudResizeDirections(nativeDrag: boolean): readonly HudResizeDirection[] { - return nativeDrag ? HUD_WAYLAND_RESIZE_DIRECTIONS : HUD_RESIZE_DIRECTIONS +/** Edges the HUD can honestly resize. Native Wayland cannot change a + * top-level's global x/y, so only edges that keep the existing origin. */ +export function hudResizeDirections(clientPlacement: boolean): readonly HudResizeDirection[] { + return clientPlacement ? HUD_RESIZE_DIRECTIONS : HUD_WAYLAND_RESIZE_DIRECTIONS } export interface HudResizeBounds { diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts index be93d8270b..52a7597ebf 100644 --- a/apps/desktop/src/global.d.ts +++ b/apps/desktop/src/global.d.ts @@ -92,6 +92,12 @@ declare global { // owns the window; `onChanged` keeps every window's toggle truthful. hud?: { nativeDrag: boolean + windowing?: { + clientPlacement: boolean + controlDrag: boolean + nativeDrag: boolean + workspaceTransfer: boolean + } open: (request?: { sessionId?: null | string; profile?: null | string }) => Promise<{ ok: boolean }> close: () => Promise<{ ok: boolean }> setIgnoreMouse: (ignore: boolean) => void