How to connect Help menu in SwiftUI MacOS DocumentGroup app to application logic?

479 views Asked by At

SwiftUI DocumentGroup apps provide standard menu items including Help.

By default, when we run the app, and click Help / "MyApp help" we see "Help isn't available for MyApp".

Here's the main app:


import SwiftUI

@main
struct MyApp: App {

    var body: some Scene {

        DocumentGroup(newDocument: { MyDocument() }) { configuration in
            ContentView()
        }
        .commands {

            // use CommandGroup to modify built-in menu behavior
            // the following would work to replace the entire help menu
            // but I just want to access the logic for the 
            // second of two Help sub-menus

            CommandGroup(replacing: .help) {

                // How do I access the logic for Help / MyApp Help?


            }
        }
    }
}

I've reviewed the standard documentation, tutorials, and UI guidelines, but don't see much on how to integrate behaviors and adjustments to the standard menu options. Any assistance would be much appreciated.

This question from a year ago is similar, but unanswered.

1

There are 1 answers

0
denise On BEST ANSWER

When working with SwiftUI menus, the key objects are CommandGroup and CommandGroupPlacement.

There's a great example in "Swift with Majid: Commands in SwiftUI dated 24 Nov 2020". Scroll nearly to the end to see how to use CommandGroupPlacement (e.g., .help or .newItem) to identify locations on the standard menu.

The example shows how to indicate whether our content should appear before, or after, or whether it should be replacing the CommandGroupPlacement menu location specified.

I generally prefer written documentation over videos, but the video and example code recommended in the question comments is quite useful - thank you user1046037.