use egui::{Response, RichText, Sense, Ui, Vec2};
use makeover_layout::{Awaiting, Token, Tone};
use makeover_timing::{MotionPreference, activity_blink};
use std::time::Duration;
use crate::Palette;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PieceStyle {
pub meter_height: f32,
pub meter_width: Option<f32>,
pub chart_height: f32,
pub radius: u8,
pub token_padding: Vec2,
pub figure_gap: f32,
pub figure_scale: f32,
pub mark_size: f32,
}
impl Default for PieceStyle {
fn default() -> Self {
Self {
meter_height: 6.0,
meter_width: None,
chart_height: 200.0,
radius: 3,
token_padding: Vec2::new(6.0, 2.0),
figure_gap: 2.0,
figure_scale: 2.0,
mark_size: 8.0,
}
}
}
pub fn token(
ui: &mut Ui,
label: &str,
kind: Token,
tone: Tone,
latched: bool,
palette: &Palette,
style: &PieceStyle,
) -> Response {
let painted = palette.tone(tone);
let radius = style.radius;
let sense = if kind.interactive() {
Sense::click()
} else {
Sense::hover()
};
let ink = match kind {
Token::Badge => palette.content,
Token::Chip { .. } if latched => palette.page,
Token::Chip { .. } => painted,
_ => palette.content,
};
let galley = ui.painter().layout_no_wrap(
label.to_owned(),
egui::TextStyle::Body.resolve(ui.style()),
ink,
);
let size = galley.size() + style.token_padding * 2.0;
let (rect, response) = ui.allocate_exact_size(size, sense);
if kind == Token::Badge {
ui.painter().rect(
rect,
radius,
palette.tone_surface(tone),
egui::Stroke::new(1.0, palette.tone_edge(tone)),
egui::StrokeKind::Inside,
);
} else if latched {
ui.painter().rect_filled(rect, radius, painted);
} else {
ui.painter().rect_stroke(
rect,
radius,
egui::Stroke::new(1.0, painted),
egui::StrokeKind::Inside,
);
}
ui.painter()
.galley(rect.center() - galley.size() / 2.0, galley, ink);
let role = if kind.interactive() {
egui::WidgetType::Button
} else {
egui::WidgetType::Label
};
response.widget_info(|| {
let mut info = egui::WidgetInfo::labeled(role, ui.is_enabled(), label);
if kind.interactive() {
info.selected = Some(latched);
}
info
});
response
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Progress {
pub delivered: Option<u64>,
pub elapsed: Option<Duration>,
}
pub fn activity(
ui: &mut Ui,
motion: MotionPreference,
palette: &Palette,
style: &PieceStyle,
) -> Response {
let (rect, response) = ui.allocate_exact_size(Vec2::splat(style.mark_size), Sense::hover());
let lit = match activity_blink(motion) {
None => true,
Some(half) => {
let half = half.as_secs_f64();
if half <= 0.0 {
true
} else {
let phase = ui.input(|input| input.time).rem_euclid(half * 2.0);
let lit = phase < half;
let next = if lit { half } else { half * 2.0 } - phase;
ui.ctx()
.request_repaint_after(Duration::from_secs_f64(next.max(0.0)));
lit
}
}
};
let colour = if lit { palette.action } else { palette.sunken };
ui.painter().rect_filled(rect, style.radius, colour);
response
}
pub fn awaiting(
ui: &mut Ui,
awaiting: Awaiting,
progress: Progress,
motion: MotionPreference,
palette: &Palette,
style: &PieceStyle,
) -> Response {
let (Some(total), Some(done)) = (awaiting.amount, progress.delivered) else {
return activity(ui, motion, palette, style);
};
let reading = match progress.elapsed {
Some(elapsed) => format!("{done}/{total} {}s", elapsed.as_secs()),
None => format!("{done}/{total}"),
};
let width = style.meter_width.unwrap_or_else(|| {
let text = RichText::new(&reading).color(palette.content_muted);
let galley = egui::WidgetText::from(text).into_galley(
ui,
Some(egui::TextWrapMode::Extend),
f32::INFINITY,
egui::TextStyle::Body,
);
(ui.available_width() - galley.size().x - ui.spacing().item_spacing.x).max(1.0)
});
ui.horizontal(|ui| {
let (rect, response) =
ui.allocate_exact_size(Vec2::new(width, style.meter_height), Sense::hover());
ui.painter().rect_filled(rect, style.radius, palette.sunken);
let share = if total == 0 {
0.0
} else {
#[expect(
clippy::cast_precision_loss,
reason = "a byte count past 2^53 is not a wait anyone is watching a bar for"
)]
let share = (done as f64 / total as f64).min(1.0);
share
};
#[expect(
clippy::cast_possible_truncation,
reason = "a share is 0..=1 and the product is a width in points"
)]
let filled = (f64::from(rect.width()) * share) as f32;
if filled > 0.0 {
let mut fill = rect;
fill.set_width(filled);
ui.painter().rect_filled(fill, style.radius, palette.action);
}
ui.label(RichText::new(reading).color(palette.content_muted));
response
})
.inner
}
#[cfg(test)]
mod tests {
use super::*;
fn announced(
draw: impl FnMut(&mut Ui),
) -> Vec<(
egui::accesskit::Role,
String,
Option<egui::accesskit::Toggled>,
)> {
let ctx = egui::Context::default();
ctx.enable_accesskit();
let mut draw = draw;
let input = || egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::vec2(600.0, 400.0),
)),
..Default::default()
};
ctx.run_ui(input(), &mut draw).textures_delta.clear();
let mut out = ctx.run_ui(input(), &mut draw);
out.textures_delta.clear();
out.platform_output
.accesskit_update
.expect("accesskit is on")
.nodes
.iter()
.map(|(_, node)| {
(
node.role(),
node.label()
.or_else(|| node.value())
.unwrap_or_default()
.to_owned(),
node.toggled(),
)
})
.collect()
}
#[test]
fn a_chip_is_announced_as_a_control_and_says_whether_it_is_held() {
let p = palette();
let style = PieceStyle::default();
let drawn = announced(|ui| {
token(
ui,
"C#",
Token::Chip { removable: false },
Tone::Neutral,
true,
&p,
&style,
);
});
let chip = drawn
.iter()
.find(|(role, name, _)| *role == egui::accesskit::Role::Button && name == "C#")
.unwrap_or_else(|| panic!("the chip is not in the tree: {drawn:?}"));
assert_eq!(
chip.2,
Some(egui::accesskit::Toggled::True),
"a latched chip is held down and says so: {drawn:?}"
);
}
#[test]
fn a_badge_is_announced_as_the_text_it_is() {
let p = palette();
let style = PieceStyle::default();
let drawn = announced(|ui| {
token(ui, "wav", Token::Badge, Tone::Neutral, false, &p, &style);
});
assert!(
drawn
.iter()
.any(|(role, name, _)| *role == egui::accesskit::Role::Label && name == "wav"),
"{drawn:?}"
);
assert!(
!drawn
.iter()
.any(|(role, _, _)| *role == egui::accesskit::Role::Button),
"a badge answers nothing and must not claim to: {drawn:?}"
);
}
fn palette() -> Palette {
use egui::Color32;
Palette {
page: Color32::from_rgb(1, 1, 1),
raised: Color32::from_rgb(2, 2, 2),
overlay: Color32::from_rgb(3, 3, 3),
well: Color32::from_rgb(4, 4, 4),
sunken: Color32::from_rgb(5, 5, 5),
bevel_light: Color32::from_rgb(6, 6, 6),
bevel_dark: Color32::from_rgb(7, 7, 7),
elevation: Color32::from_black_alpha(40),
content: Color32::from_rgb(20, 20, 20),
content_secondary: Color32::from_rgb(120, 120, 120),
content_muted: Color32::from_rgb(21, 21, 21),
action: Color32::from_rgb(22, 22, 22),
content_on_action: Color32::from_rgb(250, 250, 250),
danger: Color32::from_rgb(23, 23, 23),
success: Color32::from_rgb(24, 24, 24),
warning: Color32::from_rgb(25, 25, 25),
info: Color32::from_rgb(26, 26, 26),
border: Color32::from_rgb(200, 200, 200),
info_surface: Color32::from_rgb(201, 201, 201),
success_surface: Color32::from_rgb(202, 202, 202),
warning_surface: Color32::from_rgb(203, 203, 203),
danger_surface: Color32::from_rgb(204, 204, 204),
row_hover: Color32::from_rgb(206, 206, 206),
row_rule: Color32::from_rgb(207, 207, 207),
row_selected: Color32::from_rgb(208, 208, 208),
}
}
#[test]
fn every_tone_resolves_and_no_two_share_a_colour() {
let p = palette();
let all = [
p.tone(Tone::Neutral),
p.tone(Tone::Info),
p.tone(Tone::Success),
p.tone(Tone::Warning),
p.tone(Tone::Danger),
];
for (i, a) in all.iter().enumerate() {
for b in &all[i + 1..] {
assert_ne!(a, b, "two tones resolved to one colour");
}
}
assert_eq!(p.tone(Tone::Neutral), p.content, "neutral is ordinary text");
}
#[test]
fn a_wait_draws_a_bar_only_when_something_is_counting_it() {
let p = palette();
let style = PieceStyle::default();
egui::__run_test_ui(|ui| {
awaiting(
ui,
Awaiting::unmeasured(),
Progress::default(),
MotionPreference::Full,
&p,
&style,
);
awaiting(
ui,
Awaiting::of(41_943_040),
Progress::default(),
MotionPreference::Full,
&p,
&style,
);
awaiting(
ui,
Awaiting::of(41_943_040),
Progress {
delivered: Some(10_485_760),
elapsed: Some(Duration::from_secs(3)),
},
MotionPreference::Full,
&p,
&style,
);
awaiting(
ui,
Awaiting::of(0),
Progress {
delivered: Some(9),
elapsed: None,
},
MotionPreference::Full,
&p,
&style,
);
awaiting(
ui,
Awaiting::of(4),
Progress {
delivered: Some(9),
elapsed: None,
},
MotionPreference::Full,
&p,
&style,
);
});
}
#[test]
fn reduced_motion_stills_the_mark_and_does_not_remove_it() {
let p = palette();
let style = PieceStyle::default();
egui::__run_test_ui(|ui| {
let still = activity(ui, MotionPreference::Reduced, &p, &style);
let blinking = activity(ui, MotionPreference::Full, &p, &style);
assert_eq!(
still.rect.size(),
blinking.rect.size(),
"the mark occupies the same space either way"
);
});
}
#[test]
fn a_chip_answers_a_click_and_a_badge_does_not() {
let p = palette();
let style = PieceStyle::default();
egui::__run_test_ui(|ui| {
let badge = token(ui, "beta", Token::Badge, Tone::Info, false, &p, &style);
assert!(!badge.sense.senses_click(), "a badge answers no click");
let chip = token(
ui,
"drums",
Token::Chip { removable: false },
Tone::Neutral,
false,
&p,
&style,
);
assert!(chip.sense.senses_click(), "a chip answers a click");
});
}
#[test]
fn a_wait_keeps_its_reading_inside_the_panel() {
let ctx = egui::Context::default();
ctx.set_fonts(egui::FontDefinitions::default());
ctx.memory_mut(|m| m.options.max_passes = std::num::NonZeroUsize::new(1).unwrap());
let p = palette();
let style = PieceStyle::default();
let mut used = 0.0f32;
let mut budget = 0.0f32;
let input = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::Vec2::new(300.0, 400.0),
)),
..Default::default()
};
let output = ctx.run_ui(input, |ui| {
let left = ui.min_rect().left();
budget = ui.available_width();
awaiting(
ui,
Awaiting::of(41_943_040),
Progress {
delivered: Some(10_485_760),
elapsed: Some(Duration::from_secs(3)),
},
MotionPreference::Full,
&p,
&style,
);
used = ui.min_rect().right() - left;
});
output.drop_without_applying_deltas();
assert!(
used <= budget + 0.5,
"the bar and its reading drew to {used}, past the {budget}pt on offer"
);
}
}