I'm developing my first swift app, and I'm creating a menu bar button. The left click will perform a specific action, and the right click will drop down a menu. What's happening is that my left click will work initially. My right click wont pop up the menu on the first click, but on the second one it will. If I dont select the action, and click anywhere else that makes the menu disappear, I end up in a weird loop. Both a left click and a right click will make the menu appear.
I cant understand where the code is stuck. Everything I found online refers to older code.
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
if let button = statusItem.button {
button.title = "test"
button.action = #selector(self.doSomething(sender:))
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
}
@objc func doSomething(sender: NSStatusItem) {
let event = NSApp.currentEvent!
if event.type == NSEvent.EventType.rightMouseUp {
// Right button click
let statusBarMenu = NSMenu()
let item1 = NSMenuItem(title:"Quit", action:#selector(self.applicationQuit),keyEquivalent: "")
item1.target = self
statusBarMenu.addItem(item1)
statusItem.menu = statusBarMenu
} else {
// Left button click
print("hello world")
}
}
}