Swiftui trayicon opens main window when clicked?

78 views Asked by At

spend many hours looking at a way not found one.

i use menubarextra but cannot figure out how i could toggle a main window when clicking the tray icon.

is this possible with menubar extra swiftui class? also digged the docs highly frustrated that this seems not possible yet still is a common pattern many osx apps implement.

i then digged githib and tried every recent swift ui project to see if it was implemented somewhere: nothing!

i basically want:

  1. swiftui tray icon app with a main window initially hidden users clicks tray icon then main window appears centered under the tray icon. if it was shown it hides else its shown ( toggle on tray icon click)
  2. clicking right will open the tray menu with items like about and quit. any swiftui expert knows how i should implement this?

i literaly cannot find anything perhaps this is just not possible with swiftui menubar implementation?

would very gratefull for a solutionī™ pulling my hairs out on this one

Searched all girhub repros for swift ui tray apps see if i could learn how to nake this work.

Searched online for examples a lot tray ecamples non showing how to do my question

Tried to find a method in the menhbarextra api

After digging ios schowcase websites ones more i could only find this example:

https://github.com/sandip-mane/MacMenuBarApp

It implements a menu not with swift ui but with

    let menu = NSMenu()
    
    func createMenu() -> NSMenu {
        let contentView = ContentView()
        
        let topView = NSHostingController(rootView: contentView)
        topView.view.frame.size = CGSize(width: 225, height: 225)
        
        let customMenuItem = NSMenuItem()
        customMenuItem.view = topView.view
        menu.addItem(customMenuItem)
        menu.addItem(.separator())
        
        return menu
    }
}

but only for the main window.

What would be the best approach switch to a 'native osx' tray menu or persist in using swift ui?

0

There are 0 answers