diff --git a/src/ui/App.swift b/src/ui/App.swift index a6de7f335..b5f598291 100644 --- a/src/ui/App.swift +++ b/src/ui/App.swift @@ -16,6 +16,7 @@ class App: AppCenterApplication, NSApplicationDelegate { static let licence = Bundle.main.object(forInfoDictionaryKey: "NSHumanReadableCopyright") as! String static let repository = "https://github.com/lwouis/alt-tab-macos" static var app: App! + var menubar: Menubar! var thumbnailsPanel: ThumbnailsPanel! var previewPanel: PreviewPanel! var preferencesWindow: PreferencesWindow! @@ -53,7 +54,7 @@ class App: AppCenterApplication, NSApplicationDelegate { guard let self = self else { return } BackgroundWork.start() Preferences.initialize() - Menubar.initialize() + self.menubar = Menubar() self.loadMainMenuXib() self.thumbnailsPanel = ThumbnailsPanel() self.previewPanel = PreviewPanel() diff --git a/src/ui/Menubar.swift b/src/ui/Menubar.swift index b7075e330..ddaf6b4e6 100644 --- a/src/ui/Menubar.swift +++ b/src/ui/Menubar.swift @@ -1,42 +1,54 @@ import Cocoa class Menubar { - static var statusItem: NSStatusItem! + var statusItem: NSStatusItem! + var menu: NSMenu! - static func initialize() { - statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) - statusItem.menu = NSMenu() - statusItem.menu!.title = App.name // perf: prevent going through expensive code-path within appkit - statusItem.menu!.addItem( + init() { + menu = NSMenu() + menu.title = App.name // perf: prevent going through expensive code-path within appkit + menu.addItem( withTitle: String(format: NSLocalizedString("About %@", comment: "Menubar option. %@ is AltTab"), App.name), action: #selector(App.app.showAboutTab), keyEquivalent: "") - statusItem.menu!.addItem(NSMenuItem.separator()) - statusItem.menu!.addItem( + menu.addItem(NSMenuItem.separator()) + menu.addItem( withTitle: NSLocalizedString("Show", comment: "Menubar option"), action: #selector(App.app.showUi), keyEquivalent: "") - statusItem.menu!.addItem( + menu.addItem( withTitle: NSLocalizedString("Preferences…", comment: "Menubar option"), action: #selector(App.app.showPreferencesWindow), keyEquivalent: ",") - statusItem.menu!.addItem( + menu.addItem( withTitle: NSLocalizedString("Check for updates…", comment: "Menubar option"), action: #selector(App.app.checkForUpdatesNow), keyEquivalent: "") - statusItem.menu!.addItem( + menu.addItem( withTitle: NSLocalizedString("Send feedback…", comment: "Menubar option"), action: #selector(App.app.showFeedbackPanel), keyEquivalent: "") - statusItem.menu!.addItem(NSMenuItem.separator()) - statusItem.menu!.addItem( + menu.addItem(NSMenuItem.separator()) + menu.addItem( withTitle: String(format: NSLocalizedString("Quit %@", comment: "Menubar option. %@ is AltTab"), App.name), action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") + statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) + statusItem.target = self + statusItem.button!.action = #selector(statusItemOnClick) + statusItem.button!.sendAction(on: [.leftMouseDown, .rightMouseDown]) menubarIconCallback(nil) } - static func menubarIconCallback(_ sender: NSControl?) { + @objc func statusItemOnClick() { + if NSApp.currentEvent!.type == .leftMouseDown { + statusItem.popUpMenu(App.app.menubar.menu) + } else { + App.app.showUi() + } + } + + func menubarIconCallback(_ sender: NSControl?) { if Preferences.menubarIcon == .hidden { statusItem.isVisible = false } else { @@ -44,7 +56,7 @@ class Menubar { } } - static private func loadPreferredIcon() { + private func loadPreferredIcon() { let i = imageIndexFromPreference() let image = NSImage(named: "menubar-" + i)! image.isTemplate = i == "3" ? false : true @@ -53,7 +65,7 @@ class Menubar { statusItem.button!.imageScaling = .scaleProportionallyUpOrDown } - static private func imageIndexFromPreference() -> String { + private func imageIndexFromPreference() -> String { switch Preferences.menubarIcon { case .outlined: return "1" case .filled: return "2" diff --git a/src/ui/preferences-window/tabs/GeneralTab.swift b/src/ui/preferences-window/tabs/GeneralTab.swift index fca138a66..ea641dec3 100644 --- a/src/ui/preferences-window/tabs/GeneralTab.swift +++ b/src/ui/preferences-window/tabs/GeneralTab.swift @@ -5,7 +5,7 @@ class GeneralTab { static func initTab() -> NSView { let startAtLogin = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Start at login:", comment: ""), "startAtLogin", extraAction: startAtLoginCallback) - let menubarIcon = LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Menubar icon:", comment: ""), "menubarIcon", MenubarIconPreference.allCases, extraAction: Menubar.menubarIconCallback) + let menubarIcon = LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Menubar icon:", comment: ""), "menubarIcon", MenubarIconPreference.allCases, extraAction: App.app.menubar.menubarIconCallback) let resetPreferences = Button(NSLocalizedString("Reset preferences and restart…", comment: "")) { _ in GeneralTab.resetPreferences() } if #available(OSX 11, *) { resetPreferences.hasDestructiveAction = true } let menubarIconDropdown = menubarIcon[1] as! NSPopUpButton @@ -34,8 +34,8 @@ class GeneralTab { } private static func enableDraggingOffMenubarIcon(_ menubarIconDropdown: NSPopUpButton) { - Menubar.statusItem.behavior = .removalAllowed - menubarIsVisibleObserver = Menubar.statusItem.observe(\.isVisible, options: [.old, .new]) { _, change in + App.app.menubar.statusItem.behavior = .removalAllowed + menubarIsVisibleObserver = App.app.menubar.statusItem.observe(\.isVisible, options: [.old, .new]) { _, change in if change.oldValue == true && change.newValue == false { let hiddenIndex = Int(MenubarIconPreference.hidden.rawValue)! menubarIconDropdown.selectItem(at: hiddenIndex)