Add shift-click menu

This commit is contained in:
Shadowfacts 2021-09-01 10:34:29 -04:00
parent c4991a6671
commit f6b02d6f79
1 changed files with 32 additions and 3 deletions

View File

@ -24,7 +24,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
item.button!.target = self item.button!.target = self
item.button!.action = #selector(toggleScrollDirection) item.button!.action = #selector(menuItemClicked)
updateIcon() updateIcon()
// update the icon when system prefs changes // update the icon when system prefs changes
@ -35,9 +35,31 @@ class AppDelegate: NSObject, NSApplicationDelegate {
item.button!.image = Direction.current.image item.button!.image = Direction.current.image
} }
@objc private func menuItemClicked() {
if let event = NSApp.currentEvent, event.modifierFlags.contains(.shift) {
item.menu = createMenu()
item.button!.performClick(nil)
} else {
toggleScrollDirection()
}
}
private func createMenu() -> NSMenu {
let menu = NSMenu()
menu.delegate = self
let state = Direction.current == .natural ? "On" : "Off"
let status = NSMenuItem(title: "Natural Scrolling: \(state)", action: nil, keyEquivalent: "")
menu.addItem(status)
let verb = Direction.current == .natural ? "Disable" : "Enable"
let toggleItem = NSMenuItem(title: "\(verb) Natural Scrolling", action: #selector(toggleScrollDirection), keyEquivalent: "")
menu.addItem(toggleItem)
menu.addItem(NSMenuItem.separator())
menu.addItem(withTitle: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
return menu
}
@objc private func toggleScrollDirection() { @objc private func toggleScrollDirection() {
setDirection(.current == Direction.natural ? .normal : .natural) setDirection(.current == Direction.natural ? .normal : .natural)
updateIcon()
} }
private func setDirection(_ new: Direction) { private func setDirection(_ new: Direction) {
@ -67,6 +89,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
} }
extension AppDelegate: NSMenuDelegate {
func menuDidClose(_ menu: NSMenu) {
// remove the menu so the next time the item is clicked, it performs the primary action
item.menu = nil
}
}
enum Direction: Int, Equatable { enum Direction: Int, Equatable {
case normal, natural case normal, natural