teksilo-core 0.9.0

Core of the Teksilo GUI framework — widget trait, arena, layout engine, event dispatch, focus, signals and theming.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! Branching widget types and child-insertion trait for the `teksu!` DSL.
//!
//! `TeksiBranch{,3,4}` are two-, three-, and four-way sum types over Widget
//! implementations. They exist so `if`/`else` and small `match` arms in
//! `teksu!` can yield heterogeneous widget types from the same position
//! without boxing. Each variant implements Widget by delegating every
//! method to the active arm.
//!
//! `IntoTeksiChild` is the dispatch trait the macro uses when it cannot
//! decide at expansion time whether a child expression is a widget value
//! or a pre-registered `WidgetId` (the `#{ expr }` escape case). It
//! produces a `PendingChild`, which Category A containers already know
//! how to route through their `child()` / `add_child()` path.

use teksilo_canvas::{Canvas, Rect, SizeProposal};

use crate::accessibility::AccessNodeBuilder;
use crate::build_context::BuildContext;
use crate::widget::{LayoutContext, PaintContext, PendingChild, Widget, WidgetPlacement};
use crate::widget_builder::HandlerSet;
use crate::widget_id::WidgetId;

// ---------------------------------------------------------------------------
// TeksiBranch — two-way sum type
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub enum TeksiBranch<L: Widget, R: Widget> {
    L(L),
    R(R),
}

impl<L: Widget, R: Widget> Widget for TeksiBranch<L, R> {
    fn build(&mut self, ctx: &mut BuildContext) -> Vec<WidgetId> {
        match self {
            TeksiBranch::L(w) => w.build(ctx),
            TeksiBranch::R(w) => w.build(ctx),
        }
    }

    fn layout_response(
        &self,
        proposal: SizeProposal,
        ctx: &LayoutContext,
    ) -> crate::widget::LayoutResponse {
        match self {
            TeksiBranch::L(w) => w.layout_response(proposal, ctx),
            TeksiBranch::R(w) => w.layout_response(proposal, ctx),
        }
    }

    fn place_children(
        &self,
        bounds: Rect,
        proposal: SizeProposal,
        children: &mut [WidgetPlacement],
        ctx: &LayoutContext,
    ) {
        match self {
            TeksiBranch::L(w) => w.place_children(bounds, proposal, children, ctx),
            TeksiBranch::R(w) => w.place_children(bounds, proposal, children, ctx),
        }
    }

    fn paint(&self, bounds: Rect, canvas: &mut Canvas, ctx: &PaintContext) {
        match self {
            TeksiBranch::L(w) => w.paint(bounds, canvas, ctx),
            TeksiBranch::R(w) => w.paint(bounds, canvas, ctx),
        }
    }

    fn accessibility(&self, builder: &mut AccessNodeBuilder) {
        match self {
            TeksiBranch::L(w) => w.accessibility(builder),
            TeksiBranch::R(w) => w.accessibility(builder),
        }
    }

    fn accessible_title_hint(&self) -> Option<String> {
        match self {
            TeksiBranch::L(w) => w.accessible_title_hint(),
            TeksiBranch::R(w) => w.accessible_title_hint(),
        }
    }

    fn initial_focus_hint(&self) -> Option<WidgetId> {
        match self {
            TeksiBranch::L(w) => w.initial_focus_hint(),
            TeksiBranch::R(w) => w.initial_focus_hint(),
        }
    }

    fn children(&self) -> Vec<WidgetId> {
        match self {
            TeksiBranch::L(w) => w.children(),
            TeksiBranch::R(w) => w.children(),
        }
    }

    fn clips_children(&self) -> bool {
        match self {
            TeksiBranch::L(w) => w.clips_children(),
            TeksiBranch::R(w) => w.clips_children(),
        }
    }

    fn take_handler_set(&mut self) -> Option<HandlerSet> {
        match self {
            TeksiBranch::L(w) => w.take_handler_set(),
            TeksiBranch::R(w) => w.take_handler_set(),
        }
    }
}

// ---------------------------------------------------------------------------
// TeksiBranch3 — three-way sum type
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub enum TeksiBranch3<A: Widget, B: Widget, C: Widget> {
    A(A),
    B(B),
    C(C),
}

impl<A: Widget, B: Widget, C: Widget> Widget for TeksiBranch3<A, B, C> {
    fn build(&mut self, ctx: &mut BuildContext) -> Vec<WidgetId> {
        match self {
            TeksiBranch3::A(w) => w.build(ctx),
            TeksiBranch3::B(w) => w.build(ctx),
            TeksiBranch3::C(w) => w.build(ctx),
        }
    }

    fn layout_response(
        &self,
        proposal: SizeProposal,
        ctx: &LayoutContext,
    ) -> crate::widget::LayoutResponse {
        match self {
            TeksiBranch3::A(w) => w.layout_response(proposal, ctx),
            TeksiBranch3::B(w) => w.layout_response(proposal, ctx),
            TeksiBranch3::C(w) => w.layout_response(proposal, ctx),
        }
    }

    fn place_children(
        &self,
        bounds: Rect,
        proposal: SizeProposal,
        children: &mut [WidgetPlacement],
        ctx: &LayoutContext,
    ) {
        match self {
            TeksiBranch3::A(w) => w.place_children(bounds, proposal, children, ctx),
            TeksiBranch3::B(w) => w.place_children(bounds, proposal, children, ctx),
            TeksiBranch3::C(w) => w.place_children(bounds, proposal, children, ctx),
        }
    }

    fn paint(&self, bounds: Rect, canvas: &mut Canvas, ctx: &PaintContext) {
        match self {
            TeksiBranch3::A(w) => w.paint(bounds, canvas, ctx),
            TeksiBranch3::B(w) => w.paint(bounds, canvas, ctx),
            TeksiBranch3::C(w) => w.paint(bounds, canvas, ctx),
        }
    }

    fn accessibility(&self, builder: &mut AccessNodeBuilder) {
        match self {
            TeksiBranch3::A(w) => w.accessibility(builder),
            TeksiBranch3::B(w) => w.accessibility(builder),
            TeksiBranch3::C(w) => w.accessibility(builder),
        }
    }

    fn accessible_title_hint(&self) -> Option<String> {
        match self {
            TeksiBranch3::A(w) => w.accessible_title_hint(),
            TeksiBranch3::B(w) => w.accessible_title_hint(),
            TeksiBranch3::C(w) => w.accessible_title_hint(),
        }
    }

    fn initial_focus_hint(&self) -> Option<WidgetId> {
        match self {
            TeksiBranch3::A(w) => w.initial_focus_hint(),
            TeksiBranch3::B(w) => w.initial_focus_hint(),
            TeksiBranch3::C(w) => w.initial_focus_hint(),
        }
    }

    fn children(&self) -> Vec<WidgetId> {
        match self {
            TeksiBranch3::A(w) => w.children(),
            TeksiBranch3::B(w) => w.children(),
            TeksiBranch3::C(w) => w.children(),
        }
    }

    fn clips_children(&self) -> bool {
        match self {
            TeksiBranch3::A(w) => w.clips_children(),
            TeksiBranch3::B(w) => w.clips_children(),
            TeksiBranch3::C(w) => w.clips_children(),
        }
    }

    fn take_handler_set(&mut self) -> Option<HandlerSet> {
        match self {
            TeksiBranch3::A(w) => w.take_handler_set(),
            TeksiBranch3::B(w) => w.take_handler_set(),
            TeksiBranch3::C(w) => w.take_handler_set(),
        }
    }
}

// ---------------------------------------------------------------------------
// TeksiBranch4 — four-way sum type
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub enum TeksiBranch4<A: Widget, B: Widget, C: Widget, D: Widget> {
    A(A),
    B(B),
    C(C),
    D(D),
}

impl<A: Widget, B: Widget, C: Widget, D: Widget> Widget for TeksiBranch4<A, B, C, D> {
    fn build(&mut self, ctx: &mut BuildContext) -> Vec<WidgetId> {
        match self {
            TeksiBranch4::A(w) => w.build(ctx),
            TeksiBranch4::B(w) => w.build(ctx),
            TeksiBranch4::C(w) => w.build(ctx),
            TeksiBranch4::D(w) => w.build(ctx),
        }
    }

    fn layout_response(
        &self,
        proposal: SizeProposal,
        ctx: &LayoutContext,
    ) -> crate::widget::LayoutResponse {
        match self {
            TeksiBranch4::A(w) => w.layout_response(proposal, ctx),
            TeksiBranch4::B(w) => w.layout_response(proposal, ctx),
            TeksiBranch4::C(w) => w.layout_response(proposal, ctx),
            TeksiBranch4::D(w) => w.layout_response(proposal, ctx),
        }
    }

    fn place_children(
        &self,
        bounds: Rect,
        proposal: SizeProposal,
        children: &mut [WidgetPlacement],
        ctx: &LayoutContext,
    ) {
        match self {
            TeksiBranch4::A(w) => w.place_children(bounds, proposal, children, ctx),
            TeksiBranch4::B(w) => w.place_children(bounds, proposal, children, ctx),
            TeksiBranch4::C(w) => w.place_children(bounds, proposal, children, ctx),
            TeksiBranch4::D(w) => w.place_children(bounds, proposal, children, ctx),
        }
    }

    fn paint(&self, bounds: Rect, canvas: &mut Canvas, ctx: &PaintContext) {
        match self {
            TeksiBranch4::A(w) => w.paint(bounds, canvas, ctx),
            TeksiBranch4::B(w) => w.paint(bounds, canvas, ctx),
            TeksiBranch4::C(w) => w.paint(bounds, canvas, ctx),
            TeksiBranch4::D(w) => w.paint(bounds, canvas, ctx),
        }
    }

    fn accessibility(&self, builder: &mut AccessNodeBuilder) {
        match self {
            TeksiBranch4::A(w) => w.accessibility(builder),
            TeksiBranch4::B(w) => w.accessibility(builder),
            TeksiBranch4::C(w) => w.accessibility(builder),
            TeksiBranch4::D(w) => w.accessibility(builder),
        }
    }

    fn accessible_title_hint(&self) -> Option<String> {
        match self {
            TeksiBranch4::A(w) => w.accessible_title_hint(),
            TeksiBranch4::B(w) => w.accessible_title_hint(),
            TeksiBranch4::C(w) => w.accessible_title_hint(),
            TeksiBranch4::D(w) => w.accessible_title_hint(),
        }
    }

    fn initial_focus_hint(&self) -> Option<WidgetId> {
        match self {
            TeksiBranch4::A(w) => w.initial_focus_hint(),
            TeksiBranch4::B(w) => w.initial_focus_hint(),
            TeksiBranch4::C(w) => w.initial_focus_hint(),
            TeksiBranch4::D(w) => w.initial_focus_hint(),
        }
    }

    fn children(&self) -> Vec<WidgetId> {
        match self {
            TeksiBranch4::A(w) => w.children(),
            TeksiBranch4::B(w) => w.children(),
            TeksiBranch4::C(w) => w.children(),
            TeksiBranch4::D(w) => w.children(),
        }
    }

    fn clips_children(&self) -> bool {
        match self {
            TeksiBranch4::A(w) => w.clips_children(),
            TeksiBranch4::B(w) => w.clips_children(),
            TeksiBranch4::C(w) => w.clips_children(),
            TeksiBranch4::D(w) => w.clips_children(),
        }
    }

    fn take_handler_set(&mut self) -> Option<HandlerSet> {
        match self {
            TeksiBranch4::A(w) => w.take_handler_set(),
            TeksiBranch4::B(w) => w.take_handler_set(),
            TeksiBranch4::C(w) => w.take_handler_set(),
            TeksiBranch4::D(w) => w.take_handler_set(),
        }
    }
}

// ---------------------------------------------------------------------------
// IntoTeksiChild — widget-or-id dispatch for #{ expr } child positions
// ---------------------------------------------------------------------------

/// Dispatch trait the `teksu!` macro uses to route child expressions whose
/// static type isn't known at expansion time (the `#{ expr }` escape).
/// `impl Widget + 'static` values lower to `PendingChild::Deferred`;
/// pre-registered `WidgetId` values lower to `PendingChild::Id`.
pub trait IntoTeksiChild {
    fn into_pending(self) -> PendingChild;
}

impl<W: Widget + 'static> IntoTeksiChild for W {
    fn into_pending(self) -> PendingChild {
        PendingChild::Deferred(Box::new(self))
    }
}

impl IntoTeksiChild for WidgetId {
    fn into_pending(self) -> PendingChild {
        PendingChild::Id(self)
    }
}

// ---------------------------------------------------------------------------
// IntoTeksiCondition — reactive/static dispatch for `if bare_ident { ... }`
// ---------------------------------------------------------------------------

/// Dispatch trait the `teksu!` macro uses for `if bare_ident { Element }`
/// — the `teksu!` "reactive conditionals" pattern. The bare-identifier form
/// lowers to a call on this trait; which impl fires (and thus whether
/// the element is conditionally built or always built with bound
/// visibility) is decided at monomorphization.
///
/// - `bool`: static — the element is built only when the flag is true.
///   Returns `Some(id)` if built, `None` if skipped.
/// - `Signal<bool>` / `Prop<bool>`: reactive — the element is always
///   built, and its visibility is bound to the signal via
///   `BuildContext::visible_when`. Returns `Some(id)` unconditionally.
///
/// The return type is `Option<WidgetId>` so the macro can use a single
/// lowering shape (`if let Some(id) = ... { parent.add_child(id) }`)
/// that works for both cases.
pub trait IntoTeksiCondition {
    fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
        self,
        child: W,
        ctx: &mut crate::build_context::BuildContext,
    ) -> Option<WidgetId>;
}

impl IntoTeksiCondition for bool {
    fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
        self,
        child: W,
        ctx: &mut crate::build_context::BuildContext,
    ) -> Option<WidgetId> {
        if self { Some(ctx.add(child)) } else { None }
    }
}

impl IntoTeksiCondition for crate::signal::Signal<bool> {
    fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
        self,
        child: W,
        ctx: &mut crate::build_context::BuildContext,
    ) -> Option<WidgetId> {
        let id = ctx.add(child);
        ctx.visible_when(id, self);
        Some(id)
    }
}

impl IntoTeksiCondition for crate::signal::Prop<bool> {
    fn teksilo_into_conditional_child<W: crate::widget::Widget + 'static>(
        self,
        child: W,
        ctx: &mut crate::build_context::BuildContext,
    ) -> Option<WidgetId> {
        let id = ctx.add(child);
        ctx.visible_when(id, self);
        Some(id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_widgets::FillWidget;
    use crate::widget_tree::WidgetTree;
    use teksilo_canvas::SizeProposal;
    use teksilo_tokens::Color;

    #[test]
    fn teksilo_branch_dispatches_to_active_variant() {
        // Build two trees, one with each variant, confirm each variant's
        // widget actually runs its own build/size/paint path.
        let mut tree_l = WidgetTree::new();
        let id_l = tree_l.add(TeksiBranch::<FillWidget, FillWidget>::L(
            FillWidget::new().background(Color::RED),
        ));
        tree_l.layout(SizeProposal::exact(100.0, 50.0));
        assert!((tree_l.bounds(id_l).width - 100.0).abs() < 0.01);

        let mut tree_r = WidgetTree::new();
        let id_r = tree_r.add(TeksiBranch::<FillWidget, FillWidget>::R(
            FillWidget::new().background(Color::BLUE),
        ));
        tree_r.layout(SizeProposal::exact(80.0, 40.0));
        assert!((tree_r.bounds(id_r).width - 80.0).abs() < 0.01);
    }

    #[test]
    fn teksilo_branch3_dispatches_to_active_variant() {
        let mut tree = WidgetTree::new();
        let id = tree.add(TeksiBranch3::<FillWidget, FillWidget, FillWidget>::B(
            FillWidget::new(),
        ));
        tree.layout(SizeProposal::exact(120.0, 60.0));
        assert!((tree.bounds(id).width - 120.0).abs() < 0.01);
    }

    #[test]
    fn into_teksilo_child_routes_widget_to_deferred() {
        let pending = FillWidget::new().into_pending();
        assert!(matches!(pending, PendingChild::Deferred(_)));
    }

    #[test]
    fn into_teksilo_child_routes_widget_id_to_id() {
        let mut tree = WidgetTree::new();
        let leaf = tree.add(FillWidget::new());
        let pending = leaf.into_pending();
        match pending {
            PendingChild::Id(id) => assert_eq!(id, leaf),
            _ => panic!("expected PendingChild::Id"),
        }
    }
}