From 9d6f138e40a5322ee731f8096eaa3332c94a6532 Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Mon, 18 May 2026 17:54:35 +0800
Subject: [PATCH] Speed up repeated app list opens

---
 Apptag/ContentView.swift |   52 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/Apptag/ContentView.swift b/Apptag/ContentView.swift
index 87ee89a..16f587e 100644
--- a/Apptag/ContentView.swift
+++ b/Apptag/ContentView.swift
@@ -9,6 +9,7 @@
     static let tagLauncherAppNoteEditingChanged = Notification.Name("TagLauncherAppNoteEditingChanged")
     static let tagLauncherDataDidChange = Notification.Name("TagLauncherDataDidChange")
     static let tagLauncherOpenPreferencesRequested = Notification.Name("TagLauncherOpenPreferencesRequested")
+    static let tagLauncherOverlayDidShow = Notification.Name("TagLauncherOverlayDidShow")
 }
 
 // MARK: - Edit Phase
@@ -396,6 +397,9 @@
     private let editSidebarHorizontalInset: CGFloat = 12
     private let floatingControlsTrailingInset: CGFloat = 20
     private let floatingControlsReservedWidth: CGFloat = 120
+    private var appBubbleDisabled: Bool {
+        appDragModeActive || pendingUncategorizedDrop != nil
+    }
     private let rightSidebarFloatingClearance: CGFloat = 44
 
     private var isSideLayout: Bool {
@@ -480,14 +484,15 @@
             }
         }
         .onAppear {
-            let mousePoint = NSEvent.mouseLocation
-            let activeScreen = NSScreen.screens.first(where: {
-                NSMouseInRect(mousePoint, $0.frame, false)
-            }) ?? NSScreen.main
-            notchHeight = activeScreen?.safeAreaInsets.top ?? 0
+            refreshNotchHeight()
+            refreshApps()
+        }
+        .onReceive(NotificationCenter.default.publisher(for: .tagLauncherOverlayDidShow)) { _ in
+            refreshNotchHeight()
             refreshApps()
         }
         .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
+            guard allApps.isEmpty, !refreshInProgress else { return }
             refreshApps()
         }
         .onReceive(NotificationCenter.default.publisher(for: .tagLauncherDataDidChange)) { _ in
@@ -834,6 +839,7 @@
                             onDragModeChange: { setAppDragMode($0) },
                             onBubbleHover: handleBubbleHover,
                             onEditNote: beginEditingBubbleNote,
+                            bubbleDisabled: appBubbleDisabled,
                             hoveredAppItemID: $hoveredAppItemID,
                             onDropApp: { path, source, copy in
                                 dropApp(path: path, sourceTag: source, targetTag: group.name, copy: copy)
@@ -937,6 +943,7 @@
                         onDragModeChange: { setAppDragMode($0) },
                         onBubbleHover: handleBubbleHover,
                         onEditNote: beginEditingBubbleNote,
+                        bubbleDisabled: appBubbleDisabled,
                         itemID: "\(group.name)|\(app.path.path)",
                         hoveredAppItemID: $hoveredAppItemID,
                         onSelect: { openApp(app) }
@@ -1175,6 +1182,7 @@
                                 onDragModeChange: { setAppDragMode($0) },
                                 onBubbleHover: handleBubbleHover,
                                 onEditNote: beginEditingBubbleNote,
+                                bubbleDisabled: appBubbleDisabled,
                                 itemID: "\(group.name)|\(app.path.path)",
                                 hoveredAppItemID: $hoveredAppItemID,
                                 onSelect: { openApp(app) }
@@ -1847,7 +1855,19 @@
 
     // MARK: - Actions
 
+    private func refreshNotchHeight() {
+        let mousePoint = NSEvent.mouseLocation
+        let activeScreen = NSScreen.screens.first(where: {
+            NSMouseInRect(mousePoint, $0.frame, false)
+        }) ?? NSScreen.main
+        notchHeight = activeScreen?.safeAreaInsets.top ?? 0
+    }
+
     private func handleBubbleHover(app: AppInfo, frame: CGRect, hovering: Bool) {
+        guard !appBubbleDisabled else {
+            clearAppBubbleState()
+            return
+        }
         guard editingBubble == nil else { return }
         if hovering {
             hoveredBubble = AppBubbleContext(app: app, frame: frame)
@@ -1857,6 +1877,10 @@
     }
 
     private func beginEditingBubbleNote(app: AppInfo, frame: CGRect) {
+        guard !appBubbleDisabled else {
+            clearAppBubbleState()
+            return
+        }
         bubbleDraftNote = currentNote(for: app)
         hoveredBubble = nil
         editingBubble = AppBubbleContext(app: app, frame: frame)
@@ -1879,6 +1903,16 @@
     }
 
     private func dismissAppBubble() {
+        hoveredBubble = nil
+        if editingBubble != nil {
+            notifyAppNoteEditing(active: false)
+        }
+        editingBubble = nil
+        bubbleNoteFocused = false
+    }
+
+    private func clearAppBubbleState() {
+        hoveredAppItemID = nil
         hoveredBubble = nil
         if editingBubble != nil {
             notifyAppNoteEditing(active: false)
@@ -2089,6 +2123,7 @@
         let assignedTags = assignedRegularDisplayTags(for: app)
         guard !assignedTags.isEmpty else { return }
 
+        clearAppBubbleState()
         withAnimation(.spring(response: 0.24, dampingFraction: 0.84)) {
             pendingUncategorizedDrop = PendingUncategorizedDrop(app: app, assignedTags: assignedTags)
         }
@@ -2183,6 +2218,7 @@
     private func setAppDragMode(_ active: Bool) {
         if active {
             endTagNavReorder()
+            clearAppBubbleState()
         }
         appDragModeActive = active
         if active {
@@ -2196,8 +2232,10 @@
 
     func refreshApps(forceLayoutRefresh: Bool = false) {
         guard !refreshInProgress else {
-            refreshAgainAfterCurrent = true
-            refreshAgainForceLayout = refreshAgainForceLayout || forceLayoutRefresh
+            if forceLayoutRefresh {
+                refreshAgainAfterCurrent = true
+                refreshAgainForceLayout = true
+            }
             return
         }
 

--
Gitblit v1.9.3