tui: Add generic widget state via `StatefulWidget`
4f1b9658146be13813afd2f2f8d690b2fa3e808a
1 parent
9b96e276
crates/tui-core/src/lib.rs
+1 -1
| 72 | 72 | pub use primitive::{Constraint, Line, Size, StyledLine}; |
|
| 73 | 73 | pub use table::{Table, TableOptions}; |
|
| 74 | 74 | pub use terminal::Terminal; |
|
| 75 | 75 | pub use textarea::{TextArea, textarea}; |
|
| 76 | 76 | pub use vstack::{VStack, VStackOptions}; |
|
| 77 | - | pub use widget::{Block, Clear, Fill, Widget}; |
|
| 77 | + | pub use widget::{Block, Clear, Fill, StatefulWidget, Widget}; |
crates/tui-core/src/widget.rs
+47 -9
| 96 | 96 | } |
|
| 97 | 97 | out |
|
| 98 | 98 | } |
|
| 99 | 99 | } |
|
| 100 | 100 | ||
| 101 | + | /// Something that renders with caller-owned state. |
|
| 102 | + | pub trait StatefulWidget: fmt::Debug + Send { |
|
| 103 | + | /// State kept by the caller between render calls. |
|
| 104 | + | type State; |
|
| 105 | + | ||
| 106 | + | /// Returns the widget size under a parent constraint. |
|
| 107 | + | fn size(&self, parent: Constraint) -> Size; |
|
| 108 | + | ||
| 109 | + | /// Renders the widget into an area with caller-owned state. |
|
| 110 | + | fn render_with_state(&mut self, area: Rect, buf: &mut Buffer, state: &mut Self::State); |
|
| 111 | + | } |
|
| 112 | + | ||
| 101 | 113 | impl Widget for Box<dyn Widget + '_> { |
|
| 102 | 114 | fn size(&self, parent: Constraint) -> Size { |
|
| 103 | 115 | self.deref().size(parent) |
|
| 104 | 116 | } |
|
| 105 | 117 |
| 196 | 208 | /// Sets the title. |
|
| 197 | 209 | pub fn title(mut self, title: impl Into<Line>) -> Self { |
|
| 198 | 210 | self.title = Some(title.into()); |
|
| 199 | 211 | self |
|
| 200 | 212 | } |
|
| 201 | - | } |
|
| 202 | 213 | ||
| 203 | - | impl<W: Widget> Widget for Block<W> { |
|
| 204 | - | fn size(&self, parent: Constraint) -> Size { |
|
| 205 | - | let mut size = self.child.size(parent); |
|
| 214 | + | /// Grows a child size by the border, then constrains it to the parent. |
|
| 215 | + | fn outer_size(&self, mut size: Size, parent: Constraint) -> Size { |
|
| 206 | 216 | if self.border.is_some() { |
|
| 207 | 217 | size.cols = size.cols.saturating_add(2); |
|
| 208 | 218 | size.rows = size.rows.saturating_add(2); |
|
| 209 | 219 | } |
|
| 210 | 220 | size.constrain(parent) |
|
| 211 | 221 | } |
|
| 212 | 222 | ||
| 213 | - | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 223 | + | /// Draws the border and title, returning the area left for the child. |
|
| 224 | + | /// |
|
| 225 | + | /// Returns `None` when `area` is empty so the caller renders nothing. |
|
| 226 | + | fn frame(&self, area: Rect, buf: &mut Buffer) -> Option<Rect> { |
|
| 214 | 227 | if area.is_empty() { |
|
| 215 | - | return; |
|
| 228 | + | return None; |
|
| 216 | 229 | } |
|
| 217 | 230 | let Some(border) = self.border else { |
|
| 218 | - | self.child.render(area, buf); |
|
| 219 | - | return; |
|
| 231 | + | return Some(area); |
|
| 220 | 232 | }; |
|
| 221 | 233 | border.draw(area, buf); |
|
| 222 | 234 | ||
| 223 | 235 | if let Some(title) = self.title.clone() |
|
| 224 | 236 | && area.cols > 4 |
|
| 225 | 237 | { |
|
| 226 | 238 | let title = title.truncated(area.cols.saturating_sub(4), "…"); |
|
| 227 | 239 | buf.set_line(area.x + 2, area.y, &title); |
|
| 228 | 240 | } |
|
| 229 | - | self.child.render(area.inner(1), buf); |
|
| 241 | + | Some(area.inner(1)) |
|
| 242 | + | } |
|
| 243 | + | } |
|
| 244 | + | ||
| 245 | + | impl<W: Widget> Widget for Block<W> { |
|
| 246 | + | fn size(&self, parent: Constraint) -> Size { |
|
| 247 | + | self.outer_size(self.child.size(parent), parent) |
|
| 248 | + | } |
|
| 249 | + | ||
| 250 | + | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 251 | + | if let Some(inner) = self.frame(area, buf) { |
|
| 252 | + | self.child.render(inner, buf); |
|
| 253 | + | } |
|
| 254 | + | } |
|
| 255 | + | } |
|
| 256 | + | ||
| 257 | + | impl<W: StatefulWidget> StatefulWidget for Block<W> { |
|
| 258 | + | type State = W::State; |
|
| 259 | + | ||
| 260 | + | fn size(&self, parent: Constraint) -> Size { |
|
| 261 | + | self.outer_size(self.child.size(parent), parent) |
|
| 262 | + | } |
|
| 263 | + | ||
| 264 | + | fn render_with_state(&mut self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { |
|
| 265 | + | if let Some(inner) = self.frame(area, buf) { |
|
| 266 | + | self.child.render_with_state(inner, buf, state); |
|
| 267 | + | } |
|
| 230 | 268 | } |
|
| 231 | 269 | } |
|
| 232 | 270 | ||
| 233 | 271 | impl From<Paint<String>> for Fill { |
|
| 234 | 272 | fn from(paint: Paint<String>) -> Self { |
crates/tui-widgets/src/lib.rs
+1 -1
| 18 | 18 | /// Generic tree widget. |
|
| 19 | 19 | pub mod tree; |
|
| 20 | 20 | /// Scroll viewport primitives. |
|
| 21 | 21 | pub mod viewport; |
|
| 22 | 22 | ||
| 23 | - | pub use list::{List, Selection}; |
|
| 23 | + | pub use list::{List, ListState, Selection}; |
|
| 24 | 24 | pub use scrollbar::{Scrollbar, ScrollbarStyle}; |
|
| 25 | 25 | pub use scrollview::{ScrollLayout, ScrollMetrics, ScrollView, VirtualScrollView, scroll_layout}; |
|
| 26 | 26 | pub use tree::{ |
|
| 27 | 27 | Tree, TreeModel, TreeNodeKind, TreeNodeState, TreeRow, TreeState, TreeStyle, TreeVisibleRow, |
|
| 28 | 28 | }; |
crates/tui-widgets/src/list.rs
+156 -15
| 1 | 1 | //! Selectable list widget. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! `List` renders rows with a selected index, optional empty state, viewport |
|
| 4 | 4 | //! placement, and scrollbar styling. The separate [`Selection`] type owns the |
|
| 5 | 5 | //! clamped index movement so callers can keep list state outside the widget. |
|
| 6 | + | //! [`ListState`] combines selection with a persistent viewport for lists that |
|
| 7 | + | //! need manual scrolling to survive redraws. |
|
| 6 | 8 | ||
| 7 | - | use crate::{Placement, ScrollView, ScrollbarStyle, View, Viewport}; |
|
| 9 | + | use crate::{Placement, Scroll, ScrollView, ScrollbarStyle, View, Viewport}; |
|
| 8 | 10 | use rustik_tui_core as tui_core; |
|
| 9 | - | use tui_core::{Buffer, Color, Constraint, Line, Rect, Size, Style, Widget}; |
|
| 11 | + | use tui_core::{Buffer, Color, Constraint, Line, Rect, Size, StatefulWidget, Style, Widget}; |
|
| 10 | 12 | ||
| 11 | 13 | /// Default selected-row style for an active list. |
|
| 12 | 14 | pub const DEFAULT_SELECTED_STYLE_ACTIVE: Style = Style::new(Color::Unset).bg(Color::Blue); |
|
| 13 | 15 | /// Default selected-row style for an inactive list. |
|
| 14 | 16 | pub const DEFAULT_SELECTED_STYLE_INACTIVE: Style = Style::new(Color::Unset).bg(Color::Fixed(238)); |
| 74 | 76 | viewport.scroll_to_row(self.selected, len, rows, placement); |
|
| 75 | 77 | viewport.view(len, rows) |
|
| 76 | 78 | } |
|
| 77 | 79 | } |
|
| 78 | 80 | ||
| 81 | + | /// Stateful selection and viewport for a list. |
|
| 82 | + | #[derive(Debug, Clone)] |
|
| 83 | + | pub struct ListState { |
|
| 84 | + | /// Selected row state. |
|
| 85 | + | selection: Selection, |
|
| 86 | + | /// Persistent viewport used when rendering the list. |
|
| 87 | + | viewport: Viewport, |
|
| 88 | + | /// Whether the viewport should follow the selected row. |
|
| 89 | + | follow_selection: bool, |
|
| 90 | + | } |
|
| 91 | + | ||
| 92 | + | impl ListState { |
|
| 93 | + | /// Creates list state at the first row and top of the list. |
|
| 94 | + | pub const fn new() -> Self { |
|
| 95 | + | Self { |
|
| 96 | + | selection: Selection::new(), |
|
| 97 | + | viewport: Viewport::new(), |
|
| 98 | + | follow_selection: true, |
|
| 99 | + | } |
|
| 100 | + | } |
|
| 101 | + | ||
| 102 | + | /// Creates list state with a manual viewport offset. |
|
| 103 | + | pub const fn from_offset(offset: usize) -> Self { |
|
| 104 | + | Self { |
|
| 105 | + | selection: Selection::new(), |
|
| 106 | + | viewport: Viewport::from_offset(offset), |
|
| 107 | + | follow_selection: false, |
|
| 108 | + | } |
|
| 109 | + | } |
|
| 110 | + | ||
| 111 | + | /// Returns the selected row. |
|
| 112 | + | pub const fn selected(&self) -> usize { |
|
| 113 | + | self.selection.selected |
|
| 114 | + | } |
|
| 115 | + | ||
| 116 | + | /// Returns the current viewport offset. |
|
| 117 | + | pub const fn offset(&self) -> usize { |
|
| 118 | + | self.viewport.offset |
|
| 119 | + | } |
|
| 120 | + | ||
| 121 | + | /// Returns whether the viewport follows the selected row. |
|
| 122 | + | pub const fn follows_selection(&self) -> bool { |
|
| 123 | + | self.follow_selection |
|
| 124 | + | } |
|
| 125 | + | ||
| 126 | + | /// Selects a row and resumes following the selection. |
|
| 127 | + | pub fn select(&mut self, selected: usize, len: usize) -> bool { |
|
| 128 | + | self.follow_selection = true; |
|
| 129 | + | self.selection.select(selected, len) |
|
| 130 | + | } |
|
| 131 | + | ||
| 132 | + | /// Moves the selection and resumes following the selection. |
|
| 133 | + | pub fn move_by(&mut self, delta: isize, len: usize) -> bool { |
|
| 134 | + | self.follow_selection = true; |
|
| 135 | + | self.selection.move_by(delta, len) |
|
| 136 | + | } |
|
| 137 | + | ||
| 138 | + | /// Selects the first row and resumes following the selection. |
|
| 139 | + | pub fn select_first(&mut self) -> bool { |
|
| 140 | + | self.follow_selection = true; |
|
| 141 | + | self.selection.select_first() |
|
| 142 | + | } |
|
| 143 | + | ||
| 144 | + | /// Selects the last row and resumes following the selection. |
|
| 145 | + | pub fn select_last(&mut self, len: usize) -> bool { |
|
| 146 | + | self.follow_selection = true; |
|
| 147 | + | self.selection.select_last(len) |
|
| 148 | + | } |
|
| 149 | + | ||
| 150 | + | /// Scrolls the viewport manually and pauses selection following. |
|
| 151 | + | pub fn scroll_by(&mut self, rows: isize, len: usize) -> bool { |
|
| 152 | + | let changed = self.viewport.scroll(Scroll::Lines(rows), len, 1); |
|
| 153 | + | self.follow_selection = false; |
|
| 154 | + | changed |
|
| 155 | + | } |
|
| 156 | + | ||
| 157 | + | /// Returns a viewport, updating the persistent offset if needed. |
|
| 158 | + | pub fn view(&mut self, len: usize, rows: usize, placement: Placement) -> View { |
|
| 159 | + | if self.follow_selection && len > 0 { |
|
| 160 | + | self.viewport |
|
| 161 | + | .scroll_to_row(self.selection.selected, len, rows, placement); |
|
| 162 | + | } else { |
|
| 163 | + | self.viewport.clamp(len, rows); |
|
| 164 | + | } |
|
| 165 | + | self.viewport.view(len, rows) |
|
| 166 | + | } |
|
| 167 | + | } |
|
| 168 | + | ||
| 169 | + | impl Default for ListState { |
|
| 170 | + | fn default() -> Self { |
|
| 171 | + | Self::new() |
|
| 172 | + | } |
|
| 173 | + | } |
|
| 174 | + | ||
| 79 | 175 | /// A line-oriented selectable list. |
|
| 80 | 176 | #[derive(Debug, Clone)] |
|
| 81 | 177 | pub struct List { |
|
| 82 | 178 | /// Rendered rows in source order. |
|
| 83 | 179 | lines: Vec<Line>, |
| 157 | 253 | self.scrollbar = style; |
|
| 158 | 254 | self |
|
| 159 | 255 | } |
|
| 160 | 256 | ||
| 161 | 257 | /// Returns the scroll view for non-empty list rows. |
|
| 162 | - | fn scroll_view(&self, area: Rect) -> Option<ScrollView> { |
|
| 258 | + | fn scroll_view(&self, area: Rect, view: View, selected: usize) -> Option<ScrollView> { |
|
| 163 | 259 | if self.lines.is_empty() { |
|
| 164 | 260 | return None; |
|
| 165 | 261 | } |
|
| 166 | 262 | ||
| 167 | - | let selection = self.selected.map(Selection::at).unwrap_or_default(); |
|
| 168 | - | let view = if let Some(offset) = self.offset { |
|
| 169 | - | View::new(offset, self.lines.len(), area.rows) |
|
| 170 | - | } else { |
|
| 171 | - | selection.view(self.lines.len(), area.rows, self.placement) |
|
| 172 | - | }; |
|
| 173 | - | let selected = self |
|
| 174 | - | .selected |
|
| 175 | - | .map(|selected| selected.min(self.lines.len() - 1)); |
|
| 263 | + | let selected = selected.min(self.lines.len() - 1); |
|
| 176 | 264 | let selected_style = if self.active { |
|
| 177 | 265 | self.selected_style_active |
|
| 178 | 266 | } else { |
|
| 179 | 267 | self.selected_style_inactive |
|
| 180 | 268 | }; |
|
| 181 | 269 | let rows = |cols| { |
|
| 182 | 270 | self.lines |
|
| 183 | 271 | .iter() |
|
| 184 | 272 | .enumerate() |
|
| 185 | 273 | .map(|(index, line)| { |
|
| 186 | - | if selected == Some(index) { |
|
| 274 | + | if selected == index { |
|
| 187 | 275 | line.clone().style(selected_style).padded(cols) |
|
| 188 | 276 | } else { |
|
| 189 | 277 | line.clone() |
|
| 190 | 278 | } |
|
| 191 | 279 | }) |
|
| 192 | 280 | .collect() |
|
| 193 | 281 | }; |
|
| 194 | - | ||
| 195 | 282 | Some(ScrollView::new(area, view.offset, rows).scrollbar(self.scrollbar.clone())) |
|
| 196 | 283 | } |
|
| 197 | 284 | } |
|
| 198 | 285 | ||
| 199 | 286 | impl Widget for List { |
|
| 200 | 287 | fn size(&self, parent: Constraint) -> Size { |
|
| 201 | 288 | Size::new(parent.max.cols, parent.max.rows).constrain(parent) |
|
| 202 | 289 | } |
|
| 203 | 290 | ||
| 204 | 291 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 292 | + | let mut state = ListState { |
|
| 293 | + | selection: self.selected.map(Selection::at).unwrap_or_default(), |
|
| 294 | + | viewport: self.offset.map(Viewport::from_offset).unwrap_or_default(), |
|
| 295 | + | follow_selection: self.offset.is_none(), |
|
| 296 | + | }; |
|
| 297 | + | self.render_with_state(area, buf, &mut state); |
|
| 298 | + | } |
|
| 299 | + | } |
|
| 300 | + | ||
| 301 | + | impl StatefulWidget for List { |
|
| 302 | + | type State = ListState; |
|
| 303 | + | ||
| 304 | + | fn size(&self, parent: Constraint) -> Size { |
|
| 305 | + | Widget::size(self, parent) |
|
| 306 | + | } |
|
| 307 | + | ||
| 308 | + | fn render_with_state(&mut self, area: Rect, buf: &mut Buffer, state: &mut Self::State) { |
|
| 205 | 309 | if area.is_empty() { |
|
| 206 | 310 | return; |
|
| 207 | 311 | } |
|
| 208 | - | if let Some(mut scroll) = self.scroll_view(area) { |
|
| 312 | + | let view = state.view(self.lines.len(), area.rows, self.placement); |
|
| 313 | + | if let Some(mut scroll) = self.scroll_view(area, view, state.selected()) { |
|
| 209 | 314 | scroll.render(area, buf); |
|
| 210 | 315 | } else if let Some(line) = self.empty.clone() { |
|
| 211 | 316 | let x = area.x + 1.min(area.cols); |
|
| 212 | 317 | let y = area.y + 1.min(area.rows.saturating_sub(1)); |
|
| 213 | 318 | buf.set_line(x, y, &line.padded(area.right().saturating_sub(x))); |
| 231 | 336 | assert!(selection.move_by(-10, 5)); |
|
| 232 | 337 | assert_eq!(selection.selected, 0); |
|
| 233 | 338 | assert!(!selection.move_by(1, 0)); |
|
| 234 | 339 | } |
|
| 235 | 340 | ||
| 341 | + | #[test] |
|
| 342 | + | fn list_state_preserves_manual_scroll_until_selection_moves() { |
|
| 343 | + | let mut state = ListState::new(); |
|
| 344 | + | ||
| 345 | + | assert!(state.scroll_by(20, 60)); |
|
| 346 | + | assert_eq!(state.selected(), 0); |
|
| 347 | + | assert_eq!(state.view(60, 10, Placement::Minimal).offset, 20); |
|
| 348 | + | ||
| 349 | + | assert_eq!(state.view(60, 10, Placement::Minimal).offset, 20); |
|
| 350 | + | ||
| 351 | + | assert!(state.move_by(30, 60)); |
|
| 352 | + | assert_eq!(state.selected(), 30); |
|
| 353 | + | assert_eq!(state.view(60, 10, Placement::Minimal).offset, 21); |
|
| 354 | + | } |
|
| 355 | + | ||
| 356 | + | #[test] |
|
| 357 | + | fn list_state_clamps_viewport_to_visible_rows() { |
|
| 358 | + | let mut state = ListState::from_offset(20); |
|
| 359 | + | ||
| 360 | + | assert_eq!(state.view(30, 20, Placement::Minimal).offset, 10); |
|
| 361 | + | } |
|
| 362 | + | ||
| 363 | + | #[test] |
|
| 364 | + | fn list_render_with_state_updates_persistent_offset() { |
|
| 365 | + | let mut list = List::new(["zero", "one", "two", "three"].into_iter().map(Line::new)) |
|
| 366 | + | .selected_styles(Style::new(Color::Unset), Style::new(Color::Unset)); |
|
| 367 | + | let mut state = ListState::new(); |
|
| 368 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 5, 2)); |
|
| 369 | + | ||
| 370 | + | state.select_last(4); |
|
| 371 | + | list.render_with_state(buffer.area(), &mut buffer, &mut state); |
|
| 372 | + | ||
| 373 | + | assert_eq!(state.offset(), 2); |
|
| 374 | + | assert_eq!(buffer.lines(), vec!["two │", "thr…┃"]); |
|
| 375 | + | } |
|
| 376 | + | ||
| 236 | 377 | #[test] |
|
| 237 | 378 | fn list_keeps_selection_visible_and_styles_row() { |
|
| 238 | 379 | let mut list = List::new(["zero", "one", "two", "three"].into_iter().map(Line::new)) |
|
| 239 | 380 | .selected(3) |
|
| 240 | 381 | .placement(Placement::Minimal); |