Ariver
2026-06-07 9f15087ff92afad0508a24d8803e8e8775359d09
Apptag/ContentView.swift
@@ -318,6 +318,7 @@
    var appDragResetToken = 0
    var bubbleDraftNote = ""
    var tagNavigationHoveredGroupName: String? = nil
    var tagNavigationAppDropTargetName: String? = nil
    var tagNavigationLastHoverScrollID: String? = nil
    var tagNavigationLastHoverScrollAt: Date? = nil
}
@@ -375,6 +376,7 @@
    @State private var refreshInProgress = false
    @State private var refreshAgainAfterCurrent = false
    @State private var refreshAgainForceLayout = false
    @State private var refreshAgainUseCache = true
    @FocusState private var bubbleNoteFocused: Bool
    // Quick Search
@@ -388,6 +390,7 @@
    @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
@@ -530,7 +533,7 @@
        }
        .onAppear {
            refreshNotchHeight()
            refreshAppsIfNeeded()
            refreshAppsForOverlay()
            if let initialQuickSearchSource, !initialQuickSearchConsumed {
                initialQuickSearchConsumed = true
                if initialQuickSearchSource == QuickSearchOpenSource.globalHidden {
@@ -541,7 +544,9 @@
                    quickSearchVisible = true
                    quickSearchFocusToken &+= 1
                }
                quickSearchOpenedAt = Date()
                refreshQuickSearchResults()
                refreshAppsForQuickSearch()
                NotificationCenter.default.post(
                    name: .tagLauncherQuickSearchVisibilityChanged,
                    object: nil,
@@ -555,7 +560,7 @@
        .onReceive(NotificationCenter.default.publisher(for: .tagLauncherOverlayDidShow)) { _ in
            resetTransientDragState()
            refreshNotchHeight()
            refreshAppsIfNeeded()
            refreshAppsForOverlay()
        }
        .onReceive(NotificationCenter.default.publisher(for: .tagLauncherOverlayDidHide)) { _ in
            resetTransientDragState()
@@ -567,8 +572,9 @@
            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 }
@@ -662,7 +668,7 @@
                    .ignoresSafeArea()
                    .contentShape(Rectangle())
                    .onTapGesture {
                        closeQuickSearch()
                        closeQuickSearch(dismissalSource: QuickSearchDismissSource.backdrop)
                    }
                    .zIndex(899)
@@ -672,7 +678,7 @@
                    selectedID: quickSearchSelectedID,
                    focusToken: quickSearchFocusToken,
                    selectionScrollToken: quickSearchSelectionScrollToken,
                    isLoading: quickSearchDocuments.isEmpty && refreshInProgress,
                    isLoading: quickSearchShouldShowLoading,
                    maxVisibleRows: quickSearchMaxVisibleRows(in: proxy.size),
                    panelTopY: quickSearchPanelTopY(in: proxy.size),
                    panelHeight: quickSearchPanelContentHeight(in: proxy.size),
@@ -691,7 +697,7 @@
    }
    private func quickSearchPanelContentHeight(in size: CGSize) -> CGFloat {
        let hasResultList = !(quickSearchDocuments.isEmpty && refreshInProgress)
        let hasResultList = !quickSearchShouldShowLoading
            && quickSearchErrorMessage == nil
            && !quickSearchResults.isEmpty
        let visibleRows = min(max(1, quickSearchResults.count), quickSearchMaxVisibleRows(in: size))
@@ -725,11 +731,13 @@
        quickSearchCloseHidesOverlay = quickSearchCloseHidesOverlay
            || source == QuickSearchOpenSource.globalHidden
        quickSearchVisible = true
        quickSearchOpenedAt = Date()
        quickSearchQuery = ""
        quickSearchManualSelection = false
        quickSearchErrorMessage = nil
        quickSearchFocusToken &+= 1
        refreshQuickSearchResults()
        refreshAppsForQuickSearch()
        NotificationCenter.default.post(
            name: .tagLauncherQuickSearchVisibilityChanged,
            object: nil,
@@ -748,10 +756,30 @@
            && !quickSearchVisible
    }
    private func closeQuickSearch(notify: Bool = true, hideOverlayIfNeeded: Bool = true) {
    private var quickSearchShouldShowLoading: Bool {
        quickSearchVisible && refreshInProgress && quickSearchResults.isEmpty
    }
    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
@@ -774,9 +802,29 @@
        }
    }
    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
@@ -795,6 +843,11 @@
    }
    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)
@@ -948,6 +1001,7 @@
    private func appKitTagNavigation(orientation: TagNavigationView.Orientation) -> some View {
        let isHorizontal = orientation == .horizontal
        let appDropTargetID = appGridInteraction.tagNavigationAppDropTargetName
        return TagNavigationView(
            items: tagLabels,
            orientation: orientation,
@@ -956,6 +1010,8 @@
                : NSEdgeInsets(top: 12, left: 12, bottom: 12, right: 12),
            dragModeActive: tagNavDragModeActive,
            draggingItemID: tagNavDragItem,
            appDragModeActive: appGridInteraction.appDragModeActive,
            appDropTargetID: appDropTargetID,
            onActivate: { tagID in
                activateTagNavigation(tagID)
            },
@@ -965,6 +1021,9 @@
            canReorder: { tagID in
                canReorderTag(tagID)
            },
            canAcceptAppDrop: { tagID in
                canAssignDroppedAppToTag(tagID)
            },
            onReorderBegan: { tagID in
                beginTagNavReorder(tagID)
            },
@@ -973,6 +1032,12 @@
            },
            onReorderEnded: {
                endTagNavReorder()
            },
            onAppDropHoverChange: { tagID, active in
                handleTagNavigationAppDropHover(tagID, active: active)
            },
            onAppDrop: { path, source, targetTag, copy in
                dropAppOnTagNavigation(path: path, sourceTag: source, targetTag: targetTag, copy: copy)
            }
        )
    }
@@ -1748,6 +1813,22 @@
        refreshApps()
    }
    private func refreshAppsForOverlay() {
        // Each overlay show creates a fresh ContentView with empty `allApps`. The indexer
        // cache may still be warm from a prior overlay/settings scan, so path-signature
        // alone must not skip the first hydrate or the grid spinner never clears.
        guard allApps.isEmpty || AppIndexer.shouldRefreshForSearchPathChanges() else { return }
        refreshApps()
    }
    private func refreshAppsForQuickSearch() {
        guard allApps.isEmpty
            || quickSearchDocuments.isEmpty
            || AppIndexer.shouldRefreshForSearchPathChanges()
        else { return }
        refreshApps()
    }
    private func refreshNotchHeight() {
        let mousePoint = NSEvent.mouseLocation
        let activeScreen = NSScreen.screens.first(where: {
@@ -1903,6 +1984,13 @@
    }
    private func handleTagNavigationHover(_ id: String, active: Bool) {
        if appGridInteraction.appDragModeActive {
            if !active, appGridInteraction.tagNavigationHoveredGroupName == id {
                appGridInteraction.tagNavigationHoveredGroupName = nil
            }
            return
        }
        guard active else {
            if appGridInteraction.tagNavigationHoveredGroupName == id {
                appGridInteraction.tagNavigationHoveredGroupName = nil
@@ -1913,6 +2001,24 @@
        appGridInteraction.tagNavigationHoveredGroupName = id
        fillColorlessContainer(id)
        scrollToTagFromHover(id)
    }
    private func handleTagNavigationAppDropHover(_ id: String, active: Bool) {
        guard appGridInteraction.appDragModeActive else {
            if appGridInteraction.tagNavigationAppDropTargetName == id {
                appGridInteraction.tagNavigationAppDropTargetName = nil
            }
            return
        }
        guard active, canAssignDroppedAppToTag(id) else {
            if appGridInteraction.tagNavigationAppDropTargetName == id {
                appGridInteraction.tagNavigationAppDropTargetName = nil
            }
            return
        }
        appGridInteraction.tagNavigationAppDropTargetName = id
    }
    private func scrollToTagFromHover(_ id: String) {
@@ -2050,6 +2156,22 @@
        )
        showDropRefresh()
        refreshApps(forceLayoutRefresh: true)
    }
    private func dropAppOnTagNavigation(path: String, sourceTag: String, targetTag: String, copy: Bool) {
        resetTransientDragState()
        guard canAssignDroppedAppToTag(targetTag) else { return }
        guard let app = allApps.first(where: { $0.path.path == path }) else { return }
        guard !appHasTag(app, tagName: targetTag) else { return }
        TagEditor.appendTags([targetTag], to: [path])
        showDropRefresh()
        refreshApps(forceLayoutRefresh: true)
    }
    private func canAssignDroppedAppToTag(_ tagName: String) -> Bool {
        tagColors[tagName] != nil
            && !isAppleBuiltInDropTarget(tagName)
            && !isUncategorizedDropTarget(tagName)
    }
    private func dropAppOutsideGroup(path: String, sourceTag: String, copy: Bool) {
@@ -2263,12 +2385,17 @@
        if active {
            endTagNavReorder()
            clearAppBubbleState()
            appGridInteraction.tagNavigationAppDropTargetName = nil
        }
        appGridInteraction.appDragModeActive = active
        if !active {
            appGridInteraction.tagNavigationAppDropTargetName = nil
        }
        if active {
            DispatchQueue.main.asyncAfter(deadline: .now() + 8) {
                if appGridInteraction.appDragModeActive && !AppDragCoordinator.shared.hasActiveDrag {
                    appGridInteraction.appDragModeActive = false
                    appGridInteraction.tagNavigationAppDropTargetName = nil
                }
            }
        }
@@ -2282,6 +2409,9 @@
        AppDragCoordinator.shared.cancelDrag()
        if appGridInteraction.appDragModeActive {
            appGridInteraction.appDragModeActive = false
        }
        if appGridInteraction.tagNavigationAppDropTargetName != nil {
            appGridInteraction.tagNavigationAppDropTargetName = nil
        }
        if hadAppDragState {
            appGridInteraction.appDragResetToken &+= 1
@@ -2307,18 +2437,17 @@
        }
    }
    func refreshApps(forceLayoutRefresh: Bool = false) {
    func refreshApps(forceLayoutRefresh: Bool = false, useCache: Bool = true) {
        guard !refreshInProgress else {
            if forceLayoutRefresh {
                refreshAgainAfterCurrent = true
                refreshAgainForceLayout = true
            }
            refreshAgainAfterCurrent = true
            refreshAgainForceLayout = refreshAgainForceLayout || forceLayoutRefresh
            refreshAgainUseCache = refreshAgainUseCache && useCache
            return
        }
        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)
@@ -2328,9 +2457,11 @@
                refreshInProgress = false
                if refreshAgainAfterCurrent {
                    let shouldForceLayout = refreshAgainForceLayout
                    let shouldUseCache = refreshAgainUseCache
                    refreshAgainAfterCurrent = false
                    refreshAgainForceLayout = false
                    refreshApps(forceLayoutRefresh: shouldForceLayout)
                    refreshAgainUseCache = true
                    refreshApps(forceLayoutRefresh: shouldForceLayout, useCache: shouldUseCache)
                }
            }
        }
@@ -2355,6 +2486,20 @@
    ) {
        appGridInteraction.appDragModeActive = false
        endTagNavReorder()
        guard FileManager.default.fileExists(atPath: app.path.path) else {
            AppIndexer.invalidateScanCache()
            refreshApps()
            onFailure?()
            return
        }
        let closeQuickSearchAndOverlayBeforeOpening = closeQuickSearchOnSuccess
            && closeOverlayOnSuccess
            && (quickSearchCloseHidesOverlay || quickSearchOnlySession)
        if closeQuickSearchAndOverlayBeforeOpening {
            closeQuickSearch(hideOverlayIfNeeded: true)
            hideOverlay()
        }
        let configuration = NSWorkspace.OpenConfiguration()
        NSWorkspace.shared.openApplication(at: app.path, configuration: configuration) { _, error in
            DispatchQueue.main.async {
@@ -2366,10 +2511,10 @@
                DispatchQueue.global(qos: .utility).async {
                    TagEditor.recordLauncherOpen(for: app.path.path)
                }
                if closeQuickSearchOnSuccess {
                    closeQuickSearch(hideOverlayIfNeeded: false)
                if closeQuickSearchOnSuccess && !closeQuickSearchAndOverlayBeforeOpening {
                    closeQuickSearch(hideOverlayIfNeeded: closeOverlayOnSuccess)
                }
                if closeOverlayOnSuccess {
                if closeOverlayOnSuccess && !closeQuickSearchAndOverlayBeforeOpening {
                    hideOverlay()
                }
            }