diff --git a/apps/desktop/src/app/starmap/playback-hotkey.test.ts b/apps/desktop/src/app/starmap/playback-hotkey.test.ts new file mode 100644 index 0000000000..ca1da5389b --- /dev/null +++ b/apps/desktop/src/app/starmap/playback-hotkey.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest' + +import { shouldIgnorePlaybackHotkey } from './playback-hotkey' + +function spaceOn(target: Element | null, defaultPrevented = false) { + return { code: 'Space', defaultPrevented, key: ' ', target } as unknown as KeyboardEvent +} + +describe('shouldIgnorePlaybackHotkey', () => { + it('toggles on Space when nothing focusable owns the key', () => { + expect(shouldIgnorePlaybackHotkey(spaceOn(document.body), document.body)).toBe(false) + }) + + it('ignores Space on a focused Radix menu item (div[role=menuitem] inside role=menu)', () => { + const menu = document.createElement('div') + menu.setAttribute('role', 'menu') + const item = document.createElement('div') + item.setAttribute('role', 'menuitem') + item.tabIndex = -1 + menu.append(item) + document.body.append(menu) + + try { + expect(shouldIgnorePlaybackHotkey(spaceOn(item), item)).toBe(true) + expect(shouldIgnorePlaybackHotkey(spaceOn(document.body, true), document.body)).toBe(true) + } finally { + menu.remove() + } + }) +}) diff --git a/apps/desktop/src/app/starmap/playback-hotkey.ts b/apps/desktop/src/app/starmap/playback-hotkey.ts new file mode 100644 index 0000000000..064ce1a67e --- /dev/null +++ b/apps/desktop/src/app/starmap/playback-hotkey.ts @@ -0,0 +1,32 @@ +/** + * Decide whether a window-level Space keydown should be ignored by the Star + * Map playback toggle. Ignored when the key is not Space, when something + * already handled it (`defaultPrevented`), when the user is typing or the + * focused element handles Space natively (inputs, buttons), or when focus is + * inside a menu — Radix menu items are `div[role=menuitem]`, so the tag check + * alone would let Space both activate the item and toggle playback. + */ +export function shouldIgnorePlaybackHotkey(e: Pick, activeElement: Element | null): boolean { + if (e.code !== 'Space' && e.key !== ' ') { + return true + } + + if (e.defaultPrevented) { + return true + } + + const el = activeElement as HTMLElement | null + const tag = el?.tagName + + if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'BUTTON' || el?.isContentEditable) { + return true + } + + const target = e.target as HTMLElement | null + + if (target?.closest?.('[role=menu]') || el?.closest?.('[role=menu]')) { + return true + } + + return false +} diff --git a/apps/desktop/src/app/starmap/star-map.tsx b/apps/desktop/src/app/starmap/star-map.tsx index 73b070e159..56af21912e 100644 --- a/apps/desktop/src/app/starmap/star-map.tsx +++ b/apps/desktop/src/app/starmap/star-map.tsx @@ -11,6 +11,7 @@ import { computePalette, memoryInkFor, resolveRgb, rgba } from './color' import { RING_OUTER, TILT, ZOOM_MAX, ZOOM_MIN } from './constants' import { clamp, distToSegmentSq, fitScale, fitViewport, nodeRadius } from './geometry' import { NodeContextMenu, type NodeMenuTarget } from './node-context-menu' +import { shouldIgnorePlaybackHotkey } from './playback-hotkey' import { drawScene, drawScramble } from './render' import { decodeShareCode, encodeShareCode, ShareCodeError } from './share-code' import { ShareControls } from './share-controls' @@ -445,17 +446,11 @@ export function StarMap({ ) // Spacebar toggles playback (unless typing, or the play button itself is - // focused — that already handles Space natively, so skip to avoid a double). + // focused — that already handles Space natively, so skip to avoid a double; + // same for focused context-menu items, which Radix renders as divs). useEffect(() => { const onKey = (e: KeyboardEvent) => { - if (e.code !== 'Space' && e.key !== ' ') { - return - } - - const el = document.activeElement - const tag = el?.tagName - - if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'BUTTON' || (el as HTMLElement | null)?.isContentEditable) { + if (shouldIgnorePlaybackHotkey(e, document.activeElement)) { return }