Skip to main content

vstack

Function vstack 

Source
pub fn vstack(children: &[View]) -> View
Expand description

Create a VStack.

Examples found in repository?
examples/app.rs (lines 11-25)
9fn main() {
10    run(|| {
11        vstack(&[
12            text("Hello from Rust! ๐Ÿฆ€")
13                .padding(12.0)
14                .bg(Color::BLUE)
15                .rounded(8.0)
16                .opacity(0.95),
17            spacer(),
18            hstack(&[
19                image("star.fill"),
20                text("No Swift compiler needed"),
21            ]),
22            divider(),
23            text("Pure dlsym + arm64 asm โ†’ SwiftUI")
24                .padding(8.0),
25        ])
26        .padding(40.0)
27        .into_handle()
28    });
29}
More examples
Hide additional examples
examples/scenes.rs (lines 49-65)
48fn main_window() -> ViewHandle {
49    vstack(&[
50        text("A SwiftUI app, no Swift source")
51            .font_size(20.0)
52            .padding(8.0),
53        divider(),
54        text(&format!("count: {}", counter().get()))
55            .font_size(34.0)
56            .padding(12.0),
57        hstack(&[
58            button("โˆ’", || counter().set(counter().get() - 1)),
59            button("+", || counter().set(counter().get() + 1)),
60        ]),
61        spacer(),
62        text("โŒ˜, for Settings ยท the โ˜… in the menu bar has a menu")
63            .font_size(11.0)
64            .opacity(0.6),
65    ])
66    .padding(24.0)
67    .frame(420.0, 300.0)
68    .focused_scene_value(counter_focus_key(), counter().get())
69    .into_handle()
70}
71
72/// The focused-value key the window publishes its count under.
73///
74/// `.focusedSceneValue(\.key, value)` is how a window tells the commands above
75/// it what is selected. Both halves are synthesised at run time: the
76/// `FocusedValueKey` conformance *and* the `WritableKeyPath<FocusedValues, Int?>`
77/// โ€” `FocusedValues` exports no properties, so unlike `EnvironmentValues` there
78/// is no SwiftUI modifier holding a key path to borrow.
79///
80/// The menu cannot *read* it yet: that needs `@FocusedValue`, a `DynamicProperty`
81/// SwiftUI instantiates itself, which is a different mechanism from anything
82/// here. What this demonstrates is the publishing half, in a real app.
83fn counter_focus_key() -> &'static views::FocusKey {
84    static K: std::sync::OnceLock<views::FocusKey> = std::sync::OnceLock::new();
85    K.get_or_init(|| views::focus_key("sceneCounter"))
86}
87
88fn settings_pane() -> ViewHandle {
89    vstack(&[
90        text("Preferences").font_size(18.0),
91        divider(),
92        text("This pane is a `Settings` scene โ€” the one โŒ˜, opens."),
93        text(&format!("It sees the same state: {}", counter().get()))
94            .opacity(0.7),
95    ])
96    .padding(24.0)
97    .frame(360.0, 200.0)
98    .into_handle()
99}
100
101fn utility_window() -> ViewHandle {
102    vstack(&[
103        text("Utility").font_size(16.0),
104        text("A `Window`, not a `WindowGroup`: one of it, addressed by id.")
105            .opacity(0.7),
106    ])
107    .padding(20.0)
108    .frame(320.0, 140.0)
109    .into_handle()
110}
111
112fn menu_bar_item() -> ViewHandle {
113    vstack(&[
114        text(&format!("count: {}", counter().get())),
115        button("Reset", || counter().set(0)),
116    ])
117    .padding(10.0)
118    .into_handle()
119}
120
121/// The whole app, as one scene.
122///
123/// Built afresh every time SwiftUI reads `App.body`, which is what makes the
124/// numbers in it live rather than a snapshot taken at launch.
125///
126/// macOS only, and not by choice: `Settings`, `Window`, `MenuBarExtra` and the
127/// whole of `Commands` are `@available(iOS, unavailable)`. iOS gets
128/// [`ios_body`], which is the part of this that exists there.
129#[cfg(target_os = "macos")]
130fn body() -> SceneHandle {
131    scenes! {
132        window_group { main_window() }
133        settings { settings_pane() }
134        window("Utility", "utility") { utility_window() }
135        menu_bar_extra("Count", "star.fill") { menu_bar_item() }
136        commands {
137            // A menu of its own, beside File and Edit.
138            menu("Counter") {
139                vstack(&[
140                    button("Increment", || counter().set(counter().get() + 1)),
141                    button("Decrement", || counter().set(counter().get() - 1)),
142                    divider(),
143                    button("Reset", || counter().set(0)),
144                ])
145            }
146            // In place of the standard File โ–ธ New.
147            replacing(NewItem) { button("New Count", || counter().set(0)) }
148            // Added after the standard Help entry.
149            after(Help) {
150                button("About this Rust app", || {
151                    eprintln!("built with no Swift compiler in sight");
152                })
153            }
154        }
155    }
156}