1use crate::{Scene, SceneRegistry};
7use mgui_components::{ButtonWidget, ColumnWidget, ContainerWidget, ListViewWidget, TextFieldWidget, TextWidget};
8use mgui_core::layout::SizedBoxWidget;
9use mgui_core::{Color, Size, WidgetNode};
10use mgui_runtime::app::{CommandId, CommandOutcome, CommandRouter};
11use std::cell::{Cell, RefCell};
12use std::rc::Rc;
13
14pub struct EmptyScene;
15impl Scene for EmptyScene {
16 fn name(&self) -> &'static str {
17 "empty"
18 }
19 fn build(&self) -> WidgetNode {
20 WidgetNode::new(ContainerWidget::new())
21 }
22}
23
24pub struct ButtonScene;
25impl Scene for ButtonScene {
26 fn name(&self) -> &'static str {
27 "button"
28 }
29 fn build(&self) -> WidgetNode {
30 WidgetNode::new(
31 ContainerWidget::new().child(ButtonWidget::new(Size::new(160.0, 48.0)).with_text("Hello", 16.0)),
32 )
33 }
34}
35
36pub struct FlagshipScene;
38impl Scene for FlagshipScene {
39 fn name(&self) -> &'static str {
40 "flagship"
41 }
42 fn build(&self) -> WidgetNode {
43 let list =
44 ListViewWidget::builder(10_000, |i| WidgetNode::new(TextWidget::new(format!("row {i}")).with_size(14.0)))
45 .with_item_height(24.0)
46 .with_height(280.0);
47 let video_tile = WidgetNode::new(mgui_core::RRectWidget::new(
48 Size::new(420.0, 160.0),
49 Color::rgba(0.10, 0.12, 0.16, 1.0),
50 4.0,
51 ));
52 let mut column = ColumnWidget::new(6.0);
53 column = column.push(TextWidget::new("mgui flagship skeleton").with_size(20.0));
54 column = column.push(ButtonWidget::new(Size::new(160.0, 40.0)).with_text("Open", 16.0));
55 column = column.push(video_tile);
56 column = column.push(SizedBoxWidget::sized(420.0, 280.0, list));
57 WidgetNode::new(ContainerWidget::new().child(column))
58 }
59}
60
61pub struct TextEditingScene;
66impl Scene for TextEditingScene {
67 fn name(&self) -> &'static str {
68 "text_editing"
69 }
70 fn build(&self) -> WidgetNode {
71 let field = TextFieldWidget::new(360.0, 44.0).placeholder("IME replay").with_font_size(18.0);
72 WidgetNode::new(ContainerWidget::new().child(WidgetNode::new(field)))
73 }
74}
75
76pub struct CommandRouteScene {
81 router: RefCell<CommandRouter>,
82 shared: Rc<RefCell<RouteState>>,
83}
84
85#[derive(Default)]
86struct RouteState {
87 revision: u64,
88}
89
90impl Default for CommandRouteScene {
91 fn default() -> Self {
92 let shared = Rc::new(RefCell::new(RouteState::default()));
93 let mut router = CommandRouter::new();
94 let increment_shared = Rc::clone(&shared);
95 router.register(CommandId::new(1), move |_| {
96 let mut state = increment_shared.borrow_mut();
97 state.revision = state.revision.wrapping_add(1);
98 CommandOutcome::Consumed
99 });
100 router.register(CommandId::new(2), |_| CommandOutcome::PassThrough);
101 Self { router: RefCell::new(router), shared }
102 }
103}
104
105impl Scene for CommandRouteScene {
106 fn name(&self) -> &'static str {
107 "command_route"
108 }
109
110 fn build(&self) -> WidgetNode {
111 let column = ColumnWidget::new(8.0)
112 .push(TextWidget::new("command route / shared state").with_size(16.0))
113 .push(ButtonWidget::new(Size::new(220.0, 44.0)).with_text("increment (menu id=1)", 13.0))
114 .push(ButtonWidget::new(Size::new(220.0, 44.0)).with_text("passthrough (id=2)", 13.0));
115 WidgetNode::new(ContainerWidget::new().child(column))
116 }
117
118 fn command_router(&self) -> Option<&RefCell<CommandRouter>> {
119 Some(&self.router)
120 }
121
122 fn shared_state_summary(&self) -> Option<String> {
123 Some(format!("revision={}", self.shared.borrow().revision))
124 }
125}
126
127pub struct A11yJourneyScene {
132 activations: Rc<Cell<u32>>,
133}
134
135impl Default for A11yJourneyScene {
136 fn default() -> Self {
137 Self { activations: Rc::new(Cell::new(0)) }
138 }
139}
140
141impl Scene for A11yJourneyScene {
142 fn name(&self) -> &'static str {
143 "a11y_journey"
144 }
145
146 fn build(&self) -> WidgetNode {
147 let button = |label: &'static str, activations: &Rc<Cell<u32>>| {
148 let counter = Rc::clone(activations);
149 ButtonWidget::new(Size::new(240.0, 44.0))
150 .with_text(label, 14.0)
151 .with_a11y_label(label)
152 .on_press(move || counter.set(counter.get() + 1))
153 };
154 let column = ColumnWidget::new(8.0)
155 .push(button("Journey A", &self.activations))
156 .push(button("Journey B", &self.activations))
157 .push(button("Journey C", &self.activations));
158 WidgetNode::new(ContainerWidget::new().child(column))
159 }
160
161 fn shared_state_summary(&self) -> Option<String> {
162 Some(format!("activations={}", self.activations.get()))
163 }
164}
165
166pub fn builtin_scenes() -> SceneRegistry {
168 let mut registry = SceneRegistry::new();
169 registry.register(Box::new(EmptyScene));
170 registry.register(Box::new(ButtonScene));
171 registry.register(Box::new(FlagshipScene));
172 registry.register(Box::new(TextEditingScene));
173 registry.register(Box::new(CommandRouteScene::default()));
174 registry.register(Box::new(A11yJourneyScene::default()));
175 registry
176}
177
178#[cfg(test)]
179mod tests {
180 use super::*;
181
182 #[test]
183 fn builtin_scenes_are_stably_named_and_buildable() {
184 let registry = builtin_scenes();
185 assert_eq!(
186 registry.names(),
187 vec!["empty", "button", "flagship", "text_editing", "command_route", "a11y_journey"]
188 );
189 for name in registry.names() {
190 let _ = registry.build(name).unwrap_or_else(|e| panic!("scene {name}: {e}"));
191 }
192 assert!(registry.build("missing").is_err());
193 }
194}