From b700eb2f1c5d3855a79234e98b39c7a28dfa25a3 Mon Sep 17 00:00:00 2001
From: Ariver <ar@MacBook-Air.local>
Date: Wed, 06 May 2026 11:52:38 +0800
Subject: [PATCH] checkpoint: v2.0.2 — 标签拖拽排序改用 onDrag/onDrop,修复 List 导致无法退出编辑的 bug

---
 Apptag/ContentView.swift |   72 +++++++++++++++++++++++++----------
 1 files changed, 51 insertions(+), 21 deletions(-)

diff --git a/Apptag/ContentView.swift b/Apptag/ContentView.swift
index a9e24e6..3ea6f3b 100644
--- a/Apptag/ContentView.swift
+++ b/Apptag/ContentView.swift
@@ -202,6 +202,8 @@
     @State private var selectedAppPaths: Set<URL> = []
     @State private var selectedTagNames: Set<String> = []
     @State private var successToast: String? = nil
+    @State private var draggedTagNames: [String] = []  // live drag order
+    @State private var dragItem: String? = nil          // currently dragged tag
 
     // Configurable defaults
     @AppStorage("defaultGroupName") private var defaultGroupName = "Other"
@@ -267,8 +269,11 @@
     func setEditPhase(_ phase: EditPhase) {
         if phase != .none {
             NotificationCenter.default.post(name: .apptagEditModeChanged, object: nil, userInfo: ["active": true])
-            // Re-activate to ensure overlay window stays key during view transition
             NSApp.activate(ignoringOtherApps: true)
+            // Sync drag order from database when entering edit mode
+            if draggedTagNames.isEmpty {
+                draggedTagNames = TagEditor.orderedTagNames()
+            }
         }
         editPhase = phase
         if phase == .none {
@@ -434,13 +439,32 @@
             HStack(spacing: 0) {
                 VStack(alignment: .leading, spacing: 4) {
                     Text("Select tags:").font(.caption).foregroundStyle(.secondary).padding(.bottom, 4)
+                    Text("Drag to reorder").font(.caption2).foregroundStyle(.tertiary).padding(.bottom, 2)
                     ScrollView(.vertical, showsIndicators: false) {
                         VStack(spacing: 4) {
                             ForEach(sortedTagNames, id: \.self) { tagName in
                                 selectableTagItem(tagName)
+                                    .onDrag {
+                                        dragItem = tagName
+                                        return NSItemProvider(object: tagName as NSString)
+                                    }
+                                    .onDrop(of: [.text], isTargeted: nil) { providers, _ in
+                                        guard let fromName = dragItem,
+                                              var names = draggedTagNames as [String]?,
+                                              let fromIdx = names.firstIndex(of: fromName),
+                                              let toIdx = names.firstIndex(of: tagName),
+                                              fromIdx != toIdx
+                                        else { return false }
+                                        let toOffset = toIdx > fromIdx ? toIdx + 1 : toIdx
+                                        names.move(fromOffsets: [fromIdx], toOffset: toOffset)
+                                        draggedTagNames = names
+                                        TagEditor.reorderTags(names)
+                                        dragItem = nil
+                                        return true
+                                    }
                             }
                         }.padding(12)
-                    }.frame(width: 145)
+                    }.frame(width: 155)
                 }
                 Rectangle().fill(.secondary.opacity(0.12)).frame(width: 1)
 
@@ -483,22 +507,21 @@
 
     private func selectableTagItem(_ tagName: String) -> some View {
         let isSelected = selectedTagNames.contains(tagName)
-        return Button {
-            if isSelected { selectedTagNames.remove(tagName) } else { selectedTagNames.insert(tagName) }
-        } label: {
-            HStack(spacing: 6) {
-                Circle()
-                    .fill(isSelected ? Color.accentColor : Color.secondary.opacity(0.3))
-                    .frame(width: 16, height: 16)
-                    .overlay(isSelected ? Image(systemName: "checkmark").font(.system(size: 8, weight: .bold)).foregroundStyle(.white) : nil)
-                Text(tagName).font(.system(size: 13, weight: .medium)).foregroundStyle(.primary)
-            }
-            .padding(.horizontal, 10).padding(.vertical, 5)
-            .frame(maxWidth: .infinity, alignment: .leading)
-            .background(RoundedRectangle(cornerRadius: 6)
-                .fill(Color(nsColor: TagColor.nsColor(for: tagColors[tagName] ?? 0).withAlphaComponent(0.3))))
+        return HStack(spacing: 6) {
+            Circle()
+                .fill(isSelected ? Color.accentColor : Color.secondary.opacity(0.3))
+                .frame(width: 16, height: 16)
+                .overlay(isSelected ? Image(systemName: "checkmark").font(.system(size: 8, weight: .bold)).foregroundStyle(.white) : nil)
+            Text(tagName).font(.system(size: 13, weight: .medium)).foregroundStyle(.primary)
         }
-        .buttonStyle(.plain)
+        .padding(.horizontal, 10).padding(.vertical, 5)
+        .frame(maxWidth: .infinity, alignment: .leading)
+        .background(RoundedRectangle(cornerRadius: 6)
+            .fill(Color(nsColor: TagColor.nsColor(for: tagColors[tagName] ?? 0).withAlphaComponent(0.3))))
+        .contentShape(RoundedRectangle(cornerRadius: 6))
+        .onTapGesture {
+            if isSelected { selectedTagNames.remove(tagName) } else { selectedTagNames.insert(tagName) }
+        }
     }
 
     private func confirmAssign() {
@@ -544,9 +567,11 @@
     // MARK: - Computed
 
     private var sortedTagNames: [String] {
-        tagColors.keys
-            .filter { $0 != "Mac自带" && $0 != defaultGroupName }
-            .sorted { $0.localizedStandardCompare($1) == .orderedAscending }
+        let filtered = tagColors.keys.filter { $0 != "Mac自带" && $0 != defaultGroupName }
+        // Use user-defined order, fall back to alpha
+        let ordered = draggedTagNames.filter { filtered.contains($0) }
+        let remaining = filtered.filter { !ordered.contains($0) }.sorted()
+        return ordered + remaining
     }
 
     private var tagLabels: [TagLabel] {
@@ -554,7 +579,10 @@
     }
 
     private var groups: [TagGroup] {
-        AppIndexer.group(apps: allApps, defaultGroupName: defaultGroupName)
+        let order = draggedTagNames.isEmpty
+            ? TagEditor.orderedTagNames()
+            : draggedTagNames
+        return AppIndexer.group(apps: allApps, defaultGroupName: defaultGroupName, tagOrder: order)
     }
 
     // MARK: - Actions
@@ -570,9 +598,11 @@
             let store = TagDatabase.migrateFromFinderIfNeeded(apps: apps)
             apps = TagEditor.annotate(apps: apps)
             let colors = store.tags.mapValues { $0.color }
+            let order = TagEditor.orderedTagNames()
             DispatchQueue.main.async {
                 allApps = apps
                 tagColors = colors
+                draggedTagNames = order
             }
         }
     }

--
Gitblit v1.9.3