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
//! Scroll view widget.

pub mod layout;
pub mod node;
pub mod reconcile;
pub(crate) mod utils;

pub(crate) use self::layout::measure_scroll_view;
pub(crate) use self::reconcile::{ScrollViewReconcile, reconcile_scroll_view};

use crate::callback::Callback;
use crate::core::element::{Element, ElementKind, Key};
use crate::style::{Align, BorderStyle, Length, Padding, ScrollbarConfig, Style};
use crate::widgets::internal::StackProps;

pub(crate) use self::node::RememberedScrollAnchor;
pub use self::node::ScrollViewNode;

pub use crate::widgets::scroll::{
    ScrollAxis, ScrollBehavior, ScrollChildExitDirection, ScrollChildVisibility, ScrollClip,
    ScrollDistanceConfig, ScrollEvent, ScrollExitedChild, ScrollKeymap, ScrollMetrics,
    ScrollRequest, ScrollTarget, ScrollViewportEvent, ScrollVisibleChild, ScrollWheelBehavior,
    ScrollWheelConfig,
};

/// A scrollable vertical container.
#[derive(Clone)]
pub struct ScrollView {
    /// Layout properties.
    pub(crate) props: StackProps,
    /// Row index scrolled from top (0 = at top).
    pub(crate) offset: Option<usize>,
    /// One-shot scroll request applied relative to the current viewport.
    pub(crate) scroll_request: Option<ScrollRequest>,
    /// Framework-owned semantic scroll target.
    pub(crate) scroll_target: Option<ScrollTarget>,
    /// How semantic scroll targets are applied.
    pub(crate) scroll_behavior: ScrollBehavior,
    /// Key bindings to move the viewport.
    pub(crate) scroll_keys: ScrollKeymap,
    /// Enable mouse wheel scrolling.
    pub(crate) scroll_wheel: bool,
    /// Widget-local mouse wheel step multiplier, overriding the app default when set.
    pub(crate) scroll_wheel_multiplier: Option<u16>,
    /// Widget-local horizontal (Shift+wheel) step multiplier. Falls back to
    /// `scroll_wheel_multiplier`, then the app default, when unset.
    pub(crate) h_scroll_wheel_multiplier: Option<u16>,
    /// How mouse wheel deltas are applied.
    pub(crate) scroll_wheel_behavior: ScrollWheelBehavior,
    /// Allow PageUp/PageDown to target this view as an ambient fallback.
    pub(crate) ambient_page_scroll: bool,
    /// Whether the scroll view can receive focus.
    pub(crate) focusable: bool,
    /// Callback fired when the scroll offset changes.
    pub(crate) on_scroll: Option<Callback<ScrollEvent>>,
    /// Callback fired when the scrollbar is dragged/clicked.
    pub(crate) on_scroll_to: Option<Callback<usize>>,
    /// Callback fired when visible children or viewport metadata changes.
    pub(crate) on_viewport_change: Option<Callback<ScrollViewportEvent>>,
    /// Draw a vertical scrollbar when content overflows.
    pub(crate) scrollbar: bool,
    /// Scrollbar configuration.
    pub(crate) scrollbar_config: ScrollbarConfig,
    /// Show scroll indicators when content is clipped.
    pub(crate) show_scroll_indicators: bool,
    pub(crate) scroll_indicator_style: Style,
    pub(crate) clip_mode: ScrollClip,
    /// Hint for initial estimated height of unmeasured off-screen children.
    /// Only used as the cold-start fallback before a running average of
    /// measured children is available.
    pub(crate) estimated_child_height: u16,
    /// Stable key used for persisting scroll anchor state across remounts.
    /// When set, this key is used instead of the element key for storing
    /// and restoring remembered scroll anchors and bottom-pinning state.
    /// Useful when the element key must change (e.g. to force cache rebuild)
    /// but scroll position should be preserved.
    pub(crate) scroll_state_key: Option<Key>,
    /// Scroll axes enabled for this view.
    pub(crate) axis: ScrollAxis,
    /// Draw a horizontal scrollbar when content overflows horizontally.
    pub(crate) h_scrollbar: bool,
    /// Horizontal scrollbar configuration.
    pub(crate) h_scrollbar_config: ScrollbarConfig,
    /// Children.
    pub(crate) children: Vec<Element>,
}

impl Default for ScrollView {
    fn default() -> Self {
        Self {
            props: StackProps::default(),
            offset: None,
            scroll_request: None,
            scroll_target: None,
            scroll_behavior: ScrollBehavior::default(),
            scroll_keys: ScrollKeymap::default(),
            scroll_wheel: true,
            scroll_wheel_multiplier: None,
            h_scroll_wheel_multiplier: None,
            scroll_wheel_behavior: ScrollWheelBehavior::default(),
            ambient_page_scroll: false,
            focusable: false,
            on_scroll: None,
            on_scroll_to: None,
            on_viewport_change: None,
            scrollbar: false,
            scrollbar_config: ScrollbarConfig::default(),
            show_scroll_indicators: false,
            scroll_indicator_style: Style::default(),
            clip_mode: ScrollClip::default(),
            estimated_child_height: 3,
            scroll_state_key: None,
            axis: ScrollAxis::default(),
            h_scrollbar: false,
            h_scrollbar_config: ScrollbarConfig::default(),
            children: Vec::new(),
        }
    }
}

impl ScrollView {
    /// Create an empty scroll view.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a child.
    pub fn child(mut self, child: impl Into<Element>) -> Self {
        self.children.push(child.into());
        self
    }

    /// Replace children.
    pub fn children(mut self, children: impl IntoIterator<Item = Element>) -> Self {
        self.children = children.into_iter().collect();
        self
    }

    /// Set border.
    pub fn border(mut self, border: bool) -> Self {
        self.props.border = border;
        self
    }

    /// Set border style.
    pub fn border_style(mut self, border_style: BorderStyle) -> Self {
        self.props.border_style = border_style;
        self
    }

    /// Set padding.
    pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
        self.props.padding = padding.into();
        self
    }

    /// Set base style.
    pub fn style(mut self, style: Style) -> Self {
        self.props.style = style;
        self
    }

    /// Set gap.
    pub fn gap(mut self, gap: u16) -> Self {
        self.props.gap = gap;
        self
    }

    /// Set requested width (cross-axis for a vertical `ScrollView`).
    pub fn width(mut self, width: Length) -> Self {
        self.props.width = width;
        self
    }

    /// Set requested height (main-axis for a vertical `ScrollView`).
    pub fn height(mut self, height: Length) -> Self {
        self.props.height = height;
        self
    }

    /// Set cross-axis alignment.
    pub fn align(mut self, align: Align) -> Self {
        self.props.align = align;
        self
    }

    /// Set scroll offset (row index from top).
    pub fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Apply a one-shot scroll request relative to the current viewport.
    ///
    /// This is useful for command-driven navigation such as page up/down or
    /// jump-to-top/bottom without continuously controlling the settled offset.
    /// When set, it takes priority over `.offset(...)` but not over
    /// `.scroll_to_key(...)`.
    pub fn scroll_request(mut self, request: ScrollRequest) -> Self {
        self.scroll_request = Some(request);
        self
    }

    /// Scroll to a semantic target.
    ///
    /// Edge targets (`Top` / `Bottom`) resolve against the current content
    /// extent and do not require sentinel children. Key targets preserve the
    /// same behavior as [`Self::scroll_to_key`]. Target navigation uses
    /// [`Self::scroll_behavior`].
    pub fn scroll_to(mut self, target: ScrollTarget) -> Self {
        self.scroll_target = Some(target);
        self
    }

    /// Scroll to the start of the content.
    pub fn scroll_to_top(self) -> Self {
        self.scroll_to(ScrollTarget::Top)
    }

    /// Scroll to the end of the content.
    pub fn scroll_to_bottom(self) -> Self {
        self.scroll_to(ScrollTarget::Bottom)
    }

    /// Scroll so the first child subtree containing `key` is brought into view.
    ///
    /// This is useful for jump-to-result flows, such as scrolling a message list
    /// to a matched entry after search. When set, it takes priority over
    /// `.offset(...)`.
    pub fn scroll_to_key(mut self, key: impl Into<Key>) -> Self {
        self.scroll_target = Some(ScrollTarget::Key(key.into()));
        self
    }

    /// Scroll to `offset` rows below the first child subtree containing `key`.
    ///
    /// This is useful when a keyed row contains a large auto-height child and
    /// navigation needs to land inside that row, for example one auto-height
    /// `DiffView` per file with global hunk navigation.
    pub fn scroll_to_key_offset(mut self, key: impl Into<Key>, offset: usize) -> Self {
        self.scroll_target = Some(ScrollTarget::key_offset(key, offset));
        self
    }

    /// Configure how semantic target navigation is applied.
    ///
    /// This affects only framework-owned targets from `.scroll_to(...)`,
    /// `.scroll_to_key(...)`, `.scroll_to_top()`, and `.scroll_to_bottom()`;
    /// requests, controlled offsets, and user input remain immediate.
    pub fn scroll_behavior(mut self, behavior: ScrollBehavior) -> Self {
        self.scroll_behavior = behavior;
        self
    }

    /// Animate semantic target navigation with `config`.
    pub fn scroll_transition(mut self, config: crate::animation::TransitionConfig) -> Self {
        self.scroll_behavior = ScrollBehavior::smooth(config);
        self
    }

    /// Set a stable key for persisting scroll anchor state across remounts.
    ///
    /// When the element key must change (e.g. to force a layout cache rebuild
    /// after toggling child visibility), the scroll position is normally lost
    /// because the remembered anchor is stored under the old key. Setting a
    /// stable `scroll_state_key` ensures the anchor survives key changes.
    pub fn scroll_state_key(mut self, key: impl Into<Key>) -> Self {
        self.scroll_state_key = Some(key.into());
        self
    }

    /// Configure which keys move the viewport.
    pub fn scroll_keys(mut self, keys: ScrollKeymap) -> Self {
        self.scroll_keys = keys;
        if keys != ScrollKeymap::NONE {
            self.focusable = true;
        }
        self
    }

    /// Enable mouse wheel scrolling.
    pub fn scroll_wheel(mut self, enabled: bool) -> Self {
        self.scroll_wheel = enabled;
        self
    }

    /// Override the app-wide mouse wheel step multiplier for this scroll view.
    pub fn scroll_wheel_multiplier(mut self, multiplier: u16) -> Self {
        self.scroll_wheel_multiplier = Some(multiplier.max(1));
        self
    }

    /// Override the step multiplier for *horizontal* wheel panning (Shift+wheel).
    ///
    /// Horizontal scrolling moves in columns, which are finer-grained than the
    /// rows used for vertical scrolling, so a larger horizontal step usually
    /// feels better for wide content. Falls back to
    /// [`Self::scroll_wheel_multiplier`], then the app-wide multiplier.
    pub fn h_scroll_wheel_multiplier(mut self, multiplier: u16) -> Self {
        self.h_scroll_wheel_multiplier = Some(multiplier.max(1));
        self
    }

    /// Configure how mouse wheel input is applied.
    pub fn scroll_wheel_behavior(mut self, behavior: ScrollWheelBehavior) -> Self {
        self.scroll_wheel_behavior = behavior;
        self
    }

    /// Enable or disable smooth inertial wheel scrolling with default physics.
    pub fn smooth_wheel_scroll(mut self, enabled: bool) -> Self {
        self.scroll_wheel_behavior = if enabled {
            ScrollWheelBehavior::smooth_default()
        } else {
            ScrollWheelBehavior::Immediate
        };
        self
    }

    /// Enable smooth wheel scrolling and set the wheel acceleration impulse.
    pub fn scroll_acceleration(mut self, acceleration: f32) -> Self {
        let mut config = match self.scroll_wheel_behavior {
            ScrollWheelBehavior::Immediate => ScrollWheelConfig::default(),
            ScrollWheelBehavior::Smooth(config) => config,
        };
        config.acceleration = acceleration;
        self.scroll_wheel_behavior = ScrollWheelBehavior::Smooth(config);
        self
    }

    /// Allow PageUp/PageDown to target this scroll view even when it is not focused.
    ///
    /// This is an explicit fallback used only when normal focused-widget dispatch
    /// and component `on_key` bubbling do not handle the page key.
    pub fn ambient_page_scroll(mut self, enabled: bool) -> Self {
        self.ambient_page_scroll = enabled;
        self
    }

    /// Allow the scroll view to receive focus.
    pub fn focusable(mut self, focusable: bool) -> Self {
        self.focusable = focusable;
        self
    }

    /// Callback fired on mouse wheel scrolling.
    pub fn on_scroll(mut self, cb: Callback<ScrollEvent>) -> Self {
        self.on_scroll = Some(cb);
        self
    }

    /// Callback fired on scrollbar interaction (drag/click).
    pub fn on_scroll_to(mut self, cb: Callback<usize>) -> Self {
        self.on_scroll_to = Some(cb);
        self
    }

    /// Callback fired when visible children or viewport metadata changes.
    pub fn on_viewport_change(mut self, cb: Callback<ScrollViewportEvent>) -> Self {
        self.on_viewport_change = Some(cb);
        self
    }

    /// Draw a vertical scrollbar.
    pub fn scrollbar(mut self, scrollbar: bool) -> Self {
        self.scrollbar = scrollbar;
        self
    }

    /// Set scrollbar configuration.
    pub fn scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
        self.scrollbar_config = config;
        self
    }

    /// Enable "N more" scroll indicators when items are hidden.
    pub fn show_scroll_indicators(mut self, show: bool) -> Self {
        self.show_scroll_indicators = show;
        self
    }

    /// Set style for scroll indicators.
    pub fn scroll_indicator_style(mut self, style: Style) -> Self {
        self.scroll_indicator_style = style;
        self
    }

    /// Control how children are clipped against the viewport.
    pub fn clip_mode(mut self, clip_mode: ScrollClip) -> Self {
        self.clip_mode = clip_mode;
        self
    }

    /// Hint for the initial estimated height of unmeasured off-screen children.
    ///
    /// Only used as the cold-start fallback before a running average of
    /// measured children is available. Default: `3`.
    pub fn estimated_child_height(mut self, height: u16) -> Self {
        self.estimated_child_height = height;
        self
    }

    /// Configure which scroll axes are active.
    ///
    /// Default is [`ScrollAxis::Vertical`] (historical behavior). Use
    /// [`ScrollAxis::Both`] to enable horizontal panning for content wider than
    /// the viewport.
    pub fn axis(mut self, axis: ScrollAxis) -> Self {
        self.axis = axis;
        self
    }

    /// Draw a horizontal scrollbar when content overflows horizontally.
    ///
    /// Only effective when the axis includes horizontal scrolling.
    pub fn h_scrollbar(mut self, h_scrollbar: bool) -> Self {
        self.h_scrollbar = h_scrollbar;
        self
    }

    /// Set horizontal scrollbar configuration.
    pub fn h_scrollbar_config(mut self, config: ScrollbarConfig) -> Self {
        self.h_scrollbar_config = config;
        self
    }
}

impl From<ScrollView> for Element {
    fn from(value: ScrollView) -> Self {
        Element::new(ElementKind::ScrollView(Box::new(value)))
    }
}

impl crate::layout::hash::LayoutHash for ScrollView {
    fn layout_hash(
        &self,
        hasher: &mut impl std::hash::Hasher,
        recurse: &dyn Fn(&Element) -> Option<u64>,
    ) -> Option<()> {
        use std::hash::Hash;

        crate::layout::hash::hash_stack_props(&self.props, hasher);
        self.scrollbar.hash(hasher);
        self.scrollbar_config.variant.hash(hasher);
        self.scrollbar_config.gap.hash(hasher);
        self.show_scroll_indicators.hash(hasher);
        self.axis.hash(hasher);
        self.h_scrollbar.hash(hasher);
        self.h_scrollbar_config.variant.hash(hasher);
        self.h_scrollbar_config.gap.hash(hasher);
        crate::layout::hash::hash_children(&self.children, hasher, recurse)
    }
}