use crate::density;
use crate::theme::{self, surface, FontScale};
use iced::widget::{container, progress_bar, Container};
use iced::{Background, Border, Element, Length};
const PROGRESS_GIRTH_BASE: f32 = 4.0;
pub fn progress_line<'a, Message: 'a>(fraction: f32, scale: FontScale) -> Element<'a, Message> {
progress_bar(0.0..=1.0, fraction.clamp(0.0, 1.0))
.girth(scale.apply(PROGRESS_GIRTH_BASE))
.length(Length::Fill)
.style(|_t: &iced::Theme| progress_line_style())
.into()
}
pub fn stacked_bar<'a, Message: 'a>(parts: &[(f32, iced::Color)], scale: FontScale) -> Element<'a, Message> {
const STEPS: f32 = 1000.0;
let height = scale.apply(PROGRESS_GIRTH_BASE * 2.0);
let mut used = 0u16;
let mut bar = iced::widget::Row::new();
for &(fraction, color) in parts {
let portion = (fraction.clamp(0.0, 1.0) * STEPS).round() as u16;
if portion == 0 {
continue;
}
used = used.saturating_add(portion);
bar = bar.push(
iced::widget::container(iced::widget::Space::new())
.width(Length::FillPortion(portion))
.height(Length::Fixed(height))
.style(move |_t: &iced::Theme| iced::widget::container::Style {
background: Some(Background::Color(color)),
..iced::widget::container::Style::default()
}),
);
}
let rest = (STEPS as u16).saturating_sub(used);
if rest > 0 {
bar = bar.push(iced::widget::Space::new().width(Length::FillPortion(rest)).height(Length::Fixed(height)));
}
iced::widget::container(bar)
.width(Length::Fill)
.clip(true)
.style(move |_t: &iced::Theme| iced::widget::container::Style {
background: Some(Background::Color(surface::row())),
border: Border { radius: (height / 2.0).into(), ..Border::default() },
..iced::widget::container::Style::default()
})
.into()
}
pub fn progress_line_style() -> progress_bar::Style {
progress_bar::Style {
background: Background::Color(surface::row()),
bar: Background::Color(theme::text()),
border: Border { radius: (PROGRESS_GIRTH_BASE / 2.0).into(), ..Border::default() },
}
}
pub fn popover_card<'a, Message: 'a>(content: impl Into<Element<'a, Message>>) -> Container<'a, Message> {
container(content).style(|_t: &iced::Theme| container::Style {
background: Some(Background::Color(surface::sidebar())),
border: Border {
radius: density::card_radius().into(),
width: 1.0,
color: surface::card_border(),
},
..container::Style::default()
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_progress_line_never_fills_with_the_accent() {
let style = progress_line_style();
let accent = crate::color::to_iced(theme::active().accent);
assert_ne!(style.bar, Background::Color(accent));
assert_eq!(style.bar, Background::Color(theme::text()));
assert_ne!(style.bar, style.background, "a full bar has to look full");
}
}