egui_charts/ui_kit/list_item/content.rs
1//! ListItemContent trait for composable list item content
2
3use egui::{Color32, Rect, Response, Ui};
4
5use super::visuals::ListVisuals;
6
7/// Context provided to ListItemContent implementations when rendering
8pub struct ContentContext<'a> {
9 /// The row rect inset by the content padding. Content paints inside it and does not
10 /// allocate space of its own: the row is already allocated.
11 pub rect: Rect,
12 /// Background color of the list item (computed from visuals)
13 pub bg_color: Color32,
14 /// Whether the list item is selected
15 pub selected: bool,
16 /// Whether the list item is hovered
17 pub hovered: bool,
18 /// Reference to the list item for accessing configuration
19 pub list_item: &'a ListItem,
20 /// Reference to the response for the list item
21 pub response: &'a egui::Response,
22 /// Visual state with color computation methods
23 pub visuals: ListVisuals,
24}
25
26/// Trait for types that can be rendered as list item content
27///
28/// Implement this trait to create custom list item content types.
29/// The framework provides several built-in implementations:
30/// - [`LabelContent`](super::LabelContent) - Simple text with optional icon
31/// - [`PropertyContent`](super::PropertyContent) - Key-value pair display
32/// - [`ButtonContent`](super::ButtonContent) - Clickable button content with styles
33pub trait ListItemContent {
34 /// Paint the content inside `context.rect` and return the row response.
35 fn ui(self, ui: &mut Ui, context: &ContentContext<'_>) -> Response;
36
37 /// Whether the row reacts to the pointer. Disabled content returns `false`.
38 fn interactive(&self) -> bool {
39 true
40 }
41}
42
43/// Reference to ListItem configuration (forward declaration)
44pub struct ListItem {
45 /// Whether this item is selected
46 pub(crate) selected: bool,
47 /// Whether this item is interactive (clickable)
48 pub(crate) interactive: bool,
49 /// Whether to show a drag handle
50 pub(crate) draggable: bool,
51 /// Optional fixed height
52 pub(crate) height: Option<f32>,
53 /// Whether to show separator below
54 pub(crate) show_separator: bool,
55}
56
57impl Default for ListItem {
58 fn default() -> Self {
59 Self {
60 selected: false,
61 interactive: true,
62 draggable: false,
63 height: None,
64 show_separator: false,
65 }
66 }
67}
68
69impl ListItem {
70 /// Create a new list item
71 pub fn new() -> Self {
72 Self::default()
73 }
74
75 /// Set whether this item is selected
76 #[must_use]
77 pub fn selected(mut self, selected: bool) -> Self {
78 self.selected = selected;
79 self
80 }
81
82 /// Set whether this item is interactive (clickable)
83 #[must_use]
84 pub fn interactive(mut self, interactive: bool) -> Self {
85 self.interactive = interactive;
86 self
87 }
88
89 /// Set whether this item is draggable
90 #[must_use]
91 pub fn draggable(mut self, draggable: bool) -> Self {
92 self.draggable = draggable;
93 self
94 }
95
96 /// Set a fixed height for the item
97 #[must_use]
98 pub fn height(mut self, height: f32) -> Self {
99 self.height = Some(height);
100 self
101 }
102
103 /// Set whether to show a separator below this item
104 #[must_use]
105 pub fn show_separator(mut self, show: bool) -> Self {
106 self.show_separator = show;
107 self
108 }
109
110 /// Check if this item is selected
111 pub fn is_selected(&self) -> bool {
112 self.selected
113 }
114
115 /// Check if this item is interactive
116 pub fn is_interactive(&self) -> bool {
117 self.interactive
118 }
119}