Skip to main content

Module scene

Module scene 

Source
Expand description

Scenes and commands — what an app is, above its views.

crate::app::run builds one WindowGroup and stops there. A real macOS app is several scenes: the window group, the preferences window Settings puts behind ⌘, — a menu-bar item, a second window — and a menu bar described by Commands. This module builds each of them, and the tuples that hold several at once, so crate::app::run_scene can be given a whole app rather than one window.

use swiftui_native::{app, scene, views};

app::run_scene(|| {
    scene::commands(
        scene::group(vec![
            scene::window_group(|| views::text("main")),
            scene::settings(|| views::text("preferences")),
            scene::menu_bar_extra("Status", "star", || views::text("menu")),
        ]),
        vec![scene::command_menu("Extras", || views::button("Do it", || {}))],
    )
});

§Why the content closures are borrowed

Every initialiser here takes its content as @noescape — xyXE in the mangling, not yc — so it calls the closure before returning and keeps the value, not the closure. A borrowed crate::abi::ClosureCtx on the stack is therefore correct, and leaking one (as a sheet’s content must be) would leak once per read of App.body, which SwiftUI does repeatedly.

§What is synthesised and what is SwiftUI’s

Nothing here is a Swift wrapper: each scene is SwiftUI’s own type, instantiated by calling its own initialiser with the arguments in the registers its ABI names. _TupleScene and TupleCommandContent are what SceneBuilder and CommandsBuilder build — those builders are @_alwaysEmitIntoClient and export no symbol, so the binding calls what their bodies call.

Structs§

CommandsHandle
A value conforming to SwiftUI’s Commands — one menu, or a tuple of them.
SceneHandle
A value conforming to SwiftUI’s Scene — a window, a settings pane, a menu-bar item, or a tuple of them.

Enums§

CommandPlacement
Where in the standard menu bar a command_group goes.
Relative
Where a group sits relative to the placement it names.

Functions§

command_group
CommandGroup(replacing:/after:/before: placement) { content } — items added to, or in place of, one of the standard menu groups.
command_menu
CommandMenu(title) { content } — a whole new menu in the menu bar.
commands
.commands { … } on a scene — the app’s menu bar.
group
Several scenes as one — _TupleScene<(A, B, …)>.
menu_bar_extra
MenuBarExtra(title, systemImage: image) { content } — an item in the system menu bar, with a menu of its own.
settings
Settings { content } — the window behind ⌘, on macOS.
window
Window(title, id: id) { content } — a single window, not a group.
window_group
WindowGroup { content } — the app’s main window, and as many more as the user opens.