hyprforge_ui/widgets/
progress.rs1use crate::density;
12use crate::theme::{self, surface, FontScale};
13use iced::widget::{container, progress_bar, Container};
14use iced::{Background, Border, Element, Length};
15
16const PROGRESS_GIRTH_BASE: f32 = 4.0;
18
19pub 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
39pub 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
82pub 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
92pub 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 #[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}