freya_components/
progressbar.rs1use freya_core::prelude::*;
2use torin::size::Size;
3
4use crate::{
5 get_theme,
6 theming::component_themes::ProgressBarThemePartial,
7};
8
9#[cfg_attr(feature = "docs",
29 doc = embed_doc_image::embed_image!("progressbar", "images/gallery_progressbar.png")
30)]
31#[derive(Clone, PartialEq)]
32pub struct ProgressBar {
33 pub(crate) theme: Option<ProgressBarThemePartial>,
34 width: Size,
35 show_progress: bool,
36 progress: f32,
37 key: DiffKey,
38}
39
40impl KeyExt for ProgressBar {
41 fn write_key(&mut self) -> &mut DiffKey {
42 &mut self.key
43 }
44}
45
46impl ProgressBar {
47 pub fn new(progress: impl Into<f32>) -> Self {
48 Self {
49 width: Size::fill(),
50 theme: None,
51 show_progress: true,
52 progress: progress.into(),
53 key: DiffKey::None,
54 }
55 }
56
57 pub fn width(mut self, width: impl Into<Size>) -> Self {
58 self.width = width.into();
59 self
60 }
61
62 pub fn show_progress(mut self, show_progress: bool) -> Self {
63 self.show_progress = show_progress;
64 self
65 }
66}
67
68impl Render for ProgressBar {
69 fn render(&self) -> impl IntoElement {
70 let progressbar_theme = get_theme!(&self.theme, progressbar);
71
72 let progress = self.progress.clamp(0., 100.);
73
74 rect()
75 .a11y_alt(format!("Progress {progress}%"))
76 .a11y_focusable(true)
77 .a11y_role(AccessibilityRole::ProgressIndicator)
78 .horizontal()
79 .width(self.width.clone())
80 .height(Size::px(progressbar_theme.height))
81 .corner_radius(99.)
82 .background(progressbar_theme.background)
83 .border(
84 Border::new()
85 .width(1.)
86 .alignment(BorderAlignment::Outer)
87 .fill(progressbar_theme.background),
88 )
89 .font_size(13.)
90 .child(
91 rect()
92 .horizontal()
93 .width(Size::percent(progress))
94 .height(Size::fill())
95 .corner_radius(99.)
96 .background(progressbar_theme.progress_background)
97 .child(
98 label()
99 .width(Size::fill())
100 .color(progressbar_theme.color)
101 .text_align(TextAlign::Center)
102 .text(format!("{}%", self.progress))
103 .max_lines(1),
104 ),
105 )
106 }
107
108 fn render_key(&self) -> DiffKey {
109 self.key.clone().or(self.default_key())
110 }
111}