tui-widgets: Add `Overlay` widget
58a3409f51721f0e79ffbb1cb0dbf98f9395ea49
1 parent
4f1b9658
crates/tui-widgets/src/lib.rs
+5 -2
| 1 | 1 | //! Higher-level widgets built on the core `rustik_tui_core` crate. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! This crate contains stateful and composite widgets that depend on the core |
|
| 4 | - | //! rendering primitives from `rustik_tui_core`: selectable lists, vertical scrolling, and |
|
| 5 | - | //! scrollbar rendering. Keeping them separate leaves the core crate focused on |
|
| 4 | + | //! rendering primitives from `rustik_tui_core`: selectable lists, overlays, |
|
| 5 | + | //! vertical scrolling, and scrollbar rendering. Keeping them separate leaves the core crate focused on |
|
| 6 | 6 | //! buffers, styled text, layout, app/runtime traits, and small layout widgets. |
|
| 7 | 7 | //! |
|
| 8 | 8 | #![deny(unsafe_code)] |
|
| 9 | 9 | #![warn(missing_docs)] |
|
| 10 | 10 | #![warn(clippy::unwrap_used)] |
|
| 11 | 11 | ||
| 12 | 12 | /// Selectable list widget. |
|
| 13 | 13 | pub mod list; |
|
| 14 | + | /// Centered overlay widget. |
|
| 15 | + | pub mod overlay; |
|
| 14 | 16 | /// Scrollbar rendering. |
|
| 15 | 17 | pub mod scrollbar; |
|
| 16 | 18 | /// Scrollable line widget. |
|
| 17 | 19 | pub mod scrollview; |
|
| 18 | 20 | /// Generic tree widget. |
|
| 19 | 21 | pub mod tree; |
|
| 20 | 22 | /// Scroll viewport primitives. |
|
| 21 | 23 | pub mod viewport; |
|
| 22 | 24 | ||
| 23 | 25 | pub use list::{List, ListState, Selection}; |
|
| 26 | + | pub use overlay::{Overlay, overlay_area}; |
|
| 24 | 27 | pub use scrollbar::{Scrollbar, ScrollbarStyle}; |
|
| 25 | 28 | pub use scrollview::{ScrollLayout, ScrollMetrics, ScrollView, VirtualScrollView, scroll_layout}; |
|
| 26 | 29 | pub use tree::{ |
|
| 27 | 30 | Tree, TreeModel, TreeNodeKind, TreeNodeState, TreeRow, TreeState, TreeStyle, TreeVisibleRow, |
|
| 28 | 31 | }; |
crates/tui-widgets/src/overlay.rs
added
+121 -0
| 1 | + | //! Centered overlay widget. |
|
| 2 | + | ||
| 3 | + | use rustik_tui_core as tui_core; |
|
| 4 | + | use tui_core::{Buffer, Cell, Constraint, Rect, Size, Widget}; |
|
| 5 | + | ||
| 6 | + | /// Centered child widget rendered over a filled panel area. |
|
| 7 | + | #[derive(Debug, Clone)] |
|
| 8 | + | pub struct Overlay<W> { |
|
| 9 | + | /// Child rendered inside the overlay panel. |
|
| 10 | + | child: W, |
|
| 11 | + | /// Largest panel size. |
|
| 12 | + | max_size: Size, |
|
| 13 | + | /// Minimum parent-space margin kept around the panel. |
|
| 14 | + | margin: Size, |
|
| 15 | + | /// Cell used to fill the panel before rendering the child. |
|
| 16 | + | background: Cell, |
|
| 17 | + | } |
|
| 18 | + | ||
| 19 | + | impl<W> Overlay<W> { |
|
| 20 | + | /// Creates an overlay around a child widget. |
|
| 21 | + | pub fn new(child: W) -> Self { |
|
| 22 | + | Self { |
|
| 23 | + | child, |
|
| 24 | + | max_size: Size::MAX, |
|
| 25 | + | margin: Size::MIN, |
|
| 26 | + | background: Cell::blank(), |
|
| 27 | + | } |
|
| 28 | + | } |
|
| 29 | + | ||
| 30 | + | /// Sets the largest panel size. |
|
| 31 | + | pub const fn max_size(mut self, size: Size) -> Self { |
|
| 32 | + | self.max_size = size; |
|
| 33 | + | self |
|
| 34 | + | } |
|
| 35 | + | ||
| 36 | + | /// Sets the minimum parent-space margin around the panel. |
|
| 37 | + | pub const fn margin(mut self, margin: Size) -> Self { |
|
| 38 | + | self.margin = margin; |
|
| 39 | + | self |
|
| 40 | + | } |
|
| 41 | + | ||
| 42 | + | /// Sets the cell used to fill the panel. |
|
| 43 | + | pub fn background(mut self, cell: Cell) -> Self { |
|
| 44 | + | self.background = cell; |
|
| 45 | + | self |
|
| 46 | + | } |
|
| 47 | + | ||
| 48 | + | /// Returns the panel area inside a parent area. |
|
| 49 | + | pub fn area(&self, area: Rect) -> Rect { |
|
| 50 | + | overlay_area(area, self.max_size, self.margin) |
|
| 51 | + | } |
|
| 52 | + | } |
|
| 53 | + | ||
| 54 | + | impl<W: Widget> Widget for Overlay<W> { |
|
| 55 | + | fn size(&self, parent: Constraint) -> Size { |
|
| 56 | + | parent.bounded_size() |
|
| 57 | + | } |
|
| 58 | + | ||
| 59 | + | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
|
| 60 | + | let area = self.area(area); |
|
| 61 | + | ||
| 62 | + | if area.is_empty() { |
|
| 63 | + | return; |
|
| 64 | + | } |
|
| 65 | + | ||
| 66 | + | buf.fill(area, self.background.clone()); |
|
| 67 | + | self.child.render(area, buf); |
|
| 68 | + | } |
|
| 69 | + | } |
|
| 70 | + | ||
| 71 | + | /// Returns a centered overlay panel area inside `area`. |
|
| 72 | + | pub fn overlay_area(area: Rect, max_size: Size, margin: Size) -> Rect { |
|
| 73 | + | let available_cols = area.cols.saturating_sub(margin.cols.saturating_mul(2)); |
|
| 74 | + | let available_rows = area.rows.saturating_sub(margin.rows.saturating_mul(2)); |
|
| 75 | + | let cols = max_size.cols.min(available_cols); |
|
| 76 | + | let rows = max_size.rows.min(available_rows); |
|
| 77 | + | ||
| 78 | + | Rect::new( |
|
| 79 | + | area.x + area.cols.saturating_sub(cols) / 2, |
|
| 80 | + | area.y + area.rows.saturating_sub(rows) / 2, |
|
| 81 | + | cols, |
|
| 82 | + | rows, |
|
| 83 | + | ) |
|
| 84 | + | } |
|
| 85 | + | ||
| 86 | + | #[cfg(test)] |
|
| 87 | + | mod tests { |
|
| 88 | + | use super::*; |
|
| 89 | + | use rustik_tui_core::{Buffer, Cell, Color, Constraint, Label, Rect, Size, Style, Widget}; |
|
| 90 | + | ||
| 91 | + | #[test] |
|
| 92 | + | fn overlay_area_centers_with_max_size_and_margin() { |
|
| 93 | + | let area = overlay_area(Rect::new(0, 0, 120, 40), Size::new(96, 18), Size::new(4, 4)); |
|
| 94 | + | ||
| 95 | + | assert_eq!(area, Rect::new(12, 11, 96, 18)); |
|
| 96 | + | } |
|
| 97 | + | ||
| 98 | + | #[test] |
|
| 99 | + | fn overlay_fills_background_and_renders_child() { |
|
| 100 | + | let background = Cell::new('.', Style::new(Color::Blue)); |
|
| 101 | + | let mut overlay = Overlay::new(Label::new("hi")) |
|
| 102 | + | .max_size(Size::new(6, 3)) |
|
| 103 | + | .margin(Size::new(2, 1)) |
|
| 104 | + | .background(background); |
|
| 105 | + | let mut buffer = Buffer::empty(Rect::new(0, 0, 10, 5)); |
|
| 106 | + | ||
| 107 | + | overlay.render(buffer.area(), &mut buffer); |
|
| 108 | + | ||
| 109 | + | assert_eq!(overlay.size(Constraint::max(buffer.size())), buffer.size()); |
|
| 110 | + | assert_eq!( |
|
| 111 | + | buffer.lines(), |
|
| 112 | + | vec![ |
|
| 113 | + | " ", |
|
| 114 | + | " hi.... ", |
|
| 115 | + | " ...... ", |
|
| 116 | + | " ...... ", |
|
| 117 | + | " " |
|
| 118 | + | ] |
|
| 119 | + | ); |
|
| 120 | + | } |
|
| 121 | + | } |