tui_widget_list/legacy/traits.rs
1#![allow(deprecated)]
2use ratatui::widgets::Widget;
3
4use crate::ScrollAxis;
5
6/// This trait should be implemented for items that are intended to be used within a `List` widget.
7#[deprecated(since = "0.11.0", note = "Use ListView with ListBuilder instead.")]
8pub trait PreRender: Widget {
9 /// This method is called before rendering the widget.
10 ///
11 /// # Arguments
12 ///
13 /// - `self`: Captured by value, allowing modification within the pre-render hook.
14 /// - `context`: Rendering context providing additional information like selection
15 /// status, cross-axis size, scroll direction and the widgets index in the list.
16 ///
17 /// # Returns
18 ///
19 /// - The (modified) widget.
20 /// - The widget's main axis size, used for layouting.
21 ///
22 /// # Example
23 ///
24 ///```ignore
25 /// use ratatui::prelude::*;
26 /// use tui_widget_list::{PreRenderContext, PreRender};
27 ///
28 /// impl PreRender for MyWidget {
29 /// fn pre_render(self, context: &PreRenderContext) -> (Self, u16) {
30 /// // Modify the widget based on the selection state
31 /// if context.is_selected {
32 /// self.style = self.style.reversed();
33 /// }
34 ///
35 /// // Example: set main axis size to 1
36 /// let main_axis_size = 1;
37 ///
38 /// (self, main_axis_size)
39 /// }
40 /// }
41 /// ```
42 fn pre_render(&mut self, context: &PreRenderContext) -> u16;
43}
44
45/// The context provided during rendering.
46///
47/// It provides a set of information that can be used from [`PreRender::pre_render`].
48#[derive(Debug, Clone)]
49#[deprecated(since = "0.11.0", note = "Use ListView with ListBuilder instead.")]
50pub struct PreRenderContext {
51 /// Indicates whether the widget is selected.
52 pub is_selected: bool,
53
54 /// The cross axis size of the widget.
55 pub cross_axis_size: u16,
56
57 /// The list's scroll axis:
58 /// - `vertical` (default)
59 /// - `horizontal`
60 pub scroll_axis: ScrollAxis,
61
62 /// The index of the widget in the list.
63 pub index: usize,
64}
65
66impl PreRenderContext {
67 pub(crate) fn new(
68 is_selected: bool,
69 cross_axis_size: u16,
70 scroll_axis: ScrollAxis,
71 index: usize,
72 ) -> Self {
73 Self {
74 is_selected,
75 cross_axis_size,
76 scroll_axis,
77 index,
78 }
79 }
80}