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 {