hyprforge_ui/widgets/progress.rs
1//! How far along something is, and the card a short list of such things
2//! floats on.
3//!
4//! Files drew the first of both — a copy's bar, and the transfers
5//! popover hung from its window chrome — and neither is about files: a
6//! download in Media, a firmware flash in Settings, anything that takes
7//! long enough to watch draws the same line, and any small panel that
8//! opens from a button and closes on a click elsewhere wants the same
9//! surface.
10
11use crate::density;
12use crate::theme::{self, surface, FontScale};
13use iced::widget::{container, progress_bar, Container};
14use iced::{Background, Border, Element, Length};
15
16/// How thick a progress line is at 100%.
17const PROGRESS_GIRTH_BASE: f32 = 4.0;
18
19/// A thin bar across its width, filled to `fraction` (0 to 1).
20///
21/// The fill is the foreground text colour on the row step, and never
22/// the accent. iced's own bar takes the palette's primary, which in
23/// this suite *is* the accent — and the accent means "selected", so a
24/// running copy drawn in it read as a highlighted row beside the
25/// selected one it sat under. A bar that means something else (a
26/// failure) is a different widget, not a different colour of this one.
27///
28/// Callers pass a fraction only when there is an honest one. An
29/// indeterminate state is said in words, not drawn as a bar that fills
30/// at a made-up rate.
31pub fn progress_line<'a, Message: 'a>(fraction: f32, scale: FontScale) -> Element<'a, Message> {
32 progress_bar(0.0..=1.0, fraction.clamp(0.0, 1.0))
33 .girth(scale.apply(PROGRESS_GIRTH_BASE))
34 .length(Length::Fill)
35 .style(|_t: &iced::Theme| progress_line_style())
36 .into()
37}
38
39/// [`progress_line`]'s look, on its own so a test can hold it to the
40/// rule above.
41pub fn progress_line_style() -> progress_bar::Style {
42 progress_bar::Style {
43 background: Background::Color(surface::row()),
44 bar: Background::Color(theme::text()),
45 border: Border { radius: (PROGRESS_GIRTH_BASE / 2.0).into(), ..Border::default() },
46 }
47}
48
49/// The surface a popover is drawn on: a small panel that opens from a
50/// control and closes on a click anywhere else.
51///
52/// The context menu's surface, on purpose — `sidebar`, a one-pixel
53/// `card_border` outline, a card's corner — because a popover is a
54/// floating list with more in each row, and two floating things that
55/// look different read as two different kinds of thing. The caller
56/// wraps it in `iced::widget::opaque` where it stacks it, so a click on
57/// the padding does not fall through to the window below.
58pub fn popover_card<'a, Message: 'a>(content: impl Into<Element<'a, Message>>) -> Container<'a, Message> {
59 container(content).style(|_t: &iced::Theme| container::Style {
60 background: Some(Background::Color(surface::sidebar())),
61 border: Border {
62 radius: density::card_radius().into(),
63 width: 1.0,
64 color: surface::card_border(),
65 },
66 ..container::Style::default()
67 })
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 /// The reason this widget exists rather than iced's default bar:
75 /// the default fills with the palette's primary, which is the
76 /// accent, and the accent means selected.
77 #[test]
78 fn a_progress_line_never_fills_with_the_accent() {
79 let style = progress_line_style();
80 let accent = crate::color::to_iced(theme::active().accent);
81 assert_ne!(style.bar, Background::Color(accent));
82 assert_eq!(style.bar, Background::Color(theme::text()));
83 assert_ne!(style.bar, style.background, "a full bar has to look full");
84 }
85}