| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use makeover_layout as layout; |
| 23 |
use makeover_tui::{frame, text}; |
| 24 |
use quasi_router::{Node, RegionKind, Screen, Slot}; |
| 25 |
use ratatui::buffer::Buffer; |
| 26 |
use ratatui::layout::Rect; |
| 27 |
use ratatui::style::{Modifier, Style}; |
| 28 |
use ratatui::text::Span; |
| 29 |
|
| 30 |
use crate::{Pass, Tui, below}; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
pub(crate) fn screen_regions(pass: &mut Pass<'_>, screen: &Screen, area: Rect, buf: &mut Buffer) { |
| 39 |
let area = measured(screen.measure, area); |
| 40 |
let mut rest = area; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
for slot in screen |
| 46 |
.slots |
| 47 |
.iter() |
| 48 |
.filter(|slot| matches!(slot.kind, RegionKind::Band)) |
| 49 |
{ |
| 50 |
let used = draw(pass, slot, rest, buf); |
| 51 |
rest = below(rest, used); |
| 52 |
} |
| 53 |
|
| 54 |
let body = body_slots(screen); |
| 55 |
|
| 56 |
match screen.arrangement { |
| 57 |
layout::Arrangement::SidebarContent { share } => { |
| 58 |
let (left, right) = split(rest, share.of(rest.width)); |
| 59 |
let mut sidebars = 0; |
| 60 |
let mut content = right; |
| 61 |
for slot in &body { |
| 62 |
if matches!(slot.kind, RegionKind::Sidebar) { |
| 63 |
let used = draw(pass, slot, below(left, sidebars), buf); |
| 64 |
sidebars += used; |
| 65 |
} else { |
| 66 |
let used = draw(pass, slot, content, buf); |
| 67 |
content = below(content, used); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
layout::Arrangement::ListDetail { tabbed, share } => { |
| 72 |
if tabbed { |
| 73 |
|
| 74 |
|
| 75 |
if let Some(first) = body.first() { |
| 76 |
draw(pass, first, rest, buf); |
| 77 |
} |
| 78 |
} else { |
| 79 |
let (left, right) = split(rest, share.of(rest.width)); |
| 80 |
let mut detail = right; |
| 81 |
for (index, slot) in body.iter().enumerate() { |
| 82 |
if index == 0 { |
| 83 |
draw(pass, slot, left, buf); |
| 84 |
} else { |
| 85 |
let used = draw(pass, slot, detail, buf); |
| 86 |
detail = below(detail, used); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
for slot in screen |
| 97 |
.slots |
| 98 |
.iter() |
| 99 |
.filter(|slot| matches!(slot.kind, RegionKind::Modal)) |
| 100 |
{ |
| 101 |
draw(pass, slot, centred(area), buf); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
fn measured(measure: layout::Measure, area: Rect) -> Rect { |
| 121 |
let cap = match measure { |
| 122 |
layout::Measure::Reading => 76, |
| 123 |
layout::Measure::Contained => 100, |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
_ => return area, |
| 128 |
}; |
| 129 |
if area.width <= cap { |
| 130 |
return area; |
| 131 |
} |
| 132 |
Rect { |
| 133 |
x: area.x + (area.width - cap) / 2, |
| 134 |
width: cap, |
| 135 |
..area |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
fn body_slots(screen: &Screen) -> Vec<&Slot> { |
| 146 |
let body = screen |
| 147 |
.slots |
| 148 |
.iter() |
| 149 |
.filter(|slot| !matches!(slot.kind, RegionKind::Band | RegionKind::Modal)); |
| 150 |
|
| 151 |
match screen.arrangement { |
| 152 |
layout::Arrangement::ListDetail { tabbed: true, .. } => body.take(1).collect(), |
| 153 |
_ => body.collect(), |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
pub(crate) fn reachable(screen: &Screen) -> Vec<&Slot> { |
| 169 |
let modals: Vec<&Slot> = screen |
| 170 |
.slots |
| 171 |
.iter() |
| 172 |
.filter(|slot| matches!(slot.kind, RegionKind::Modal)) |
| 173 |
.collect(); |
| 174 |
if !modals.is_empty() { |
| 175 |
return modals; |
| 176 |
} |
| 177 |
|
| 178 |
screen |
| 179 |
.slots |
| 180 |
.iter() |
| 181 |
.filter(|slot| matches!(slot.kind, RegionKind::Band)) |
| 182 |
.chain(body_slots(screen)) |
| 183 |
.collect() |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
pub(crate) fn height(tui: &Tui, slot: &Slot, width: u16) -> u16 { |
| 188 |
let inner = width.saturating_sub(2); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
let body: u16 = if slot.showing.selective() { |
| 195 |
slot.body |
| 196 |
.iter() |
| 197 |
.map(|node| crate::node::height(tui, node, inner)) |
| 198 |
.max() |
| 199 |
.unwrap_or(0) |
| 200 |
+ 1 |
| 201 |
} else { |
| 202 |
slot.body |
| 203 |
.iter() |
| 204 |
.map(|node| crate::node::height(tui, node, inner)) |
| 205 |
.sum() |
| 206 |
}; |
| 207 |
|
| 208 |
body + if framed(slot.kind.depth()) { 2 } else { 0 } |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
pub(crate) fn draw(pass: &mut Pass<'_>, slot: &Slot, area: Rect, buf: &mut Buffer) -> u16 { |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
let pending = matches!(slot.readiness, layout::Readiness::Pending); |
| 217 |
|
| 218 |
if area.width == 0 || area.height == 0 { |
| 219 |
if !pending { |
| 220 |
for node in &slot.body { |
| 221 |
crate::node::draw(pass, node, area, buf); |
| 222 |
} |
| 223 |
} |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
let tui = pass.tui; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
let depth = slot.kind.depth(); |
| 234 |
let inner = if framed(depth) { |
| 235 |
frame(buf, area, depth, tui.palette()) |
| 236 |
} else { |
| 237 |
area |
| 238 |
}; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if pending { |
| 244 |
let used = text::draw( |
| 245 |
"Loading", |
| 246 |
Style::default() |
| 247 |
.fg(tui.theme().content_muted) |
| 248 |
.add_modifier(Modifier::ITALIC), |
| 249 |
inner, |
| 250 |
buf, |
| 251 |
); |
| 252 |
return used + if framed(depth) { 2 } else { 0 }; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
let used = if slot.showing.selective() { |
| 261 |
showing_body(pass, slot, inner, buf) |
| 262 |
} else { |
| 263 |
body(pass, slot, &slot.body, inner, buf) |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
used + if framed(depth) { 2 } else { 0 } |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
fn showing_body(pass: &mut Pass<'_>, slot: &Slot, inner: Rect, buf: &mut Buffer) -> u16 { |
| 291 |
let at = pass.view.shown(slot); |
| 292 |
let labels = slot.labels(); |
| 293 |
let mut used = 0; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
if !labels.is_empty() { |
| 299 |
used += text::draw_spans( |
| 300 |
&showing_spans(pass.tui, &labels, at), |
| 301 |
below(inner, used), |
| 302 |
buf, |
| 303 |
); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if let Some(index) = at |
| 309 |
&& index < slot.body.len() |
| 310 |
{ |
| 311 |
used += body( |
| 312 |
pass, |
| 313 |
slot, |
| 314 |
&slot.body[index..=index], |
| 315 |
below(inner, used), |
| 316 |
buf, |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
if labels.is_empty() { |
| 321 |
used += text::draw_spans( |
| 322 |
&counter_spans(pass.tui, at, slot.body.len()), |
| 323 |
below(inner, used), |
| 324 |
buf, |
| 325 |
); |
| 326 |
} |
| 327 |
|
| 328 |
used.min(inner.height) |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
fn showing_spans(tui: &Tui, labels: &[&str], at: Option<usize>) -> Vec<Span<'static>> { |
| 337 |
let mut spans = Vec::new(); |
| 338 |
for (index, label) in labels.iter().enumerate() { |
| 339 |
if !spans.is_empty() { |
| 340 |
spans.push(Span::raw(" ")); |
| 341 |
} |
| 342 |
let picked = at == Some(index); |
| 343 |
let style = if picked { |
| 344 |
Style::default() |
| 345 |
.fg(tui.theme().selection_on) |
| 346 |
.bg(tui.theme().action_primary) |
| 347 |
} else { |
| 348 |
Style::default().fg(tui.theme().content_secondary) |
| 349 |
}; |
| 350 |
spans.push(Span::styled(format!(" {label} "), style)); |
| 351 |
} |
| 352 |
spans |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
fn counter_spans(tui: &Tui, at: Option<usize>, total: usize) -> Vec<Span<'static>> { |
| 362 |
let control = Style::default().fg(tui.theme().content_secondary); |
| 363 |
vec![ |
| 364 |
Span::styled("< Prev >", control), |
| 365 |
Span::styled( |
| 366 |
format!(" {} / {total} ", at.map_or(0, |index| index + 1)), |
| 367 |
Style::default().fg(tui.theme().content_muted), |
| 368 |
), |
| 369 |
Span::styled("< Next >", control), |
| 370 |
] |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
fn body(pass: &mut Pass<'_>, slot: &Slot, nodes: &[Node], inner: Rect, buf: &mut Buffer) -> u16 { |
| 388 |
let offset = pass.view.scroll(&slot.id); |
| 389 |
if offset == 0 { |
| 390 |
let mut used = 0; |
| 391 |
for node in nodes { |
| 392 |
used += crate::node::draw(pass, node, below(inner, used), buf); |
| 393 |
} |
| 394 |
return used.min(inner.height); |
| 395 |
} |
| 396 |
|
| 397 |
let content: u16 = nodes |
| 398 |
.iter() |
| 399 |
.map(|node| crate::node::height(pass.tui, node, inner.width)) |
| 400 |
.sum(); |
| 401 |
let offset = offset.min(content.saturating_sub(inner.height)); |
| 402 |
|
| 403 |
let tall = Rect { |
| 404 |
height: content.max(inner.height), |
| 405 |
..inner |
| 406 |
}; |
| 407 |
let mut scratch = Buffer::empty(tall); |
| 408 |
let mut used = 0; |
| 409 |
for node in nodes { |
| 410 |
used += crate::node::draw(pass, node, below(tall, used), &mut scratch); |
| 411 |
} |
| 412 |
|
| 413 |
let shown = inner.height.min(content.saturating_sub(offset)); |
| 414 |
for row in 0..shown { |
| 415 |
for column in 0..inner.width { |
| 416 |
let from = (inner.x + column, inner.y + offset + row); |
| 417 |
let to = (inner.x + column, inner.y + row); |
| 418 |
if let Some(cell) = scratch.cell(from).cloned() |
| 419 |
&& let Some(target) = buf.cell_mut(to) |
| 420 |
{ |
| 421 |
*target = cell; |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
shown |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
fn framed(depth: layout::Depth) -> bool { |
| 434 |
!matches!(depth, layout::Depth::Flat) |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
fn split(area: Rect, width: u16) -> (Rect, Rect) { |
| 439 |
let width = width.min(area.width); |
| 440 |
( |
| 441 |
Rect { width, ..area }, |
| 442 |
Rect { |
| 443 |
x: area.x + width, |
| 444 |
width: area.width - width, |
| 445 |
..area |
| 446 |
}, |
| 447 |
) |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
fn centred(area: Rect) -> Rect { |
| 452 |
let width = area.width / 2; |
| 453 |
let height = area.height / 2; |
| 454 |
Rect { |
| 455 |
x: area.x + width / 2, |
| 456 |
y: area.y + height / 2, |
| 457 |
width, |
| 458 |
height, |
| 459 |
} |
| 460 |
} |
| 461 |
|