2

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")

        }

    }

}
user747242
  • 31
  • 2
  • Does this answer your question? [Swift: NSStatusItem menu behaviour in 10.10 (e.g. show only on right mouse click)](https://stackoverflow.com/questions/26924454/swift-nsstatusitem-menu-behaviour-in-10-10-e-g-show-only-on-right-mouse-click) – Willeke Nov 13 '19 at 11:43
  • The problem is that it's deprecated, and refers to old versions of swift. – user747242 Nov 14 '19 at 14:47
  • See [What is alternative to NSStatusItem.popUpMenu?](https://stackoverflow.com/questions/52585275/what-is-alternative-to-nsstatusitem-popupmenu) – Willeke Nov 14 '19 at 22:24

2 Answers2

1

Thanks to Willeke, we have the answer. The code should read:

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
        statusItem.button?.performClick(nil)
        statusItem.menu = nil
    }

Reference: What is alternative to NSStatusItem.popUpMenu?

user747242
  • 31
  • 2
0
class AppDelegate: NSObject, NSApplicationDelegate {
    private var menuBarIcon: NSStatusItem!

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        createMenuBarIcon()
    }
}

// Dock Icon
extension AppDelegate {
    func createMenuBarIcon() {
        self.menuBarIcon = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
        
        self.menuBarIcon?.button?.toolTip = "FileBo"
        
        self.menuBarIcon?.button?.action = #selector(menuBarIconClicksAction(_:))
        self.menuBarIcon?.button?.sendAction(on: [.leftMouseUp, .rightMouseUp])
        self.menuBarIcon?.button?.target = self
        
        let icon = NSImage(named: "MenuBarAppIcon")
        icon?.size = .init(width: 20, height: 20)
        
        self.menuBarIcon?.button?.image = icon
    }
    
    func destroyMenuBarIcon() {
        self.menuBarIcon = nil
    }
}


// Dock Menu
extension AppDelegate {
    @IBAction func menuBarIconClicksAction(_ sender: AnyObject) {
        guard let event = NSApp.currentEvent else { return }
        
        if event.type == NSEvent.EventType.rightMouseUp {
            menuBarIcon.menu = dockContextMenu(NSApplication.shared)
            menuBarIcon.button?.performClick(nil)
            menuBarIcon.menu = nil
        } else {
            toggleMainWnd()
        }
    }
    
    func dockContextMenu(_ sender: NSApplication) -> NSMenu {
        let dockMenu = NSMenu()
        
        dockMenu.addItem( NSMenuItem(title: String(localized: "Show/Hide FileBo"), action: #selector(ToggleWndFileBo(_:)), keyEquivalent: "") )
        dockMenu.addItem( NSMenuItem(title: String(localized: "Quit FileBo") , action: #selector(QuitFileBo(_:)), keyEquivalent: "") )
        
        return dockMenu
    }
    
    @IBAction func QuitFileBo(_ sender: AnyObject) {
        NSApplication.shared.terminate(nil);
    }
    
    @IBAction func ToggleWndFileBo(_ sender: AnyObject) {
        toggleMainWnd()
    }
}
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101