| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
use quasi_router::{Band, Chrome, Node, Place, Role}; |
| 51 |
use ratatui::buffer::Buffer; |
| 52 |
use ratatui::layout::Rect; |
| 53 |
|
| 54 |
use crate::{Local, Pass, Tui}; |
| 55 |
|
| 56 |
|
| 57 |
pub(crate) fn rows(tui: &Tui, chrome: &Chrome, width: u16, local: &Local<'_>) -> u16 { |
| 58 |
let panels: u16 = chrome |
| 59 |
.panels |
| 60 |
.iter() |
| 61 |
.map(|panel| crate::node::height(tui, &panel.content, width, local)) |
| 62 |
.sum(); |
| 63 |
panels.saturating_add(nav_rows(chrome)) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub(crate) fn nav_rows(chrome: &Chrome) -> u16 { |
| 72 |
let band = chrome.band.as_ref(); |
| 73 |
let branded = band.is_some_and(|band| band.brand.is_some()); |
| 74 |
if chrome.nav.is_empty() && !branded { |
| 75 |
return u16::from(band.is_some_and(|band| band.search.is_some())); |
| 76 |
} |
| 77 |
let levels = 1 + u16::from(chrome.nav.iter().any(|place| !place.within.is_empty())); |
| 78 |
levels + u16::from(band.is_some_and(|band| band.search.is_some())) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
pub(crate) fn draw_nav( |
| 90 |
pass: &mut Pass<'_>, |
| 91 |
chrome: &Chrome, |
| 92 |
at: Option<&str>, |
| 93 |
area: Rect, |
| 94 |
buf: &mut Buffer, |
| 95 |
) { |
| 96 |
let band = chrome.band.as_ref(); |
| 97 |
let branded = band.and_then(|band| band.brand.as_ref()); |
| 98 |
if (chrome.nav.is_empty() && branded.is_none()) || area.height == 0 { |
| 99 |
return; |
| 100 |
} |
| 101 |
let mut rest = area; |
| 102 |
if !chrome.nav.is_empty() || branded.is_some() { |
| 103 |
line(pass, branded, &chrome.nav, at, rest, buf); |
| 104 |
rest = down(rest, 1); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
let Some(open) = chrome |
| 109 |
.nav |
| 110 |
.iter() |
| 111 |
.find(|place| at.is_some_and(|key| place.holds(key))) |
| 112 |
else { |
| 113 |
return; |
| 114 |
}; |
| 115 |
if rest.height > 0 { |
| 116 |
line(pass, None, &open.within, at, rest, buf); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
pub(crate) fn draw_search(pass: &mut Pass<'_>, band: &Band, area: Rect, buf: &mut Buffer) { |
| 126 |
let Some(search) = &band.search else { |
| 127 |
return; |
| 128 |
}; |
| 129 |
if area.height == 0 { |
| 130 |
return; |
| 131 |
} |
| 132 |
let row = Rect { height: 1, ..area }; |
| 133 |
crate::node::draw(pass, &Node::Field(Box::new(search.clone())), row, buf); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
const fn down(area: Rect, rows: u16) -> Rect { |
| 138 |
Rect { |
| 139 |
y: area.y.saturating_add(rows), |
| 140 |
height: area.height.saturating_sub(rows), |
| 141 |
..area |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
fn line( |
| 147 |
pass: &mut Pass<'_>, |
| 148 |
brand: Option<&quasi_router::Brand>, |
| 149 |
places: &[Place], |
| 150 |
at: Option<&str>, |
| 151 |
area: Rect, |
| 152 |
buf: &mut Buffer, |
| 153 |
) { |
| 154 |
let row = Rect { height: 1, ..area }; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
let mut spans: Vec<ratatui::text::Span<'_>> = Vec::new(); |
| 161 |
if let Some(brand) = brand { |
| 162 |
spans.push(ratatui::text::Span::styled( |
| 163 |
format!("{} ", brand.name), |
| 164 |
ratatui::style::Style::default().add_modifier(ratatui::style::Modifier::BOLD), |
| 165 |
)); |
| 166 |
} |
| 167 |
spans.extend::<Vec<ratatui::text::Span<'_>>>( |
| 168 |
places |
| 169 |
.iter() |
| 170 |
.flat_map(|place| { |
| 171 |
let style = if at.is_some_and(|key| place.holds(key)) { |
| 172 |
ratatui::style::Style::default() |
| 173 |
.add_modifier(ratatui::style::Modifier::REVERSED) |
| 174 |
} else { |
| 175 |
ratatui::style::Style::default() |
| 176 |
}; |
| 177 |
[ |
| 178 |
ratatui::text::Span::styled(format!(" {} ", place.label), style), |
| 179 |
ratatui::text::Span::raw(" "), |
| 180 |
] |
| 181 |
}) |
| 182 |
.collect(), |
| 183 |
); |
| 184 |
let _ = pass; |
| 185 |
ratatui::widgets::Widget::render( |
| 186 |
ratatui::widgets::Paragraph::new(ratatui::text::Line::from(spans)), |
| 187 |
row, |
| 188 |
buf, |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
pub(crate) fn draw(pass: &mut Pass<'_>, chrome: &Chrome, area: Rect, buf: &mut Buffer) { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
let ordered = chrome |
| 203 |
.panels |
| 204 |
.iter() |
| 205 |
.filter(|panel| panel.role == Role::Activity) |
| 206 |
.chain( |
| 207 |
chrome |
| 208 |
.panels |
| 209 |
.iter() |
| 210 |
.filter(|panel| panel.role == Role::Status), |
| 211 |
); |
| 212 |
|
| 213 |
let mut top = area.y; |
| 214 |
for panel in ordered { |
| 215 |
if top >= area.bottom() { |
| 216 |
return; |
| 217 |
} |
| 218 |
let content: &Node = &panel.content; |
| 219 |
let height = crate::node::height(pass.tui, content, area.width, &pass.local()) |
| 220 |
.min(area.bottom() - top); |
| 221 |
let slice = Rect { |
| 222 |
x: area.x, |
| 223 |
y: top, |
| 224 |
width: area.width, |
| 225 |
height, |
| 226 |
}; |
| 227 |
crate::node::draw(pass, content, slice, buf); |
| 228 |
top = top.saturating_add(height); |
| 229 |
} |
| 230 |
} |
| 231 |
|