Browse Source

Toggle status bar titlebar button to add or remove shortcut

Replace add-only action with toggle: remove current app from menu bar when
already present. Update icon (arrow up vs down), tooltip, and toggled style
when URL or status bar membership changes.

Made-with: Cursor
huzaifahayat12 3 months ago
parent
commit
ab069f2d6c
1 changed files with 43 additions and 12 deletions
  1. 43 12
      google_apps/InAppBrowserWindow.swift

+ 43 - 12
google_apps/InAppBrowserWindow.swift

@@ -254,7 +254,7 @@ final class InAppBrowserWindowController: NSWindowController, NSWindowDelegate {
         addToStatusBarButton = makeIconButton(
             symbolName: "arrow.up.square",
             accessibility: "Add to Status Bar",
-            action: #selector(addCurrentPageToStatusBar),
+            action: #selector(toggleCurrentPageStatusBar),
             fallbackSymbolName: "square.and.arrow.up"
         )
 
@@ -519,7 +519,7 @@ final class InAppBrowserWindowController: NSWindowController, NSWindowDelegate {
         )
     }
 
-    @objc private func addCurrentPageToStatusBar() {
+    @objc private func toggleCurrentPageStatusBar() {
         guard PremiumStore.shared.isPremiumUnlocked else {
             showAlert(
                 title: "Premium required",
@@ -535,15 +535,22 @@ final class InAppBrowserWindowController: NSWindowController, NSWindowDelegate {
             return
         }
         var ids = LauncherApp.statusBarShortcutIDSet()
-        guard ids.insert(app.id).inserted else {
-            showAlert(title: "Already added", message: "“\(app.name)” is already in the menu bar.")
-            return
+        if ids.contains(app.id) {
+            ids.remove(app.id)
+            LauncherApp.persistStatusBarShortcutIDs(ids)
+            showAlert(
+                title: "Removed from Status Bar",
+                message: "“\(app.name)” was removed from the menu bar."
+            )
+        } else {
+            ids.insert(app.id)
+            LauncherApp.persistStatusBarShortcutIDs(ids)
+            showAlert(
+                title: "Added to Status Bar",
+                message: "“\(app.name)” now has its own icon in the menu bar. Click it to open, or right‑click to remove."
+            )
         }
-        LauncherApp.persistStatusBarShortcutIDs(ids)
-        showAlert(
-            title: "Added to Status Bar",
-            message: "“\(app.name)” now has its own icon in the menu bar. Click it to open, or right‑click to remove."
-        )
+        updateAddToStatusBarButtonAppearance()
     }
 
     @objc private func addCurrentPageToDesktop() {
@@ -598,13 +605,37 @@ final class InAppBrowserWindowController: NSWindowController, NSWindowDelegate {
     }
 
     private func updateAddToStatusBarButtonAppearance() {
-        if resolvedLauncherAppForCurrentPage() == nil {
+        let cfg = NSImage.SymbolConfiguration(pointSize: 12, weight: .semibold)
+        guard let app = resolvedLauncherAppForCurrentPage() else {
             addToStatusBarButton.isEnabled = false
             addToStatusBarButton.toolTip = "Add to Status Bar — open this site from the launcher first"
+            let img = NSImage(systemSymbolName: "arrow.up.square", accessibilityDescription: "Add to Status Bar")?
+                .withSymbolConfiguration(cfg)
+            addToStatusBarButton.image = img
+            addToStatusBarButton.image?.isTemplate = true
+            setToggledStyle(addToStatusBarButton, isOn: false)
+            return
+        }
+        addToStatusBarButton.isEnabled = true
+        let inBar = LauncherApp.statusBarShortcutIDSet().contains(app.id)
+        if inBar {
+            let img = NSImage(systemSymbolName: "arrow.down.square", accessibilityDescription: "Remove from Status Bar")?
+                .withSymbolConfiguration(cfg)
+                ?? NSImage(systemSymbolName: "minus.square", accessibilityDescription: "Remove from Status Bar")?
+                .withSymbolConfiguration(cfg)
+            addToStatusBarButton.image = img
+            addToStatusBarButton.image?.isTemplate = true
+            addToStatusBarButton.toolTip = "Remove from Status Bar"
+            setToggledStyle(addToStatusBarButton, isOn: true)
         } else {
-            addToStatusBarButton.isEnabled = true
+            let img = NSImage(systemSymbolName: "arrow.up.square", accessibilityDescription: "Add to Status Bar")?
+                .withSymbolConfiguration(cfg)
+            addToStatusBarButton.image = img
+            addToStatusBarButton.image?.isTemplate = true
             addToStatusBarButton.toolTip = "Add to Status Bar"
+            setToggledStyle(addToStatusBarButton, isOn: false)
         }
+        addToStatusBarButton.refreshVisuals()
     }
 
     @objc private func togglePinOnTop() {