1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::{api::prelude::*, prelude::*, proc_macros::*};
static RANGE_MIN: f64 = 0.0;
static RANGE_MAX: f64 = 1.0;
pub static STYLE_PROGRESS_BAR: &str = "progress_bar";
pub static STYLE_PROGRESS_BAR_INDICATOR: &str = "progress_bar_indicator";
static ID_INDICATOR: &str = "PGBAR_INDICATOR";
#[derive(Default, AsAny)]
struct BarState {
indicator: Entity,
}
impl State for BarState {
fn init(&mut self, registry: &mut Registry, ctx: &mut Context) {
self.indicator = ctx
.entity_of_child(ID_INDICATOR)
.expect("BarState.init(): Child could not be found!");
self.update_post_layout(registry, ctx);
}
fn update_post_layout(&mut self, _: &mut Registry, ctx: &mut Context) {
let val = ctx.widget().clone_or_default::<f64>("val");
let max_width = ctx.widget().get::<Rectangle>("bounds").width()
- ctx.widget().get::<Thickness>("padding").left()
- ctx.widget().get::<Thickness>("padding").right();
let new_width = calculate_width(val, max_width);
ctx.get_widget(self.indicator)
.get_mut::<Constraint>("constraint")
.set_width(new_width);
}
}
fn calculate_width(current_progress: f64, max_width: f64) -> f64 {
if (current_progress - RANGE_MIN).abs() < f64::EPSILON {
return 0.01;
} else if (current_progress - RANGE_MAX).abs() < f64::EPSILON {
return max_width * 0.99;
} else if current_progress > RANGE_MIN && current_progress < RANGE_MAX {
return max_width * current_progress;
}
max_width * 0.99
}
widget!(
ProgressBar<BarState> {
background: Brush,
border_brush: Brush,
border_radius: f64,
border_width: Thickness,
padding: Thickness,
val: f64
}
);
impl Template for ProgressBar {
fn template(self, _: Entity, ctx: &mut BuildContext) -> Self {
self.name("ProgressBar")
.style(STYLE_PROGRESS_BAR)
.on_changed_filter(vec!["val"])
.val(0.0)
.background("#000000")
.border_brush("#BABABA")
.border_radius(4)
.border_width(1)
.height(34)
.min_width(100.0)
.padding((2, 4, 2, 4))
.child(
Container::new()
.id(ID_INDICATOR)
.style(STYLE_PROGRESS_BAR_INDICATOR)
.background("#EFD035")
.border_radius(1.0)
.width(0.0)
.height(24.0)
.v_align("center")
.h_align("start")
.build(ctx),
)
}
fn render_object(&self) -> Box<dyn RenderObject> {
Box::new(RectangleRenderObject)
}
fn layout(&self) -> Box<dyn Layout> {
Box::new(PaddingLayout::new())
}
}
#[cfg(test)]
mod tests {
use super::*;
const ERROR: f64 = f64::EPSILON;
#[test]
fn test_calculate_width() {
assert!((0.01 - calculate_width(0.0, 100.0)).abs() < ERROR);
assert!((50.0 - calculate_width(0.5, 100.0)).abs() < ERROR);
assert!((99.0 - calculate_width(1.0, 100.0)).abs() < ERROR);
assert!((99.0 - calculate_width(1.23, 100.0)).abs() < ERROR);
assert!((99.0 - calculate_width(-1.23, 100.0)).abs() < ERROR);
}
}