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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Meter component — measurement indicator with zones (low/optimal/high).
use maud::{html, Markup};
/// Meter rendering properties
#[derive(Debug, Clone)]
pub struct Props {
/// Current value of the meter
pub value: f64,
/// Minimum value (default 0.0)
pub min: f64,
/// Maximum value (default 100.0)
pub max: f64,
/// Threshold below which the zone is "suboptimum" (warning)
pub low: Option<f64>,
/// Threshold above which the zone is "suboptimum" (warning)
pub high: Option<f64>,
/// The ideal value; if present, determines which side is "good"
pub optimum: Option<f64>,
/// Label for the meter (accessibility)
pub label: String,
}
impl Default for Props {
fn default() -> Self {
Self {
value: 0.0,
min: 0.0,
max: 100.0,
low: None,
high: None,
optimum: None,
label: String::new(),
}
}
}
/// Determine the zone class based on value, thresholds, and optimum.
fn zone_class(value: f64, min: f64, max: f64, low: Option<f64>, high: Option<f64>, optimum: Option<f64>) -> &'static str {
// If no optimum, always optimum
if optimum.is_none() {
return "optimum";
}
let opt = optimum.unwrap();
let mid = (min + max) / 2.0;
// Determine which side is ideal based on optimum position
if opt >= mid {
// Ideal side is "high" (e.g., battery level — higher is better)
// low = minimum acceptable, high = good threshold
if let Some(l) = low {
if value < l {
return "danger";
}
}
if let Some(h) = high {
if value >= h {
return "optimum";
}
}
"suboptimum"
} else {
// Ideal side is "low" (e.g., disk usage — lower is better)
// Zones are: optimum (before midpoint), suboptimum (midpoint to high), danger (above high)
if let Some(h) = high {
if value > h {
return "danger";
}
}
let zone_threshold = if let (Some(l), Some(h)) = (low, high) {
(l + h) / 2.0
} else {
low.unwrap_or(high.unwrap_or(mid))
};
if value <= zone_threshold {
"optimum"
} else {
"suboptimum"
}
}
}
/// Render a single meter with the given properties
pub fn render(props: Props) -> Markup {
let pct = ((props.value - props.min) / (props.max - props.min) * 100.0).clamp(0.0, 100.0);
let zone = zone_class(props.value, props.min, props.max, props.low, props.high, props.optimum);
let width_style = format!("width: {:.1}%", pct);
html! {
div class="mui-meter" role="meter" aria-valuenow=(props.value) aria-valuemin=(props.min) aria-valuemax=(props.max) aria-label=(props.label) {
div.mui-meter__track {
div class={
"mui-meter__bar "
"mui-meter__bar--" (zone)
} style=(width_style) {}
}
}
}
}
/// Showcase battery level and disk usage meters
pub fn showcase() -> Markup {
html! {
div.mui-showcase__grid {
// Battery level (higher is better)
div {
p.mui-showcase__caption { "Battery level (higher is better)" }
div.mui-showcase__row {
// Low (danger)
div {
(render(Props {
value: 15.0,
min: 0.0,
max: 100.0,
low: Some(20.0),
high: Some(80.0),
optimum: Some(100.0),
label: "Battery 15%".into(),
}))
p.mui-showcase__label { "15%" }
}
// Medium (warning)
div {
(render(Props {
value: 60.0,
min: 0.0,
max: 100.0,
low: Some(20.0),
high: Some(80.0),
optimum: Some(100.0),
label: "Battery 60%".into(),
}))
p.mui-showcase__label { "60%" }
}
// High (success)
div {
(render(Props {
value: 90.0,
min: 0.0,
max: 100.0,
low: Some(20.0),
high: Some(80.0),
optimum: Some(100.0),
label: "Battery 90%".into(),
}))
p.mui-showcase__label { "90%" }
}
}
}
// Disk usage (lower is better)
div {
p.mui-showcase__caption { "Disk usage (lower is better)" }
div.mui-showcase__row {
// Low (success)
div {
(render(Props {
value: 25.0,
min: 0.0,
max: 100.0,
low: Some(20.0),
high: Some(80.0),
optimum: Some(0.0),
label: "Disk 25%".into(),
}))
p.mui-showcase__label { "25%" }
}
// Medium (warning)
div {
(render(Props {
value: 70.0,
min: 0.0,
max: 100.0,
low: Some(20.0),
high: Some(80.0),
optimum: Some(0.0),
label: "Disk 70%".into(),
}))
p.mui-showcase__label { "70%" }
}
// High (danger)
div {
(render(Props {
value: 95.0,
min: 0.0,
max: 100.0,
low: Some(20.0),
high: Some(80.0),
optimum: Some(0.0),
label: "Disk 95%".into(),
}))
p.mui-showcase__label { "95%" }
}
}
}
}
}
}