From aba608b433a9dd7e218239947c2c983741fc7fc7 Mon Sep 17 00:00:00 2001
From: Ariver <shanghai3168@gmail.com>
Date: Wed, 17 Jun 2026 20:42:14 +0800
Subject: [PATCH] Prepare 7.9.2 build 20260617.2028

---
 src/Apptag/ApptagApp.swift |   48 +++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/src/Apptag/ApptagApp.swift b/src/Apptag/ApptagApp.swift
index e4efd6d..f0df77a 100644
--- a/src/Apptag/ApptagApp.swift
+++ b/src/Apptag/ApptagApp.swift
@@ -342,11 +342,53 @@
         }
     }
 
+    private func foregroundWindowForActivationPolicyRestore() -> NSWindow? {
+        if let settingsWindow, settingsWindow.isVisible {
+            return settingsWindow
+        }
+        if let overlayWindow, overlayWindow.isVisible {
+            return overlayWindow
+        }
+        return NSApp.keyWindow
+    }
+
+    private func setLauncherActivationPolicy(_ desiredPolicy: NSApplication.ActivationPolicy) {
+        guard NSApp.activationPolicy() != desiredPolicy else { return }
+
+        let restoreWindow = desiredPolicy == .accessory && NSApp.isActive
+            ? foregroundWindowForActivationPolicyRestore()
+            : nil
+
+        // Deactivate before switching regular -> accessory; otherwise Dock can
+        // keep a stale running tile until Dock itself restarts.
+        if desiredPolicy == .accessory && NSApp.isActive {
+            NSApp.deactivate()
+        }
+
+        NSApp.setActivationPolicy(desiredPolicy)
+
+        guard desiredPolicy == .accessory,
+              let restoreWindow,
+              restoreWindow.isVisible
+        else { return }
+
+        DispatchQueue.main.async { [weak self, weak restoreWindow] in
+            guard let self,
+                  let restoreWindow,
+                  restoreWindow.isVisible,
+                  self.requiresForegroundOwnership
+            else { return }
+            NSApp.activate(ignoringOtherApps: true)
+            restoreWindow.makeKeyAndOrderFront(nil)
+            restoreWindow.orderFrontRegardless()
+        }
+    }
+
     private func beginLauncherForegroundOwnership(activate: Bool = true, keyWindow: NSWindow? = nil) {
         let showDock = UserDefaults.standard.bool(forKey: Self.showDockIconKey)
         let desiredPolicy: NSApplication.ActivationPolicy = showDock ? .regular : .accessory
         if NSApp.activationPolicy() != desiredPolicy {
-            NSApp.setActivationPolicy(desiredPolicy)
+            setLauncherActivationPolicy(desiredPolicy)
         }
         if activate, showDock {
             claimLauncherForeground(keyWindow: keyWindow)
@@ -363,7 +405,7 @@
         overlayGeneration expectedOverlayGeneration: Int? = nil
     ) {
         if NSApp.activationPolicy() != .regular {
-            NSApp.setActivationPolicy(.regular)
+            setLauncherActivationPolicy(.regular)
         }
 
         if isOverlayVisible && !NSApp.presentationOptions.contains(.hideDock) {
@@ -425,7 +467,7 @@
             ? .regular
             : (showDock ? .regular : .accessory))))
         if NSApp.activationPolicy() != desiredPolicy {
-            NSApp.setActivationPolicy(desiredPolicy)
+            setLauncherActivationPolicy(desiredPolicy)
         }
 
         let desiredPresentation: NSApplication.PresentationOptions = isOverlayVisible ? [.hideDock] : []

--
Gitblit v1.9.3