|
@@ -10,6 +10,7 @@ import Cocoa
|
|
|
@main
|
|
@main
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
private var statusItem: NSStatusItem?
|
|
private var statusItem: NSStatusItem?
|
|
|
|
|
+ private let customMenuBarIconPath = "/Users/devmac1/Downloads/menu_bar_icon.png"
|
|
|
|
|
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
|
configureMainWindowIfNeeded()
|
|
configureMainWindowIfNeeded()
|
|
@@ -22,13 +23,44 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
statusItem = item
|
|
statusItem = item
|
|
|
|
|
|
|
|
guard let button = item.button else { return }
|
|
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.action = #selector(statusBarButtonClicked(_:))
|
|
|
button.target = self
|
|
button.target = self
|
|
|
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
|
|
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
|
|
@objc
|
|
|
private func statusBarButtonClicked(_ sender: Any?) {
|
|
private func statusBarButtonClicked(_ sender: Any?) {
|
|
|
guard let event = NSApp.currentEvent else {
|
|
guard let event = NSApp.currentEvent else {
|