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?
- 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?
- 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.