| 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 |
use egui::{Align, Layout, Response, RichText, Sense, Ui, Vec2}; |
| 27 |
use makeover_layout::{Act, Awaiting, Bar, Chart, Figure, Meter, State, Token, Tone}; |
| 28 |
use makeover_timing::activity_blink; |
| 29 |
use std::time::Duration; |
| 30 |
|
| 31 |
use crate::Palette; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#[derive(Debug, Clone, Copy, PartialEq)] |
| 39 |
pub struct WidgetStyle { |
| 40 |
|
| 41 |
pub meter_height: f32, |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
pub meter_width: Option<f32>, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
pub chart_height: f32, |
| 55 |
|
| 56 |
pub radius: u8, |
| 57 |
|
| 58 |
pub token_padding: Vec2, |
| 59 |
|
| 60 |
pub figure_gap: f32, |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
pub figure_scale: f32, |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub mark_size: f32, |
| 73 |
} |
| 74 |
|
| 75 |
impl Default for WidgetStyle { |
| 76 |
|
| 77 |
|
| 78 |
fn default() -> Self { |
| 79 |
Self { |
| 80 |
meter_height: 6.0, |
| 81 |
meter_width: None, |
| 82 |
chart_height: 200.0, |
| 83 |
radius: 3, |
| 84 |
token_padding: Vec2::new(6.0, 2.0), |
| 85 |
figure_gap: 2.0, |
| 86 |
figure_scale: 2.0, |
| 87 |
mark_size: 8.0, |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
pub fn meter(ui: &mut Ui, meter: &Meter<'_>, palette: &Palette, style: &WidgetStyle) -> Response { |
| 108 |
let width = style |
| 109 |
.meter_width |
| 110 |
.unwrap_or_else(|| ui.available_width().max(1.0)); |
| 111 |
ui.horizontal(|ui| { |
| 112 |
let (rect, response) = |
| 113 |
ui.allocate_exact_size(Vec2::new(width, style.meter_height), Sense::hover()); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
ui.painter().rect_filled(rect, style.radius, palette.sunken); |
| 119 |
let share = if meter.total == 0 { |
| 120 |
0.0 |
| 121 |
} else { |
| 122 |
(f64::from(meter.done) / f64::from(meter.total)).min(1.0) |
| 123 |
}; |
| 124 |
#[expect( |
| 125 |
clippy::cast_possible_truncation, |
| 126 |
reason = "a share is 0..=1 and the product is a width in points" |
| 127 |
)] |
| 128 |
let filled = (f64::from(rect.width()) * share) as f32; |
| 129 |
if filled > 0.0 { |
| 130 |
let mut fill = rect; |
| 131 |
fill.set_width(filled); |
| 132 |
ui.painter() |
| 133 |
.rect_filled(fill, style.radius, palette.tone(meter.tone)); |
| 134 |
} |
| 135 |
let reading = match meter.label { |
| 136 |
Some(label) => format!("{}/{} {label}", meter.done, meter.total), |
| 137 |
None => format!("{}/{}", meter.done, meter.total), |
| 138 |
}; |
| 139 |
ui.label(RichText::new(reading).color(palette.content_muted)); |
| 140 |
response |
| 141 |
}) |
| 142 |
.inner |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
pub fn token( |
| 165 |
ui: &mut Ui, |
| 166 |
label: &str, |
| 167 |
kind: Token, |
| 168 |
tone: Tone, |
| 169 |
latched: bool, |
| 170 |
palette: &Palette, |
| 171 |
style: &WidgetStyle, |
| 172 |
) -> Response { |
| 173 |
let painted = palette.tone(tone); |
| 174 |
let radius = match kind { |
| 175 |
|
| 176 |
Token::Badge => u8::MAX, |
| 177 |
Token::Chip { .. } => style.radius, |
| 178 |
}; |
| 179 |
let sense = if kind.interactive() { |
| 180 |
Sense::click() |
| 181 |
} else { |
| 182 |
Sense::hover() |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
let ink = if latched { palette.page } else { painted }; |
| 189 |
let galley = ui.painter().layout_no_wrap( |
| 190 |
label.to_owned(), |
| 191 |
egui::TextStyle::Body.resolve(ui.style()), |
| 192 |
ink, |
| 193 |
); |
| 194 |
let size = galley.size() + style.token_padding * 2.0; |
| 195 |
let (rect, response) = ui.allocate_exact_size(size, sense); |
| 196 |
|
| 197 |
if latched { |
| 198 |
ui.painter().rect_filled(rect, radius, painted); |
| 199 |
} else { |
| 200 |
ui.painter().rect_stroke( |
| 201 |
rect, |
| 202 |
radius, |
| 203 |
egui::Stroke::new(1.0, painted), |
| 204 |
egui::StrokeKind::Inside, |
| 205 |
); |
| 206 |
} |
| 207 |
ui.painter() |
| 208 |
.galley(rect.center() - galley.size() / 2.0, galley, ink); |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
let role = if kind.interactive() { |
| 223 |
egui::WidgetType::Button |
| 224 |
} else { |
| 225 |
egui::WidgetType::Label |
| 226 |
}; |
| 227 |
response.widget_info(|| { |
| 228 |
let mut info = egui::WidgetInfo::labeled(role, ui.is_enabled(), label); |
| 229 |
if kind.interactive() { |
| 230 |
info.selected = Some(latched); |
| 231 |
} |
| 232 |
info |
| 233 |
}); |
| 234 |
response |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
pub fn act(ui: &mut Ui, act: &Act<'_>, palette: &Palette, _style: &WidgetStyle) -> Response { |
| 251 |
let disabled = act.state.is_some_and(State::suppresses_interaction); |
| 252 |
let label = match act.key { |
| 253 |
Some(key) => format!("{} ({key})", act.label), |
| 254 |
None => act.label.to_owned(), |
| 255 |
}; |
| 256 |
let colour = if disabled { |
| 257 |
palette.content_muted |
| 258 |
} else { |
| 259 |
palette.tone(act.tone) |
| 260 |
}; |
| 261 |
let drawn = ui.add_enabled( |
| 262 |
!disabled, |
| 263 |
egui::Button::new(RichText::new(label).color(colour)), |
| 264 |
); |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
match act.hint { |
| 273 |
Some(hint) => drawn.on_hover_text(hint), |
| 274 |
None => drawn, |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
pub fn figure( |
| 286 |
ui: &mut Ui, |
| 287 |
figure: &Figure<'_>, |
| 288 |
palette: &Palette, |
| 289 |
style: &WidgetStyle, |
| 290 |
) -> Response { |
| 291 |
ui.with_layout(Layout::top_down(Align::Min), |ui| { |
| 292 |
let value = match figure.change { |
| 293 |
Some(change) => format!("{} {change}", figure.value), |
| 294 |
None => figure.value.to_owned(), |
| 295 |
}; |
| 296 |
let size = egui::TextStyle::Body.resolve(ui.style()).size * style.figure_scale; |
| 297 |
let shown = ui.label( |
| 298 |
RichText::new(value) |
| 299 |
.color(palette.tone(figure.tone)) |
| 300 |
.size(size) |
| 301 |
.strong(), |
| 302 |
); |
| 303 |
ui.add_space(style.figure_gap); |
| 304 |
ui.label(RichText::new(figure.caption).color(palette.content_muted)); |
| 305 |
shown |
| 306 |
}) |
| 307 |
.inner |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 317 |
pub struct Progress { |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
pub delivered: Option<u64>, |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
pub elapsed: Option<Duration>, |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
pub fn activity(ui: &mut Ui, reduced: bool, palette: &Palette, style: &WidgetStyle) -> Response { |
| 350 |
let (rect, response) = ui.allocate_exact_size(Vec2::splat(style.mark_size), Sense::hover()); |
| 351 |
let lit = match activity_blink(reduced) { |
| 352 |
|
| 353 |
None => true, |
| 354 |
Some(half) => { |
| 355 |
let half = half.as_secs_f64(); |
| 356 |
|
| 357 |
|
| 358 |
if half <= 0.0 { |
| 359 |
true |
| 360 |
} else { |
| 361 |
let phase = ui.input(|input| input.time).rem_euclid(half * 2.0); |
| 362 |
let lit = phase < half; |
| 363 |
let next = if lit { half } else { half * 2.0 } - phase; |
| 364 |
ui.ctx() |
| 365 |
.request_repaint_after(Duration::from_secs_f64(next.max(0.0))); |
| 366 |
lit |
| 367 |
} |
| 368 |
} |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
let colour = if lit { palette.action } else { palette.sunken }; |
| 374 |
ui.painter().rect_filled(rect, style.radius, colour); |
| 375 |
response |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
pub fn awaiting( |
| 398 |
ui: &mut Ui, |
| 399 |
awaiting: Awaiting, |
| 400 |
progress: Progress, |
| 401 |
reduced: bool, |
| 402 |
palette: &Palette, |
| 403 |
style: &WidgetStyle, |
| 404 |
) -> Response { |
| 405 |
let (Some(total), Some(done)) = (awaiting.amount, progress.delivered) else { |
| 406 |
return activity(ui, reduced, palette, style); |
| 407 |
}; |
| 408 |
let width = style |
| 409 |
.meter_width |
| 410 |
.unwrap_or_else(|| ui.available_width().max(1.0)); |
| 411 |
ui.horizontal(|ui| { |
| 412 |
let (rect, response) = |
| 413 |
ui.allocate_exact_size(Vec2::new(width, style.meter_height), Sense::hover()); |
| 414 |
ui.painter().rect_filled(rect, style.radius, palette.sunken); |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
let share = if total == 0 { |
| 419 |
0.0 |
| 420 |
} else { |
| 421 |
#[expect( |
| 422 |
clippy::cast_precision_loss, |
| 423 |
reason = "a byte count past 2^53 is not a wait anyone is watching a bar for" |
| 424 |
)] |
| 425 |
let share = (done as f64 / total as f64).min(1.0); |
| 426 |
share |
| 427 |
}; |
| 428 |
#[expect( |
| 429 |
clippy::cast_possible_truncation, |
| 430 |
reason = "a share is 0..=1 and the product is a width in points" |
| 431 |
)] |
| 432 |
let filled = (f64::from(rect.width()) * share) as f32; |
| 433 |
if filled > 0.0 { |
| 434 |
let mut fill = rect; |
| 435 |
fill.set_width(filled); |
| 436 |
ui.painter().rect_filled(fill, style.radius, palette.action); |
| 437 |
} |
| 438 |
let reading = match progress.elapsed { |
| 439 |
Some(elapsed) => format!("{done}/{total} {}s", elapsed.as_secs()), |
| 440 |
None => format!("{done}/{total}"), |
| 441 |
}; |
| 442 |
ui.label(RichText::new(reading).color(palette.content_muted)); |
| 443 |
response |
| 444 |
}) |
| 445 |
.inner |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
pub fn chart( |
| 466 |
ui: &mut Ui, |
| 467 |
chart: &Chart<'_>, |
| 468 |
bars: &[Bar<'_>], |
| 469 |
palette: &Palette, |
| 470 |
style: &WidgetStyle, |
| 471 |
) -> Response { |
| 472 |
let width = ui.available_width().max(1.0); |
| 473 |
let (rect, response) = |
| 474 |
ui.allocate_exact_size(Vec2::new(width, style.chart_height), Sense::hover()); |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
let font = egui::TextStyle::Small.resolve(ui.style()); |
| 481 |
let band = ui.text_style_height(&egui::TextStyle::Small); |
| 482 |
let floor = (rect.bottom() - band).max(rect.top()); |
| 483 |
let standing = floor - rect.top(); |
| 484 |
|
| 485 |
if bars.is_empty() { |
| 486 |
return response; |
| 487 |
} |
| 488 |
|
| 489 |
#[expect( |
| 490 |
clippy::cast_precision_loss, |
| 491 |
reason = "a bar count is small and this is a width in points" |
| 492 |
)] |
| 493 |
let each = rect.width() / bars.len() as f32; |
| 494 |
for (index, bar) in bars.iter().enumerate() { |
| 495 |
#[expect( |
| 496 |
clippy::cast_precision_loss, |
| 497 |
reason = "an index into the bars, which are few" |
| 498 |
)] |
| 499 |
let left = rect.left() + each * index as f32; |
| 500 |
let reached = standing * bar.fraction(chart); |
| 501 |
let mut column = egui::Rect::from_min_size( |
| 502 |
egui::Pos2::new(left, floor - reached), |
| 503 |
Vec2::new(each, reached), |
| 504 |
); |
| 505 |
|
| 506 |
|
| 507 |
if column.height() < 1.0 { |
| 508 |
column.set_top(floor - 1.0); |
| 509 |
} |
| 510 |
ui.painter() |
| 511 |
.rect_filled(column, style.radius, palette.tone(chart.tone)); |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
ui.painter().text( |
| 518 |
egui::Pos2::new(left + each / 2.0, floor), |
| 519 |
egui::Align2::CENTER_TOP, |
| 520 |
bar.at, |
| 521 |
font.clone(), |
| 522 |
palette.content_muted, |
| 523 |
); |
| 524 |
} |
| 525 |
|
| 526 |
response |
| 527 |
} |
| 528 |
|
| 529 |
#[cfg(test)] |
| 530 |
mod tests { |
| 531 |
use super::*; |
| 532 |
|
| 533 |
|
| 534 |
fn announced( |
| 535 |
draw: impl FnMut(&mut Ui), |
| 536 |
) -> Vec<( |
| 537 |
egui::accesskit::Role, |
| 538 |
String, |
| 539 |
Option<egui::accesskit::Toggled>, |
| 540 |
)> { |
| 541 |
let ctx = egui::Context::default(); |
| 542 |
ctx.enable_accesskit(); |
| 543 |
let mut draw = draw; |
| 544 |
let input = || egui::RawInput { |
| 545 |
screen_rect: Some(egui::Rect::from_min_size( |
| 546 |
egui::Pos2::ZERO, |
| 547 |
egui::vec2(600.0, 400.0), |
| 548 |
)), |
| 549 |
..Default::default() |
| 550 |
}; |
| 551 |
let _ = ctx.run_ui(input(), &mut draw); |
| 552 |
let out = ctx.run_ui(input(), &mut draw); |
| 553 |
out.platform_output |
| 554 |
.accesskit_update |
| 555 |
.expect("accesskit is on") |
| 556 |
.nodes |
| 557 |
.iter() |
| 558 |
.map(|(_, node)| { |
| 559 |
( |
| 560 |
node.role(), |
| 561 |
node.label() |
| 562 |
.or_else(|| node.value()) |
| 563 |
.unwrap_or_default() |
| 564 |
.to_owned(), |
| 565 |
node.toggled(), |
| 566 |
) |
| 567 |
}) |
| 568 |
.collect() |
| 569 |
} |
| 570 |
|
| 571 |
#[test] |
| 572 |
fn a_chip_is_announced_as_a_control_and_says_whether_it_is_held() { |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
let p = palette(); |
| 577 |
let style = WidgetStyle::default(); |
| 578 |
let drawn = announced(|ui| { |
| 579 |
token( |
| 580 |
ui, |
| 581 |
"C#", |
| 582 |
Token::Chip { removable: false }, |
| 583 |
Tone::Neutral, |
| 584 |
true, |
| 585 |
&p, |
| 586 |
&style, |
| 587 |
); |
| 588 |
}); |
| 589 |
|
| 590 |
let chip = drawn |
| 591 |
.iter() |
| 592 |
.find(|(role, name, _)| *role == egui::accesskit::Role::Button && name == "C#") |
| 593 |
.unwrap_or_else(|| panic!("the chip is not in the tree: {drawn:?}")); |
| 594 |
assert_eq!( |
| 595 |
chip.2, |
| 596 |
Some(egui::accesskit::Toggled::True), |
| 597 |
"a latched chip is held down and says so: {drawn:?}" |
| 598 |
); |
| 599 |
} |
| 600 |
|
| 601 |
#[test] |
| 602 |
fn a_badge_is_announced_as_the_text_it_is() { |
| 603 |
|
| 604 |
|
| 605 |
let p = palette(); |
| 606 |
let style = WidgetStyle::default(); |
| 607 |
let drawn = announced(|ui| { |
| 608 |
token(ui, "wav", Token::Badge, Tone::Neutral, false, &p, &style); |
| 609 |
}); |
| 610 |
|
| 611 |
assert!( |
| 612 |
drawn |
| 613 |
.iter() |
| 614 |
.any(|(role, name, _)| *role == egui::accesskit::Role::Label && name == "wav"), |
| 615 |
"{drawn:?}" |
| 616 |
); |
| 617 |
assert!( |
| 618 |
!drawn |
| 619 |
.iter() |
| 620 |
.any(|(role, _, _)| *role == egui::accesskit::Role::Button), |
| 621 |
"a badge answers nothing and must not claim to: {drawn:?}" |
| 622 |
); |
| 623 |
} |
| 624 |
|
| 625 |
fn palette() -> Palette { |
| 626 |
use egui::Color32; |
| 627 |
Palette { |
| 628 |
page: Color32::from_rgb(1, 1, 1), |
| 629 |
raised: Color32::from_rgb(2, 2, 2), |
| 630 |
overlay: Color32::from_rgb(3, 3, 3), |
| 631 |
well: Color32::from_rgb(4, 4, 4), |
| 632 |
sunken: Color32::from_rgb(5, 5, 5), |
| 633 |
bevel_light: Color32::from_rgb(6, 6, 6), |
| 634 |
bevel_dark: Color32::from_rgb(7, 7, 7), |
| 635 |
elevation: Color32::from_black_alpha(40), |
| 636 |
content: Color32::from_rgb(20, 20, 20), |
| 637 |
content_secondary: Color32::from_rgb(120, 120, 120), |
| 638 |
content_muted: Color32::from_rgb(21, 21, 21), |
| 639 |
action: Color32::from_rgb(22, 22, 22), |
| 640 |
danger: Color32::from_rgb(23, 23, 23), |
| 641 |
success: Color32::from_rgb(24, 24, 24), |
| 642 |
warning: Color32::from_rgb(25, 25, 25), |
| 643 |
info: Color32::from_rgb(26, 26, 26), |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
#[test] |
| 648 |
fn every_tone_resolves_and_no_two_share_a_colour() { |
| 649 |
|
| 650 |
|
| 651 |
let p = palette(); |
| 652 |
let all = [ |
| 653 |
p.tone(Tone::Neutral), |
| 654 |
p.tone(Tone::Info), |
| 655 |
p.tone(Tone::Success), |
| 656 |
p.tone(Tone::Warning), |
| 657 |
p.tone(Tone::Danger), |
| 658 |
]; |
| 659 |
for (i, a) in all.iter().enumerate() { |
| 660 |
for b in &all[i + 1..] { |
| 661 |
assert_ne!(a, b, "two tones resolved to one colour"); |
| 662 |
} |
| 663 |
} |
| 664 |
assert_eq!(p.tone(Tone::Neutral), p.content, "neutral is ordinary text"); |
| 665 |
} |
| 666 |
|
| 667 |
#[test] |
| 668 |
fn a_meter_draws_and_an_overrun_does_not_panic() { |
| 669 |
|
| 670 |
|
| 671 |
let p = palette(); |
| 672 |
let style = WidgetStyle::default(); |
| 673 |
egui::__run_test_ui(|ui| { |
| 674 |
meter(ui, &Meter::new(3, 6), &p, &style); |
| 675 |
meter(ui, &Meter::new(9, 6), &p, &style); |
| 676 |
|
| 677 |
meter(ui, &Meter::new(0, 0), &p, &style); |
| 678 |
|
| 679 |
meter(ui, &Meter::new(u32::MAX, u32::MAX), &p, &style); |
| 680 |
}); |
| 681 |
} |
| 682 |
|
| 683 |
#[test] |
| 684 |
fn a_wait_draws_a_bar_only_when_something_is_counting_it() { |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
let p = palette(); |
| 689 |
let style = WidgetStyle::default(); |
| 690 |
egui::__run_test_ui(|ui| { |
| 691 |
awaiting( |
| 692 |
ui, |
| 693 |
Awaiting::unmeasured(), |
| 694 |
Progress::default(), |
| 695 |
false, |
| 696 |
&p, |
| 697 |
&style, |
| 698 |
); |
| 699 |
awaiting( |
| 700 |
ui, |
| 701 |
Awaiting::of(41_943_040), |
| 702 |
Progress::default(), |
| 703 |
false, |
| 704 |
&p, |
| 705 |
&style, |
| 706 |
); |
| 707 |
awaiting( |
| 708 |
ui, |
| 709 |
Awaiting::of(41_943_040), |
| 710 |
Progress { |
| 711 |
delivered: Some(10_485_760), |
| 712 |
elapsed: Some(Duration::from_secs(3)), |
| 713 |
}, |
| 714 |
false, |
| 715 |
&p, |
| 716 |
&style, |
| 717 |
); |
| 718 |
|
| 719 |
awaiting( |
| 720 |
ui, |
| 721 |
Awaiting::of(0), |
| 722 |
Progress { |
| 723 |
delivered: Some(9), |
| 724 |
elapsed: None, |
| 725 |
}, |
| 726 |
false, |
| 727 |
&p, |
| 728 |
&style, |
| 729 |
); |
| 730 |
awaiting( |
| 731 |
ui, |
| 732 |
Awaiting::of(4), |
| 733 |
Progress { |
| 734 |
delivered: Some(9), |
| 735 |
elapsed: None, |
| 736 |
}, |
| 737 |
false, |
| 738 |
&p, |
| 739 |
&style, |
| 740 |
); |
| 741 |
}); |
| 742 |
} |
| 743 |
|
| 744 |
#[test] |
| 745 |
fn reduced_motion_stills_the_mark_and_does_not_remove_it() { |
| 746 |
|
| 747 |
|
| 748 |
let p = palette(); |
| 749 |
let style = WidgetStyle::default(); |
| 750 |
egui::__run_test_ui(|ui| { |
| 751 |
let still = activity(ui, true, &p, &style); |
| 752 |
let blinking = activity(ui, false, &p, &style); |
| 753 |
assert_eq!( |
| 754 |
still.rect.size(), |
| 755 |
blinking.rect.size(), |
| 756 |
"the mark occupies the same space either way" |
| 757 |
); |
| 758 |
}); |
| 759 |
} |
| 760 |
|
| 761 |
#[test] |
| 762 |
fn a_chip_answers_a_click_and_a_badge_does_not() { |
| 763 |
|
| 764 |
|
| 765 |
let p = palette(); |
| 766 |
let style = WidgetStyle::default(); |
| 767 |
egui::__run_test_ui(|ui| { |
| 768 |
let badge = token(ui, "beta", Token::Badge, Tone::Info, false, &p, &style); |
| 769 |
assert!(!badge.sense.senses_click(), "a badge answers no click"); |
| 770 |
|
| 771 |
let chip = token( |
| 772 |
ui, |
| 773 |
"drums", |
| 774 |
Token::Chip { removable: false }, |
| 775 |
Tone::Neutral, |
| 776 |
false, |
| 777 |
&p, |
| 778 |
&style, |
| 779 |
); |
| 780 |
assert!(chip.sense.senses_click(), "a chip answers a click"); |
| 781 |
}); |
| 782 |
} |
| 783 |
|
| 784 |
#[test] |
| 785 |
fn a_disabled_control_is_drawn_and_does_not_answer() { |
| 786 |
|
| 787 |
|
| 788 |
let p = palette(); |
| 789 |
let style = WidgetStyle::default(); |
| 790 |
egui::__run_test_ui(|ui| { |
| 791 |
let live = act(ui, &Act::new("Save"), &p, &style); |
| 792 |
assert!(live.enabled()); |
| 793 |
|
| 794 |
let gone = act(ui, &Act::new("Save").state(State::Disabled), &p, &style); |
| 795 |
assert!(!gone.enabled(), "a disabled control still answers"); |
| 796 |
}); |
| 797 |
} |
| 798 |
|
| 799 |
#[test] |
| 800 |
fn a_control_shows_the_key_the_description_named() { |
| 801 |
|
| 802 |
|
| 803 |
let p = palette(); |
| 804 |
let style = WidgetStyle::default(); |
| 805 |
egui::__run_test_ui(|ui| { |
| 806 |
act(ui, &Act::new("New").key("n"), &p, &style); |
| 807 |
act(ui, &Act::new("New"), &p, &style); |
| 808 |
}); |
| 809 |
} |
| 810 |
|
| 811 |
#[test] |
| 812 |
fn a_figure_draws_its_movement_beside_its_value() { |
| 813 |
let p = palette(); |
| 814 |
let style = WidgetStyle::default(); |
| 815 |
egui::__run_test_ui(|ui| { |
| 816 |
figure(ui, &Figure::new("17", "Current streak"), &p, &style); |
| 817 |
figure( |
| 818 |
ui, |
| 819 |
&Figure::new("17", "Current streak") |
| 820 |
.change("+3") |
| 821 |
.tone(Tone::Success), |
| 822 |
&p, |
| 823 |
&style, |
| 824 |
); |
| 825 |
}); |
| 826 |
} |
| 827 |
} |
| 828 |
|