mirror of
https://github.com/lwouis/alt-tab-macos.git
synced 2026-05-24 11:20:36 +00:00
feat: show the ui by right-clicking the menubar icon (closes #2647)
This commit is contained in:
+2
-1
@@ -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()
|
||||
|
||||
+28
-16
@@ -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"
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user