How to Add a Menu to The Application in The Dock

adding menu items to the right click app menu in the dock casues them to be re-added each time i click

You have to create the NSMenu inside the method

func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {

let dockMenu = NSMenu()
dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent: "")
return dockMenu
}

or remove the items

let dockMenu = NSMenu()

func applicationDockMenu(_ sender: NSApplication) -> NSMenu? {

dockMenu.removeAllItems()
dockMenu.addItem(withTitle: "test1", action: nil, keyEquivalent: "")
return dockMenu
}

However I would declare it in Interface Builder and use an outlet.

Adding Items to the Dock Menu from my View Controller in my Cocoa App

Since applicationDockMenu: is a delegate method, having an instance method add menu items would conflict with the delegate return.

What you could do is make the dock menu a property/instance variable in your application delegate class. This way, your view controller could modify the menu either by passing the reference to the menu from your application delegate to your view controller (which you would have a dockMenu property) or referencing it globally (less recommended).

class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
var dockMenu = NSMenu(title: "MyMenu")

func applicationDidFinishLaunching(aNotification: NSNotification) {
if let viewController = ViewController(nibName: "ViewController", bundle: nil) {
viewController.dockMenu = self.dockMenu
self.window.contentViewController = viewController
}
}

func applicationDockMenu(sender: NSApplication) -> NSMenu? {
return self.dockMenu
}

class ViewController: NSViewController {
var dockMenu: NSMenu?

// Button action
@IBAction func updateDockMenu(sender: AnyObject) {
self.dockMenu?.addItem(NSMenuItem(title: "An Item", action: nil, keyEquivalent: ""))
}
}

How to Make macOS App Window Hidden When Closed and Reopened With Menu Bar Item?

Don't use the Application is agent approach, but change the activationPolicy of the NSApp.

To dynamically hide the icon after closing the (last) window use this in your AppDelegate:

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
NSApp.setActivationPolicy(.accessory)
return false
}

And use something simular to this to initialise your menubar icon and activate the window including a dock icon:

class ViewController: NSViewController {

var status: NSStatusItem?

override func viewDidLoad() {
super.viewDidLoad()

status = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
status?.button?.title = "Test"
status?.button?.action = #selector(activateWindow(_:))
status?.button?.target = self
}

@IBAction func activateWindow(_ sender: AnyObject) {
NSApp.setActivationPolicy(.regular)
DispatchQueue.main.async {
NSApp.windows.first?.orderFrontRegardless()
}
}
}

MacOSX Add command to Dock icon

There are multiple ways, one would be to assign NSMenu in applicationDockMenu: in AppDelegate.

The second option is to use xib file and Info.plist. This is Xamarin's tutorial for that, but it is similar in any framework: https://learn.microsoft.com/en-us/xamarin/mac/user-interface/menu



Related Topics



Leave a reply



Submit