1use std::{cell::RefCell, rc::Rc};
2
3use sui_core::{
4 Color, Event, ImeEvent, KeyState, PointerEventKind, Result, SemanticsAction, SemanticsNode,
5 SemanticsRole, SemanticsValue, Size, TimerToken, WakeEvent,
6};
7use sui_layout::Constraints;
8use sui_runtime::{
9 Application, ArrangeCtx, EventCtx, MeasureCtx, PaintCtx, SemanticsCtx, Widget, WidgetChildren,
10 WidgetPodMutVisitor, WidgetPodVisitor, WindowBuilder,
11};
12use sui_testing::prelude::*;
13
14fn main() -> Result<()> {
15 let app = TestApp::new(|| {
16 let state = Rc::new(RefCell::new(AppState::default()));
17 Application::new().window(
18 WindowBuilder::new()
19 .title("SUI Testing Example")
20 .root(ExampleRoot::new(state)),
21 )
22 })?;
23
24 let window = app.main_window()?;
25
26 window
27 .get_by_role(SemanticsRole::TextInput)
28 .with_name("Name")
29 .fill("Ada")?;
30 window
31 .get_by_role(SemanticsRole::Button)
32 .with_name("Save")
33 .click()?;
34 window.get_by_text("Saved").expect().to_be_visible()?;
35
36 println!("Example passed: filled the name field and observed the Saved status.");
37 Ok(())
38}
39
40#[derive(Debug, Default)]
41struct AppState {
42 name: String,
43 status: String,
44 save_timer: Option<TimerToken>,
45}
46
47struct ExampleRoot {
48 children: WidgetChildren,
49}
50
51impl ExampleRoot {
52 fn new(state: Rc<RefCell<AppState>>) -> Self {
53 let mut children = WidgetChildren::new();
54 children.push(NameInput::new(Rc::clone(&state)));
55 children.push(SaveButton::new(state));
56 Self { children }
57 }
58}
59
60impl Widget for ExampleRoot {
61 fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
62 let viewport = constraints.clamp(Size::new(420.0, 220.0));
63 let control = Constraints::tight(Size::new(200.0, 44.0));
64
65 self.children.measure_child(0, ctx, control);
66 self.children.measure_child(1, ctx, control);
67
68 viewport
69 }
70
71 fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: sui_core::Rect) {
72 self.children.arrange_child(
73 0,
74 ctx,
75 sui_core::Rect::new(bounds.x() + 24.0, bounds.y() + 24.0, 200.0, 44.0),
76 );
77 self.children.arrange_child(
78 1,
79 ctx,
80 sui_core::Rect::new(bounds.x() + 24.0, bounds.y() + 92.0, 200.0, 44.0),
81 );
82 }
83
84 fn paint(&self, ctx: &mut PaintCtx) {
85 ctx.clear(Color::rgba(0.08, 0.09, 0.11, 1.0));
86 self.children.paint(ctx);
87 }
88
89 fn semantics(&self, ctx: &mut SemanticsCtx) {
90 let mut root = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Window, ctx.bounds());
91 root.name = Some("SUI Testing Example".to_string());
92 ctx.push(root);
93 self.children.semantics(ctx);
94 }
95
96 fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
97 self.children.visit_children(visitor);
98 }
99
100 fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
101 self.children.visit_children_mut(visitor);
102 }
103}
104
105struct NameInput {
106 state: Rc<RefCell<AppState>>,
107}
108
109impl NameInput {
110 fn new(state: Rc<RefCell<AppState>>) -> Self {
111 Self { state }
112 }
113}
114
115impl Widget for NameInput {
116 fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
117 match event {
118 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Down => {
119 ctx.request_focus();
120 ctx.request_paint();
121 ctx.request_semantics();
122 ctx.set_handled();
123 }
124 Event::Ime(ImeEvent::CompositionCommit { text }) if ctx.is_focused() => {
125 self.state.borrow_mut().name = text.clone();
126 ctx.request_paint();
127 ctx.request_semantics();
128 ctx.set_handled();
129 }
130 _ => {}
131 }
132 }
133
134 fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
135 constraints.clamp(Size::new(200.0, 44.0))
136 }
137
138 fn paint(&self, ctx: &mut PaintCtx) {
139 let background = if ctx.is_focused() {
140 Color::rgba(0.18, 0.23, 0.32, 1.0)
141 } else {
142 Color::rgba(0.13, 0.15, 0.20, 1.0)
143 };
144 ctx.fill_bounds(background);
145 ctx.label(
146 ctx.bounds(),
147 format!("Name: {}", self.state.borrow().name),
148 Color::rgba(0.95, 0.96, 0.98, 1.0),
149 );
150 }
151
152 fn semantics(&self, ctx: &mut SemanticsCtx) {
153 let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::TextInput, ctx.bounds());
154 node.name = Some("Name".to_string());
155 node.value = Some(SemanticsValue::Text(self.state.borrow().name.clone()));
156 node.actions = vec![SemanticsAction::Focus, SemanticsAction::SetValue];
157 ctx.push(node);
158 }
159
160 fn accepts_focus(&self) -> bool {
161 true
162 }
163
164 fn focus_changed(&mut self, ctx: &mut EventCtx, _focused: bool) {
165 ctx.request_paint();
166 ctx.request_semantics();
167 }
168}
169
170struct SaveButton {
171 state: Rc<RefCell<AppState>>,
172}
173
174impl SaveButton {
175 fn new(state: Rc<RefCell<AppState>>) -> Self {
176 Self { state }
177 }
178}
179
180impl Widget for SaveButton {
181 fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
182 match event {
183 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Down => {
184 let mut state = self.state.borrow_mut();
185 state.status = "Saving".to_string();
186 state.save_timer = Some(ctx.schedule_timer_after(1.0));
187 ctx.request_focus();
188 ctx.request_paint();
189 ctx.request_semantics();
190 ctx.set_handled();
191 }
192 Event::Keyboard(key)
193 if key.state == KeyState::Pressed
194 && ctx.is_focused()
195 && matches!(key.key.as_str(), "Enter" | " ") =>
196 {
197 let mut state = self.state.borrow_mut();
198 state.status = "Saving".to_string();
199 state.save_timer = Some(ctx.schedule_timer_after(1.0));
200 ctx.request_paint();
201 ctx.request_semantics();
202 ctx.set_handled();
203 }
204 Event::Wake(WakeEvent::Timer { token, .. }) => {
205 let mut state = self.state.borrow_mut();
206 if state.save_timer == Some(*token) {
207 state.status = "Saved".to_string();
208 state.save_timer = None;
209 ctx.request_paint();
210 ctx.request_semantics();
211 ctx.set_handled();
212 }
213 }
214 _ => {}
215 }
216 }
217
218 fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
219 constraints.clamp(Size::new(200.0, 44.0))
220 }
221
222 fn paint(&self, ctx: &mut PaintCtx) {
223 let state = self.state.borrow();
224 let background = if ctx.is_focused() {
225 Color::rgba(0.25, 0.52, 0.88, 1.0)
226 } else {
227 Color::rgba(0.17, 0.24, 0.37, 1.0)
228 };
229 ctx.fill_bounds(background);
230 ctx.label(
231 ctx.bounds(),
232 format!("Save {}", state.status),
233 Color::rgba(0.95, 0.96, 0.98, 1.0),
234 );
235 }
236
237 fn semantics(&self, ctx: &mut SemanticsCtx) {
238 let state = self.state.borrow();
239
240 let mut button = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Button, ctx.bounds());
241 button.name = Some("Save".to_string());
242 button.description = Some("Persist the current form".to_string());
243 button.actions = vec![SemanticsAction::Focus, SemanticsAction::Activate];
244 ctx.push(button);
245
246 let mut status = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Text, ctx.bounds());
247 status.parent = Some(ctx.root_widget_id());
248 status.name = Some(state.status.clone());
249 ctx.push(status);
250 }
251
252 fn accepts_focus(&self) -> bool {
253 true
254 }
255
256 fn focus_changed(&mut self, ctx: &mut EventCtx, _focused: bool) {
257 ctx.request_paint();
258 ctx.request_semantics();
259 }
260}