tui-lipan 0.1.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
use super::overlay::reconcile_portal;
use super::state::{
    ReconcileCtx, SingleChildReconcile, reconcile_single_child_required, resolve_rect_with_auto,
    reuse_or_replace_kind,
};
use crate::core::element::{Element, ElementKind};
use crate::core::node::{NodeId, NodeKind};
use crate::layout::tag::can_reuse;
#[cfg(feature = "big-text")]
use crate::widgets::internal::reconcile_big_text;
#[cfg(feature = "image")]
use crate::widgets::internal::reconcile_image;
#[cfg(feature = "terminal")]
use crate::widgets::internal::reconcile_terminal;
use crate::widgets::internal::{
    CanvasReconcile, DividerReconcile, GridReconcile, HStackReconcile, PanViewReconcile,
    ScrollViewReconcile, SparklineNode, SplitterReconcile, StatusBarLayoutReconcile,
    VStackReconcile, measure_input, reconcile_animated, reconcile_ascii_canvas, reconcile_button,
    reconcile_canvas, reconcile_center, reconcile_center_pin, reconcile_chart, reconcile_checkbox,
    reconcile_class_diagram, reconcile_divider, reconcile_document_view, reconcile_drag_source,
    reconcile_draggable_tab_bar, reconcile_drop_target, reconcile_effect_scope,
    reconcile_er_diagram, reconcile_flow, reconcile_flowchart, reconcile_frame,
    reconcile_gantt_diagram, reconcile_graph, reconcile_grid, reconcile_hex_area, reconcile_hstack,
    reconcile_list, reconcile_mouse_region, reconcile_pan_view, reconcile_progress_bar,
    reconcile_scroll_view, reconcile_slider, reconcile_spacer, reconcile_spinner,
    reconcile_state_diagram, reconcile_status_bar_layout, reconcile_tabs, reconcile_text,
    reconcile_text_area, reconcile_vstack, reconcile_zstack,
};

pub(crate) struct ElementReconcile<'a> {
    pub reuse: Option<NodeId>,
    pub parent: Option<NodeId>,
    pub el: &'a Element,
    pub rect: crate::style::Rect,
}

pub(crate) fn reconcile_element(ctx: &mut ReconcileCtx<'_>, args: ElementReconcile<'_>) -> NodeId {
    let ElementReconcile {
        reuse,
        parent,
        el,
        rect,
    } = args;
    let epoch = ctx.epoch;
    let focus = ctx.focus;
    // ThemeProvider scopes the active theme for every realized descendant.
    if let ElementKind::ThemeProvider(tp) = &el.kind {
        ctx.tree.push_active_theme(tp.theme.clone());
        let id = reconcile_element(
            ctx,
            ElementReconcile {
                reuse,
                parent,
                el: &tp.child,
                rect,
            },
        );
        ctx.tree.pop_active_theme();
        return id;
    }
    if let ElementKind::ContextProvider(cp) = &el.kind {
        return reconcile_element(
            ctx,
            ElementReconcile {
                reuse,
                parent,
                el: &cp.child,
                rect,
            },
        );
    }
    if let ElementKind::Memo(_) = &el.kind {
        if cfg!(debug_assertions) {
            panic!("memo elements must be expanded before layout");
        }

        let id = match reuse {
            Some(id) if ctx.tree.is_valid(id) => id,
            _ => ctx.tree.alloc(),
        };
        let active_theme = ctx.tree.current_active_theme();
        let node = ctx.tree.node_mut(id);
        node.epoch = epoch;
        node.key = el.key.clone();
        node.parent = parent;
        node.rect = rect;
        node.set_active_theme(active_theme);
        node.children.clear();
        node.kind = NodeKind::Text(crate::widgets::internal::TextNode::from(
            crate::widgets::Text::new("ERROR: Unexpanded memo"),
        ));
        ctx.tree.note_kind_set(id);
        return id;
    }

    let id = match reuse {
        Some(id) if ctx.tree.is_valid(id) && can_reuse(ctx.tree.node(id), el) => id,
        _ => ctx.tree.alloc(),
    };

    {
        let active_theme = ctx.tree.current_active_theme();
        let node = ctx.tree.node_mut(id);
        node.epoch = epoch;
        node.key = el.key.clone();
        node.parent = parent;
        node.set_active_theme(active_theme);
    }

    let result_id = match &el.kind {
        ElementKind::Text(text) => reconcile_text(ctx.tree, id, text, rect, &el.layout),
        #[cfg(feature = "big-text")]
        ElementKind::BigText(big_text) => {
            reconcile_big_text(ctx.tree, id, big_text, rect, &el.layout)
        }
        ElementKind::AsciiCanvas(canvas) => {
            reconcile_ascii_canvas(ctx.tree, id, canvas, rect, &el.layout)
        }
        ElementKind::Button(button) => reconcile_button(ctx.tree, id, button, rect, &el.layout),
        #[cfg(feature = "image")]
        ElementKind::Image(image) => reconcile_image(ctx.tree, id, image, rect, &el.layout),

        ElementKind::Input(input) => {
            let (w, h) = measure_input(input);
            let rect = resolve_rect_with_auto(rect, &el.layout, input.width, input.height, w, h);

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();
            // Trivial payload: replace each reconcile.
            reuse_or_replace_kind(
                &mut node.kind,
                |_existing| false,
                || NodeKind::from((**input).clone()),
            );

            id
        }
        ElementKind::HexArea(hex_area) => {
            reconcile_hex_area(ctx.tree, id, hex_area, rect, &el.layout)
        }
        ElementKind::TextArea(text_area) => {
            reconcile_text_area(ctx.tree, id, text_area, rect, &el.layout)
        }
        #[cfg(feature = "terminal")]
        ElementKind::Terminal(terminal) => {
            reconcile_terminal(ctx.tree, id, terminal, rect, &el.layout)
        }
        ElementKind::Popover(popover) => crate::widgets::internal::reconcile_popover(
            ctx.tree,
            epoch,
            id,
            popover,
            rect,
            focus,
            ctx.overlay_state,
        ),
        ElementKind::Portal(portal) => reconcile_portal(
            ctx.tree,
            epoch,
            id,
            portal,
            &el.layout,
            focus,
            ctx.overlay_state,
        ),
        ElementKind::List(list) => reconcile_list(ctx.tree, id, list, rect),
        ElementKind::Table(table) => {
            crate::widgets::internal::reconcile_table(ctx.tree, id, table, rect)
        }
        ElementKind::Tabs(tabs) => reconcile_tabs(ctx.tree, id, tabs, rect, &el.layout),
        ElementKind::DraggableTabBar(bar) => reconcile_draggable_tab_bar(ctx.tree, id, bar, rect),
        ElementKind::Component(_component) => {
            if cfg!(debug_assertions) {
                panic!("component elements must be expanded before layout");
            }

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();
            node.kind = NodeKind::Text(crate::widgets::internal::TextNode::from(
                crate::widgets::Text::new("ERROR: Unexpanded component"),
            ));

            id
        }
        ElementKind::Group(group) => {
            let old_children = {
                let node = ctx.tree.node_mut(id);
                node.rect = rect;
                node.kind = NodeKind::Group(crate::core::node::GroupNode { scope: group.scope });
                std::mem::take(&mut node.children)
            };
            debug_assert!(
                old_children.len() <= 1,
                "group must contain at most one reusable child"
            );

            let new_children = reconcile_single_child_required(
                ctx,
                SingleChildReconcile {
                    parent_id: id,
                    child: Some(group.child.as_ref()),
                    rect,
                    old_children,
                },
            );

            let node = ctx.tree.node_mut(id);
            node.children = new_children;
            debug_assert!(
                node.children.len() == 1,
                "group must contain exactly one reconciled child"
            );

            id
        }
        ElementKind::EffectScope(scope) => {
            reconcile_effect_scope(ctx.tree, epoch, id, scope, rect, focus, ctx.overlay_state)
        }
        ElementKind::Animated(animated) => reconcile_animated(
            ctx.tree,
            epoch,
            id,
            animated,
            rect,
            focus,
            ctx.overlay_state,
        ),
        ElementKind::DragSource(source) => {
            reconcile_drag_source(ctx.tree, epoch, id, source, rect, focus, ctx.overlay_state)
        }
        ElementKind::DropTarget(target) => {
            reconcile_drop_target(ctx.tree, epoch, id, target, rect, focus, ctx.overlay_state)
        }
        ElementKind::MouseRegion(region) => {
            reconcile_mouse_region(ctx.tree, epoch, id, region, rect, focus, ctx.overlay_state)
        }
        ElementKind::ScrollView(sv) => {
            let anchor_key = sv.scroll_state_key.clone().or_else(|| el.key.clone());
            reconcile_scroll_view(
                ctx,
                ScrollViewReconcile {
                    id,
                    sv,
                    scroll_key: anchor_key,
                    rect,
                },
            )
        }
        ElementKind::PanView(pan) => {
            let pan_key = pan.pan_state_key.clone().or_else(|| el.key.clone());
            reconcile_pan_view(
                ctx,
                PanViewReconcile {
                    id,
                    pan,
                    pan_key,
                    rect,
                    constraints: &el.layout,
                },
            )
        }
        ElementKind::Divider(div) => reconcile_divider(
            ctx,
            DividerReconcile {
                id,
                divider: div,
                rect,
                constraints: &el.layout,
            },
        ),
        ElementKind::Spacer(spacer) => reconcile_spacer(ctx.tree, id, spacer, rect, &el.layout),
        ElementKind::VStack(vs) => {
            let old_children = {
                let node = ctx.tree.node_mut(id);
                node.rect = rect;
                if let NodeKind::VStack(existing) = &mut node.kind {
                    // Update element-derived fields in place;
                    // layout_cache and last_focused_key are preserved automatically.
                    existing.props = vs.props.clone();
                    existing.tab_titles = vs.tab_titles.clone();
                    existing.active_tab = vs.active_tab;
                    existing.on_tab_change = vs.on_tab_change.clone();
                    existing.active_tab_style = vs.active_tab_style;
                    existing.inactive_tab_style = vs.inactive_tab_style;
                    existing.tab_variant = vs.tab_variant;
                    existing.title_prefix = vs.title_prefix.clone();
                } else {
                    node.kind = NodeKind::VStack(crate::widgets::internal::StackNode {
                        props: vs.props.clone(),
                        tab_titles: vs.tab_titles.clone(),
                        active_tab: vs.active_tab,
                        on_tab_change: vs.on_tab_change.clone(),
                        active_tab_style: vs.active_tab_style,
                        inactive_tab_style: vs.inactive_tab_style,
                        tab_variant: vs.tab_variant,
                        title_prefix: vs.title_prefix.clone(),
                        layout_cache: None,
                        last_focused_key: None,
                    });
                }
                std::mem::take(&mut node.children)
            };

            let inner = rect.inner(vs.props.border, vs.props.padding);
            let new_children = reconcile_vstack(
                ctx,
                VStackReconcile {
                    parent: id,
                    old_children,
                    vs,
                    bounds: inner,
                },
            );

            let node = ctx.tree.node_mut(id);
            node.children = new_children;

            id
        }
        ElementKind::HStack(hs) => {
            let old_children = {
                let node = ctx.tree.node_mut(id);
                node.rect = rect;
                if let NodeKind::HStack(existing) = &mut node.kind {
                    // Update only the element-derived field in place;
                    // layout_cache, last_focused_key, and tab fields are preserved.
                    existing.props = hs.props.clone();
                } else {
                    node.kind = NodeKind::HStack(crate::widgets::internal::StackNode {
                        props: hs.props.clone(),
                        tab_titles: Vec::new(),
                        active_tab: 0,
                        on_tab_change: None,
                        active_tab_style: crate::style::StyleSlot::Inherit,
                        inactive_tab_style: crate::style::Style::default(),
                        tab_variant: crate::widgets::TabVariant::default(),
                        title_prefix: None,
                        layout_cache: None,
                        last_focused_key: None,
                    });
                }
                std::mem::take(&mut node.children)
            };

            let inner = rect.inner(hs.props.border, hs.props.padding);
            let new_children = reconcile_hstack(
                ctx,
                HStackReconcile {
                    parent: id,
                    old_children,
                    hs,
                    bounds: inner,
                },
            );

            let node = ctx.tree.node_mut(id);
            node.children = new_children;

            id
        }
        ElementKind::Grid(grid) => {
            let old_children = {
                let node = ctx.tree.node_mut(id);
                node.rect = rect;
                if let NodeKind::Grid(existing) = &mut node.kind {
                    existing.props = grid.props.clone();
                } else {
                    node.kind = NodeKind::Grid(crate::widgets::internal::GridNode {
                        props: grid.props.clone(),
                        layout_cache: None,
                    });
                }
                std::mem::take(&mut node.children)
            };

            let inner = rect.inner(grid.props.border, grid.props.padding);
            let new_children = reconcile_grid(
                ctx,
                GridReconcile {
                    parent: id,
                    old_children,
                    grid,
                    bounds: inner,
                },
            );

            let node = ctx.tree.node_mut(id);
            node.children = new_children;

            id
        }
        ElementKind::Flow(flow) => reconcile_flow(ctx, id, flow, rect),
        ElementKind::Canvas(canvas) => reconcile_canvas(
            ctx,
            CanvasReconcile {
                id,
                canvas,
                rect,
                constraints: &el.layout,
            },
        ),
        ElementKind::Splitter(splitter) => {
            let old_children = {
                let node = ctx.tree.node_mut(id);
                node.rect = rect;
                std::mem::take(&mut node.children)
            };

            let new_children = crate::widgets::internal::reconcile_splitter(
                ctx,
                SplitterReconcile {
                    parent: id,
                    old_children,
                    splitter,
                    bounds: rect,
                    constraints: &el.layout,
                },
            );

            let node = ctx.tree.node_mut(id);
            node.children = new_children;

            id
        }
        ElementKind::ZStack(zs) => {
            reconcile_zstack(ctx.tree, id, zs, rect, focus, ctx.overlay_state, epoch)
        }
        ElementKind::Center(center) => {
            reconcile_center(ctx.tree, id, center, rect, focus, ctx.overlay_state, epoch)
        }
        ElementKind::CenterPin(cp) => {
            reconcile_center_pin(ctx.tree, id, cp, rect, focus, ctx.overlay_state, epoch)
        }
        ElementKind::Frame(frame) => {
            reconcile_frame(ctx.tree, epoch, id, frame, rect, focus, ctx.overlay_state)
        }
        ElementKind::StatusBarLayout(layout) => reconcile_status_bar_layout(
            ctx,
            StatusBarLayoutReconcile {
                id,
                layout,
                rect,
                constraints: &el.layout,
            },
        ),
        ElementKind::Checkbox(checkbox) => {
            reconcile_checkbox(ctx.tree, id, checkbox, rect, &el.layout)
        }
        ElementKind::ProgressBar(progress) => {
            reconcile_progress_bar(ctx.tree, id, progress, rect, &el.layout)
        }
        ElementKind::Spinner(spinner) => reconcile_spinner(ctx.tree, id, spinner, rect),
        ElementKind::Slider(slider) => reconcile_slider(ctx.tree, id, slider, rect, &el.layout),
        ElementKind::Sparkline(sparkline) => {
            let (w, h) = crate::widgets::internal::measure_sparkline(sparkline);
            let rect =
                resolve_rect_with_auto(rect, &el.layout, sparkline.width, sparkline.height, w, h);
            let alloc_w = Some(rect.w);

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();

            // Cache-heavy payload: prefer in-place reconcile to preserve render caches.
            reuse_or_replace_kind(
                &mut node.kind,
                |existing| {
                    if let NodeKind::Sparkline(existing_node) = existing {
                        crate::widgets::internal::reconcile_sparkline(
                            sparkline,
                            existing_node,
                            alloc_w,
                        );
                        true
                    } else {
                        false
                    }
                },
                || {
                    let mut n = SparklineNode::default();
                    crate::widgets::internal::reconcile_sparkline(sparkline, &mut n, alloc_w);
                    NodeKind::Sparkline(n)
                },
            );

            id
        }
        ElementKind::Chart(chart) => {
            let (w, h) = crate::widgets::internal::measure_chart(chart);
            let rect = resolve_rect_with_auto(rect, &el.layout, chart.width, chart.height, w, h);

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();

            reuse_or_replace_kind(
                &mut node.kind,
                |existing| {
                    if let NodeKind::Chart(existing_node) = existing {
                        reconcile_chart(chart, existing_node);
                        true
                    } else {
                        false
                    }
                },
                || NodeKind::from((**chart).clone()),
            );

            id
        }
        ElementKind::Graph(graph) => {
            let (w, h) = crate::widgets::internal::measure_graph(graph);
            let rect = resolve_rect_with_auto(rect, &el.layout, graph.width, graph.height, w, h);

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();

            reuse_or_replace_kind(
                &mut node.kind,
                |existing| {
                    if let NodeKind::Graph(existing_node) = existing {
                        reconcile_graph(graph, existing_node);
                        true
                    } else {
                        false
                    }
                },
                || NodeKind::from((**graph).clone()),
            );

            id
        }
        ElementKind::SequenceDiagram(sequence) => {
            let (w, h) = crate::widgets::internal::measure_sequence_diagram(sequence);
            let rect =
                resolve_rect_with_auto(rect, &el.layout, sequence.width, sequence.height, w, h);

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();

            reuse_or_replace_kind(
                &mut node.kind,
                |existing| {
                    if let NodeKind::SequenceDiagram(existing_node) = existing {
                        let content_width = rect.inner(sequence.border, sequence.padding).w;
                        crate::widgets::internal::reconcile_sequence_diagram_with_width(
                            sequence,
                            existing_node,
                            Some(content_width),
                        );
                        true
                    } else {
                        false
                    }
                },
                || {
                    let mut node = crate::widgets::internal::SequenceDiagramNode::default();
                    let content_width = rect.inner(sequence.border, sequence.padding).w;
                    crate::widgets::internal::reconcile_sequence_diagram_with_width(
                        sequence,
                        &mut node,
                        Some(content_width),
                    );
                    NodeKind::SequenceDiagram(node)
                },
            );

            id
        }
        ElementKind::Flowchart(flowchart) => {
            let (w, h) = crate::widgets::internal::measure_flowchart(flowchart);
            let rect =
                resolve_rect_with_auto(rect, &el.layout, flowchart.width, flowchart.height, w, h);

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();

            reuse_or_replace_kind(
                &mut node.kind,
                |existing| {
                    if let NodeKind::Flowchart(existing_node) = existing {
                        reconcile_flowchart(flowchart, existing_node);
                        true
                    } else {
                        false
                    }
                },
                || NodeKind::from((**flowchart).clone()),
            );

            id
        }
        ElementKind::ClassDiagram(diagram) => {
            reconcile_class_diagram(ctx.tree, id, diagram, rect, &el.layout)
        }
        ElementKind::StateDiagram(diagram) => {
            reconcile_state_diagram(ctx.tree, id, diagram, rect, &el.layout)
        }
        ElementKind::ErDiagram(diagram) => {
            reconcile_er_diagram(ctx.tree, id, diagram, rect, &el.layout)
        }
        ElementKind::GanttDiagram(diagram) => {
            reconcile_gantt_diagram(ctx.tree, id, diagram, rect, &el.layout)
        }
        ElementKind::Heatmap(heatmap) => {
            let (w, h) = crate::widgets::internal::measure_heatmap(heatmap);
            let rect =
                resolve_rect_with_auto(rect, &el.layout, heatmap.width, heatmap.height, w, h);

            let node = ctx.tree.node_mut(id);
            node.rect = rect;
            node.children.clear();

            reuse_or_replace_kind(
                &mut node.kind,
                |existing| {
                    if let NodeKind::Heatmap(existing_node) = existing {
                        crate::widgets::internal::reconcile_heatmap_node(heatmap, existing_node);
                        true
                    } else {
                        false
                    }
                },
                || NodeKind::from(heatmap.clone()),
            );

            id
        }
        ElementKind::DocumentView(dv) => {
            reconcile_document_view(ctx.tree, id, parent, dv, rect, &el.layout)
        }
        ElementKind::ThemeProvider(_) => {
            // Handled by early return above; unreachable.
            unreachable!("ThemeProvider should be handled before node allocation")
        }
        ElementKind::ContextProvider(_) => {
            unreachable!("ContextProvider should be handled before node allocation")
        }
        ElementKind::Memo(_) => unreachable!("Memo should be handled before node allocation"),
    };

    // Incrementally track hover/mouse/animation capabilities instead of a
    // post-reconciliation full-tree DFS (refresh_hoverables).
    ctx.tree.note_kind_set(result_id);

    result_id
}