Add shift-click menu
This commit is contained in:
parent
c4991a6671
commit
f6b02d6f79
|
@ -15,7 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||
private var prefPanesSupport: Bundle!
|
||||
private var item: NSStatusItem!
|
||||
private var observation: NSKeyValueObservation?
|
||||
|
||||
|
||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||
|
||||
// this is where _setSwipeScrollDirection is defined
|
||||
|
@ -24,7 +24,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||
|
||||
item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
|
||||
item.button!.target = self
|
||||
item.button!.action = #selector(toggleScrollDirection)
|
||||
item.button!.action = #selector(menuItemClicked)
|
||||
updateIcon()
|
||||
|
||||
// update the icon when system prefs changes
|
||||
|
@ -35,9 +35,31 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||
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() {
|
||||
setDirection(.current == Direction.natural ? .normal : .natural)
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
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 {
|
||||
case normal, natural
|
||||
|
||||
|
|
Loading…
Reference in New Issue