tui_widget_list/legacy/
traits.rs

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
#![allow(deprecated)]
use ratatui::widgets::Widget;

use crate::ScrollAxis;

/// This trait should be implemented for items that are intended to be used within a `List` widget.
#[deprecated(since = "0.11.0", note = "Use ListView with ListBuilder instead.")]
pub trait PreRender: Widget {
    /// This method is called before rendering the widget.
    ///
    /// # Arguments
    ///
    /// - `self`: Captured by value, allowing modification within the pre-render hook.
    /// - `context`: Rendering context providing additional information like selection
    ///    status, cross-axis size, scroll direction and the widgets index in the list.
    ///
    /// # Returns
    ///
    /// - The (modified) widget.
    /// - The widget's main axis size, used for layouting.
    ///
    /// # Example
    ///
    ///```ignore
    /// use ratatui::prelude::*;
    /// use tui_widget_list::{PreRenderContext, PreRender};
    ///
    /// impl PreRender for MyWidget {
    ///     fn pre_render(self, context: &PreRenderContext) -> (Self, u16) {
    ///         // Modify the widget based on the selection state
    ///         if context.is_selected {
    ///             self.style = self.style.reversed();
    ///         }
    ///
    ///         // Example: set main axis size to 1
    ///         let main_axis_size = 1;
    ///
    ///         (self, main_axis_size)
    ///     }
    /// }
    /// ```
    fn pre_render(&mut self, context: &PreRenderContext) -> u16;
}

/// The context provided during rendering.
///
/// It provides a set of information that can be used from [`PreRender::pre_render`].
#[derive(Debug, Clone)]
#[deprecated(since = "0.11.0", note = "Use ListView with ListBuilder instead.")]
pub struct PreRenderContext {
    /// Indicates whether the widget is selected.
    pub is_selected: bool,

    /// The cross axis size of the widget.
    pub cross_axis_size: u16,

    /// The list's scroll axis:
    /// - `vertical` (default)
    /// - `horizontal`
    pub scroll_axis: ScrollAxis,

    /// The index of the widget in the list.
    pub index: usize,
}

impl PreRenderContext {
    pub(crate) fn new(
        is_selected: bool,
        cross_axis_size: u16,
        scroll_axis: ScrollAxis,
        index: usize,
    ) -> Self {
        Self {
            is_selected,
            cross_axis_size,
            scroll_axis,
            index,
        }
    }
}