How it works
InspectorCommands is a built-in commands group that contributes the standard Show Inspector and Hide Inspector menu items to your app's main menu, keeping them in sync with the inspectors you present elsewhere in your interface. Rather than wiring up your own menu item, keyboard shortcut, and visibility state by hand, you add this group to a scene's command set and SwiftUI manages the menu entry for you. Reach for it whenever a document or content window uses an inspector and you want users to be able to toggle that inspector from the menu bar, matching the conventions of system apps.
Add InspectorCommands to a scene's commands
InspectorCommands conforms to the Commands protocol, so you supply it inside a commands(content:) builder attached to a Scene such as a WindowGroup or DocumentGroup. Listing it there inserts the Show/Hide Inspector item into the standard menu without any per-item configuration on your part.
Back the menu item with an inspector presentation
The command only does useful work when there is an inspector to toggle. In the example, the content view presents one via .inspector(isPresented: $showInspector) { ... }, whose trailing closure holds the inspector's own VStack with a Text("Inspector") heading. InspectorCommands drives the visibility of exactly this kind of inspector surface.
Bind visibility through shared state
Because the menu item and the inspector must agree on whether the panel is showing, both read and write the same source of truth. Here that is @State private var showInspector, passed as the $showInspector binding to .inspector(isPresented:); selecting the menu item flips this value, which is the same value the in-view Toggle("Show Inspector", isOn: $showInspector) controls.
Rely on the standard title and shortcut
InspectorCommands supplies the localized menu title and the system keyboard shortcut for toggling the inspector, so the entry reads Show Inspector or Hide Inspector depending on the current state of bindings like $showInspector. You don't define the label or key equivalent yourself, which keeps the behavior consistent with the rest of the platform.
Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct InspectorCommandsDemo: View {
@State private var showInspector = true
var body: some View {
VStack {
Text("Document")
.font(.title2)
Text("InspectorCommands adds a Show/Hide Inspector item to the menu.")
.font(.footnote)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
.padding()
.inspector(isPresented: $showInspector) {
VStack(alignment: .leading) {
Text("Inspector")
.font(.headline)
Toggle("Show Inspector", isOn: $showInspector)
}
.padding()
}
}
}