Difference between revisions of "Swift Snippets"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (Created page with " * https://stackoverflow.com/questions/59635971/show-nsmenu-only-on-nsstatusbarbutton-right-click <pre> func applicationDidFinishLaunching(_ aNotification: Notification) { statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) statusItem.button?.action = #selector(onClick) statusItem.button?.sendAction(on: [.leftMouseUp, .rightMouseUp]) menu = NSMenu() menu.addItem(NSMenuItem(title: "Quit", ac...") |
PeterHarding (talk | contribs) |
||
Line 34: | Line 34: | ||
</pre> | </pre> | ||
OK Got exercise working but status bar truncates length of time. Setting a length of 53 seems to be th e max - any larger an the display fails. Have not worked out how to user a smaller font so it will not overflow the space available. | |||
Revision as of 09:42, 11 December 2022
func applicationDidFinishLaunching(_ aNotification: Notification) { statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) statusItem.button?.action = #selector(onClick) statusItem.button?.sendAction(on: [.leftMouseUp, .rightMouseUp]) menu = NSMenu() menu.addItem(NSMenuItem(title: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")) menu.delegate = self } @objc func onClick(sender: NSStatusItem) { let event = NSApp.currentEvent! if event.type == NSEvent.EventType.rightMouseUp { // right click, show quit menu statusItem.menu = menu; menu.popUp(positioning: nil, at: NSPoint(x: 0, y: statusItem.statusBar!.thickness), in: statusItem.button) } else { // main click } } @objc func menuDidClose(_ menu: NSMenu) { // remove menu when closed so we can override left click behavior statusItem.menu = nil }
OK Got exercise working but status bar truncates length of time. Setting a length of 53 seems to be th e max - any larger an the display fails. Have not worked out how to user a smaller font so it will not overflow the space available.