Переглянути джерело

Update status bar icon to match menu bar background

Add a dedicated menu bar icon asset and use a template status item image so macOS tints it correctly and updates on appearance changes.

Co-authored-by: Cursor <cursoragent@cursor.com>
huzaifahayat12 2 місяців тому
батько
коміт
7e6d73b281

+ 47 - 7
google_apps/AppDelegate.swift

@@ -15,6 +15,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
     private weak var launcherWindowReference: NSWindow?
     private var launcherWindowResignObserver: NSObjectProtocol?
     private var refocusLauncherObserver: NSObjectProtocol?
+    private var appearanceChangedObserver: NSObjectProtocol?
+    private var effectiveAppearanceObservation: NSKeyValueObservation?
     private var startupPaywallWindow: NSWindow?
     private var hasPresentedStartupPaywall = false
     private var premiumUnlockObservation: NSKeyValueObservation?
@@ -197,13 +199,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
         statusItem = item
 
         guard let button = item.button else { return }
-        if let menuBarIcon = NSImage(named: "menu_bar_icon") {
-            button.image = menuBarIcon
-            button.image?.isTemplate = false
-        } else {
-            button.image = NSImage(systemSymbolName: "square.grid.2x2.fill", accessibilityDescription: "My Apps")
-            button.image?.isTemplate = true
-        }
+        updateStatusBarIconForCurrentAppearance()
+        observeAppearanceChanges()
         button.imageScaling = .scaleProportionallyDown
         button.imagePosition = .imageOnly
         button.action = #selector(statusBarButtonClicked(_:))
@@ -211,6 +208,43 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
         button.sendAction(on: [.leftMouseUp, .rightMouseUp])
     }
 
+    private func updateStatusBarIconForCurrentAppearance() {
+        guard let button = statusItem?.button else { return }
+
+        // Use a template image so macOS tints it appropriately (white on dark menu bar,
+        // black on light menu bar), matching other status bar icons.
+        if let menuBarIcon = NSImage(named: "menu_bar_dark_icon") ?? NSImage(named: "menu_bar_icon") {
+            menuBarIcon.isTemplate = true
+            button.image = menuBarIcon
+            button.image?.isTemplate = true
+            return
+        }
+
+        button.image = NSImage(systemSymbolName: "square.grid.2x2.fill", accessibilityDescription: "My Apps")
+        button.image?.isTemplate = true
+    }
+
+    private func observeAppearanceChanges() {
+        if let appearanceChangedObserver {
+            DistributedNotificationCenter.default().removeObserver(appearanceChangedObserver)
+            self.appearanceChangedObserver = nil
+        }
+        effectiveAppearanceObservation?.invalidate()
+        effectiveAppearanceObservation = nil
+
+        appearanceChangedObserver = DistributedNotificationCenter.default().addObserver(
+            forName: NSNotification.Name("AppleInterfaceThemeChangedNotification"),
+            object: nil,
+            queue: .main
+        ) { [weak self] _ in
+            self?.updateStatusBarIconForCurrentAppearance()
+        }
+
+        effectiveAppearanceObservation = NSApp.observe(\.effectiveAppearance, options: [.new]) { [weak self] _, _ in
+            self?.updateStatusBarIconForCurrentAppearance()
+        }
+    }
+
     @objc
     private func statusBarButtonClicked(_ sender: Any?) {
         guard let event = NSApp.currentEvent else {
@@ -399,6 +433,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
             NotificationCenter.default.removeObserver(observer)
             refocusLauncherObserver = nil
         }
+        if let observer = appearanceChangedObserver {
+            DistributedNotificationCenter.default().removeObserver(observer)
+            appearanceChangedObserver = nil
+        }
+        effectiveAppearanceObservation?.invalidate()
+        effectiveAppearanceObservation = nil
         premiumStateMonitorTask?.cancel()
         premiumStateMonitorTask = nil
         premiumUnlockObservation?.invalidate()

+ 21 - 0
google_apps/Assets.xcassets/menu_bar_dark_icon.imageset/Contents.json

@@ -0,0 +1,21 @@
+{
+  "images" : [
+    {
+      "filename" : "menu_bar_dark_icon.png",
+      "idiom" : "universal",
+      "scale" : "1x"
+    },
+    {
+      "idiom" : "universal",
+      "scale" : "2x"
+    },
+    {
+      "idiom" : "universal",
+      "scale" : "3x"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  }
+}

BIN
google_apps/Assets.xcassets/menu_bar_dark_icon.imageset/menu_bar_dark_icon.png