vizia_core/views/progressbar.rs
1use crate::prelude::*;
2
3/// A simple progress bar that can be used to show the progress of something.
4///
5/// The input lens need to be a lens to an [f32] with range of `0.0..1.0`.
6///
7/// # Example
8///
9/// ### Vertical ProgressBar bound to the input lens
10/// ```
11/// # use vizia_core::prelude::*;
12/// # use vizia_derive::*;
13/// # let mut cx = &mut Context::default();
14/// # #[derive(Lens, Default)]
15/// # pub struct AppData {
16/// # progress: f32,
17/// # }
18/// # impl Model for AppData {}
19/// # AppData::default().build(cx);
20/// ProgressBar::vertical(cx, AppData::progress);
21/// ```
22///
23/// ### Horizontal ProgressBar bound to the input lens
24/// ```
25/// # use vizia_core::prelude::*;
26/// # use vizia_derive::*;
27/// # let mut cx = &mut Context::default();
28/// # #[derive(Lens, Default)]
29/// # pub struct AppData {
30/// # progress: f32,
31/// # }
32/// # impl Model for AppData {}
33/// # AppData::default().build(cx);
34/// ProgressBar::horizontal(cx, AppData::progress);
35/// ```
36///
37/// ### A Horizontal ProgressBar with a label beside it to show the progress
38/// ```
39/// # use vizia_core::prelude::*;
40/// # use vizia_derive::*;
41/// # let mut cx = &mut Context::default();
42/// # #[derive(Lens, Default)]
43/// # pub struct AppData {
44/// # progress: f32,
45/// # }
46/// # impl Model for AppData {}
47/// # AppData::default().build(cx);
48/// HStack::new(cx, |cx| {
49/// ProgressBar::horizontal(cx, AppData::progress);
50/// Label::new(cx, AppData::progress.map(|v| format!("{:.0}%", v * 100.0)));
51/// });
52/// ```
53pub struct ProgressBar;
54
55impl View for ProgressBar {
56 fn element(&self) -> Option<&'static str> {
57 Some("progressbar")
58 }
59}
60
61impl ProgressBar {
62 /// Creates a new progress bar bound to the value targeted by the lens.
63 ///
64 /// # Example
65 ///
66 /// ```
67 /// # use vizia_core::prelude::*;
68 /// # use vizia_derive::*;
69 /// # let mut cx = &mut Context::default();
70 /// # #[derive(Lens, Default)]
71 /// # pub struct AppData {
72 /// # value: f32,
73 /// # }
74 /// # impl Model for AppData {}
75 /// # AppData::default().build(cx);
76 /// ProgressBar::new(cx, AppData::value, Orientation::Horizontal);
77 /// ```
78 pub fn new<L>(cx: &mut Context, lens: L, orientation: Orientation) -> Handle<Self>
79 where
80 L: Lens<Target = f32>,
81 {
82 match orientation {
83 Orientation::Horizontal => Self::horizontal(cx, lens),
84 Orientation::Vertical => Self::vertical(cx, lens),
85 }
86 }
87
88 /// Creates a new horizontal progress bar bound to the value targeted by the lens.
89 pub fn horizontal<L>(cx: &mut Context, lens: L) -> Handle<Self>
90 where
91 L: Lens<Target = f32>,
92 {
93 Self.build(cx, |cx| {
94 let progress = lens.map(|v| Units::Percentage(v * 100.0));
95 Element::new(cx).width(progress).class("progressbar-bar");
96 })
97 }
98
99 /// Creates a new vertical progress bar bound to the value targeted by the lens.
100 pub fn vertical<L>(cx: &mut Context, lens: L) -> Handle<Self>
101 where
102 L: Lens<Target = f32>,
103 {
104 Self.build(cx, |cx| {
105 let progress = lens.map(|v| Units::Percentage(v * 100.0));
106 Element::new(cx).top(Stretch(1.0)).height(progress).class("progressbar-bar");
107 })
108 }
109}