.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
benches/
src/
action_bar.rs
4.7 KiB
badge.rs
2.6 KiB
dialog.rs
15.5 KiB
input.rs
13.3 KiB
lib.rs
1.6 KiB
list.rs
13.1 KiB
overlay.rs
3.4 KiB
panel.rs
18.6 KiB
scrollbar.rs
5.9 KiB
scrollview.rs
13.2 KiB
tree.rs
27.9 KiB
viewport.rs
15.7 KiB
tests/
Cargo.toml
408 B
README.md
294 B
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/tui-widgets/src/action_bar.rs
raw
| 1 | //! Generic action metadata and one-line action hint bar. |
| 2 | |
| 3 | use crate::Badge; |
| 4 | use rustik_tui_core as tui_core; |
| 5 | use tui_core::{Buffer, Constraint, Label, Line, Rect, Size, Style, Widget}; |
| 6 | |
| 7 | /// UI metadata for one action. |
| 8 | #[derive(Debug, Clone)] |
| 9 | pub struct ActionItem { |
| 10 | /// Primary action label. |
| 11 | name: Label, |
| 12 | /// Optional key or gesture label. |
| 13 | key: Option<Label>, |
| 14 | } |
| 15 | |
| 16 | impl ActionItem { |
| 17 | /// Creates an action item. |
| 18 | pub fn new(name: impl Into<Label>) -> Self { |
| 19 | Self { |
| 20 | name: name.into(), |
| 21 | key: None, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// Sets the key or gesture label. |
| 26 | pub fn key(mut self, key: impl Into<Label>) -> Self { |
| 27 | self.key = Some(key.into()); |
| 28 | self |
| 29 | } |
| 30 | |
| 31 | /// Returns the primary action label. |
| 32 | pub const fn name(&self) -> &Label { |
| 33 | &self.name |
| 34 | } |
| 35 | |
| 36 | /// Returns the optional key or gesture label. |
| 37 | pub const fn key_label(&self) -> Option<&Label> { |
| 38 | self.key.as_ref() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// Ordered list of action metadata. |
| 43 | #[derive(Debug, Clone, Default)] |
| 44 | pub struct ActionList { |
| 45 | /// Action items in render order. |
| 46 | items: Vec<ActionItem>, |
| 47 | } |
| 48 | |
| 49 | impl ActionList { |
| 50 | /// Creates an empty action list. |
| 51 | pub fn new() -> Self { |
| 52 | Self::default() |
| 53 | } |
| 54 | |
| 55 | /// Adds an item. |
| 56 | pub fn item(mut self, item: ActionItem) -> Self { |
| 57 | self.items.push(item); |
| 58 | self |
| 59 | } |
| 60 | |
| 61 | /// Adds an item with a key label and primary action label. |
| 62 | pub fn action(self, key: impl Into<Label>, name: impl Into<Label>) -> Self { |
| 63 | self.item(ActionItem::new(name).key(key)) |
| 64 | } |
| 65 | |
| 66 | /// Returns the action items. |
| 67 | pub fn items(&self) -> &[ActionItem] { |
| 68 | &self.items |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// One-line bar of key badges and action labels. |
| 73 | #[derive(Debug, Clone)] |
| 74 | pub struct ActionBar { |
| 75 | /// Action metadata rendered from left to right. |
| 76 | actions: ActionList, |
| 77 | /// Style applied to badge caps. |
| 78 | cap_style: Style, |
| 79 | /// Style applied to badge text. |
| 80 | key_style: Style, |
| 81 | /// Style applied to action labels. |
| 82 | action_style: Style, |
| 83 | } |
| 84 | |
| 85 | impl ActionBar { |
| 86 | /// Creates an action bar from action metadata. |
| 87 | pub fn new(actions: ActionList) -> Self { |
| 88 | Self { |
| 89 | actions, |
| 90 | cap_style: Style::default(), |
| 91 | key_style: Style::default(), |
| 92 | action_style: Style::default(), |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// Sets the badge cap style. |
| 97 | pub const fn cap_style(mut self, style: Style) -> Self { |
| 98 | self.cap_style = style; |
| 99 | self |
| 100 | } |
| 101 | |
| 102 | /// Sets the badge text style. |
| 103 | pub const fn key_style(mut self, style: Style) -> Self { |
| 104 | self.key_style = style; |
| 105 | self |
| 106 | } |
| 107 | |
| 108 | /// Sets the action label style. |
| 109 | pub const fn action_style(mut self, style: Style) -> Self { |
| 110 | self.action_style = style; |
| 111 | self |
| 112 | } |
| 113 | |
| 114 | /// Returns the action bar as a line. |
| 115 | pub fn line(&self) -> Line { |
| 116 | let mut line = Line::default(); |
| 117 | |
| 118 | for action in self.actions.items() { |
| 119 | let Some(key) = action.key_label() else { |
| 120 | continue; |
| 121 | }; |
| 122 | line = line |
| 123 | .extend( |
| 124 | Badge::new(key.clone()) |
| 125 | .cap_style(self.cap_style) |
| 126 | .style(self.key_style) |
| 127 | .line(), |
| 128 | ) |
| 129 | .item(Label::new(" ")) |
| 130 | .item(action.name().clone().style(self.action_style)) |
| 131 | .item(Label::new(" ")); |
| 132 | } |
| 133 | line |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | impl Widget for ActionBar { |
| 138 | fn size(&self, _parent: Constraint) -> Size { |
| 139 | Size::new(self.line().width(), 1) |
| 140 | } |
| 141 | |
| 142 | fn render(&mut self, area: Rect, buf: &mut Buffer) { |
| 143 | self.line().render(area, buf); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | #[cfg(test)] |
| 148 | mod tests { |
| 149 | use super::*; |
| 150 | use tui_core::Color; |
| 151 | |
| 152 | #[test] |
| 153 | fn action_bar_renders_badged_actions() { |
| 154 | let actions = ActionList::new().action("j/k", "move").action("q", "quit"); |
| 155 | let bar = ActionBar::new(actions) |
| 156 | .cap_style(Style::new(Color::Blue)) |
| 157 | .key_style(Style::new(Color::White).bg(Color::Blue).bold()) |
| 158 | .action_style(Style::new(Color::Fixed(244))); |
| 159 | |
| 160 | assert_eq!(bar.line().to_string(), "j/k move q quit "); |
| 161 | assert_eq!(bar.size(Constraint::UNBOUNDED), Size::new(22, 1)); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn empty_action_bar_is_blank() { |
| 166 | let bar = ActionBar::new(ActionList::new()); |
| 167 | |
| 168 | assert_eq!(bar.line().to_string(), ""); |
| 169 | assert_eq!(bar.size(Constraint::UNBOUNDED), Size::new(0, 1)); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn action_list_items_carry_bar_metadata() { |
| 174 | let actions = ActionList::new().action("t", "Track"); |
| 175 | let item = &actions.items()[0]; |
| 176 | |
| 177 | assert_eq!(item.name().to_string(), "Track"); |
| 178 | assert_eq!(item.key_label().unwrap().to_string(), "t"); |
| 179 | } |
| 180 | } |