UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Managing menus

Forums > macOS

I'm trying to create my first non-trivial MacOS app using SwiftUI and am having some trouble getting my head around menu management. I've got code that can add/remove menu items, so the basics are there. However, it's starting to get complicated and I can't find any useful documentation online (so am probably looking in the wrong place!).

Can anyone give me some pointers how to deal with these issues please?

  1. If I have multiple windows open, I may need to set the disable state on a menu based on the content of the window. How do I set a menu to disabled/enabled when my window becomes active? How does my window get access to the menu, which is defined at the App level?
  2. I may have a menu item that needs to get my window to run an action (simplest example is a "refresh items" menu option that tells the view to refresh itself). How do I connect a menu item to the currently selected window?

I did come up with a solution that involved creating a global "AppState" through which I could set the state of menu items. As my window becomes active, it updates the global state which, hopefully, refreshes the menu. It doesn't deal with point 2, but I'm sure I can work round that - perhaps store a reference to the active window in the AppState.

That, however, feels clunky. I also have a bit of an aversion to globals (too many years coding on Windows in C#).

Does anyone know of any good blog posts on the subject or can point me to any github repos for SwiftUI MacOS apps that have menus and multiple windows? I'm still a bit new to this and want to do it right, but am not sure whether I would recognise 'right' if it slapped me round the head!

Many thanks Steve

p.s. If you're interested what I'm trying to pull together, it's on GitHub: https://github.com/sabarnett/MacTemplateApp The idea is to build a template for a "typical" Mac app so I have a starter project when I come to write more apps in the future. I'm just starting work on the Menus.

3      

You have to create a focused value for the window and add the .focusedSceneValue modifier to a SwiftUI view. Usually the view is the app's main view.

I wrote an article recently on showing a sheet from a menu item that demonstrates how to work with focused values. But I can't link to it because the forum doesn't allow us to add links to articles from most domains as an anti-spam measure. If you enter Swift Dev Journal in a search engine, you should be able to find the site and the article.

3      

Thank you for this. I didn't even know that existed... I will have a play with it and see if it can deal with my needs. For my first pass, I have dropped back to AppKit and searching the menu structure. That will give me a helper to enable/disable menus. Hopefully, this will get me the access to the window I need for triggering events.

3      

If anyone is interested, I ended up with @FocusedObject to run code in my view model when a menu item is selected. If you want to see the final code its in GitHub... https://github.com/sabarnett/MacTemplateApp

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.