| 1 | //! Native work-dock tab fitting and painting. The host supplies availability, |
| 2 | //! counts, actual close copy and interaction state; this owns no focus policy. |
| 3 | |
| 4 | use super::{WorkbarPanel, WorkbarTab}; |
| 5 | use ratatui::{ |
| 6 | buffer::Buffer, |
| 7 | layout::Rect, |
| 8 | style::Style, |
| 9 | text::{Line, Span}, |
| 10 | widgets::{Paragraph, Widget}, |
| 11 | }; |
| 12 | use std::borrow::Cow; |
| 13 | |
| 14 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 15 | pub enum DockTabTarget { |
| 16 | Panel(WorkbarPanel), |
| 17 | Close, |
| 18 | } |
| 19 | |
| 20 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 21 | pub struct DockTabStyles { |
| 22 | pub idle: Style, |
| 23 | pub active: Style, |
| 24 | pub hovered: Style, |
| 25 | pub close: Style, |
| 26 | pub close_hovered: Style, |
| 27 | } |
| 28 | |
| 29 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 30 | pub struct DockTabEntry { |
| 31 | pub tab: WorkbarTab, |
| 32 | pub area: Rect, |
| 33 | pub label: String, |
| 34 | pub style: Style, |
| 35 | } |
| 36 | |
| 37 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 38 | pub struct DockTabPlan { |
| 39 | pub tabs: Vec<DockTabEntry>, |
| 40 | pub close_area: Rect, |
| 41 | pub close: String, |
| 42 | close_style: Style, |
| 43 | } |
| 44 | |
| 45 | impl DockTabPlan { |
| 46 | #[must_use] |
| 47 | pub fn hitboxes(&self) -> Vec<(DockTabTarget, Rect)> { |
| 48 | let mut boxes = self |
| 49 | .tabs |
| 50 | .iter() |
| 51 | .filter(|tab| !tab.area.is_empty()) |
| 52 | .map(|tab| (DockTabTarget::Panel(tab.tab.panel), tab.area)) |
| 53 | .collect::<Vec<_>>(); |
| 54 | if !self.close_area.is_empty() { |
| 55 | boxes.push((DockTabTarget::Close, self.close_area)); |
| 56 | } |
| 57 | boxes |
| 58 | } |
| 59 | |
| 60 | /// Paint this exact plan; hitboxes and painted cells share one snapshot. |
| 61 | pub fn paint(&self, buf: &mut Buffer) { |
| 62 | for tab in &self.tabs { |
| 63 | Paragraph::new(Line::from(Span::styled(tab.label.as_str(), tab.style))) |
| 64 | .render(tab.area, buf); |
| 65 | } |
| 66 | Paragraph::new(Line::from(Span::styled( |
| 67 | self.close.clone(), |
| 68 | self.close_style, |
| 69 | ))) |
| 70 | .render(self.close_area, buf); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// One native row. All styles are supplied by the live host theme, including |
| 75 | /// backgrounds and modifiers. Close copy describes the host's actual action. |
| 76 | #[derive(Clone, Debug)] |
| 77 | pub struct DockTabRow<'a> { |
| 78 | pub tabs: &'a [WorkbarTab], |
| 79 | pub active: WorkbarPanel, |
| 80 | pub bottom: bool, |
| 81 | pub close: Cow<'a, str>, |
| 82 | pub hovered: Option<DockTabTarget>, |
| 83 | pub pressed: Option<WorkbarPanel>, |
| 84 | pub styles: DockTabStyles, |
| 85 | } |
| 86 | |
| 87 | impl DockTabRow<'_> { |
| 88 | #[must_use] |
| 89 | pub fn plan(&self, area: Rect) -> DockTabPlan { |
| 90 | let width = usize::from(area.width); |
| 91 | let safe_close = crate::text::display_safe(self.close.as_ref()).into_owned(); |
| 92 | let close_width = |
| 93 | unicode_width::UnicodeWidthStr::width(safe_close.as_str()).min(width) as u16; |
| 94 | let y = if self.bottom { |
| 95 | area.y |
| 96 | .saturating_add(1) |
| 97 | .min(area.bottom().saturating_sub(1)) |
| 98 | } else { |
| 99 | area.y |
| 100 | }; |
| 101 | let close_area = Rect::new( |
| 102 | area.right().saturating_sub(close_width), |
| 103 | y, |
| 104 | close_width, |
| 105 | u16::from(!area.is_empty()), |
| 106 | ); |
| 107 | let mut plan = DockTabPlan { |
| 108 | tabs: Vec::new(), |
| 109 | close_area, |
| 110 | close: safe_close, |
| 111 | close_style: if self.hovered == Some(DockTabTarget::Close) { |
| 112 | self.styles.close_hovered |
| 113 | } else { |
| 114 | self.styles.close |
| 115 | }, |
| 116 | }; |
| 117 | if area.is_empty() { |
| 118 | return plan; |
| 119 | } |
| 120 | let mut entries = WorkbarPanel::ORDER |
| 121 | .into_iter() |
| 122 | .filter_map(|panel| self.tabs.iter().find(|tab| tab.panel == panel).cloned()) |
| 123 | .collect::<Vec<_>>(); |
| 124 | if !entries.iter().any(|tab| tab.panel == self.active) { |
| 125 | entries.push(WorkbarTab::new(self.active)); |
| 126 | entries.sort_by_key(|tab| { |
| 127 | WorkbarPanel::ORDER |
| 128 | .iter() |
| 129 | .position(|panel| *panel == tab.panel) |
| 130 | }); |
| 131 | } |
| 132 | let fits = |tabs: &[WorkbarTab], counts: bool| { |
| 133 | tabs.iter() |
| 134 | .map(|tab| { |
| 135 | unicode_width::UnicodeWidthStr::width(tab.panel.label()) |
| 136 | + if counts && tab.count.is_some_and(|n| n > 0) { |
| 137 | 1 + tab.count.unwrap_or(0).to_string().len() |
| 138 | } else { |
| 139 | 0 |
| 140 | } |
| 141 | + 2 |
| 142 | }) |
| 143 | .sum::<usize>() |
| 144 | .saturating_add(tabs.len().saturating_sub(1).saturating_mul(2)) |
| 145 | .saturating_add(usize::from(close_width) + 2) |
| 146 | <= width |
| 147 | }; |
| 148 | let show_counts = fits(&entries, true); |
| 149 | while !fits(&entries, show_counts) && entries.len() > 1 { |
| 150 | let Some(index) = entries.iter().rposition(|tab| tab.panel != self.active) else { |
| 151 | break; |
| 152 | }; |
| 153 | entries.remove(index); |
| 154 | } |
| 155 | let mut x = area.x.saturating_add(1); |
| 156 | for tab in entries { |
| 157 | let label = if show_counts && tab.count.is_some_and(|n| n > 0) { |
| 158 | format!("{} {}", tab.panel.label(), tab.count.unwrap_or(0)) |
| 159 | } else { |
| 160 | tab.panel.label().to_string() |
| 161 | }; |
| 162 | let tab_width = u16::try_from( |
| 163 | unicode_width::UnicodeWidthStr::width(label.as_str()).saturating_add(2), |
| 164 | ) |
| 165 | .unwrap_or(u16::MAX) |
| 166 | .min(area.width); |
| 167 | let style = if tab.panel == self.active || self.pressed == Some(tab.panel) { |
| 168 | self.styles.active |
| 169 | } else if self.hovered == Some(DockTabTarget::Panel(tab.panel)) { |
| 170 | self.styles.hovered |
| 171 | } else { |
| 172 | self.styles.idle |
| 173 | }; |
| 174 | if x.saturating_add(tab_width) > close_area.x { |
| 175 | if plan.tabs.is_empty() && tab.panel == self.active { |
| 176 | let avail = close_area.x.saturating_sub(x); |
| 177 | if avail > 0 { |
| 178 | let rendered = if avail >= 3 { |
| 179 | format!( |
| 180 | " {} ", |
| 181 | crate::text::truncate( |
| 182 | label.as_str(), |
| 183 | usize::from(avail - 2), |
| 184 | false |
| 185 | ) |
| 186 | ) |
| 187 | } else { |
| 188 | crate::text::truncate(label.as_str(), usize::from(avail), false) |
| 189 | .into_owned() |
| 190 | }; |
| 191 | plan.tabs.push(DockTabEntry { |
| 192 | tab, |
| 193 | area: Rect::new(x, y, avail, 1), |
| 194 | label: rendered, |
| 195 | style, |
| 196 | }); |
| 197 | } |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | plan.tabs.push(DockTabEntry { |
| 202 | tab, |
| 203 | area: Rect::new(x, y, tab_width, 1), |
| 204 | label: format!(" {label} "), |
| 205 | style, |
| 206 | }); |
| 207 | x = x.saturating_add(tab_width).saturating_add(2); |
| 208 | } |
| 209 | plan |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | impl Widget for &DockTabRow<'_> { |
| 214 | fn render(self, area: Rect, buf: &mut Buffer) { |
| 215 | self.plan(area).paint(buf); |
| 216 | } |
| 217 | } |
| 218 |