| 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 |
use std::collections::BTreeMap; |
| 33 |
|
| 34 |
use egui::{RichText, Ui}; |
| 35 |
use makeover_immediate::widget; |
| 36 |
use makeover_immediate::{Filling, field, frame}; |
| 37 |
use quasi_router::layout; |
| 38 |
use quasi_router::{ |
| 39 |
Act, Action, Bar, Clock, Image, Node, Params, Placed, RegionKind, Rest, Row, Slot, |
| 40 |
}; |
| 41 |
|
| 42 |
use crate::view::Asking; |
| 43 |
use crate::{Immediate, Pass}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
const CELL_WIDTH: f32 = 120.0; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pub(crate) fn draw(pass: &mut Pass<'_>, ui: &mut Ui, node: &Node) { |
| 74 |
match node { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
Node::Heading { .. } |
| 79 |
| Node::Text { .. } |
| 80 |
| Node::Rich { .. } |
| 81 |
| Node::Code { .. } |
| 82 |
| Node::Figure(_) |
| 83 |
| Node::Image(_) |
| 84 |
| Node::Meter(_) |
| 85 |
| Node::Since { .. } |
| 86 |
| Node::Until { .. } |
| 87 |
| Node::Age { .. } => leaf(pass.immediate, ui, node), |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
Node::Notice { act, .. } => { |
| 93 |
leaf(pass.immediate, ui, node); |
| 94 |
if let Some(act) = act { |
| 95 |
act_node(pass, ui, act); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
Node::Act(act) => { |
| 100 |
act_node(pass, ui, act); |
| 101 |
} |
| 102 |
|
| 103 |
Node::Link { text, action, .. } => { |
| 104 |
|
| 105 |
|
| 106 |
if ui |
| 107 |
.link(RichText::new(text).color(pass.immediate.palette.action)) |
| 108 |
.clicked() |
| 109 |
{ |
| 110 |
pass.fire(action, Params::new(), None); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
Node::Token(tag) => { |
| 115 |
let mut pressed = widget::token( |
| 116 |
ui, |
| 117 |
&tag.label, |
| 118 |
tag.kind, |
| 119 |
tag.tone, |
| 120 |
tag.latched, |
| 121 |
&pass.immediate.palette, |
| 122 |
&pass.immediate.widget, |
| 123 |
); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
if let Some(hint) = &tag.hint { |
| 129 |
pressed = pressed.on_hover_text(hint); |
| 130 |
} |
| 131 |
if let Some(action) = &tag.action |
| 132 |
&& pressed.clicked() |
| 133 |
{ |
| 134 |
pass.fire(action, Params::new(), None); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
Node::StandIn { message, act, .. } => { |
| 139 |
ui.label(RichText::new(message).color(pass.immediate.palette.content_muted)); |
| 140 |
if let Some(act) = act { |
| 141 |
act_node(pass, ui, act); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
Node::Field(_) |
| 149 |
| Node::Region(_) |
| 150 |
| Node::Form { .. } |
| 151 |
| Node::Table { .. } |
| 152 |
| Node::Timeline { .. } |
| 153 |
| Node::Stats { .. } => container(pass, ui, node), |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
_ => undrawn(pass.immediate, ui), |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
fn leaf(immediate: &Immediate, ui: &mut Ui, node: &Node) { |
| 172 |
match node { |
| 173 |
Node::Heading { level, text } => { |
| 174 |
let size = ui.text_style_height(&egui::TextStyle::Body) |
| 175 |
* match level { |
| 176 |
layout::Heading::Page => 1.6, |
| 177 |
layout::Heading::Section => 1.3, |
| 178 |
layout::Heading::Subsection => 1.1, |
| 179 |
}; |
| 180 |
ui.label( |
| 181 |
RichText::new(text) |
| 182 |
.size(size) |
| 183 |
.strong() |
| 184 |
.color(immediate.palette.content), |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
Node::Text { text, .. } => { |
| 189 |
ui.label(RichText::new(text).color(immediate.palette.content)); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
Node::Rich { source, .. } => { |
| 199 |
ui.label( |
| 200 |
RichText::new(docengine::render_plain(source)).color(immediate.palette.content), |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
Node::Code { runs, inline, .. } => { |
| 214 |
let colour = |syntax: layout::Syntax| match syntax { |
| 215 |
layout::Syntax::Comment => immediate.palette.content_muted, |
| 216 |
layout::Syntax::String => immediate.palette.success, |
| 217 |
layout::Syntax::Keyword => immediate.palette.action, |
| 218 |
layout::Syntax::Constant => immediate.palette.warning, |
| 219 |
layout::Syntax::Entity => immediate.palette.info, |
| 220 |
layout::Syntax::Variable => immediate.palette.danger, |
| 221 |
layout::Syntax::Support => immediate.palette.content_secondary, |
| 222 |
|
| 223 |
|
| 224 |
_ => immediate.palette.content, |
| 225 |
}; |
| 226 |
let draw_runs = |ui: &mut Ui| { |
| 227 |
ui.spacing_mut().item_spacing.x = 0.0; |
| 228 |
for run in runs { |
| 229 |
ui.label( |
| 230 |
RichText::new(run.text.clone()) |
| 231 |
.monospace() |
| 232 |
.color(colour(run.syntax)), |
| 233 |
); |
| 234 |
} |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
if *inline { |
| 242 |
ui.horizontal(draw_runs); |
| 243 |
} else { |
| 244 |
ui.vertical(draw_runs); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
Node::Figure(figure) => { |
| 249 |
widget::figure( |
| 250 |
ui, |
| 251 |
&figure.as_layout(), |
| 252 |
&immediate.palette, |
| 253 |
&immediate.widget, |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
Node::Image(picture) => { |
| 258 |
image(immediate, ui, picture); |
| 259 |
} |
| 260 |
|
| 261 |
Node::Notice { tone, text, .. } => { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
frame( |
| 267 |
ui, |
| 268 |
layout::Depth::Raised, |
| 269 |
&immediate.palette, |
| 270 |
immediate.frame, |
| 271 |
|ui| { |
| 272 |
ui.label(RichText::new(text).color(immediate.palette.tone(*tone))); |
| 273 |
}, |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
Node::Meter(meter) => { |
| 278 |
widget::meter( |
| 279 |
ui, |
| 280 |
&meter.as_layout(), |
| 281 |
&immediate.palette, |
| 282 |
&immediate.widget, |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
Node::Chart { axis, bars, .. } => { |
| 290 |
let borrowed: Vec<_> = bars.iter().map(Bar::as_layout).collect(); |
| 291 |
widget::chart( |
| 292 |
ui, |
| 293 |
&axis.as_layout(), |
| 294 |
&borrowed, |
| 295 |
&immediate.palette, |
| 296 |
&immediate.widget, |
| 297 |
); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
Node::Since { at } => clock_label(immediate, ui, Clock::Since, *at), |
| 306 |
Node::Until { at } => clock_label(immediate, ui, Clock::Until, *at), |
| 307 |
Node::Age { at } => clock_label(immediate, ui, Clock::Age, *at), |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
Node::Act(_) |
| 314 |
| Node::Link { .. } |
| 315 |
| Node::Token(_) |
| 316 |
| Node::StandIn { .. } |
| 317 |
| Node::Field(_) |
| 318 |
| Node::Form { .. } |
| 319 |
| Node::Table { .. } |
| 320 |
| Node::Timeline { .. } |
| 321 |
| Node::Stats { .. } |
| 322 |
| Node::Region(_) => unreachable!("an addressed node reached the leaf walk"), |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
_ => undrawn(immediate, ui), |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
fn clock_label(immediate: &Immediate, ui: &mut Ui, clock: Clock, at: std::time::SystemTime) { |
| 342 |
let words = crate::clock::text(clock, at, std::time::SystemTime::now()); |
| 343 |
ui.label(RichText::new(words).color(immediate.palette.content)); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
fn undrawn(immediate: &Immediate, ui: &mut Ui) { |
| 357 |
ui.label( |
| 358 |
RichText::new("(not drawn: this renderer does not know this yet)") |
| 359 |
.italics() |
| 360 |
.color(immediate.palette.content_muted), |
| 361 |
); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
fn unfilled(immediate: &Immediate, ui: &mut Ui) { |
| 374 |
ui.label( |
| 375 |
RichText::new("(not drawn: this host has no fill for this)") |
| 376 |
.italics() |
| 377 |
.color(immediate.palette.content_muted), |
| 378 |
); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
fn rest_controls(pass: &mut Pass<'_>, ui: &mut Ui, rest: &Rest) { |
| 392 |
let paging = rest.as_layout(); |
| 393 |
ui.horizontal(|ui| { |
| 394 |
if ui |
| 395 |
.add_enabled(rest.back.is_some(), egui::Button::new("Prev")) |
| 396 |
.clicked() |
| 397 |
&& let Some(action) = &rest.back |
| 398 |
{ |
| 399 |
pass.fire(action, Params::new(), None); |
| 400 |
} |
| 401 |
|
| 402 |
if rest.jumps.is_empty() { |
| 403 |
ui.label(match (paging.page(), paging.pages_total()) { |
| 404 |
(Some(page), Some(total)) => format!("{page} / {total}"), |
| 405 |
_ => match paging.total() { |
| 406 |
Some(total) => format!("{} of {total}", paging.shown()), |
| 407 |
None => "Showing what arrived".to_owned(), |
| 408 |
}, |
| 409 |
}); |
| 410 |
} else { |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
for jump in &rest.jumps { |
| 416 |
if jump.here { |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
ui.label(jump.page.to_string()); |
| 422 |
continue; |
| 423 |
} |
| 424 |
if ui.button(jump.page.to_string()).clicked() { |
| 425 |
pass.fire(&jump.action, Params::new(), None); |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
if ui |
| 431 |
.add_enabled(rest.forward.is_some(), egui::Button::new("Next")) |
| 432 |
.clicked() |
| 433 |
&& let Some(action) = &rest.forward |
| 434 |
{ |
| 435 |
pass.fire(action, Params::new(), None); |
| 436 |
} |
| 437 |
}); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
fn image(immediate: &Immediate, ui: &mut Ui, picture: &Image) { |
| 448 |
let available = ui.available_width(); |
| 449 |
|
| 450 |
let mut shown = egui::Image::new(&picture.src) |
| 451 |
|
| 452 |
|
| 453 |
.show_loading_spinner(matches!(picture.loading, layout::Loading::Eager)); |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
if picture.speaks() { |
| 460 |
shown = shown.alt_text(picture.alt.clone()); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
shown = match picture.intrinsic { |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
Some(extent) if extent.width > 0 && extent.height > 0 => { |
| 478 |
let width = available.min(f64_ish(extent.width)); |
| 479 |
let height = width * f64_ish(extent.height) / f64_ish(extent.width); |
| 480 |
shown.fit_to_exact_size(egui::vec2(width, height)) |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
_ => shown.max_width(available).maintain_aspect_ratio(true), |
| 486 |
}; |
| 487 |
|
| 488 |
ui.add(shown); |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
if let Some(caption) = &picture.caption { |
| 494 |
ui.label(RichText::new(caption).color(immediate.palette.content_muted)); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
#[expect( |
| 504 |
clippy::cast_precision_loss, |
| 505 |
reason = "a texture dimension is exact in f32 far past any real picture" |
| 506 |
)] |
| 507 |
fn f64_ish(pixels: u32) -> f32 { |
| 508 |
pixels as f32 |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
const TICK_LINES: f32 = 1.5; |
| 518 |
|
| 519 |
|
| 520 |
const RULER_PADDING: f32 = 8.0; |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
fn timeline( |
| 529 |
pass: &mut Pass<'_>, |
| 530 |
ui: &mut Ui, |
| 531 |
track: layout::Track, |
| 532 |
entries: &[Placed], |
| 533 |
focus: Option<u16>, |
| 534 |
) { |
| 535 |
let text_height = ui.text_style_height(&egui::TextStyle::Body); |
| 536 |
let slots = track.slots(); |
| 537 |
let per_tick = if track.slot == 0 || track.tick == 0 { |
| 538 |
0 |
| 539 |
} else { |
| 540 |
track.tick / track.slot |
| 541 |
}; |
| 542 |
let slot_height = if per_tick == 0 { |
| 543 |
text_height |
| 544 |
} else { |
| 545 |
text_height * TICK_LINES / f32::from(per_tick) |
| 546 |
}; |
| 547 |
let height = f32::from(slots) * slot_height; |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
let sample = tick_label(track.unit, track.span.from()); |
| 553 |
let ruler = ui |
| 554 |
.painter() |
| 555 |
.layout_no_wrap( |
| 556 |
sample, |
| 557 |
egui::TextStyle::Body.resolve(ui.style()), |
| 558 |
pass.immediate.palette.content_muted, |
| 559 |
) |
| 560 |
.rect |
| 561 |
.width() |
| 562 |
+ RULER_PADDING; |
| 563 |
|
| 564 |
let (rect, _) = ui.allocate_exact_size( |
| 565 |
egui::vec2(ui.available_width(), height), |
| 566 |
egui::Sense::hover(), |
| 567 |
); |
| 568 |
let painter = ui.painter().clone(); |
| 569 |
let palette = pass.immediate.palette; |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
for slot in 0..slots { |
| 574 |
let y = rect.top() + f32::from(slot) * slot_height; |
| 575 |
let ticked = per_tick > 0 && slot % per_tick == 0; |
| 576 |
painter.hline( |
| 577 |
rect.x_range(), |
| 578 |
y, |
| 579 |
egui::Stroke::new( |
| 580 |
1.0, |
| 581 |
if ticked { |
| 582 |
palette.bevel_dark |
| 583 |
} else { |
| 584 |
palette.sunken |
| 585 |
}, |
| 586 |
), |
| 587 |
); |
| 588 |
if ticked { |
| 589 |
painter.text( |
| 590 |
egui::pos2(rect.left(), y), |
| 591 |
egui::Align2::LEFT_TOP, |
| 592 |
tick_label(track.unit, track.span.from() + slot * track.slot), |
| 593 |
egui::TextStyle::Body.resolve(ui.style()), |
| 594 |
palette.content_muted, |
| 595 |
); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
let mut lanes: Vec<usize> = Vec::with_capacity(entries.len()); |
| 607 |
for (i, entry) in entries.iter().enumerate() { |
| 608 |
let mut lane = 0; |
| 609 |
while entries[..i] |
| 610 |
.iter() |
| 611 |
.zip(&lanes) |
| 612 |
.any(|(other, &taken)| taken == lane && entry.overlaps(other)) |
| 613 |
{ |
| 614 |
lane += 1; |
| 615 |
} |
| 616 |
lanes.push(lane); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
let across = lanes.iter().copied().max().map_or(1, |most| most + 1); |
| 623 |
let body = rect.with_min_x(rect.left() + ruler); |
| 624 |
let lane_width = body.width() / f32::from(u16::try_from(across).unwrap_or(u16::MAX).max(1)); |
| 625 |
|
| 626 |
for (entry, &lane) in entries.iter().zip(&lanes) { |
| 627 |
let top = rect.top() + track.fraction(entry.placement.at()) * height; |
| 628 |
let bottom = rect.top() + track.fraction(entry.placement.end()) * height; |
| 629 |
let at = egui::Rect::from_min_size( |
| 630 |
egui::pos2( |
| 631 |
body.left() + f32::from(u16::try_from(lane).unwrap_or(u16::MAX)) * lane_width, |
| 632 |
top, |
| 633 |
), |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
egui::vec2(lane_width, (bottom - top).max(text_height)), |
| 638 |
); |
| 639 |
painter.rect_filled(at, 2.0, palette.well); |
| 640 |
ui.scope_builder(egui::UiBuilder::new().max_rect(at.shrink(2.0)), |ui| { |
| 641 |
|
| 642 |
|
| 643 |
list_row(pass, ui, &entry.row, 0, false); |
| 644 |
}); |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if let Some(minute) = focus { |
| 653 |
let id = ui.id().with("track-focus"); |
| 654 |
if ui.data(|data| data.get_temp::<u16>(id)) != Some(minute) { |
| 655 |
ui.data_mut(|data| data.insert_temp(id, minute)); |
| 656 |
let y = rect.top() + track.fraction(minute) * height; |
| 657 |
ui.scroll_to_rect( |
| 658 |
egui::Rect::from_min_size( |
| 659 |
egui::pos2(rect.left(), y), |
| 660 |
egui::vec2(rect.width(), text_height), |
| 661 |
), |
| 662 |
Some(egui::Align::Center), |
| 663 |
); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
fn tick_label(unit: layout::Unit, offset: u16) -> String { |
| 674 |
match unit { |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
layout::Unit::Minutes => format!("{:02}:{:02}", (offset / 60) % 24, offset % 60), |
| 679 |
|
| 680 |
|
| 681 |
layout::Unit::Days => format!("{}", offset + 1), |
| 682 |
|
| 683 |
|
| 684 |
_ => String::new(), |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
fn container(pass: &mut Pass<'_>, ui: &mut Ui, node: &Node) { |
| 694 |
match node { |
| 695 |
Node::Field(described) => { |
| 696 |
field_node(pass, ui, described); |
| 697 |
} |
| 698 |
|
| 699 |
Node::Region(slot) => { |
| 700 |
region(pass, ui, slot); |
| 701 |
} |
| 702 |
|
| 703 |
Node::Form { |
| 704 |
fields, |
| 705 |
submit, |
| 706 |
action, |
| 707 |
.. |
| 708 |
} => { |
| 709 |
form(pass, ui, fields, submit, action); |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
Node::Table { |
| 717 |
columns, |
| 718 |
rows, |
| 719 |
more, |
| 720 |
.. |
| 721 |
} if columns.is_empty() => { |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
let branches = rows.iter().any(|row| row.open.is_some()); |
| 729 |
let shown = unfolded(rows, pass.view); |
| 730 |
for (within, row) in rows.iter().enumerate() { |
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
if !shown.iter().any(|kept| std::ptr::eq(*kept, row)) { |
| 736 |
continue; |
| 737 |
} |
| 738 |
list_row(pass, ui, row, within, branches); |
| 739 |
} |
| 740 |
if let Some(rest) = more { |
| 741 |
rest_controls(pass, ui, rest); |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
Node::Table { |
| 746 |
columns, |
| 747 |
rows, |
| 748 |
more, |
| 749 |
.. |
| 750 |
} => { |
| 751 |
table(pass, ui, columns, rows); |
| 752 |
|
| 753 |
|
| 754 |
if let Some(rest) = more { |
| 755 |
rest_controls(pass, ui, rest); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
Node::Timeline { |
| 760 |
track, |
| 761 |
entries, |
| 762 |
focus, |
| 763 |
.. |
| 764 |
} => { |
| 765 |
timeline(pass, ui, *track, entries, *focus); |
| 766 |
} |
| 767 |
|
| 768 |
Node::Stats { figures, .. } => { |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
ui.horizontal(|ui| { |
| 773 |
for (figure, address) in figures { |
| 774 |
let shown = widget::figure( |
| 775 |
ui, |
| 776 |
&figure.as_layout(), |
| 777 |
&pass.immediate.palette, |
| 778 |
&pass.immediate.widget, |
| 779 |
); |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
if let Some(action) = address |
| 784 |
&& shown.interact(egui::Sense::click()).clicked() |
| 785 |
{ |
| 786 |
pass.fire(action, Params::new(), None); |
| 787 |
} |
| 788 |
} |
| 789 |
}); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
_ => unreachable!("a leaf reached the container walk"), |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
fn act_node(pass: &mut Pass<'_>, ui: &mut Ui, act: &Act) { |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
let over = act.over.as_deref(); |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
for field in &act.asks { |
| 815 |
field_node(pass, ui, &field.as_asked()); |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
let chosen = over.map(|_| pass.view.ticks().count()); |
| 828 |
let label = match chosen { |
| 829 |
Some(0) | None => act.label.clone(), |
| 830 |
Some(chosen) => format!("{} ({chosen})", act.label), |
| 831 |
}; |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
let busy = pass.view.busy(&act.action); |
| 838 |
let described = layout::Act { |
| 839 |
label: &label, |
| 840 |
key: act.key.as_deref(), |
| 841 |
tone: act.tone, |
| 842 |
state: if chosen == Some(0) || busy { |
| 843 |
Some(layout::State::Disabled) |
| 844 |
} else { |
| 845 |
act.state |
| 846 |
}, |
| 847 |
|
| 848 |
|
| 849 |
hint: act.hint.as_deref(), |
| 850 |
}; |
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
let pressed = match &act.shows { |
| 864 |
Some(picture) => { |
| 865 |
ui.vertical(|ui| { |
| 866 |
image(pass.immediate, ui, picture); |
| 867 |
widget::act( |
| 868 |
ui, |
| 869 |
&described, |
| 870 |
&pass.immediate.palette, |
| 871 |
&pass.immediate.widget, |
| 872 |
) |
| 873 |
}) |
| 874 |
.inner |
| 875 |
} |
| 876 |
None => widget::act( |
| 877 |
ui, |
| 878 |
&described, |
| 879 |
&pass.immediate.palette, |
| 880 |
&pass.immediate.widget, |
| 881 |
), |
| 882 |
}; |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
if let Some(id) = &act.id { |
| 896 |
crate::geometry::note_act(ui, id, pressed.rect); |
| 897 |
} |
| 898 |
if pressed.clicked() && chosen != Some(0) && !busy { |
| 899 |
let mut payload = gathered(pass, over); |
| 900 |
payload.absorb(asked(pass, &act.asks)); |
| 901 |
deposit(pass, act); |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
if let Some(value) = &act.copies { |
| 911 |
ui.ctx().copy_text(value.clone()); |
| 912 |
} |
| 913 |
pass.fire(&act.action, payload, act.confirm.as_deref()); |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
fn deposit(pass: &mut Pass<'_>, act: &Act) { |
| 940 |
let Some(fill) = &act.fills else { |
| 941 |
return; |
| 942 |
}; |
| 943 |
let mut value = pass.view.edit(&fill.field).unwrap_or_default().to_owned(); |
| 944 |
value.push_str(&fill.value); |
| 945 |
pass.view.set(&fill.field, value); |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
fn checkbox_node( |
| 955 |
pass: &mut Pass<'_>, |
| 956 |
ui: &mut Ui, |
| 957 |
described: &quasi_router::Field, |
| 958 |
name: &str, |
| 959 |
offered: bool, |
| 960 |
) { |
| 961 |
let mut on = pass |
| 962 |
.view |
| 963 |
.edit(name) |
| 964 |
.map_or(offered, |value| !value.is_empty()); |
| 965 |
let before = on; |
| 966 |
described.with_layout(|borrowed| { |
| 967 |
field( |
| 968 |
ui, |
| 969 |
&borrowed, |
| 970 |
Filling::On(&mut on), |
| 971 |
None, |
| 972 |
&pass.immediate.palette, |
| 973 |
&pass.immediate.field, |
| 974 |
); |
| 975 |
}); |
| 976 |
if on == before { |
| 977 |
return; |
| 978 |
} |
| 979 |
pass.view.set(name, if on { "on" } else { "" }); |
| 980 |
|
| 981 |
|
| 982 |
pass.stirred.insert(name.to_owned()); |
| 983 |
if let Some(action) = &described.writes { |
| 984 |
let mut payload = Params::new(); |
| 985 |
payload.insert(name.to_owned(), if on { "on" } else { "" }.to_owned()); |
| 986 |
pass.fire(action, payload, None); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
fn upper_moved( |
| 1002 |
pass: &mut Pass<'_>, |
| 1003 |
described: &quasi_router::Field, |
| 1004 |
lower: (&str, &str), |
| 1005 |
upper: (&str, &str), |
| 1006 |
) { |
| 1007 |
let (lower_name, lower_value) = lower; |
| 1008 |
let (upper_name, upper_value) = upper; |
| 1009 |
pass.view.set(upper_name, upper_value.to_owned()); |
| 1010 |
let Some(action) = &described.writes else { |
| 1011 |
return; |
| 1012 |
}; |
| 1013 |
let mut payload = Params::new(); |
| 1014 |
payload.insert(lower_name.to_owned(), lower_value.to_owned()); |
| 1015 |
payload.insert(upper_name.to_owned(), upper_value.to_owned()); |
| 1016 |
pass.fire(action, payload, None); |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
fn repeat_node(pass: &mut Pass<'_>, ui: &mut Ui, described: &quasi_router::Field) { |
| 1031 |
let Some(repeat) = described.repeats.clone() else { |
| 1032 |
return; |
| 1033 |
}; |
| 1034 |
ui.label(RichText::new(described.label.as_str()).color(pass.immediate.palette.content)); |
| 1035 |
|
| 1036 |
|
| 1037 |
if let Some(error) = &described.error { |
| 1038 |
ui.label( |
| 1039 |
RichText::new(error.as_str()).color(pass.immediate.palette.tone(layout::Tone::Danger)), |
| 1040 |
); |
| 1041 |
} |
| 1042 |
let standing = pass.view.standing(described); |
| 1043 |
for at in 0..standing { |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
let slots = described.instance_fields(at); |
| 1048 |
let held = repeat.instances.get(at); |
| 1049 |
let busy = held.is_some_and(|slot| slot.progress.busy()); |
| 1050 |
ui.horizontal(|ui| { |
| 1051 |
for slot in &slots { |
| 1052 |
field_node(pass, ui, slot); |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
if repeat.fewer(standing) |
| 1057 |
&& !busy |
| 1058 |
&& ui.button(RichText::new(repeat.remove.as_str())).clicked() |
| 1059 |
{ |
| 1060 |
pass.view.remove_slot(described, at); |
| 1061 |
} |
| 1062 |
}); |
| 1063 |
|
| 1064 |
|
| 1065 |
if let Some(held) = held { |
| 1066 |
if let Some(error) = &held.error { |
| 1067 |
ui.label( |
| 1068 |
RichText::new(error.as_str()) |
| 1069 |
.color(pass.immediate.palette.tone(layout::Tone::Danger)), |
| 1070 |
); |
| 1071 |
} |
| 1072 |
if let quasi_router::Progress::Working(Some(meter)) = &held.progress { |
| 1073 |
widget::meter( |
| 1074 |
ui, |
| 1075 |
&meter.as_layout(), |
| 1076 |
&pass.immediate.palette, |
| 1077 |
&pass.immediate.widget, |
| 1078 |
); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
if let Some(label) = repeat.add.label().filter(|_| repeat.more(standing)) |
| 1086 |
&& ui.button(RichText::new(label)).clicked() |
| 1087 |
{ |
| 1088 |
pass.view.add_slot(described); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
fn push_deadlines(pass: &mut Pass<'_>, described: &quasi_router::Field, name: &str, buffer: &str) { |
| 1099 |
for (at, consult) in questions(described) { |
| 1100 |
if consult.asks_about(buffer) { |
| 1101 |
pass.view.wait_to_consult( |
| 1102 |
Asking::Field(name.to_owned()), |
| 1103 |
at, |
| 1104 |
std::time::Instant::now() + consult.after, |
| 1105 |
); |
| 1106 |
} else { |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
pass.view.consulted(Asking::Field(name.to_owned()), at); |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
fn field_node(pass: &mut Pass<'_>, ui: &mut Ui, described: &quasi_router::Field) { |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
if pass.hidden.field_out(&described.name) { |
| 1121 |
return; |
| 1122 |
} |
| 1123 |
let name = described.name.clone(); |
| 1124 |
let kind = described.kind; |
| 1125 |
let offered = described.value.clone(); |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
if described.repeats.is_some() { |
| 1130 |
repeat_node(pass, ui, described); |
| 1131 |
return; |
| 1132 |
} |
| 1133 |
|
| 1134 |
if kind == layout::FieldKind::Checkbox { |
| 1135 |
checkbox_node(pass, ui, described, &name, offered.is_some()); |
| 1136 |
return; |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
let mut buffer = pass.view.buffer(&name, offered.as_deref()).clone(); |
| 1143 |
let before = buffer.clone(); |
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
let upper_name = described |
| 1148 |
.upper_name |
| 1149 |
.clone() |
| 1150 |
.filter(|_| kind == layout::FieldKind::Interval); |
| 1151 |
let mut upper = upper_name.as_ref().map(|upper_name| { |
| 1152 |
pass.view |
| 1153 |
.buffer(upper_name, described.upper_value.as_deref()) |
| 1154 |
.clone() |
| 1155 |
}); |
| 1156 |
let upper_before = upper.clone(); |
| 1157 |
let width = described.width; |
| 1158 |
let response = sized(ui, width, |ui| { |
| 1159 |
described.with_layout(|borrowed| { |
| 1160 |
let filling = match upper.as_mut() { |
| 1161 |
Some(upper) => Filling::Between { |
| 1162 |
lower: &mut buffer, |
| 1163 |
upper, |
| 1164 |
}, |
| 1165 |
None => Filling::Text(&mut buffer), |
| 1166 |
}; |
| 1167 |
field( |
| 1168 |
ui, |
| 1169 |
&borrowed, |
| 1170 |
filling, |
| 1171 |
None, |
| 1172 |
&pass.immediate.palette, |
| 1173 |
&pass.immediate.field, |
| 1174 |
) |
| 1175 |
}) |
| 1176 |
}); |
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
if let Some(response) = response.as_ref() |
| 1183 |
&& pass.view.claims_caret(&name) |
| 1184 |
{ |
| 1185 |
response.request_focus(); |
| 1186 |
} |
| 1187 |
|
| 1188 |
if upper_before != upper |
| 1189 |
&& let Some((upper_name, upper)) = upper_name.as_ref().zip(upper.as_ref()) |
| 1190 |
{ |
| 1191 |
|
| 1192 |
|
| 1193 |
pass.stirred.insert(upper_name.clone()); |
| 1194 |
upper_moved(pass, described, (&name, &buffer), (upper_name, upper)); |
| 1195 |
} |
| 1196 |
if buffer != before { |
| 1197 |
pass.view.set(&name, buffer.clone()); |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
pass.stirred.insert(name.clone()); |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
push_deadlines(pass, described, &name, &buffer); |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
let settled = response |
| 1230 |
.as_ref() |
| 1231 |
.is_some_and(|response| response.lost_focus() || response.drag_stopped()); |
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
let built_up = !(kind.offers_options() || kind.takes_files() || kind.offers_themes()); |
| 1237 |
let complete = if built_up { settled } else { buffer != before }; |
| 1238 |
if complete |
| 1239 |
&& described.writes.is_some() |
| 1240 |
&& pass.view.unwritten(&name, &buffer, offered.as_deref()) |
| 1241 |
{ |
| 1242 |
pass.view.wrote(&name, &buffer); |
| 1243 |
if let Some(action) = &described.writes { |
| 1244 |
let mut payload = Params::new(); |
| 1245 |
payload.insert(name.clone(), buffer.clone()); |
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
if let Some((upper_name, upper)) = upper_name.as_ref().zip(upper.as_ref()) { |
| 1250 |
payload.insert(upper_name.clone(), upper.clone()); |
| 1251 |
} |
| 1252 |
pass.fire(action, payload, None); |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
if let Some(owned) = described.suggests.as_ref() |
| 1259 |
&& !owned.asks_about(&buffer) |
| 1260 |
{ |
| 1261 |
pass.view.unsuggest(); |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
for (at, consult) in questions(described) { |
| 1267 |
let Some(due) = pass.view.consult_due(Asking::Field(name.clone()), at) else { |
| 1268 |
continue; |
| 1269 |
}; |
| 1270 |
let now = std::time::Instant::now(); |
| 1271 |
if now >= due { |
| 1272 |
pass.view.consulted(Asking::Field(name.clone()), at); |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
let mut payload = pass.view.contributed(&consult.sends); |
| 1277 |
payload.insert(name.clone(), buffer.clone()); |
| 1278 |
pass.fire(&consult.action, payload, None); |
| 1279 |
} else { |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
ui.ctx().request_repaint_after(due - now); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
suggestions(pass, &name, &mut buffer, ui); |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
fn questions(field: &quasi_router::Field) -> impl Iterator<Item = (usize, &quasi_router::Consult)> { |
| 1298 |
field.consults.iter().enumerate().chain( |
| 1299 |
field |
| 1300 |
.suggests |
| 1301 |
.iter() |
| 1302 |
.map(|owned| (field.consults.len(), owned)), |
| 1303 |
) |
| 1304 |
} |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
fn suggestions(pass: &mut Pass<'_>, name: &str, buffer: &mut String, ui: &mut egui::Ui) { |
| 1330 |
let Some(options) = pass.view.suggesting(name) else { |
| 1331 |
return; |
| 1332 |
}; |
| 1333 |
|
| 1334 |
|
| 1335 |
let options = options.to_vec(); |
| 1336 |
let mut picked = None; |
| 1337 |
for candidate in options { |
| 1338 |
let mut text = egui::text::LayoutJob::default(); |
| 1339 |
text.append( |
| 1340 |
&candidate.label, |
| 1341 |
0.0, |
| 1342 |
egui::TextFormat::simple(egui::FontId::default(), ui.visuals().text_color()), |
| 1343 |
); |
| 1344 |
if let Some(detail) = &candidate.detail { |
| 1345 |
text.append( |
| 1346 |
detail, |
| 1347 |
8.0, |
| 1348 |
egui::TextFormat::simple(egui::FontId::default(), ui.visuals().weak_text_color()), |
| 1349 |
); |
| 1350 |
} |
| 1351 |
if ui.add(egui::Button::new(text).frame(false)).clicked() { |
| 1352 |
picked = Some(candidate.clone()); |
| 1353 |
} |
| 1354 |
} |
| 1355 |
let Some(candidate) = picked else { |
| 1356 |
return; |
| 1357 |
}; |
| 1358 |
pass.view.unsuggest(); |
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
if let Some(action) = candidate.picks { |
| 1363 |
pass.fire(&action, Params::new(), None); |
| 1364 |
return; |
| 1365 |
} |
| 1366 |
buffer.clone_from(&candidate.value); |
| 1367 |
pass.view.set(name, candidate.value); |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
const ASKED: f32 = 220.0; |
| 1384 |
|
| 1385 |
|
| 1386 |
fn sized<R>(ui: &mut Ui, width: layout::Width, draw: impl FnOnce(&mut Ui) -> R) -> R { |
| 1387 |
match width { |
| 1388 |
layout::Width::Fill => draw(ui), |
| 1389 |
_ => { |
| 1390 |
ui.scope(|ui| { |
| 1391 |
ui.set_max_width(ASKED.min(ui.available_width())); |
| 1392 |
draw(ui) |
| 1393 |
}) |
| 1394 |
.inner |
| 1395 |
} |
| 1396 |
} |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
fn asked(pass: &Pass<'_>, asks: &[quasi_router::Field]) -> Params { |
| 1404 |
if asks.is_empty() { |
| 1405 |
return Params::new(); |
| 1406 |
} |
| 1407 |
let (names, described) = submitted(pass, asks); |
| 1408 |
pass.view.submission(&names, &described) |
| 1409 |
} |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
fn submitted( |
| 1419 |
pass: &Pass<'_>, |
| 1420 |
fields: &[quasi_router::Field], |
| 1421 |
) -> (Vec<String>, BTreeMap<String, String>) { |
| 1422 |
let mut names = Vec::new(); |
| 1423 |
let mut described = BTreeMap::new(); |
| 1424 |
for field in fields { |
| 1425 |
for at in 0..pass.view.standing(field) { |
| 1426 |
let slots = if field.repeats.is_some() { |
| 1427 |
field.instance_fields(at) |
| 1428 |
} else { |
| 1429 |
vec![field.clone()] |
| 1430 |
}; |
| 1431 |
for slot in slots { |
| 1432 |
if let Some(value) = slot.value { |
| 1433 |
described.insert(slot.name.clone(), value); |
| 1434 |
} |
| 1435 |
names.push(slot.name); |
| 1436 |
} |
| 1437 |
} |
| 1438 |
} |
| 1439 |
(names, described) |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
fn gathered(pass: &Pass<'_>, over: Option<&str>) -> Params { |
| 1448 |
over.map_or_else(Params::new, |_| { |
| 1449 |
pass.view.gathering(quasi_router::Node::TICKED) |
| 1450 |
}) |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
fn form( |
| 1455 |
pass: &mut Pass<'_>, |
| 1456 |
ui: &mut Ui, |
| 1457 |
fields: &[quasi_router::Field], |
| 1458 |
submit: &str, |
| 1459 |
action: &Action, |
| 1460 |
) { |
| 1461 |
for described in fields { |
| 1462 |
field_node(pass, ui, described); |
| 1463 |
} |
| 1464 |
if ui.button(RichText::new(submit)).clicked() { |
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
let (names, described) = submitted(pass, fields); |
| 1469 |
let payload = pass.view.submission(&names, &described); |
| 1470 |
pass.fire(action, payload, None); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
fn unfolded<'a, T: quasi_router::Outline>(rows: &'a [T], view: &crate::View) -> Vec<&'a T> { |
| 1481 |
let hidden = quasi_router::folded_by(rows.iter().map(|row| { |
| 1482 |
let open = row.open().map(|described| view.open(&row.key(), described)); |
| 1483 |
(row.depth(), open) |
| 1484 |
})); |
| 1485 |
rows.iter() |
| 1486 |
.zip(hidden) |
| 1487 |
.filter(|(_, hidden)| !*hidden) |
| 1488 |
.map(|(row, _)| row) |
| 1489 |
.collect() |
| 1490 |
} |
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
fn outline_lead( |
| 1504 |
ui: &mut Ui, |
| 1505 |
view: &crate::View, |
| 1506 |
row: &impl quasi_router::Outline, |
| 1507 |
) -> (Option<(String, bool)>, Option<egui::Rect>) { |
| 1508 |
let step = ui.spacing().indent; |
| 1509 |
let depth = f32::from(row.depth().level); |
| 1510 |
if row.depth().is_nested() { |
| 1511 |
ui.add_space(step * depth); |
| 1512 |
} |
| 1513 |
let Some(described) = row.open() else { |
| 1514 |
ui.add_space(step); |
| 1515 |
return (None, None); |
| 1516 |
}; |
| 1517 |
let key = row.key(); |
| 1518 |
let open = view.open(&key, described); |
| 1519 |
let pressed = ui.small_button(if open { "\u{25bc}" } else { "\u{25b6}" }); |
| 1520 |
let folded = pressed.clicked().then_some((key, open)); |
| 1521 |
(folded, Some(pressed.rect)) |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
fn list_row(pass: &mut Pass<'_>, ui: &mut Ui, row: &Row, within: usize, branches: bool) { |
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
let mut folded = None; |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
let mut chevron = None; |
| 1540 |
let strip = ui.horizontal(|ui| { |
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
if branches { |
| 1547 |
(folded, chevron) = outline_lead(ui, pass.view, row); |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
if let Some(ticked) = row.selected { |
| 1554 |
let mut on = row |
| 1555 |
.value |
| 1556 |
.as_ref() |
| 1557 |
.map_or(ticked, |value| pass.view.is_ticked(value)); |
| 1558 |
if ui.checkbox(&mut on, "").changed() { |
| 1559 |
if let Some(action) = &row.toggle { |
| 1560 |
pass.fire(action, Params::new(), None); |
| 1561 |
} else if let Some(value) = &row.value { |
| 1562 |
pass.view.tick(value); |
| 1563 |
} |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
let spacing = ui.spacing().item_spacing.x; |
| 1577 |
for (index, part) in row.cells.iter().enumerate() { |
| 1578 |
let after = tail_width(ui, pass.immediate, &row.cells[index + 1..], spacing); |
| 1579 |
row_part(pass, ui, part, after); |
| 1580 |
} |
| 1581 |
}); |
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
crate::geometry::note_row( |
| 1588 |
ui, |
| 1589 |
row.value.as_deref(), |
| 1590 |
within, |
| 1591 |
strip.response.rect, |
| 1592 |
row.chosen == Some(true), |
| 1593 |
); |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
if let Some((key, open)) = folded { |
| 1614 |
pass.view.fold(&key, open); |
| 1615 |
return; |
| 1616 |
} |
| 1617 |
|
| 1618 |
if row.activate.is_some() || !row.menu.is_empty() { |
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
let id = match row.value.as_deref() { |
| 1632 |
Some(value) => ui.id().with(("row", value)), |
| 1633 |
None => ui.id().with(("row-at", within)), |
| 1634 |
}; |
| 1635 |
|
| 1636 |
let mut target = strip.response.rect; |
| 1637 |
if let Some(chevron) = chevron { |
| 1638 |
target.min.x = target.min.x.max(chevron.max.x); |
| 1639 |
} |
| 1640 |
let response = ui.interact(target, id, egui::Sense::click()); |
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
let named = row_name(row); |
| 1656 |
response.widget_info(|| { |
| 1657 |
egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), &named) |
| 1658 |
}); |
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
if let Some((action, confirm)) = row_menu(pass.immediate, &response, &row.menu) { |
| 1663 |
pass.fire(&action, Params::new(), confirm.as_deref()); |
| 1664 |
} else if let Some(action) = &row.activate |
| 1665 |
&& response.clicked() |
| 1666 |
{ |
| 1667 |
pass.fire(action, choosing(ui, row.chosen), None); |
| 1668 |
} |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
fn announce_row(ui: &Ui, response: &egui::Response, index: usize, row: &Row) { |
| 1683 |
if index != 0 || (row.activate.is_none() && row.menu.is_empty()) { |
| 1684 |
return; |
| 1685 |
} |
| 1686 |
let named = row_name(row); |
| 1687 |
response.widget_info(|| { |
| 1688 |
egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), &named) |
| 1689 |
}); |
| 1690 |
} |
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
fn row_leaf_text(parts: &[Node]) -> String { |
| 1695 |
parts |
| 1696 |
.iter() |
| 1697 |
.find_map(|part| match part { |
| 1698 |
Node::Text { text, .. } | Node::Heading { text, .. } | Node::Link { text, .. } => { |
| 1699 |
Some(text.clone()) |
| 1700 |
} |
| 1701 |
_ => None, |
| 1702 |
}) |
| 1703 |
.unwrap_or_default() |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
fn row_name(row: &quasi_router::Row) -> String { |
| 1718 |
row.cells |
| 1719 |
.iter() |
| 1720 |
.find_map(|cell| { |
| 1721 |
let said = row_leaf_text(&cell.content); |
| 1722 |
(!said.is_empty()).then_some(said) |
| 1723 |
}) |
| 1724 |
.unwrap_or_default() |
| 1725 |
} |
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
fn row_part(pass: &mut Pass<'_>, ui: &mut Ui, part: &quasi_router::Cell, after: Option<f32>) { |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
let flow = part.room(); |
| 1743 |
for node in &part.content { |
| 1744 |
match node { |
| 1745 |
Node::Text { text, .. } => capped(pass.immediate, ui, text, flow, after), |
| 1746 |
Node::Rich { source, .. } => { |
| 1747 |
capped( |
| 1748 |
pass.immediate, |
| 1749 |
ui, |
| 1750 |
&docengine::render_plain(source), |
| 1751 |
flow, |
| 1752 |
after, |
| 1753 |
); |
| 1754 |
} |
| 1755 |
other => draw(pass, ui, other), |
| 1756 |
} |
| 1757 |
} |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
fn tail_width( |
| 1768 |
ui: &Ui, |
| 1769 |
immediate: &Immediate, |
| 1770 |
rest: &[quasi_router::Cell], |
| 1771 |
spacing: f32, |
| 1772 |
) -> Option<f32> { |
| 1773 |
if rest.is_empty() { |
| 1774 |
return None; |
| 1775 |
} |
| 1776 |
let mut total = 0.0; |
| 1777 |
for part in rest { |
| 1778 |
total += intrinsic_width(ui, immediate, part)? + spacing; |
| 1779 |
} |
| 1780 |
Some(total) |
| 1781 |
} |
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
fn intrinsic_width(ui: &Ui, immediate: &Immediate, part: &quasi_router::Cell) -> Option<f32> { |
| 1789 |
let mut total = 0.0; |
| 1790 |
for node in &part.content { |
| 1791 |
total += leaf_width(ui, immediate, node)?; |
| 1792 |
} |
| 1793 |
Some(total) |
| 1794 |
} |
| 1795 |
|
| 1796 |
|
| 1797 |
fn leaf_width(ui: &Ui, immediate: &Immediate, node: &Node) -> Option<f32> { |
| 1798 |
match node { |
| 1799 |
Node::Text { text, .. } => Some(text_width(ui, text)), |
| 1800 |
Node::Rich { source, .. } => Some(text_width(ui, &docengine::render_plain(source))), |
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
Node::Token(tag) => { |
| 1806 |
Some(text_width(ui, &tag.label) + immediate.widget.token_padding.x * 2.0) |
| 1807 |
} |
| 1808 |
Node::Act(act) => { |
| 1809 |
let drawn = act.as_layout(); |
| 1810 |
let label = match drawn.key { |
| 1811 |
Some(key) => format!("{} ({key})", drawn.label), |
| 1812 |
None => drawn.label.to_owned(), |
| 1813 |
}; |
| 1814 |
|
| 1815 |
|
| 1816 |
Some(text_width(ui, &label) + ui.spacing().button_padding.x * 2.0) |
| 1817 |
} |
| 1818 |
_ => None, |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
fn text_width(ui: &Ui, text: &str) -> f32 { |
| 1824 |
let job = egui::text::LayoutJob::single_section( |
| 1825 |
text.to_owned(), |
| 1826 |
egui::TextFormat { |
| 1827 |
font_id: egui::TextStyle::Body.resolve(ui.style()), |
| 1828 |
..Default::default() |
| 1829 |
}, |
| 1830 |
); |
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
ui.painter().layout_job(job).rect.width() |
| 1836 |
} |
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
fn budget(available: f32, after: Option<f32>) -> f32 { |
| 1845 |
match after { |
| 1846 |
Some(reserved) if reserved < available => available - reserved, |
| 1847 |
_ => available, |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
fn capped(immediate: &Immediate, ui: &mut Ui, text: &str, flow: layout::Flow, after: Option<f32>) { |
| 1863 |
let mut job = egui::text::LayoutJob::single_section( |
| 1864 |
text.to_owned(), |
| 1865 |
egui::TextFormat { |
| 1866 |
font_id: egui::TextStyle::Body.resolve(ui.style()), |
| 1867 |
color: immediate.palette.content, |
| 1868 |
..Default::default() |
| 1869 |
}, |
| 1870 |
); |
| 1871 |
job.wrap = egui::text::TextWrapping { |
| 1872 |
max_width: budget(ui.available_width(), after), |
| 1873 |
max_rows: flow.lines() as usize, |
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
break_anywhere: flow.lines() == 1, |
| 1878 |
overflow_character: Some('\u{2026}'), |
| 1879 |
}; |
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
let galley = ui.painter().layout_job(job); |
| 1889 |
ui.add(egui::Label::new(galley)); |
| 1890 |
} |
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
fn cell_row_response(ui: &mut Ui) -> egui::Response { |
| 1905 |
ui.interact( |
| 1906 |
ui.min_rect(), |
| 1907 |
ui.id().with("cell-row"), |
| 1908 |
egui::Sense::click(), |
| 1909 |
) |
| 1910 |
} |
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
fn choosing(ui: &Ui, chosen: Option<bool>) -> Params { |
| 1932 |
if chosen.is_none() { |
| 1933 |
return Params::new(); |
| 1934 |
} |
| 1935 |
let modifiers = ui.ctx().input(|input| input.modifiers); |
| 1936 |
let meant = if modifiers.shift { |
| 1937 |
quasi_router::Choosing::Through |
| 1938 |
} else if modifiers.command { |
| 1939 |
quasi_router::Choosing::Also |
| 1940 |
} else { |
| 1941 |
quasi_router::Choosing::Only |
| 1942 |
}; |
| 1943 |
Params::new().with( |
| 1944 |
quasi_router::Node::CHOOSING.to_owned(), |
| 1945 |
meant.as_str().to_owned(), |
| 1946 |
) |
| 1947 |
} |
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
fn row_menu( |
| 1971 |
immediate: &crate::Immediate, |
| 1972 |
response: &egui::Response, |
| 1973 |
menu: &[Act], |
| 1974 |
) -> Option<(Action, Option<String>)> { |
| 1975 |
if menu.is_empty() { |
| 1976 |
return None; |
| 1977 |
} |
| 1978 |
let mut picked = None; |
| 1979 |
response.context_menu(|ui| { |
| 1980 |
for act in menu { |
| 1981 |
let pressed = widget::act(ui, &act.as_layout(), &immediate.palette, &immediate.widget); |
| 1982 |
if pressed.clicked() && act.state != Some(layout::State::Disabled) { |
| 1983 |
picked = Some((act.action.clone(), act.confirm.clone())); |
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
ui.close(); |
| 1989 |
} |
| 1990 |
} |
| 1991 |
}); |
| 1992 |
picked |
| 1993 |
} |
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
const TICK_COLUMN: &str = "select"; |
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
fn table_columns<'a>(columns: &'a [quasi_router::Column], ticks: bool) -> Vec<layout::Column<'a>> { |
| 2020 |
let mut borrowed: Vec<layout::Column<'a>> = |
| 2021 |
Vec::with_capacity(columns.len() + usize::from(ticks)); |
| 2022 |
if ticks { |
| 2023 |
borrowed.push(layout::Column { |
| 2024 |
width: layout::Width::Fixed, |
| 2025 |
priority: layout::Priority::Essential, |
| 2026 |
..layout::Column::new(TICK_COLUMN) |
| 2027 |
}); |
| 2028 |
} |
| 2029 |
borrowed.extend(columns.iter().map(|c| c.as_layout())); |
| 2030 |
borrowed |
| 2031 |
} |
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
fn tick_cell(ui: &mut Ui, view: &crate::View, row: &Row) -> Option<String> { |
| 2046 |
let (Some(_), Some(value)) = (row.selected, row.value.as_ref()) else { |
| 2047 |
return None; |
| 2048 |
}; |
| 2049 |
let mut on = view.is_ticked(value); |
| 2050 |
ui.checkbox(&mut on, "").changed().then(|| value.clone()) |
| 2051 |
} |
| 2052 |
|
| 2053 |
fn table(pass: &mut Pass<'_>, ui: &mut Ui, columns: &[quasi_router::Column], rows: &[Row]) { |
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
let branches = rows.iter().any(|row| row.open.is_some()); |
| 2064 |
let shown = unfolded(rows, pass.view); |
| 2065 |
let rows = shown.as_slice(); |
| 2066 |
|
| 2067 |
let ticks = rows.iter().any(|row| row.selected.is_some()); |
| 2068 |
let borrowed = table_columns(columns, ticks); |
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
let current = |at: usize| { |
| 2082 |
rows.get(at) |
| 2083 |
.is_some_and(|row| row.current || row.chosen == Some(true)) |
| 2084 |
}; |
| 2085 |
let body = makeover_immediate::table::Body { |
| 2086 |
rows: rows.len(), |
| 2087 |
selected: Some(¤t), |
| 2088 |
scroll_to: None, |
| 2089 |
}; |
| 2090 |
let sizing = makeover_immediate::table::Sizing { |
| 2091 |
lengths: &[], |
| 2092 |
fallback: CELL_WIDTH, |
| 2093 |
}; |
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
let mut fired: Option<(Action, Params)> = None; |
| 2099 |
|
| 2100 |
|
| 2101 |
let mut toggled: Option<String> = None; |
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
let mut menued: Option<(Action, Option<String>)> = None; |
| 2107 |
|
| 2108 |
|
| 2109 |
let mut folded: Option<(String, bool)> = None; |
| 2110 |
|
| 2111 |
let reordered = makeover_immediate::table::table( |
| 2112 |
ui, |
| 2113 |
&borrowed, |
| 2114 |
&body, |
| 2115 |
&sizing, |
| 2116 |
&pass.immediate.palette, |
| 2117 |
&pass.immediate.table, |
| 2118 |
|ui, column, at| { |
| 2119 |
let Some(row) = rows.get(at) else { return }; |
| 2120 |
|
| 2121 |
|
| 2122 |
'cell: { |
| 2123 |
if ticks && column.name == TICK_COLUMN { |
| 2124 |
if let Some(value) = tick_cell(ui, pass.view, row) { |
| 2125 |
toggled = Some(value); |
| 2126 |
} |
| 2127 |
break 'cell; |
| 2128 |
} |
| 2129 |
|
| 2130 |
let Some(index) = columns.iter().position(|c| c.name == column.name) else { |
| 2131 |
break 'cell; |
| 2132 |
}; |
| 2133 |
let Some(cell) = row.cells.get(index) else { |
| 2134 |
break 'cell; |
| 2135 |
}; |
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
if index == 0 && branches { |
| 2141 |
let (pressed, _) = outline_lead(ui, pass.view, *row); |
| 2142 |
folded = folded.take().or(pressed); |
| 2143 |
} |
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
if index == 0 |
| 2150 |
&& let Some(change) = row.change |
| 2151 |
{ |
| 2152 |
let (sign, colour) = match change { |
| 2153 |
layout::Change::Added => ("+", pass.immediate.palette.success), |
| 2154 |
layout::Change::Removed => ("-", pass.immediate.palette.danger), |
| 2155 |
|
| 2156 |
|
| 2157 |
_ => (" ", pass.immediate.palette.content_muted), |
| 2158 |
}; |
| 2159 |
ui.label(RichText::new(sign).monospace().color(colour)); |
| 2160 |
} |
| 2161 |
for node in &cell.content { |
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
match node { |
| 2166 |
Node::Act(act) if act.state != Some(layout::State::Disabled) => { |
| 2167 |
if widget::act( |
| 2168 |
ui, |
| 2169 |
&act.as_layout(), |
| 2170 |
&pass.immediate.palette, |
| 2171 |
&pass.immediate.widget, |
| 2172 |
) |
| 2173 |
.clicked() |
| 2174 |
{ |
| 2175 |
fired = Some((act.action.clone(), Params::new())); |
| 2176 |
} |
| 2177 |
} |
| 2178 |
Node::Link { text, action } => { |
| 2179 |
if ui |
| 2180 |
.link(RichText::new(text).color(pass.immediate.palette.action)) |
| 2181 |
.clicked() |
| 2182 |
{ |
| 2183 |
fired = Some((action.clone(), Params::new())); |
| 2184 |
} |
| 2185 |
} |
| 2186 |
other => leaf(pass.immediate, ui, other), |
| 2187 |
} |
| 2188 |
} |
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
let response = cell_row_response(ui); |
| 2199 |
announce_row(ui, &response, index, row); |
| 2200 |
if let Some((action, confirm)) = row_menu(pass.immediate, &response, &row.menu) { |
| 2201 |
menued = Some((action, confirm)); |
| 2202 |
} else if let Some(action) = &row.activate |
| 2203 |
&& response.clicked() |
| 2204 |
{ |
| 2205 |
fired = Some((action.clone(), choosing(ui, row.chosen))); |
| 2206 |
} |
| 2207 |
} |
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
crate::geometry::note_row( |
| 2213 |
ui, |
| 2214 |
row.value.as_deref(), |
| 2215 |
at, |
| 2216 |
ui.min_rect(), |
| 2217 |
row.chosen == Some(true), |
| 2218 |
); |
| 2219 |
}, |
| 2220 |
); |
| 2221 |
|
| 2222 |
if let Some(value) = toggled { |
| 2223 |
pass.view.tick(&value); |
| 2224 |
} |
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
let reorder = reordered |
| 2229 |
.and_then(|pressed| columns.iter().find(|column| column.name == pressed.name)) |
| 2230 |
.and_then(|column| column.reorder.clone()); |
| 2231 |
table_pressed(pass, folded, menued, fired, reorder); |
| 2232 |
} |
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
fn table_pressed( |
| 2237 |
pass: &mut Pass<'_>, |
| 2238 |
folded: Option<(String, bool)>, |
| 2239 |
menued: Option<(Action, Option<String>)>, |
| 2240 |
fired: Option<(Action, Params)>, |
| 2241 |
reorder: Option<Action>, |
| 2242 |
) { |
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
if let Some((key, open)) = folded { |
| 2248 |
pass.view.fold(&key, open); |
| 2249 |
} else { |
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
if let Some((action, confirm)) = menued { |
| 2255 |
pass.fire(&action, Params::new(), confirm.as_deref()); |
| 2256 |
} |
| 2257 |
|
| 2258 |
if let Some((action, payload)) = fired { |
| 2259 |
pass.fire(&action, payload, None); |
| 2260 |
} |
| 2261 |
} |
| 2262 |
|
| 2263 |
if let Some(action) = reorder { |
| 2264 |
pass.fire(&action, Params::new(), None); |
| 2265 |
} |
| 2266 |
} |
| 2267 |
|
| 2268 |
|
| 2269 |
pub(crate) fn region(pass: &mut Pass<'_>, ui: &mut Ui, slot: &Slot) { |
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
let drawn = ui.scope(|ui| region_body(pass, ui, slot)); |
| 2281 |
crate::geometry::note_region(ui, &slot.id, drawn.response.rect); |
| 2282 |
} |
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
fn region_consults(pass: &mut Pass<'_>, ui: &mut Ui, slot: &Slot) { |
| 2297 |
if slot.consults.is_empty() { |
| 2298 |
return; |
| 2299 |
} |
| 2300 |
let inside = slot.questions(); |
| 2301 |
let moved = inside |
| 2302 |
.iter() |
| 2303 |
.find(|field| pass.stirred.contains(&field.name)) |
| 2304 |
.map(|field| { |
| 2305 |
pass.view |
| 2306 |
.buffer(&field.name, field.value.as_deref()) |
| 2307 |
.clone() |
| 2308 |
}); |
| 2309 |
|
| 2310 |
for (at, consult) in slot.consults.iter().enumerate() { |
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
if let Some(moved) = &moved { |
| 2315 |
if consult.asks_about(moved) { |
| 2316 |
pass.view.wait_to_consult( |
| 2317 |
Asking::Region(slot.id.clone()), |
| 2318 |
at, |
| 2319 |
std::time::Instant::now() + consult.after, |
| 2320 |
); |
| 2321 |
} else { |
| 2322 |
|
| 2323 |
|
| 2324 |
pass.view.consulted(Asking::Region(slot.id.clone()), at); |
| 2325 |
} |
| 2326 |
} |
| 2327 |
|
| 2328 |
let Some(due) = pass.view.consult_due(Asking::Region(slot.id.clone()), at) else { |
| 2329 |
continue; |
| 2330 |
}; |
| 2331 |
let now = std::time::Instant::now(); |
| 2332 |
if now < due { |
| 2333 |
|
| 2334 |
|
| 2335 |
ui.ctx().request_repaint_after(due - now); |
| 2336 |
continue; |
| 2337 |
} |
| 2338 |
pass.view.consulted(Asking::Region(slot.id.clone()), at); |
| 2339 |
|
| 2340 |
|
| 2341 |
let mut payload = pass.view.contributed(&consult.sends); |
| 2342 |
for field in &inside { |
| 2343 |
|
| 2344 |
|
| 2345 |
|
| 2346 |
payload.insert( |
| 2347 |
field.name.clone(), |
| 2348 |
pass.view |
| 2349 |
.buffer(&field.name, field.value.as_deref()) |
| 2350 |
.clone(), |
| 2351 |
); |
| 2352 |
} |
| 2353 |
pass.fire(&consult.action, payload, None); |
| 2354 |
} |
| 2355 |
} |
| 2356 |
|
| 2357 |
|
| 2358 |
fn region_body(pass: &mut Pass<'_>, ui: &mut Ui, slot: &Slot) { |
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
if pass.hidden.out(&slot.id) { |
| 2364 |
return; |
| 2365 |
} |
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
match slot.readiness { |
| 2370 |
layout::Readiness::Pending => { |
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 |
|
| 2381 |
widget::awaiting( |
| 2382 |
ui, |
| 2383 |
slot.awaiting().unwrap_or_default(), |
| 2384 |
pass.view.progress_at(std::time::Instant::now()), |
| 2385 |
pass.immediate.reduced_motion(), |
| 2386 |
pass.immediate.palette(), |
| 2387 |
&pass.immediate.widget, |
| 2388 |
); |
| 2389 |
return; |
| 2390 |
} |
| 2391 |
layout::Readiness::Failed => { |
| 2392 |
ui.label( |
| 2393 |
RichText::new("This did not load.") |
| 2394 |
.color(pass.immediate.palette.tone(layout::Tone::Danger)), |
| 2395 |
); |
| 2396 |
return; |
| 2397 |
} |
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
_ => {} |
| 2402 |
} |
| 2403 |
|
| 2404 |
let cutoff = cutoff(ui.available_width()); |
| 2405 |
run(pass, ui, slot, cutoff); |
| 2406 |
if slot.showing().selective() { |
| 2407 |
showing_body(pass, ui, slot, cutoff); |
| 2408 |
} else if let Some(repeating) = slot.repeating.as_deref() { |
| 2409 |
repeating_body(pass, ui, slot, repeating); |
| 2410 |
} else { |
| 2411 |
for placed in slot.body.iter().filter(|placed| placed.kept_at(cutoff)) { |
| 2412 |
draw(pass, ui, &placed.node); |
| 2413 |
} |
| 2414 |
} |
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
region_consults(pass, ui, slot); |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
if let RegionKind::Handover { .. } | RegionKind::Ceded { .. } = slot.kind { |
| 2426 |
if let Some(fill) = pass.immediate.fill(&slot.id) { |
| 2427 |
fill(pass.immediate, ui); |
| 2428 |
} else if slot.kind.as_layout().owed() { |
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
unfilled(pass.immediate, ui); |
| 2433 |
} |
| 2434 |
} |
| 2435 |
} |
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
fn repeating_body( |
| 2455 |
pass: &mut Pass<'_>, |
| 2456 |
ui: &mut Ui, |
| 2457 |
slot: &Slot, |
| 2458 |
repeating: &quasi_router::Repeating, |
| 2459 |
) { |
| 2460 |
let standing = slot.body.len(); |
| 2461 |
for (at, placed) in slot.body.iter().enumerate() { |
| 2462 |
|
| 2463 |
draw( |
| 2464 |
pass, |
| 2465 |
ui, |
| 2466 |
&Node::section(format!("{} {}", repeating.one, at + 1)), |
| 2467 |
); |
| 2468 |
draw(pass, ui, &placed.node); |
| 2469 |
|
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
if let Node::Region(child) = &placed.node |
| 2476 |
&& let Some(removes) = &child.removes |
| 2477 |
{ |
| 2478 |
draw( |
| 2479 |
pass, |
| 2480 |
ui, |
| 2481 |
&Node::Act(bounded(removes, repeating.may_remove(standing))), |
| 2482 |
); |
| 2483 |
} |
| 2484 |
} |
| 2485 |
draw( |
| 2486 |
pass, |
| 2487 |
ui, |
| 2488 |
&Node::Act(bounded(&repeating.add, repeating.may_add(standing))), |
| 2489 |
); |
| 2490 |
} |
| 2491 |
|
| 2492 |
|
| 2493 |
fn bounded(act: &quasi_router::Act, allowed: bool) -> quasi_router::Act { |
| 2494 |
if allowed { |
| 2495 |
act.clone() |
| 2496 |
} else { |
| 2497 |
act.clone().disabled() |
| 2498 |
} |
| 2499 |
} |
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
|
| 2511 |
|
| 2512 |
|
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
fn showing_body(pass: &mut Pass<'_>, ui: &mut Ui, slot: &Slot, cutoff: layout::Priority) { |
| 2534 |
let at = pass.view.shown(slot); |
| 2535 |
let labels = slot.labels(); |
| 2536 |
let total = slot.body.len(); |
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
|
| 2541 |
let disclosure = slot.showing().dismissible() && total == 1 && labels.len() == 1; |
| 2542 |
|
| 2543 |
if disclosure { |
| 2544 |
if ui.selectable_label(at.is_some(), labels[0]).clicked() { |
| 2545 |
pass.view.disclose(slot); |
| 2546 |
} |
| 2547 |
} else if !labels.is_empty() { |
| 2548 |
|
| 2549 |
|
| 2550 |
ui.horizontal(|ui| { |
| 2551 |
for (index, label) in labels.iter().enumerate() { |
| 2552 |
if ui.selectable_label(at == Some(index), *label).clicked() { |
| 2553 |
pass.view.show(&slot.id, index); |
| 2554 |
|
| 2555 |
|
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
if let Some(action) = fed_child(slot, index) { |
| 2560 |
pass.fire(action, Params::new(), None); |
| 2561 |
} |
| 2562 |
} |
| 2563 |
} |
| 2564 |
}); |
| 2565 |
} |
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
if let Some(index) = at |
| 2570 |
&& let Some(placed) = slot.body.get(index) |
| 2571 |
&& placed.kept_at(cutoff) |
| 2572 |
{ |
| 2573 |
draw(pass, ui, &placed.node); |
| 2574 |
} |
| 2575 |
|
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
if labels.is_empty() && !disclosure { |
| 2580 |
ui.horizontal(|ui| { |
| 2581 |
if ui.button("Prev").clicked() { |
| 2582 |
pass.view.show_by(slot, -1); |
| 2583 |
} |
| 2584 |
ui.label(format!("{} / {total}", at.map_or(0, |index| index + 1))); |
| 2585 |
if ui.button("Next").clicked() { |
| 2586 |
pass.view.show_by(slot, 1); |
| 2587 |
} |
| 2588 |
}); |
| 2589 |
} |
| 2590 |
} |
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
fn fed_child(slot: &Slot, at: usize) -> Option<&Action> { |
| 2598 |
match &slot.body.get(at)?.node { |
| 2599 |
Node::Region(child) => child.fed_by.as_deref(), |
| 2600 |
_ => None, |
| 2601 |
} |
| 2602 |
} |
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
|
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
fn run(pass: &mut Pass<'_>, ui: &mut Ui, slot: &Slot, cutoff: layout::Priority) { |
| 2643 |
let Some(run) = slot.run.as_ref() else { |
| 2644 |
return; |
| 2645 |
}; |
| 2646 |
let kept = run.kept_at(cutoff); |
| 2647 |
ui.horizontal_wrapped(|ui| { |
| 2648 |
let mut fills = kept |
| 2649 |
.iter() |
| 2650 |
.filter(|placed| matches!(placed.width, layout::Width::Fill)) |
| 2651 |
.count(); |
| 2652 |
for placed in &kept { |
| 2653 |
if !matches!(placed.width, layout::Width::Fill) { |
| 2654 |
draw(pass, ui, &placed.node); |
| 2655 |
continue; |
| 2656 |
} |
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
let share = ui.available_width() / fills as f32; |
| 2665 |
fills = fills.saturating_sub(1); |
| 2666 |
let height = ui.available_height(); |
| 2667 |
ui.allocate_ui(egui::vec2(share, height), |ui| { |
| 2668 |
draw(pass, ui, &placed.node); |
| 2669 |
}); |
| 2670 |
} |
| 2671 |
if matches!(run.fallback, layout::Fallback::Menu) { |
| 2672 |
let shed: Vec<&quasi_router::Ranked> = run |
| 2673 |
.members |
| 2674 |
.iter() |
| 2675 |
.filter(|member| !member.kept_at(cutoff)) |
| 2676 |
.collect(); |
| 2677 |
if !shed.is_empty() { |
| 2678 |
|
| 2679 |
|
| 2680 |
|
| 2681 |
ui.menu_button(format!("{} more", shed.len()), |ui| { |
| 2682 |
for placed in shed { |
| 2683 |
draw(pass, ui, &placed.node); |
| 2684 |
} |
| 2685 |
}); |
| 2686 |
} |
| 2687 |
} |
| 2688 |
}); |
| 2689 |
} |
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
|
| 2696 |
|
| 2697 |
|
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
pub(crate) fn cutoff(width: f32) -> layout::Priority { |
| 2706 |
if width < 600.0 { |
| 2707 |
layout::Priority::Essential |
| 2708 |
} else if width < 840.0 { |
| 2709 |
layout::Priority::Secondary |
| 2710 |
} else { |
| 2711 |
layout::Priority::Optional |
| 2712 |
} |
| 2713 |
} |
| 2714 |
|