Explorar el Código

Use a custom PNG for the main menu bar icon.

Load and scale the icon from a local file path, while preserving the existing SF Symbol as a fallback when the file is unavailable.

Made-with: Cursor
huzaifahayat12 hace 3 meses
padre
commit
8d5d15c678
Se han modificado 1 ficheros con 34 adiciones y 2 borrados
  1. 34 2
      google_apps/AppDelegate.swift

+ 34 - 2
google_apps/AppDelegate.swift

@@ -10,6 +10,7 @@ import Cocoa
 @main
 class AppDelegate: NSObject, NSApplicationDelegate {
     private var statusItem: NSStatusItem?
+    private let customMenuBarIconPath = "/Users/devmac1/Downloads/menu_bar_icon.png"
 
     func applicationDidFinishLaunching(_ aNotification: Notification) {
         configureMainWindowIfNeeded()
@@ -22,13 +23,44 @@ class AppDelegate: NSObject, NSApplicationDelegate {
         statusItem = item
 
         guard let button = item.button else { return }
-        button.image = NSImage(systemSymbolName: "square.grid.2x2.fill", accessibilityDescription: "My Apps")
-        button.image?.isTemplate = true
+        if let customIcon = NSImage(contentsOfFile: customMenuBarIconPath) {
+            button.image = scaledMenuBarIcon(from: customIcon)
+            button.image?.isTemplate = false
+        } else {
+            button.image = NSImage(systemSymbolName: "square.grid.2x2.fill", accessibilityDescription: "My Apps")
+            button.image?.isTemplate = true
+        }
+        button.imageScaling = .scaleProportionallyDown
+        button.imagePosition = .imageOnly
         button.action = #selector(statusBarButtonClicked(_:))
         button.target = self
         button.sendAction(on: [.leftMouseUp, .rightMouseUp])
     }
 
+    private func scaledMenuBarIcon(from source: NSImage) -> NSImage {
+        let side: CGFloat = 16
+        let target = NSSize(width: side, height: side)
+        let srcSize = source.size
+        guard srcSize.width > 0, srcSize.height > 0 else { return source }
+
+        let image = NSImage(size: target, flipped: false) { _ in
+            let scale = min(side / srcSize.width, side / srcSize.height)
+            let width = srcSize.width * scale
+            let height = srcSize.height * scale
+            let x = (side - width) / 2
+            let y = (side - height) / 2
+            let destinationRect = NSRect(x: x, y: y, width: width, height: height)
+            source.draw(
+                in: destinationRect,
+                from: NSRect(origin: .zero, size: srcSize),
+                operation: .sourceOver,
+                fraction: 1
+            )
+            return true
+        }
+        return image
+    }
+
     @objc
     private func statusBarButtonClicked(_ sender: Any?) {
         guard let event = NSApp.currentEvent else {