From 6c9e4d14e51a1f736cde9940d1ddc35ec3fba95a Mon Sep 17 00:00:00 2001 From: Lawrence Chen Date: Fri, 13 Mar 2026 16:06:40 -0700 Subject: [PATCH] Reset palette results on scope change --- Sources/ContentView.swift | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Sources/ContentView.swift b/Sources/ContentView.swift index 1e5e0946..f966d851 100644 --- a/Sources/ContentView.swift +++ b/Sources/ContentView.swift @@ -3208,12 +3208,22 @@ struct ContentView: View { updateCommandPaletteScrollTarget(resultCount: commandPaletteVisibleResults.count, animated: false) resetCommandPaletteSearchFocus() } - .onChange(of: commandPaletteQuery) { _ in + .onChange(of: commandPaletteQuery) { oldValue, newValue in commandPaletteSelectedResultIndex = 0 commandPaletteSelectionAnchorCommandID = nil commandPaletteHoveredResultIndex = nil commandPaletteScrollTargetIndex = nil commandPaletteScrollTargetAnchor = nil + if Self.commandPaletteShouldResetVisibleResultsForQueryTransition( + oldQuery: oldValue, + newQuery: newValue, + hasVisibleResults: commandPaletteVisibleResultsScope != nil + ) { + cachedCommandPaletteResults = [] + commandPaletteVisibleResults = [] + commandPaletteVisibleResultsScope = nil + commandPaletteVisibleResultsFingerprint = nil + } scheduleCommandPaletteResultsRefresh() updateCommandPaletteScrollTarget(resultCount: commandPaletteVisibleResults.count, animated: false) syncCommandPaletteDebugStateForObservedWindow() @@ -3346,14 +3356,26 @@ struct ContentView: View { } private var commandPaletteListScope: CommandPaletteListScope { - if commandPaletteQuery.hasPrefix(Self.commandPaletteCommandsPrefix) { + Self.commandPaletteListScope(for: commandPaletteQuery) + } + + private var commandPaletteCurrentSearchFingerprint: Int { + commandPaletteEntriesFingerprint(for: commandPaletteListScope) + } + + private static func commandPaletteListScope(for query: String) -> CommandPaletteListScope { + if query.hasPrefix(Self.commandPaletteCommandsPrefix) { return .commands } return .switcher } - private var commandPaletteCurrentSearchFingerprint: Int { - commandPaletteEntriesFingerprint(for: commandPaletteListScope) + static func commandPaletteShouldResetVisibleResultsForQueryTransition( + oldQuery: String, + newQuery: String, + hasVisibleResults: Bool + ) -> Bool { + hasVisibleResults && commandPaletteListScope(for: oldQuery) != commandPaletteListScope(for: newQuery) } private var commandPaletteSwitcherIncludesSurfaceEntries: Bool {