Skip to content

Commit

Permalink
feat: show the ui by right-clicking the menubar icon (closes #2647)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Jul 12, 2023
1 parent 0673381 commit 63502d5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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()
Expand Down
44 changes: 28 additions & 16 deletions src/ui/Menubar.swift
Original file line number Diff line number Diff line change
@@ -1,50 +1,62 @@
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 {
loadPreferredIcon()
}
}

static private func loadPreferredIcon() {
private func loadPreferredIcon() {
let i = imageIndexFromPreference()
let image = NSImage(named: "menubar-" + i)!
image.isTemplate = i == "3" ? false : true
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/ui/preferences-window/tabs/GeneralTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 63502d5

Please sign in to comment.