BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / tests / controlled_change_callbacks.rs

controlled_change_callbacks.rs

controlled_change_callbacks.rs 6.7 KiB Download Raw
1use std::{cell::RefCell, rc::Rc};
2
3use gpui::{
4 App, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers, Render, Styled,
5 TestAppContext, Window, div, prelude::*, px,
6};
7use gpui_component::{
8 Disableable,
9 checkbox::Checkbox,
10 radio::{Radio, RadioGroup},
11 switch::Switch,
12};
13
14#[derive(Clone, Copy, Debug)]
15enum Control {
16 Checkbox,
17 Switch,
18 Radio,
19 RadioGroup,
20}
21
22#[derive(Clone, Copy, Debug)]
23enum Callback {
24 Click,
25 Change,
26 ClickThenChange,
27 ChangeThenClick,
28}
29
30struct Harness {
31 control: Control,
32 callback: Callback,
33 disabled: bool,
34 requests: Rc<RefCell<Vec<String>>>,
35}
36
37impl Render for Harness {
38 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
39 macro_rules! bind {
40 ($control:expr, $value:ty) => {{
41 let requests = self.requests.clone();
42 let record = move |value: &$value, _: &mut Window, _: &mut App| {
43 requests.borrow_mut().push(format!("{value:?}"));
44 };
45 let replaced = |_: &$value, _: &mut Window, _: &mut App| {
46 panic!("an earlier callback must be replaced");
47 };
48 let control = $control;
49 match self.callback {
50 Callback::Click => control.on_click(record),
51 Callback::Change => control.on_change(record),
52 Callback::ClickThenChange => control.on_click(replaced).on_change(record),
53 Callback::ChangeThenClick => control.on_change(replaced).on_click(record),
54 }
55 .into_any_element()
56 }};
57 }
58 let control = match self.control {
59 Control::Checkbox => bind!(
60 Checkbox::new("control")
61 .checked(false)
62 .disabled(self.disabled)
63 .label("Choice"),
64 bool
65 ),
66 Control::Switch => bind!(
67 Switch::new("control")
68 .checked(false)
69 .disabled(self.disabled)
70 .label("Choice"),
71 bool
72 ),
73 Control::Radio => bind!(
74 Radio::new("control")
75 .checked(false)
76 .disabled(self.disabled)
77 .label("Choice"),
78 bool
79 ),
80 Control::RadioGroup => bind!(
81 RadioGroup::new("control")
82 .selected_index(None)
83 .child(Radio::new("choice").label("Choice"))
84 .disabled(self.disabled),
85 usize
86 ),
87 };
88 div().id("parent").tab_group().w(px(200.)).child(
89 div()
90 .debug_selector(|| "control-target".into())
91 .child(control),
92 )
93 }
94}
95
96#[gpui::test]
97fn controlled_change_callbacks_preserve_activation_and_replace_aliases(cx: &mut TestAppContext) {
98 cx.update(gpui_component::init);
99 for control in [
100 Control::Checkbox,
101 Control::Switch,
102 Control::Radio,
103 Control::RadioGroup,
104 ] {
105 for callback in [
106 Callback::Click,
107 Callback::Change,
108 Callback::ClickThenChange,
109 Callback::ChangeThenClick,
110 ] {
111 for disabled in [false, true] {
112 let requests = Rc::new(RefCell::new(Vec::new()));
113 let (_, visual) = cx.add_window_view({
114 let requests = requests.clone();
115 move |_, _| Harness {
116 control,
117 callback,
118 disabled,
119 requests,
120 }
121 });
122 visual.update(|window, cx| window.draw(cx).clear(cx));
123 let target = visual.debug_bounds("control-target").unwrap();
124 // Click inside the leading control, not the full-width wrapper.
125 visual.simulate_click(
126 gpui::point(target.left() + px(8.), target.center().y),
127 Modifiers::default(),
128 );
129 visual.update(|window, cx| {
130 window.blur(cx);
131 window.focus_next(cx);
132 });
133 for key in ["enter", "space"] {
134 let keystroke = Keystroke::parse(key).unwrap();
135 visual.simulate_event(KeyDownEvent {
136 keystroke: keystroke.clone(),
137 is_held: false,
138 prefer_character_input: false,
139 });
140 visual.simulate_event(KeyUpEvent { keystroke });
141 }
142 let expected = match control {
143 Control::RadioGroup => "0",
144 _ => "true",
145 };
146 assert_eq!(
147 *requests.borrow(),
148 if disabled {
149 vec![]
150 } else {
151 vec![expected.to_owned(); 3]
152 },
153 "{control:?}, {callback:?}, disabled={disabled}"
154 );
155 }
156 }
157 }
158}
159
160#[gpui::test]
161fn owner_applies_requested_value_before_the_next_activation(cx: &mut TestAppContext) {
162 struct Owner {
163 checked: bool,
164 requests: Vec<bool>,
165 }
166
167 impl Render for Owner {
168 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
169 div().id("owner").tab_group().child(
170 Checkbox::new("owned-checkbox")
171 .checked(self.checked)
172 .label("Choice")
173 .on_change(cx.listener(|this, checked, _, cx| {
174 this.checked = *checked;
175 this.requests.push(*checked);
176 cx.notify();
177 })),
178 )
179 }
180 }
181
182 cx.update(gpui_component::init);
183 let (owner, visual) = cx.add_window_view(|_, _| Owner {
184 checked: false,
185 requests: vec![],
186 });
187 visual.update(|window, cx| window.draw(cx).clear(cx));
188 visual.simulate_click(gpui::point(px(8.), px(8.)), Modifiers::default());
189 visual.update(|window, cx| {
190 window.draw(cx).clear(cx);
191 window.focus_next(cx);
192 });
193 let keystroke = Keystroke::parse("space").unwrap();
194 visual.simulate_event(KeyDownEvent {
195 keystroke: keystroke.clone(),
196 is_held: false,
197 prefer_character_input: false,
198 });
199 visual.simulate_event(KeyUpEvent { keystroke });
200 visual.update(|_, cx| {
201 assert_eq!(owner.read(cx).requests, [true, false]);
202 assert!(!owner.read(cx).checked);
203 });
204}