1use teksilo_canvas::{Canvas, Point, Rect, SizeProposal};
19
20use crate::accessibility::AccessNodeBuilder;
21use crate::build_context::BuildContext;
22use crate::widget::{
23 LayoutContext, PaintContext, PendingChild, Widget, WidgetPlacement, WidgetTreeView,
24};
25use crate::widget_builder::HandlerSet;
26use crate::widget_id::WidgetId;
27
28macro_rules! impl_widget_for_branch {
47 ($branch:ident, $($arm:ident),+) => {
48 #[deny(clippy::missing_trait_methods)]
49 impl<$($arm: Widget),+> Widget for $branch<$($arm),+> {
50 fn type_name(&self) -> &'static str {
51 match self { $($branch::$arm(w) => w.type_name()),+ }
52 }
53
54 fn build(&mut self, ctx: &mut BuildContext) -> Vec<WidgetId> {
55 match self { $($branch::$arm(w) => w.build(ctx)),+ }
56 }
57
58 fn layout_response(
59 &self,
60 proposal: SizeProposal,
61 ctx: &LayoutContext,
62 ) -> crate::widget::LayoutResponse {
63 match self { $($branch::$arm(w) => w.layout_response(proposal, ctx)),+ }
64 }
65
66 fn cacheable_layout(&self) -> bool {
67 match self { $($branch::$arm(w) => w.cacheable_layout()),+ }
68 }
69
70 fn place_children(
71 &self,
72 bounds: Rect,
73 proposal: SizeProposal,
74 children: &mut [WidgetPlacement],
75 ctx: &LayoutContext,
76 ) {
77 match self {
78 $($branch::$arm(w) => w.place_children(bounds, proposal, children, ctx)),+
79 }
80 }
81
82 fn paint(&self, bounds: Rect, canvas: &mut Canvas, ctx: &PaintContext) {
83 match self { $($branch::$arm(w) => w.paint(bounds, canvas, ctx)),+ }
84 }
85
86 fn wants_after_paint(&self) -> bool {
87 match self { $($branch::$arm(w) => w.wants_after_paint()),+ }
88 }
89
90 fn after_paint(&self, view: &WidgetTreeView<'_>, ctx: &PaintContext) {
91 match self { $($branch::$arm(w) => w.after_paint(view, ctx)),+ }
92 }
93
94 fn wants_post_paint(&self) -> bool {
95 match self { $($branch::$arm(w) => w.wants_post_paint()),+ }
96 }
97
98 fn post_paint(&self, bounds: Rect, canvas: &mut Canvas, ctx: &PaintContext) {
99 match self { $($branch::$arm(w) => w.post_paint(bounds, canvas, ctx)),+ }
100 }
101
102 fn accessibility(&self, builder: &mut AccessNodeBuilder) {
103 match self { $($branch::$arm(w) => w.accessibility(builder)),+ }
104 }
105
106 fn culls_children(&self) -> bool {
107 match self { $($branch::$arm(w) => w.culls_children()),+ }
108 }
109
110 fn wants_descendant_redirects(&self) -> bool {
111 match self { $($branch::$arm(w) => w.wants_descendant_redirects()),+ }
112 }
113
114 fn a11y_redirect_descendant(
115 &self,
116 self_id: WidgetId,
117 descendant: WidgetId,
118 ) -> Option<accesskit::NodeId> {
119 match self {
120 $($branch::$arm(w) => w.a11y_redirect_descendant(self_id, descendant)),+
121 }
122 }
123
124 fn accessible_title_hint(&self) -> Option<String> {
125 match self { $($branch::$arm(w) => w.accessible_title_hint()),+ }
126 }
127
128 fn accessible_title_node(&self) -> Option<crate::widget_id::WidgetId> {
129 match self { $($branch::$arm(w) => w.accessible_title_node()),+ }
130 }
131
132 fn initial_focus_hint(&self) -> Option<WidgetId> {
133 match self { $($branch::$arm(w) => w.initial_focus_hint()),+ }
134 }
135
136 fn accessibility_proxy(&self) -> Option<WidgetId> {
137 match self { $($branch::$arm(w) => w.accessibility_proxy()),+ }
138 }
139
140 fn context_menu_key_target(&self) -> Option<WidgetId> {
141 match self { $($branch::$arm(w) => w.context_menu_key_target()),+ }
142 }
143
144 fn children(&self) -> Vec<WidgetId> {
145 match self { $($branch::$arm(w) => w.children()),+ }
146 }
147
148 fn accessibility_children(&self) -> Option<Vec<WidgetId>> {
149 match self { $($branch::$arm(w) => w.accessibility_children()),+ }
150 }
151
152 fn as_any(&self) -> Option<&dyn std::any::Any> {
153 match self { $($branch::$arm(w) => w.as_any()),+ }
154 }
155
156 fn as_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
157 match self { $($branch::$arm(w) => w.as_any_mut()),+ }
158 }
159
160 fn clips_children(&self) -> bool {
161 match self { $($branch::$arm(w) => w.clips_children()),+ }
162 }
163
164 fn focus_reveal_rect(&self, bounds: Rect) -> Option<Rect> {
165 match self { $($branch::$arm(w) => w.focus_reveal_rect(bounds)),+ }
166 }
167
168 fn hit_shape(&self, local_point: Point, bounds: Rect) -> bool {
169 match self { $($branch::$arm(w) => w.hit_shape(local_point, bounds)),+ }
170 }
171
172 fn accepts_child_hit(&self, child: WidgetId, point: Point) -> bool {
173 match self { $($branch::$arm(w) => w.accepts_child_hit(child, point)),+ }
174 }
175
176 fn hit_outset(
177 &self,
178 kind: teksilo_tokens::PointerKind,
179 tokens: &teksilo_tokens::InputTokens,
180 ) -> teksilo_canvas::EdgeInsets {
181 match self { $($branch::$arm(w) => w.hit_outset(kind, tokens)),+ }
182 }
183
184 fn hit_slop(
185 &self,
186 kind: teksilo_tokens::PointerKind,
187 tokens: &teksilo_tokens::InputTokens,
188 ) -> Option<crate::pointer::hit_slop::HitSlop> {
189 match self { $($branch::$arm(w) => Widget::hit_slop(w, kind, tokens)),+ }
193 }
194
195 fn hit_distance(&self, local_point: Point, bounds: Rect) -> Option<f32> {
196 match self { $($branch::$arm(w) => w.hit_distance(local_point, bounds)),+ }
197 }
198
199 fn target_regions(&self, bounds: Rect) -> Vec<crate::partition::TargetRegion> {
200 match self { $($branch::$arm(w) => w.target_regions(bounds)),+ }
201 }
202
203 fn preserves_children_on_rebuild(&self) -> bool {
204 match self { $($branch::$arm(w) => w.preserves_children_on_rebuild()),+ }
205 }
206
207 fn tooltip_has_content(&self) -> bool {
208 match self { $($branch::$arm(w) => w.tooltip_has_content()),+ }
209 }
210
211 fn declare_shortcuts(&self) -> Vec<crate::shortcut::Shortcut> {
212 match self { $($branch::$arm(w) => w.declare_shortcuts()),+ }
213 }
214
215 fn take_handler_set(&mut self) -> Option<HandlerSet> {
216 match self { $($branch::$arm(w) => w.take_handler_set()),+ }
217 }
218 }
219 };
220}
221
222#[derive(Debug)]
227pub enum TeksiBranch<L: Widget, R: Widget> {
228 L(L),
229 R(R),
230}
231
232impl_widget_for_branch!(TeksiBranch, L, R);
233
234#[derive(Debug)]
239pub enum TeksiBranch3<A: Widget, B: Widget, C: Widget> {
240 A(A),
241 B(B),
242 C(C),
243}
244
245impl_widget_for_branch!(TeksiBranch3, A, B, C);
246
247#[derive(Debug)]
252pub enum TeksiBranch4<A: Widget, B: Widget, C: Widget, D: Widget> {
253 A(A),
254 B(B),
255 C(C),
256 D(D),
257}
258
259impl_widget_for_branch!(TeksiBranch4, A, B, C, D);
260
261pub trait IntoTeksiChild {
270 fn into_pending(self) -> PendingChild;
271}
272
273impl<W: Widget + 'static> IntoTeksiChild for W {
274 fn into_pending(self) -> PendingChild {
275 PendingChild::Deferred(Box::new(self))
276 }
277}
278
279impl IntoTeksiChild for WidgetId {
280 fn into_pending(self) -> PendingChild {
281 PendingChild::Id(self)
282 }
283}
284
285pub trait IntoTeksiCondition {
312 fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
313 self,
314 child: W,
315 ctx: &mut crate::build_context::BuildContext,
316 ) -> Option<WidgetId>;
317}
318
319impl IntoTeksiCondition for bool {
320 fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
321 self,
322 child: W,
323 ctx: &mut crate::build_context::BuildContext,
324 ) -> Option<WidgetId> {
325 if self { Some(ctx.add(child)) } else { None }
326 }
327}
328
329impl IntoTeksiCondition for crate::signal::Signal<bool> {
330 fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
331 self,
332 child: W,
333 ctx: &mut crate::build_context::BuildContext,
334 ) -> Option<WidgetId> {
335 let id = ctx.add(child);
336 ctx.visible_when(id, self);
337 Some(id)
338 }
339}
340
341impl IntoTeksiCondition for crate::signal::Prop<bool> {
342 fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
343 self,
344 child: W,
345 ctx: &mut crate::build_context::BuildContext,
346 ) -> Option<WidgetId> {
347 let id = ctx.add(child);
348 ctx.visible_when(id, self);
349 Some(id)
350 }
351}
352
353#[cfg(test)]
354mod tests {
355 use super::*;
356 use crate::test_widgets::FillWidget;
357 use crate::widget_tree::WidgetTree;
358 use teksilo_canvas::SizeProposal;
359 use teksilo_tokens::Color;
360
361 #[test]
362 fn teksilo_branch_dispatches_to_active_variant() {
363 let mut tree_l = WidgetTree::new();
366 let id_l = tree_l.add(TeksiBranch::<FillWidget, FillWidget>::L(
367 FillWidget::new().background(Color::RED),
368 ));
369 tree_l.layout(SizeProposal::exact(100.0, 50.0));
370 assert!((tree_l.bounds(id_l).width - 100.0).abs() < 0.01);
371
372 let mut tree_r = WidgetTree::new();
373 let id_r = tree_r.add(TeksiBranch::<FillWidget, FillWidget>::R(
374 FillWidget::new().background(Color::BLUE),
375 ));
376 tree_r.layout(SizeProposal::exact(80.0, 40.0));
377 assert!((tree_r.bounds(id_r).width - 80.0).abs() < 0.01);
378 }
379
380 #[test]
381 fn teksilo_branch3_dispatches_to_active_variant() {
382 let mut tree = WidgetTree::new();
383 let id = tree.add(TeksiBranch3::<FillWidget, FillWidget, FillWidget>::B(
384 FillWidget::new(),
385 ));
386 tree.layout(SizeProposal::exact(120.0, 60.0));
387 assert!((tree.bounds(id).width - 120.0).abs() < 0.01);
388 }
389
390 #[test]
391 fn into_teksilo_child_routes_widget_to_deferred() {
392 let pending = FillWidget::new().into_pending();
393 assert!(matches!(pending, PendingChild::Deferred(_)));
394 }
395
396 #[test]
397 fn into_teksilo_child_routes_widget_id_to_id() {
398 let mut tree = WidgetTree::new();
399 let leaf = tree.add(FillWidget::new());
400 let pending = leaf.into_pending();
401 match pending {
402 PendingChild::Id(id) => assert_eq!(id, leaf),
403 _ => panic!("expected PendingChild::Id"),
404 }
405 }
406}