From f7ed0fe62e2792ede1000fd13370f9528bb68a03 Mon Sep 17 00:00:00 2001
From: Ariver <ar@MacBook-Air.local>
Date: Wed, 06 May 2026 18:39:07 +0800
Subject: [PATCH] checkpoint: v3.1.2 — Dock动态生效 + 开机启动默认勾选

---
 Apptag/ApptagApp.swift |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/Apptag/ApptagApp.swift b/Apptag/ApptagApp.swift
index 640545d..a55758d 100644
--- a/Apptag/ApptagApp.swift
+++ b/Apptag/ApptagApp.swift
@@ -1,6 +1,7 @@
 import SwiftUI
 import AppKit
 import Carbon
+import ServiceManagement
 
 // MARK: - Application Entry Point
 
@@ -25,16 +26,46 @@
 
     func applicationDidFinishLaunching(_ notification: Notification) {
         L10n.setup()
-        NSApp.setActivationPolicy(.accessory)
+        let showDock = UserDefaults.standard.bool(forKey: "showDockIcon")
+        NSApp.setActivationPolicy(showDock ? .regular : .accessory)
         setupMenuBar()
         registerHotkey()
         observeOtherWindows()
         observeEditMode()
+        observeDockSetting()
+        setupLaunchAtLogin()
+    }
+
+    /// Observe Show in Dock changes so it takes effect immediately.
+    private func observeDockSetting() {
+        NotificationCenter.default.addObserver(
+            forName: UserDefaults.didChangeNotification,
+            object: nil, queue: .main
+        ) { _ in
+            let show = UserDefaults.standard.bool(forKey: "showDockIcon")
+            NSApp.setActivationPolicy(show ? .regular : .accessory)
+        }
+    }
+
+    /// Enable launch at login by default; prompt user if disabled.
+    private func setupLaunchAtLogin() {
+        let key = "launchAtLogin"
+        if UserDefaults.standard.object(forKey: key) == nil {
+            // First launch: enable by default
+            UserDefaults.standard.set(true, forKey: key)
+            try? SMAppService.mainApp.register()
+        } else if UserDefaults.standard.bool(forKey: key) {
+            try? SMAppService.mainApp.register()
+        }
     }
 
     // MARK: - Menu Bar
 
     private func setupMenuBar() {
+        // Remove old status item if re-creating (language switch, etc.)
+        if let existing = statusItem {
+            NSStatusBar.system.removeStatusItem(existing)
+        }
         statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
 
         if let button = statusItem.button {
@@ -80,7 +111,7 @@
 
         // Language submenu
         let langMenu = NSMenu()
-        let currentLang = UserDefaults.standard.string(forKey: "appLanguage") ?? "en"
+        let currentLang = L10n.currentCode
         for (code, name) in L10n.supported {
             let item = NSMenuItem(title: name, action: #selector(switchLanguage(_:)), keyEquivalent: "")
             item.representedObject = code
@@ -343,6 +374,8 @@
     @AppStorage("defaultGroupName") private var defaultGroupName = "Other"
     @AppStorage("displayMode") private var displayMode = "flat"
     @AppStorage("hideAppNames") private var hideAppNames = false
+    @AppStorage("showDockIcon") private var showDockIcon = false
+    @AppStorage("launchAtLogin") private var launchAtLogin = true
 
     @State private var allApps: [AppInfo] = []
     @State private var tagColors: [String: Int] = [:]
@@ -409,6 +442,17 @@
             // Tab 1: General
             Form {
                 Section {
+                    Toggle("Launch at login", isOn: $launchAtLogin)
+                        .onChange(of: launchAtLogin) { _, enabled in
+                            if enabled {
+                                try? SMAppService.mainApp.register()
+                            } else {
+                                try? SMAppService.mainApp.unregister()
+                            }
+                        }
+                }
+
+                Section {
                     LabeledContent("App list style:") {
                         Picker("", selection: $displayMode) {
                             Text("Flat").tag("flat")
@@ -468,6 +512,10 @@
                 }
 
                 Section {
+                    Toggle("Show in Dock", isOn: $showDockIcon)
+                }
+
+                Section {
                     Toggle("Hide app names", isOn: $hideAppNames)
                 }
             }

--
Gitblit v1.9.3