burin 0.1.2

A Rust GUI framework built on the auralis reactive signal kernel
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
use crate::core::config::{EventHandler, StateFlags};
use crate::core::context::MountContext;
use crate::core::dirty_registry;
use crate::core::element::DirtyFlags;
use crate::core::element::ElementId;
use crate::core::widget::Widget;
use crate::event::types::Key;
use crate::resource::icons::Icon as IconKind;
use crate::style::styled::{StyleRefinement, Styled};
use crate::style::{Alignment, Color, Padding};
use crate::theme::m3::roles::{ComponentRole, InteractiveRole, ResolvedComponentStyle, TabStyle};
use crate::widgets::display::{Icon, Text};
use auralis_signal::Signal;
use std::cell::Cell;
use std::rc::Rc;

// ── Tab data ─────────────────────────────────────────────────────

/// Data for a single tab in a tab bar.
pub struct Tab {
    pub label: String,
    pub icon: Option<Icon>,
    pub closable: bool,
    pub disabled: bool,
    pub tooltip: Option<String>,
}

impl Tab {
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            icon: None,
            closable: false,
            disabled: false,
            tooltip: None,
        }
    }
    pub fn icon(mut self, icon: Icon) -> Self {
        self.icon = Some(icon);
        self
    }
    pub fn closable(mut self) -> Self {
        self.closable = true;
        self
    }
    pub fn disabled(mut self) -> Self {
        self.disabled = true;
        self
    }
    pub fn tooltip(mut self, t: impl Into<String>) -> Self {
        self.tooltip = Some(t.into());
        self
    }
}

// ── Per-tab element tracking ─────────────────────────────────────

#[derive(Clone)]
struct TabElements {
    container: ElementId,
    indicator: ElementId,
    text: ElementId,
}

// ── TabBar ───────────────────────────────────────────────────────

/// A horizontal bar of selectable tabs with keyboard navigation.
pub struct TabBar {
    tabs: Vec<Tab>,
    active: Signal<usize>,
    on_close: Option<Rc<dyn Fn(usize)>>,
    on_reorder: Option<Rc<dyn Fn(usize, usize)>>,
    on_change: Option<Rc<dyn Fn(usize)>>,
    style: StyleRefinement,
}

impl TabBar {
    pub fn new(active: Signal<usize>) -> Self {
        Self {
            tabs: Vec::new(),
            active,
            on_close: None,
            on_reorder: None,
            on_change: None,
            style: StyleRefinement::default(),
        }
    }

    pub fn tab(mut self, label: impl Into<String>) -> Self {
        self.tabs.push(Tab::new(label));
        self
    }

    pub fn tab_full(mut self, tab: Tab) -> Self {
        self.tabs.push(tab);
        self
    }

    pub fn tabs(mut self, tabs: Vec<Tab>) -> Self {
        self.tabs = tabs;
        self
    }

    pub fn on_close(mut self, f: impl Fn(usize) + 'static) -> Self {
        self.on_close = Some(Rc::new(f));
        self
    }

    pub fn on_reorder(mut self, f: impl Fn(usize, usize) + 'static) -> Self {
        self.on_reorder = Some(Rc::new(f));
        self
    }

    pub fn on_change(mut self, f: impl Fn(usize) + 'static) -> Self {
        self.on_change = Some(Rc::new(f));
        self
    }
}

impl Styled for TabBar {
    fn style_refinement(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Widget for TabBar {
    fn component_mask(&self) -> u64 {
        use crate::ecs::components;
        components::LAYOUT | components::STYLE | components::LIFECYCLE
    }

    fn mount_box(self: Box<Self>, ctx: &mut MountContext<'_>) -> ElementId {
        let tab_count = self.tabs.len();
        let active_sig = self.active;
        let on_close = self.on_close;
        let on_change = self.on_change;

        let tab_style = {
            let tab_role = ComponentRole::Interactive(InteractiveRole::Tab { selected: false });
            let resolved = ctx.theme.resolve_component(&tab_role);
            match &resolved {
                ResolvedComponentStyle::Tab(s) => s.clone(),
                _ => TabStyle {
                    background: Color::TRANSPARENT,
                    foreground: Color::rgba8(100, 100, 100, 255),
                    selected_bg: Color::rgba8(200, 200, 200, 255),
                    selected_fg: Color::BLACK,
                    indicator_color: Color::BLACK,
                    indicator_height: 3.0,
                    hover_bg: Color::rgba8(0, 0, 0, 20),
                    hover_fg: Color::BLACK,
                    pressed_bg: Color::rgba8(0, 0, 0, 40),
                    pressed_fg: Color::BLACK,
                    focused_bg: Color::rgba8(0, 0, 0, 10),
                    focused_fg: Color::BLACK,
                    disabled: crate::theme::m3::states::DisabledColors {
                        background: Color::rgba8(200, 200, 200, 255),
                        foreground: Color::rgba8(150, 150, 150, 255),
                        border: Color::rgba8(200, 200, 200, 255),
                    },
                    font_size: 14.0,
                    height: 48.0,
                    tab_gap: 4.0,
                    pill_radius: crate::style::CornerRadii::all(6.0),
                },
            }
        };

        // ── Container element ──────────────────────────────────────
        let id = ctx.arena.allocate();
        {
            let Some(el) = ctx.arena.get_mut(id) else {
                return id;
            };
            el.set_layout_direction(crate::core::LayoutDirection::Horizontal);
            el.set_gap(self.style.gap.unwrap_or(tab_style.tab_gap));
            el.set_alignment(Alignment::Start);
        }

        // ── Mount each tab ────────────────────────────────────────
        let mut tab_refs: Vec<TabElements> = Vec::with_capacity(tab_count);

        for (i, tab) in self.tabs.into_iter().enumerate() {
            let is_active = active_sig.read() == i;
            let is_disabled = tab.disabled;

            // Tab container (VStack — interactive, holds button row + indicator bar)
            let tab_container_id = ctx.arena.allocate();
            {
                let Some(el) = ctx.arena.get_mut(tab_container_id) else {
                    return id;
                };
                el.set_layout_direction(crate::core::LayoutDirection::Vertical);
                el.set_gap(0.0);
                el.set_alignment(Alignment::Stretch);

                if is_disabled {
                    crate::core::dirty_registry::set_state(
                        tab_container_id,
                        StateFlags::DISABLED,
                        true,
                    );
                } else {
                    el.set_focusable(true);
                    crate::core::dirty_registry::set_state(
                        tab_container_id,
                        StateFlags::CHECKED,
                        is_active,
                    );
                }
            }

            // Tab button (HStack — the visual pill, no events)
            let tab_button_id = ctx.arena.allocate();
            {
                let Some(el) = ctx.arena.get_mut(tab_button_id) else {
                    return id;
                };
                el.set_layout_direction(crate::core::LayoutDirection::Horizontal);
                el.set_alignment(Alignment::Center);
                el.set_content_align(Alignment::Center);
                el.set_gap(6.0);
                el.set_preferred_height(tab_style.height);
                el.set_padding(Padding {
                    left: 12.0,
                    right: 12.0,
                    top: 0.0,
                    bottom: 0.0,
                });
                el.set_corner_radii(tab_style.pill_radius);
                el.set_background(if is_active {
                    tab_style.selected_bg
                } else {
                    Color::TRANSPARENT
                });

                el.with_state_style(|ss| {
                    ss.checked.background = Some(tab_style.selected_bg);
                    ss.checked.foreground = Some(tab_style.selected_fg);
                    ss.hovered.background = Some(tab_style.hover_bg);
                    ss.hovered.foreground = Some(tab_style.hover_fg);
                    ss.pressed.background = Some(tab_style.pressed_bg);
                    ss.pressed.foreground = Some(tab_style.pressed_fg);
                    ss.focused.background = Some(tab_style.focused_bg);
                    ss.focused.foreground = Some(tab_style.focused_fg);
                    ss.disabled.foreground = Some(tab_style.disabled.foreground);
                });
            }

            // Icon
            if let Some(icon) = tab.icon {
                let icon_id = Box::new(icon).mount_box(&mut ctx.child_with_events(tab_button_id));
                ctx.arena.add_child(tab_button_id, icon_id);
            }

            // Label
            let text_fg = if is_active {
                tab_style.selected_fg
            } else {
                tab_style.foreground
            };
            let text_widget = Box::new(Text::new(&tab.label).font_size(tab_style.font_size).color(
                if is_disabled {
                    tab_style.disabled.foreground
                } else {
                    text_fg
                },
            ));
            let text_id = text_widget.mount_box(&mut ctx.child_with_events(tab_button_id));
            ctx.arena.add_child(tab_button_id, text_id);

            // Close button
            if tab.closable {
                if let Some(ref _oc) = on_close {
                    let close_icon = Box::new(
                        Icon::new(IconKind::X)
                            .size(tab_style.font_size * 0.75)
                            .color(if is_active {
                                tab_style.selected_fg
                            } else {
                                tab_style.foreground
                            }),
                    );
                    let close_id = close_icon.mount_box(&mut ctx.child_with_events(tab_button_id));
                    if !is_disabled {
                        let oc = on_close.clone();
                        let ix = i;
                        let mut close_events = EventHandler::new();
                        close_events = close_events.on_click(move || {
                            if let Some(ref cb) = oc {
                                cb(ix);
                            }
                        });
                        if let Some(reg) = ctx.event_registry.as_mut() {
                            close_events.register_all(reg, close_id);
                        }
                    }
                    ctx.arena.add_child(tab_button_id, close_id);
                }
            }

            // ── Events on tab container ───────────────────────────
            let mut events = EventHandler::new();
            if !is_disabled {
                let a = active_sig.clone();
                let ix = i;
                let oc = on_change.clone();
                events = events.on_click(move || {
                    a.set(ix);
                    if let Some(ref cb) = oc {
                        cb(ix);
                    }
                });

                let a_sig = active_sig.clone();
                let len = tab_count;
                let oc2 = on_change.clone();
                events = events.on_key_down(move |key, _mods| {
                    let cur = a_sig.read();
                    let set_and_notify = |idx: usize| {
                        a_sig.set(idx);
                        if let Some(ref cb) = oc2 {
                            cb(idx);
                        }
                    };
                    match key {
                        Key::ArrowLeft => {
                            if cur > 0 {
                                set_and_notify(cur - 1);
                            }
                            true
                        }
                        Key::ArrowRight => {
                            if cur + 1 < len {
                                set_and_notify(cur + 1);
                            }
                            true
                        }
                        Key::Home => {
                            set_and_notify(0);
                            true
                        }
                        Key::End => {
                            set_and_notify(len - 1);
                            true
                        }
                        _ => false,
                    }
                });
            }

            if let Some(reg) = ctx.event_registry.as_mut() {
                events.register_all(reg, tab_container_id);
            }

            // Theme registration
            let sel_role = ComponentRole::Interactive(InteractiveRole::Tab {
                selected: is_active,
            });
            let sel_resolved = ctx.theme.resolve_component(&sel_role);
            ctx.register_theme_component(tab_button_id, &sel_resolved, &sel_role, &self.style);
            ctx.register_theme_component(tab_container_id, &sel_resolved, &sel_role, &self.style);

            // Indicator element (colored bar beneath the tab button)
            let indicator_id = ctx.arena.allocate();
            {
                let Some(el) = ctx.arena.get_mut(indicator_id) else {
                    return id;
                };
                el.set_preferred_height(tab_style.indicator_height);
                el.set_background(if is_active {
                    tab_style.indicator_color
                } else {
                    Color::TRANSPARENT
                });
                el.set_affected_by_child_size(false);
            }

            // Connect: button first (top), indicator second (bottom) in vertical layout
            ctx.arena.add_child(tab_container_id, tab_button_id);
            ctx.arena.add_child(tab_container_id, indicator_id);
            ctx.arena.add_child(id, tab_container_id);

            tab_refs.push(TabElements {
                container: tab_container_id,
                indicator: indicator_id,
                text: text_id,
            });
        }

        // ── Subscribe to active signal ─────────────────────────
        let tabs_info = tab_refs.clone();
        let container_id = id;
        let ts = tab_style.clone();
        let prev_active = Rc::new(Cell::new(active_sig.read()));
        let sub_sig = active_sig.clone();
        if let Some(el) = ctx.arena.get(id) {
            let dirty = el.dirty.clone();
            crate::core::signal_bridge::subscribe_owned(id, &active_sig, move || {
                let new_active = sub_sig.read();
                let old_active = prev_active.get();
                if new_active == old_active {
                    return;
                }
                prev_active.set(new_active);

                // Toggle CHECKED state on containers
                if let Some(info) = tabs_info.get(old_active) {
                    dirty_registry::set_state(info.container, StateFlags::CHECKED, false);
                    dirty_registry::mark_dirty(info.container, DirtyFlags::REPAINT);
                    dirty_registry::register_dirty(info.container, DirtyFlags::REPAINT);
                    dirty_registry::bump_subtree_gen(info.container);
                    dirty_registry::mark_dirty(info.indicator, DirtyFlags::REPAINT);
                    dirty_registry::register_dirty(info.indicator, DirtyFlags::REPAINT);
                    dirty_registry::bump_subtree_gen(info.indicator);
                    dirty_registry::mark_dirty(info.text, DirtyFlags::REPAINT);
                    dirty_registry::register_dirty(info.text, DirtyFlags::REPAINT);
                    dirty_registry::bump_subtree_gen(info.text);
                }
                if let Some(info) = tabs_info.get(new_active) {
                    dirty_registry::set_state(info.container, StateFlags::CHECKED, true);
                    dirty_registry::mark_dirty(info.container, DirtyFlags::REPAINT);
                    dirty_registry::register_dirty(info.container, DirtyFlags::REPAINT);
                    dirty_registry::bump_subtree_gen(info.container);
                    dirty_registry::mark_dirty(info.indicator, DirtyFlags::REPAINT);
                    dirty_registry::register_dirty(info.indicator, DirtyFlags::REPAINT);
                    dirty_registry::bump_subtree_gen(info.indicator);
                    dirty_registry::mark_dirty(info.text, DirtyFlags::REPAINT);
                    dirty_registry::register_dirty(info.text, DirtyFlags::REPAINT);
                    dirty_registry::bump_subtree_gen(info.text);
                }

                dirty.set(dirty.get() | DirtyFlags::REPAINT);
                dirty_registry::register_dirty(container_id, DirtyFlags::REPAINT);

                // Defer indicator + text color updates
                let infos = tabs_info.clone();
                let ts = ts.clone();
                dirty_registry::defer_action(move |arena, _, _| {
                    let mut ct = arena.component_tables.borrow_mut();
                    if let Some(info) = infos.get(old_active) {
                        ct.style.entry(info.indicator).or_default().background =
                            Some(Color::TRANSPARENT);
                        ct.style.entry(info.text).or_default().foreground = Some(ts.foreground);
                    }
                    if let Some(info) = infos.get(new_active) {
                        ct.style.entry(info.indicator).or_default().background =
                            Some(ts.indicator_color);
                        ct.style.entry(info.text).or_default().foreground = Some(ts.selected_fg);
                    }
                });
            });
        }

        crate::ecs::register_theme_element(id);
        id
    }
}

impl std::fmt::Debug for TabBar {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TabBar")
            .field("tabs", &self.tabs.len())
            .finish_non_exhaustive()
    }
}

// ── TabPanel ─────────────────────────────────────────────────────

/// A content panel linked to a tab by index.
pub struct TabPanel {
    index: usize,
    active: Signal<usize>,
    child: Option<Box<dyn Widget>>,
    style: StyleRefinement,
}

impl TabPanel {
    pub fn new(index: usize, active: Signal<usize>, widget: impl Widget + 'static) -> Self {
        Self {
            index,
            active,
            child: Some(Box::new(widget)),
            style: StyleRefinement::default(),
        }
    }
}

impl Styled for TabPanel {
    fn style_refinement(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

impl Widget for TabPanel {
    fn mount_box(self: Box<Self>, ctx: &mut MountContext<'_>) -> ElementId {
        let id = ctx.arena.allocate();
        let eid = id;

        {
            let Some(element) = ctx.arena.get_mut(id) else {
                return id;
            };
            element.set_layout_direction(crate::core::LayoutDirection::Vertical);

            if let Some(bg) = self.style.background {
                element.set_background(bg);
            }
            if let Some(grow) = self.style.flex_grow {
                element.set_flex_grow(grow);
            }

            let is_active = self.active.read() == self.index;
            element.slot_inactive.set(!is_active);

            let idx = self.index;
            let active_sig = self.active.clone();
            let dirty = element.dirty.clone();
            let slot = element.slot_inactive.clone();
            crate::core::signal_bridge::subscribe_owned(id, &self.active, move || {
                let visible = active_sig.read() == idx;
                if slot.get() == visible {
                    slot.set(!visible);
                    dirty.set(dirty.get() | DirtyFlags::MEASURE);
                    dirty_registry::register_dirty(eid, DirtyFlags::MEASURE);
                    dirty_registry::bump_subtree_gen(eid);
                    if let Some(pid) = dirty_registry::parent_of(eid) {
                        dirty_registry::mark_structurally_changed(pid);
                    }
                }
            });
        }

        if let Some(child) = self.child {
            let child_id = child.mount_box(&mut ctx.child_with_events(id));
            ctx.arena.add_child(id, child_id);
        }

        id
    }
}

impl std::fmt::Debug for TabPanel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TabPanel")
            .field("index", &self.index)
            .finish_non_exhaustive()
    }
}