| 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::{RegionKind, Screen, Slot}; |
| 25 |
use ratatui::buffer::Buffer; |
| 26 |
use ratatui::layout::Rect; |
| 27 |
use ratatui::style::{Modifier, Style}; |
| 28 |
|
| 29 |
use crate::{Pass, Tui, below}; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
pub(crate) fn screen_regions(pass: &mut Pass<'_>, screen: &Screen, area: Rect, buf: &mut Buffer) { |
| 38 |
let area = measured(screen.measure, area); |
| 39 |
let mut rest = area; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
for slot in screen |
| 45 |
.slots |
| 46 |
.iter() |
| 47 |
.filter(|slot| matches!(slot.kind, RegionKind::Band)) |
| 48 |
{ |
| 49 |
let used = draw(pass, slot, rest, buf); |
| 50 |
rest = below(rest, used); |
| 51 |
} |
| 52 |
|
| 53 |
let body = body_slots(screen); |
| 54 |
|
| 55 |
match screen.arrangement { |
| 56 |
layout::Arrangement::SidebarContent { share } => { |
| 57 |
let (left, right) = split(rest, share.of(rest.width)); |
| 58 |
let mut sidebars = 0; |
| 59 |
let mut content = right; |
| 60 |
for slot in &body { |
| 61 |
if matches!(slot.kind, RegionKind::Sidebar) { |
| 62 |
let used = draw(pass, slot, below(left, sidebars), buf); |
| 63 |
sidebars += used; |
| 64 |
} else { |
| 65 |
let used = draw(pass, slot, content, buf); |
| 66 |
content = below(content, used); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
layout::Arrangement::ListDetail { tabbed, share } => { |
| 71 |
if tabbed { |
| 72 |
|
| 73 |
|
| 74 |
if let Some(first) = body.first() { |
| 75 |
draw(pass, first, rest, buf); |
| 76 |
} |
| 77 |
} else { |
| 78 |
let (left, right) = split(rest, share.of(rest.width)); |
| 79 |
let mut detail = right; |
| 80 |
for (index, slot) in body.iter().enumerate() { |
| 81 |
if index == 0 { |
| 82 |
draw(pass, slot, left, buf); |
| 83 |
} else { |
| 84 |
let used = draw(pass, slot, detail, buf); |
| 85 |
detail = below(detail, used); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
for slot in screen |
| 96 |
.slots |
| 97 |
.iter() |
| 98 |
.filter(|slot| matches!(slot.kind, RegionKind::Modal)) |
| 99 |
{ |
| 100 |
draw(pass, slot, centred(area), buf); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
fn measured(measure: layout::Measure, area: Rect) -> Rect { |
| 120 |
let cap = match measure { |
| 121 |
layout::Measure::Reading => 76, |
| 122 |
layout::Measure::Contained => 100, |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
_ => return area, |
| 127 |
}; |
| 128 |
if area.width <= cap { |
| 129 |
return area; |
| 130 |
} |
| 131 |
Rect { |
| 132 |
x: area.x + (area.width - cap) / 2, |
| 133 |
width: cap, |
| 134 |
..area |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
fn body_slots(screen: &Screen) -> Vec<&Slot> { |
| 145 |
let body = screen |
| 146 |
.slots |
| 147 |
.iter() |
| 148 |
.filter(|slot| !matches!(slot.kind, RegionKind::Band | RegionKind::Modal)); |
| 149 |
|
| 150 |
match screen.arrangement { |
| 151 |
layout::Arrangement::ListDetail { tabbed: true, .. } => body.take(1).collect(), |
| 152 |
_ => body.collect(), |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
pub(crate) fn reachable(screen: &Screen) -> Vec<&Slot> { |
| 168 |
let modals: Vec<&Slot> = screen |
| 169 |
.slots |
| 170 |
.iter() |
| 171 |
.filter(|slot| matches!(slot.kind, RegionKind::Modal)) |
| 172 |
.collect(); |
| 173 |
if !modals.is_empty() { |
| 174 |
return modals; |
| 175 |
} |
| 176 |
|
| 177 |
screen |
| 178 |
.slots |
| 179 |
.iter() |
| 180 |
.filter(|slot| matches!(slot.kind, RegionKind::Band)) |
| 181 |
.chain(body_slots(screen)) |
| 182 |
.collect() |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
pub(crate) fn height(tui: &Tui, slot: &Slot, width: u16) -> u16 { |
| 187 |
let inner = width.saturating_sub(2); |
| 188 |
let body: u16 = slot |
| 189 |
.body |
| 190 |
.iter() |
| 191 |
.map(|node| crate::node::height(tui, node, inner)) |
| 192 |
.sum(); |
| 193 |
|
| 194 |
body + if framed(slot.kind.depth()) { 2 } else { 0 } |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
pub(crate) fn draw(pass: &mut Pass<'_>, slot: &Slot, area: Rect, buf: &mut Buffer) -> u16 { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
let pending = matches!(slot.readiness, layout::Readiness::Pending); |
| 203 |
|
| 204 |
if area.width == 0 || area.height == 0 { |
| 205 |
if !pending { |
| 206 |
for node in &slot.body { |
| 207 |
crate::node::draw(pass, node, area, buf); |
| 208 |
} |
| 209 |
} |
| 210 |
return 0; |
| 211 |
} |
| 212 |
|
| 213 |
let tui = pass.tui; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
let depth = slot.kind.depth(); |
| 220 |
let inner = if framed(depth) { |
| 221 |
frame(buf, area, depth, tui.palette()) |
| 222 |
} else { |
| 223 |
area |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if pending { |
| 230 |
let used = text::draw( |
| 231 |
"Loading", |
| 232 |
Style::default() |
| 233 |
.fg(tui.theme().content_muted) |
| 234 |
.add_modifier(Modifier::ITALIC), |
| 235 |
inner, |
| 236 |
buf, |
| 237 |
); |
| 238 |
return used + if framed(depth) { 2 } else { 0 }; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
let used = body(pass, slot, inner, buf); |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
used + if framed(depth) { 2 } else { 0 } |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
fn body(pass: &mut Pass<'_>, slot: &Slot, inner: Rect, buf: &mut Buffer) -> u16 { |
| 269 |
let offset = pass.view.scroll(&slot.id); |
| 270 |
if offset == 0 { |
| 271 |
let mut used = 0; |
| 272 |
for node in &slot.body { |
| 273 |
used += crate::node::draw(pass, node, below(inner, used), buf); |
| 274 |
} |
| 275 |
return used.min(inner.height); |
| 276 |
} |
| 277 |
|
| 278 |
let content: u16 = slot |
| 279 |
.body |
| 280 |
.iter() |
| 281 |
.map(|node| crate::node::height(pass.tui, node, inner.width)) |
| 282 |
.sum(); |
| 283 |
let offset = offset.min(content.saturating_sub(inner.height)); |
| 284 |
|
| 285 |
let tall = Rect { |
| 286 |
height: content.max(inner.height), |
| 287 |
..inner |
| 288 |
}; |
| 289 |
let mut scratch = Buffer::empty(tall); |
| 290 |
let mut used = 0; |
| 291 |
for node in &slot.body { |
| 292 |
used += crate::node::draw(pass, node, below(tall, used), &mut scratch); |
| 293 |
} |
| 294 |
|
| 295 |
let shown = inner.height.min(content.saturating_sub(offset)); |
| 296 |
for row in 0..shown { |
| 297 |
for column in 0..inner.width { |
| 298 |
let from = (inner.x + column, inner.y + offset + row); |
| 299 |
let to = (inner.x + column, inner.y + row); |
| 300 |
if let Some(cell) = scratch.cell(from).cloned() |
| 301 |
&& let Some(target) = buf.cell_mut(to) |
| 302 |
{ |
| 303 |
*target = cell; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
shown |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
fn framed(depth: layout::Depth) -> bool { |
| 316 |
!matches!(depth, layout::Depth::Flat) |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
fn split(area: Rect, width: u16) -> (Rect, Rect) { |
| 321 |
let width = width.min(area.width); |
| 322 |
( |
| 323 |
Rect { width, ..area }, |
| 324 |
Rect { |
| 325 |
x: area.x + width, |
| 326 |
width: area.width - width, |
| 327 |
..area |
| 328 |
}, |
| 329 |
) |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
fn centred(area: Rect) -> Rect { |
| 334 |
let width = area.width / 2; |
| 335 |
let height = area.height / 2; |
| 336 |
Rect { |
| 337 |
x: area.x + width / 2, |
| 338 |
y: area.y + height / 2, |
| 339 |
width, |
| 340 |
height, |
| 341 |
} |
| 342 |
} |
| 343 |
|