Skip to main content

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/// One bar split into parts: where something went — CPU time by kind,
40/// memory by use, a disk by what fills it. Each part is a fraction of
41/// the whole and a colour from the theme's roles (never the accent,
42/// which means selected); what the parts leave is the empty track.
43///
44/// Drawn as proportioned blocks, not a canvas, so it lays out like any
45/// other row and a zero part takes no room at all.
46pub fn stacked_bar<'a, Message: 'a>(parts: &[(f32, iced::Color)], scale: FontScale) -> Element<'a, Message> {
47    const STEPS: f32 = 1000.0;
48    let height = scale.apply(PROGRESS_GIRTH_BASE * 2.0);
49    let mut used = 0u16;
50    let mut bar = iced::widget::Row::new();
51    for &(fraction, color) in parts {
52        let portion = (fraction.clamp(0.0, 1.0) * STEPS).round() as u16;
53        if portion == 0 {
54            continue;
55        }
56        used = used.saturating_add(portion);
57        bar = bar.push(
58            iced::widget::container(iced::widget::Space::new())
59                .width(Length::FillPortion(portion))
60                .height(Length::Fixed(height))
61                .style(move |_t: &iced::Theme| iced::widget::container::Style {
62                    background: Some(Background::Color(color)),
63                    ..iced::widget::container::Style::default()
64                }),
65        );
66    }
67    let rest = (STEPS as u16).saturating_sub(used);
68    if rest > 0 {
69        bar = bar.push(iced::widget::Space::new().width(Length::FillPortion(rest)).height(Length::Fixed(height)));
70    }
71    iced::widget::container(bar)
72        .width(Length::Fill)
73        .clip(true)
74        .style(move |_t: &iced::Theme| iced::widget::container::Style {
75            background: Some(Background::Color(surface::row())),
76            border: Border { radius: (height / 2.0).into(), ..Border::default() },
77            ..iced::widget::container::Style::default()
78        })
79        .into()
80}
81
82/// [`progress_line`]'s look, on its own so a test can hold it to the
83/// rule above.
84pub fn progress_line_style() -> progress_bar::Style {
85    progress_bar::Style {
86        background: Background::Color(surface::row()),
87        bar: Background::Color(theme::text()),
88        border: Border { radius: (PROGRESS_GIRTH_BASE / 2.0).into(), ..Border::default() },
89    }
90}
91
92/// The surface a popover is drawn on: a small panel that opens from a
93/// control and closes on a click anywhere else.
94///
95/// The context menu's surface, on purpose — `sidebar`, a one-pixel
96/// `card_border` outline, a card's corner — because a popover is a
97/// floating list with more in each row, and two floating things that
98/// look different read as two different kinds of thing. The caller
99/// wraps it in `iced::widget::opaque` where it stacks it, so a click on
100/// the padding does not fall through to the window below.
101pub fn popover_card<'a, Message: 'a>(content: impl Into<Element<'a, Message>>) -> Container<'a, Message> {
102    container(content).style(|_t: &iced::Theme| container::Style {
103        background: Some(Background::Color(surface::sidebar())),
104        border: Border {
105            radius: density::card_radius().into(),
106            width: 1.0,
107            color: surface::card_border(),
108        },
109        ..container::Style::default()
110    })
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    /// The reason this widget exists rather than iced's default bar:
118    /// the default fills with the palette's primary, which is the
119    /// accent, and the accent means selected.
120    #[test]
121    fn a_progress_line_never_fills_with_the_accent() {
122        let style = progress_line_style();
123        let accent = crate::color::to_iced(theme::active().accent);
124        assert_ne!(style.bar, Background::Color(accent));
125        assert_eq!(style.bar, Background::Color(theme::text()));
126        assert_ne!(style.bar, style.background, "a full bar has to look full");
127    }
128}