| | |
| | | @State private var quickSearchSelectionScrollToken = 0 |
| | | @State private var quickSearchCloseHidesOverlay = false |
| | | @State private var quickSearchOnlySession = false |
| | | @State private var quickSearchOpenedAt: Date? = nil |
| | | @State private var quickSearchErrorMessage: String? = nil |
| | | @State private var initialQuickSearchConsumed = false |
| | | |
| | |
| | | quickSearchVisible = true |
| | | quickSearchFocusToken &+= 1 |
| | | } |
| | | quickSearchOpenedAt = Date() |
| | | refreshQuickSearchResults() |
| | | NotificationCenter.default.post( |
| | | name: .tagLauncherQuickSearchVisibilityChanged, |
| | |
| | | let source = notification.userInfo?["source"] as? String ?? QuickSearchOpenSource.mainOverlay |
| | | openQuickSearch(source: source) |
| | | } |
| | | .onReceive(NotificationCenter.default.publisher(for: .tagLauncherQuickSearchDismissRequested)) { _ in |
| | | closeQuickSearch() |
| | | .onReceive(NotificationCenter.default.publisher(for: .tagLauncherQuickSearchDismissRequested)) { notification in |
| | | let source = notification.userInfo?["source"] as? String |
| | | closeQuickSearch(dismissalSource: source) |
| | | } |
| | | .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in |
| | | guard allApps.isEmpty, !refreshInProgress else { return } |
| | |
| | | .ignoresSafeArea() |
| | | .contentShape(Rectangle()) |
| | | .onTapGesture { |
| | | closeQuickSearch() |
| | | closeQuickSearch(dismissalSource: QuickSearchDismissSource.backdrop) |
| | | } |
| | | .zIndex(899) |
| | | |
| | |
| | | quickSearchCloseHidesOverlay = quickSearchCloseHidesOverlay |
| | | || source == QuickSearchOpenSource.globalHidden |
| | | quickSearchVisible = true |
| | | quickSearchOpenedAt = Date() |
| | | quickSearchQuery = "" |
| | | quickSearchManualSelection = false |
| | | quickSearchErrorMessage = nil |
| | | quickSearchFocusToken &+= 1 |
| | | refreshQuickSearchResults() |
| | | refreshApps() |
| | | NotificationCenter.default.post( |
| | | name: .tagLauncherQuickSearchVisibilityChanged, |
| | | object: nil, |
| | |
| | | && !quickSearchVisible |
| | | } |
| | | |
| | | private func closeQuickSearch(notify: Bool = true, hideOverlayIfNeeded: Bool = true) { |
| | | private func closeQuickSearch( |
| | | notify: Bool = true, |
| | | hideOverlayIfNeeded: Bool = true, |
| | | dismissalSource: String? = nil |
| | | ) { |
| | | Diagnostics.log("content.quickSearch.closeRequest", [ |
| | | "source": dismissalSource, |
| | | "notify": notify, |
| | | "hideOverlayIfNeeded": hideOverlayIfNeeded, |
| | | "quickSearchVisible": quickSearchVisible, |
| | | "quickSearchCloseHidesOverlay": quickSearchCloseHidesOverlay, |
| | | "quickSearchOnlySession": quickSearchOnlySession |
| | | ]) |
| | | if shouldIgnoreEarlyQuickSearchDismiss(from: dismissalSource) { |
| | | return |
| | | } |
| | | let shouldHideOverlay = quickSearchVisible && quickSearchCloseHidesOverlay && hideOverlayIfNeeded |
| | | guard quickSearchVisible else { return } |
| | | quickSearchVisible = false |
| | | quickSearchOpenedAt = nil |
| | | quickSearchQuery = "" |
| | | quickSearchResults = [] |
| | | quickSearchSelectedID = nil |
| | |
| | | } |
| | | } |
| | | |
| | | private func shouldIgnoreEarlyQuickSearchDismiss(from source: String?) -> Bool { |
| | | guard quickSearchVisible, |
| | | let quickSearchOpenedAt, |
| | | source == QuickSearchDismissSource.mouse || source == QuickSearchDismissSource.backdrop |
| | | else { return false } |
| | | |
| | | let age = Date().timeIntervalSince(quickSearchOpenedAt) |
| | | let gracePeriod: TimeInterval = quickSearchOnlySession ? 1.25 : 0.30 |
| | | guard age < gracePeriod else { return false } |
| | | Diagnostics.log("content.quickSearch.ignoredEarlyDismiss", [ |
| | | "source": source, |
| | | "age": String(format: "%.3f", age), |
| | | "gracePeriod": String(format: "%.2f", gracePeriod) |
| | | ]) |
| | | return true |
| | | } |
| | | |
| | | private func refreshQuickSearchResults() { |
| | | let previousSelection = quickSearchSelectedID |
| | | quickSearchResults = QuickSearchEngine.search(quickSearchQuery, documents: quickSearchDocuments) |
| | | quickSearchResults = QuickSearchEngine.search( |
| | | quickSearchQuery, |
| | | documents: quickSearchDocuments.filter { FileManager.default.fileExists(atPath: $0.app.path.path) } |
| | | ) |
| | | |
| | | if quickSearchResults.isEmpty { |
| | | quickSearchSelectedID = nil |
| | |
| | | } |
| | | |
| | | private func handleQuickSearchCommand(_ command: QuickSearchCommand) { |
| | | Diagnostics.log("content.quickSearch.command", [ |
| | | "command": String(describing: command), |
| | | "quickSearchCloseHidesOverlay": quickSearchCloseHidesOverlay, |
| | | "quickSearchOnlySession": quickSearchOnlySession |
| | | ]) |
| | | switch command { |
| | | case .moveUp: |
| | | moveQuickSearchSelection(by: -1) |
| | |
| | | } |
| | | } |
| | | |
| | | func refreshApps(forceLayoutRefresh: Bool = false) { |
| | | func refreshApps(forceLayoutRefresh: Bool = false, useCache: Bool = true) { |
| | | guard !refreshInProgress else { |
| | | if forceLayoutRefresh { |
| | | refreshAgainAfterCurrent = true |
| | |
| | | |
| | | refreshInProgress = true |
| | | DispatchQueue.global(qos: .userInitiated).async { |
| | | let result = AppLibraryController.refresh() |
| | | let result = AppLibraryController.refresh(useCache: useCache) |
| | | DispatchQueue.main.async { |
| | | applyAppLibrarySnapshot(result.snapshot) |
| | | handleSmartStartRunResult(result.smartStartResult) |
| | |
| | | ) { |
| | | appGridInteraction.appDragModeActive = false |
| | | endTagNavReorder() |
| | | guard FileManager.default.fileExists(atPath: app.path.path) else { |
| | | AppIndexer.invalidateScanCache() |
| | | refreshApps() |
| | | onFailure?() |
| | | return |
| | | } |
| | | |
| | | let closeQuickSearchAndOverlayBeforeOpening = closeQuickSearchOnSuccess |
| | | && closeOverlayOnSuccess |
| | | && (quickSearchCloseHidesOverlay || quickSearchOnlySession) |