diff --git a/src/web/public/app.js b/src/web/public/app.js index 166d5829..24b7be3e 100644 --- a/src/web/public/app.js +++ b/src/web/public/app.js @@ -1427,6 +1427,13 @@ class CodemanApp { this.closeTileCountMenu({ refocus: true }); return; } + // And so does the rail's search box while it holds text: that Escape + // clears the search and nothing else. This listener runs in the capture + // phase, before the box's own onkeydown, so the box cannot claim it there. + if (e.target?.id === 'tabRailSearch' && this._tabRailSearch) { + this.handleTabRailSearchKeydown(e); + return; + } this.closeAllPanels(); this.closeHelp(); if (this.attachmentHistoryDrawerOpen) this.closeAttachmentHistory(); @@ -5859,10 +5866,16 @@ class CodemanApp { input?.focus(); } + /** + * Escape in a box that holds text clears the search and nothing else. The + * global key handler (setupEventListeners) runs in the CAPTURE phase, before + * the box's inline onkeydown, so it is the one that routes the key here and + * returns before its close-every-panel branch; stopping propagation from the + * inline handler would come too late. An empty box leaves Escape to it, and + * an Escape that cancels an IME composition is the IME's. + */ handleTabRailSearchKeydown(event) { - if (event.key !== 'Escape' || !this._tabRailSearch) return; - // Consumed here: the global Escape handler would otherwise close whatever - // else is open on the same keypress. + if (event.key !== 'Escape' || event.isComposing || !this._tabRailSearch) return; event.preventDefault(); event.stopPropagation(); this.clearTabRailSearch(); diff --git a/test/tab-rail-search.browser.test.ts b/test/tab-rail-search.browser.test.ts index 8b4d10b1..436a160e 100644 --- a/test/tab-rail-search.browser.test.ts +++ b/test/tab-rail-search.browser.test.ts @@ -117,6 +117,14 @@ describe('vertical rail session search in Chromium', () => { }; w.__setApp(app); w.__app = app; + // The real global key handler (document, capture phase). The close + // methods it calls live in settings-ui.js / panels-ui.js, which are not + // loaded here, so they record instead. + app.setupColorPicker = () => undefined; + app.closeAllPanels = () => w.__closed.push('panels'); + app.closeHelp = () => w.__closed.push('help'); + app.closeSessionManager = () => w.__closed.push('session-manager'); + app.setupEventListeners(); }); }); @@ -144,6 +152,7 @@ describe('vertical rail session search in Chromium', () => { app._resetTabRailSearch(); app._fullRenderSessionTabs(); w.__activation = null; + w.__closed = []; }, LAYOUT); }); @@ -201,6 +210,21 @@ describe('vertical rail session search in Chromium', () => { expect(await paintedRows()).toEqual(['alpha', 'web', 'notes']); }); + it('Escape with text in the box clears it and closes nothing else', async () => { + const search = page.getByRole('searchbox', { name: 'Search sessions' }); + await search.fill('review'); + expect(await paintedRows()).toEqual(['review']); + + await search.press('Escape'); + expect(await search.inputValue()).toBe(''); + expect(await paintedRows()).toEqual(['alpha', 'web', 'notes']); + expect(await page.evaluate(() => (window as any).__closed)).toEqual([]); + + // An empty box leaves Escape to the global handler, which closes as always. + await search.press('Escape'); + expect(await page.evaluate(() => (window as any).__closed)).toEqual(['panels', 'help', 'session-manager']); + }); + it('walks only the matches with the arrow keys', async () => { await page.getByRole('searchbox', { name: 'Search sessions' }).fill('a'); // 'a' matches Alpha API, Roadmap, API Review and Dashboard, not Notes. diff --git a/test/tab-rail-search.test.ts b/test/tab-rail-search.test.ts index 29c57679..b5c9ccec 100644 --- a/test/tab-rail-search.test.ts +++ b/test/tab-rail-search.test.ts @@ -532,6 +532,80 @@ describe('the sidebar filter keeps its own matching', () => { }); }); +describe('Escape in the search box', () => { + /** + * The real global key handler from setupEventListeners(), on document in the + * capture phase, with the close methods it calls (settings-ui.js and + * panels-ui.js are not loaded here) as recorders. The box in this harness + * has no inline onkeydown, so only the global handler can clear it. + */ + function installGlobalKeys(app: Record) { + const closed: string[] = []; + app.setupColorPicker = () => {}; + app.closeAllPanels = () => closed.push('panels'); + app.closeHelp = () => closed.push('help'); + app.closeSessionManager = () => closed.push('session-manager'); + app.closeCommandPalette = () => {}; + app.closeShortcutOverlay = () => {}; + const add = vi.spyOn(document, 'addEventListener'); + app.setupEventListeners(); + const listener = add.mock.calls.find(([type]) => type === 'keydown')![1] as EventListener; + add.mockRestore(); + return { closed, remove: () => document.removeEventListener('keydown', listener, true) }; + } + const escape = () => new window.KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true }); + + it('clears a search and closes nothing else, though the global handler runs first', () => { + const app = makeApp(); + app._fullRenderSessionTabs(); + const keys = installGlobalKeys(app); + try { + input().value = 'review'; + app.setTabRailSearch(input().value); + input().focus(); + + const event = escape(); + input().dispatchEvent(event); + expect(event.defaultPrevented).toBe(true); + expect(input().value).toBe(''); + expect(app._tabRailSearch).toBe(''); + expect(visibleRows()).toEqual(['alpha', 'web', 'roadmap', 'review', 'notes']); + expect(keys.closed).toEqual([]); + + // The box is empty now: the next Escape is the global handler's again. + input().dispatchEvent(escape()); + expect(keys.closed).toEqual(['panels', 'help', 'session-manager']); + } finally { + keys.remove(); + } + }); + + it('only claims an Escape that lands in the box', () => { + const app = makeApp(); + app._fullRenderSessionTabs(); + const keys = installGlobalKeys(app); + try { + app.setTabRailSearch('review'); + document.body.dispatchEvent(escape()); + expect(keys.closed).toEqual(['panels', 'help', 'session-manager']); + expect(app._tabRailSearch).toBe('review'); + expect(visibleRows()).toEqual(['review']); + } finally { + keys.remove(); + } + }); + + it('leaves an Escape that cancels an IME composition to the IME', () => { + const app = makeApp(); + app._fullRenderSessionTabs(); + app.setTabRailSearch('review'); + const composing = new window.KeyboardEvent('keydown', { key: 'Escape', isComposing: true, cancelable: true }); + app.handleTabRailSearchKeydown(composing); + expect(composing.defaultPrevented).toBe(false); + expect(app._tabRailSearch).toBe('review'); + }); +}); + // ─── Markup + zh-CN ─────────────────────────────────────────────────────────── describe('markup and zh-CN', () => {