Hi, I'm in the process of writing my first Mac Menu Bar app. I must include a way to quit the app by the normal [Command+Q] option. I got the below code from a YouTube video and it works on the video, but not in my app. I am also using SwiftUI.
The code's filename is ApplicationMenu.swift and is written in Swift. According to the video (which I cannot include in this message) the below example shows the menu appearing at the bottom of the window, but when I do it, it doesn't show up.
Thank you.
Here's the code:
import SwiftUI
class ApplicationMenus: NSObject
{
let menu = NSMenu()
func createMenu() -> NSMenu
{
let clockView = ActualClock()
let topView = NSHostingController(rootView: clockView)
topView.view.frame.size = CGSize(width: 255, height: 255)
let customMenuItem = NSMenuItem()
customMenuItem.view = topView.view
menu.addItem(customMenuItem)
menu.addItem(NSMenuItem.separator())
let aboutMenuItem = NSMenuItem(title: "About",
action: #selector(about),
keyEquivalent: "")
aboutMenuItem.target = self
menu.addItem(aboutMenuItem)
return menu
}
@objc func about(sender: NSMenuItem)
{
NSApp.orderFrontStandardAboutPanel()
}
@objc func support(sender: NSMenuItem)
{
NSApp.orderFrontStandardAboutPanel()
}
}