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
use std::cell::RefCell;

use ratatui::text::Text as RText;
use ratatui::widgets::{Block, Borders, Clear, Paragraph};

use crate::backend::ratatui_backend::common::{
    DEFAULT_SCROLLBAR_THUMB, IndicatorDirection, IntegratedScrollbarAppearance,
    ScrollbarAppearance, ScrollbarScrollState, calculate_visible_borders,
    integrated_vscrollbar_track_char, render_hscrollbar, render_integrated_scrollbar_with_metrics,
    render_integrated_vscrollbar_half_block, render_vscrollbar_half_block,
    render_vscrollbar_with_metrics, resolve_scrollbar_thumb_style, scroll_indicator_line,
    style_paints_bg, to_ratatui_border_set, to_ratatui_border_type, to_ratatui_rect,
    to_ratatui_style_with_terminal_bg,
};
use crate::backend::ratatui_backend::render::{
    FrameIntegratedVTrack, RenderState, ancestor_frame_integrated_vtrack,
};
use crate::style::resolve::{resolve_base_style, resolve_scrollbar_theme};
use crate::style::{Color, Rect, ScrollbarVariant, Style};
use crate::utils::scrollbar::ScrollbarMetricsCache;
use crate::widgets::internal::StackProps;
use crate::widgets::scroll_view_scrollbar_metrics;

pub(crate) struct ScrollViewRenderCtx<'a> {
    pub offset: usize,
    pub scroll_offset: usize,
    pub content_height: u16,
    pub scrollbar: bool,
    pub scrollbar_variant: ScrollbarVariant,
    pub scrollbar_gap: u16,
    pub scrollbar_thumb: Option<char>,
    pub scrollbar_thumb_style: Option<Style>,
    pub scrollbar_thumb_focus_style: Option<Style>,
    pub scrollbar_track_style: Option<Style>,
    pub reserve_bottom_rows: u16,
    pub is_focused: bool,
    pub show_scroll_indicators: bool,
    pub scroll_indicator_style: Style,
    pub top_indicator: bool,
    pub bottom_indicator: bool,
    pub bottom_count: usize,
    pub parent_integrated_v: Option<FrameIntegratedVTrack>,
    pub borders: Borders,
    pub clip_rect: Option<Rect>,
    pub metrics_cache: Option<&'a RefCell<ScrollbarMetricsCache>>,
    pub terminal_bg: Option<Color>,
}

pub(crate) fn render_scroll_view(
    f: &mut ratatui::Frame<'_>,
    props: &StackProps,
    rect: Rect,
    rrect: ratatui::layout::Rect,
    ctx: ScrollViewRenderCtx<'_>,
) {
    let ScrollViewRenderCtx {
        offset,
        scroll_offset,
        content_height,
        scrollbar,
        scrollbar_variant,
        scrollbar_gap,
        scrollbar_thumb,
        scrollbar_thumb_style,
        scrollbar_thumb_focus_style,
        scrollbar_track_style,
        reserve_bottom_rows,
        is_focused,
        show_scroll_indicators,
        scroll_indicator_style,
        top_indicator,
        bottom_indicator,
        bottom_count,
        parent_integrated_v,
        borders,
        clip_rect,
        metrics_cache: _metrics_cache,
        terminal_bg,
    } = ctx;
    let to_ratatui_style = |style| to_ratatui_style_with_terminal_bg(style, terminal_bg);
    let needs_bg = style_paints_bg(props.style);
    let base_style = props.style;
    let clip_rrect = clip_rect.map(to_ratatui_rect);

    if needs_bg || props.border {
        if needs_bg {
            f.render_widget(Clear, rrect);
        }

        let mut block = Block::default().style(to_ratatui_style(base_style));
        if props.border {
            block = block
                .borders(borders)
                .border_type(to_ratatui_border_type(props.border_style));

            if let Some(set) = to_ratatui_border_set(props.border_style) {
                block = block.border_set(set);
            }
        }

        f.render_widget(block, rrect);
    }

    let mut inner = rect;
    if props.border {
        if borders.contains(Borders::LEFT) {
            inner.x = inner.x.saturating_add(1);
            inner.w = inner.w.saturating_sub(1);
        }
        if borders.contains(Borders::RIGHT) {
            inner.w = inner.w.saturating_sub(1);
        }
        if borders.contains(Borders::TOP) {
            inner.y = inner.y.saturating_add(1);
            inner.h = inner.h.saturating_sub(1);
        }
        if borders.contains(Borders::BOTTOM) {
            inner.h = inner.h.saturating_sub(1);
        }
    }
    inner = inner.inset(props.padding);
    if inner.w == 0 || inner.h == 0 {
        return;
    }

    // Reserve the standalone horizontal scrollbar row so the vertical scrollbar
    // (and bottom indicator) stop above it, leaving a clean corner.
    inner.h = inner.h.saturating_sub(reserve_bottom_rows);
    if inner.h == 0 {
        return;
    }

    // Determine scrollbar mode
    let use_integrated = (props.border || parent_integrated_v.is_some())
        && matches!(scrollbar_variant, ScrollbarVariant::Integrated);
    let use_standalone = scrollbar && !use_integrated;

    // If standalone scrollbar, reserve 1 column
    let mut content_inner = inner;
    if use_standalone && content_inner.w > 0 {
        content_inner.w = content_inner
            .w
            .saturating_sub(1u16.saturating_add(scrollbar_gap));
    }

    // Determine reserved rows for indicators (if show_scroll_indicators is on)
    if show_scroll_indicators {
        if top_indicator {
            let indicator_line = scroll_indicator_line(
                offset,
                IndicatorDirection::Top,
                base_style,
                scroll_indicator_style,
            );
            let text = RText::from(vec![indicator_line]);
            let indicator_rect = to_ratatui_rect(Rect {
                x: content_inner.x,
                y: content_inner.y,
                w: content_inner.w,
                h: 1,
            });
            let effective_indicator = indicator_rect.intersection(rrect);
            if effective_indicator.area() > 0 {
                let p = Paragraph::new(text);
                let dx = (effective_indicator.x as i32)
                    .saturating_sub(indicator_rect.x as i32)
                    .max(0) as u16;
                let dy = (effective_indicator.y as i32)
                    .saturating_sub(indicator_rect.y as i32)
                    .max(0) as u16;
                let p = p.scroll((dy, dx));
                f.render_widget(p, effective_indicator);
            }
        }

        if bottom_indicator && bottom_count > 0 {
            let indicator_line = scroll_indicator_line(
                bottom_count,
                IndicatorDirection::Bottom,
                base_style,
                scroll_indicator_style,
            );
            let text = RText::from(vec![indicator_line]);
            let indicator_rect = to_ratatui_rect(Rect {
                x: content_inner.x,
                y: content_inner
                    .y
                    .saturating_add(content_inner.h.saturating_sub(1) as i16),
                w: content_inner.w,
                h: 1,
            });
            let effective_indicator = indicator_rect.intersection(rrect);
            if effective_indicator.area() > 0 {
                let p = Paragraph::new(text);
                let dx = (effective_indicator.x as i32)
                    .saturating_sub(indicator_rect.x as i32)
                    .max(0) as u16;
                let dy = (effective_indicator.y as i32)
                    .saturating_sub(indicator_rect.y as i32)
                    .max(0) as u16;
                let p = p.scroll((dy, dx));
                f.render_widget(p, effective_indicator);
            }
        }
    }

    if scrollbar && inner.w > 0 && inner.h > 0 && content_height > 0 {
        let total = content_height as usize;
        // The thumb size and scroll range are based on the full content viewport
        // (matching reconcile's `max_offset`); only the physical track is one row
        // shorter for the reserved horizontal scrollbar row. Reducing the viewport
        // here instead would make the fully-scrolled thumb stop half a cell short.
        let viewport_h = inner.h.saturating_add(reserve_bottom_rows) as usize;
        let thumb = scrollbar_thumb.unwrap_or(DEFAULT_SCROLLBAR_THUMB);
        let use_half = thumb == DEFAULT_SCROLLBAR_THUMB;
        let Some(metrics) = scroll_view_scrollbar_metrics(
            scroll_offset,
            total,
            viewport_h,
            inner.h as usize,
            show_scroll_indicators,
            use_half,
        ) else {
            return;
        };

        if use_integrated {
            let border_x = parent_integrated_v
                .map(|p| p.track_x)
                .unwrap_or_else(|| rect.x.saturating_add(rect.w.saturating_sub(1) as i16));
            let sb_rect = Rect {
                x: border_x,
                y: inner.y,
                w: 1,
                h: inner.h,
            };
            if sb_rect.h > 0 {
                let thumb_style = resolve_scrollbar_thumb_style(
                    is_focused,
                    scrollbar_thumb_style,
                    scrollbar_thumb_focus_style,
                );

                let b_style = parent_integrated_v
                    .map(|p| p.border_style_fallback)
                    .unwrap_or(props.border_style);
                let track_glyph = parent_integrated_v.and_then(|p| p.track_glyph);
                let mut track_scratch = [0u8; 4];
                let border_char =
                    integrated_vscrollbar_track_char(track_glyph, b_style, &mut track_scratch);
                let integrated_base_style = parent_integrated_v
                    .map(|p| p.track_style)
                    .unwrap_or(base_style);
                if use_half {
                    render_integrated_vscrollbar_half_block(
                        f,
                        sb_rect,
                        metrics,
                        IntegratedScrollbarAppearance {
                            thumb_char: thumb,
                            border_char,
                            base_style: integrated_base_style,
                            thumb_style,
                            track_style: scrollbar_track_style,
                            clip_rect: None,
                            metrics_cache: None,
                        },
                    );
                } else {
                    render_integrated_scrollbar_with_metrics(
                        f,
                        sb_rect,
                        metrics,
                        IntegratedScrollbarAppearance {
                            thumb_char: thumb,
                            border_char,
                            base_style: integrated_base_style,
                            thumb_style,
                            track_style: scrollbar_track_style,
                            clip_rect: None,
                            metrics_cache: None,
                        },
                    );
                }
            }
        } else {
            // Standalone
            let sb_rect = Rect {
                x: inner.x.saturating_add(inner.w.saturating_sub(1) as i16),
                y: inner.y,
                w: 1,
                h: inner.h,
            };
            let thumb_style = resolve_scrollbar_thumb_style(
                is_focused,
                scrollbar_thumb_style,
                scrollbar_thumb_focus_style,
            );
            if use_half {
                render_vscrollbar_half_block(
                    f,
                    sb_rect,
                    metrics,
                    thumb_style,
                    scrollbar_track_style,
                    clip_rrect,
                );
            } else {
                render_vscrollbar_with_metrics(
                    f,
                    sb_rect,
                    metrics,
                    thumb,
                    thumb_style,
                    scrollbar_track_style,
                    clip_rrect,
                );
            }
        }
    }
}

pub(crate) fn render_scroll_view_node(
    state: &mut RenderState<'_, '_, '_>,
    node: &crate::core::node::Node,
    scroll_view: &crate::widgets::internal::ScrollViewNode,
    rect: Rect,
    rrect: ratatui::layout::Rect,
    clip_bounds: Option<Rect>,
) -> Rect {
    let is_focused = state.focus_chain.contains(&node.id);
    let parent_integrated_v = if !scroll_view.props.border
        && scroll_view.scrollbar
        && matches!(scroll_view.scrollbar_variant, ScrollbarVariant::Integrated)
    {
        ancestor_frame_integrated_vtrack(state, node.parent)
    } else {
        None
    };

    let borders = if scroll_view.props.border {
        calculate_visible_borders(rect, clip_bounds)
    } else {
        Borders::empty()
    };

    let mut props = scroll_view.props.clone();
    props.style = resolve_base_style(node.active_theme(), props.style);
    let scroll_indicator_style = crate::style::resolve::resolve_muted_style(
        node.active_theme(),
        scroll_view.scroll_indicator_style,
    );
    let (scrollbar_thumb_style, scrollbar_thumb_focus_style, scrollbar_track_style) =
        resolve_scrollbar_theme(
            node.active_theme(),
            scroll_view.scrollbar_thumb_style,
            scroll_view.scrollbar_thumb_focus_style,
            scroll_view.scrollbar_track_style,
        );

    // A standalone horizontal scrollbar reserves the bottom content row; reserve
    // the same row for the vertical scrollbar so the two don't share a cell.
    let h_integrated = scroll_view.props.border
        && scroll_view.h_scrollbar
        && matches!(
            scroll_view.h_scrollbar_variant,
            ScrollbarVariant::Integrated
        );
    let reserve_bottom_rows =
        if scroll_view.h_scrollbar && scroll_view.h_max_offset > 0 && !h_integrated {
            1u16.saturating_add(scroll_view.h_scrollbar_gap)
        } else {
            0
        };

    let scrollbar_cache = &state.ctx.scrollbar_metrics_cache;
    let f = &mut *state.f;
    render_scroll_view(
        f,
        &props,
        rect,
        rrect,
        ScrollViewRenderCtx {
            offset: scroll_view.offset,
            scroll_offset: scroll_view.scroll_offset as usize,
            content_height: scroll_view.content_height,
            scrollbar: scroll_view.scrollbar,
            scrollbar_variant: scroll_view.scrollbar_variant,
            scrollbar_gap: scroll_view.scrollbar_gap,
            scrollbar_thumb: scroll_view.scrollbar_thumb,
            scrollbar_thumb_style,
            scrollbar_thumb_focus_style,
            scrollbar_track_style,
            reserve_bottom_rows,
            is_focused,
            show_scroll_indicators: scroll_view.show_scroll_indicators,
            scroll_indicator_style,
            top_indicator: scroll_view.top_indicator,
            bottom_indicator: scroll_view.bottom_indicator,
            bottom_count: scroll_view.bottom_count,
            parent_integrated_v,
            borders,
            clip_rect: clip_bounds,
            metrics_cache: Some(scrollbar_cache),
            terminal_bg: state
                .ctx
                .terminal_bg
                .map(crate::backend::ratatui_backend::common::from_ratatui_color),
        },
    );

    let mut inner = rect.inner(scroll_view.props.border, scroll_view.props.padding);
    if scroll_view.show_scroll_indicators {
        if scroll_view.top_indicator {
            inner.y = inner.y.saturating_add(1);
            inner.h = inner.h.saturating_sub(1);
        }
        if scroll_view.bottom_indicator {
            inner.h = inner.h.saturating_sub(1);
        }
    }
    let use_integrated = (scroll_view.props.border || parent_integrated_v.is_some())
        && matches!(scroll_view.scrollbar_variant, ScrollbarVariant::Integrated);
    let use_standalone = scroll_view.scrollbar && !use_integrated;
    if use_standalone && inner.w > 0 {
        inner.w = inner
            .w
            .saturating_sub(1u16.saturating_add(scroll_view.scrollbar_gap));
    }

    let h_standalone = scroll_view.h_scrollbar && scroll_view.h_max_offset > 0 && !h_integrated;
    if h_standalone && inner.h > 0 {
        inner.h = inner
            .h
            .saturating_sub(1u16.saturating_add(scroll_view.h_scrollbar_gap));
    }

    if scroll_view.h_scrollbar && scroll_view.h_max_offset > 0 {
        let (h_thumb_style, h_thumb_focus_style, h_track_style) = resolve_scrollbar_theme(
            node.active_theme(),
            scroll_view.h_scrollbar_thumb_style,
            scroll_view.h_scrollbar_thumb_focus_style,
            scroll_view.h_scrollbar_track_style,
        );
        let h_thumb = scroll_view
            .h_scrollbar_thumb
            .unwrap_or(DEFAULT_SCROLLBAR_THUMB);
        let h_thumb_style =
            resolve_scrollbar_thumb_style(is_focused, h_thumb_style, h_thumb_focus_style);
        let viewport_cols = inner.w;
        let sb_rect = if h_integrated {
            Rect {
                x: inner.x,
                y: rect.y.saturating_add(rect.h.saturating_sub(1) as i16),
                w: inner.w,
                h: 1,
            }
        } else {
            Rect {
                x: inner.x,
                y: inner.y.saturating_add(inner.h as i16),
                w: inner.w,
                h: 1,
            }
        };
        render_hscrollbar(
            f,
            sb_rect,
            ScrollbarScrollState {
                offset: scroll_view.h_offset,
                visible: viewport_cols as usize,
                total: scroll_view.content_width as usize,
            },
            ScrollbarAppearance {
                thumb_char: h_thumb,
                thumb_style: h_thumb_style,
                track_style: h_track_style,
                clip_rect: clip_bounds.map(to_ratatui_rect),
                metrics_cache: Some(scrollbar_cache),
            },
        );
    }

    clip_bounds.map(|c| inner.intersection(&c)).unwrap_or(inner)
}