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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! 仪表盘组件(半圆弧进度)。
use crate::{prelude::FluentBuilder as _, *};
use std::rc::Rc;
/// 仪表盘尺寸。
#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub enum GaugeSize {
/// 小尺寸。
Sm,
/// 中等尺寸。
#[default]
Md,
/// 大尺寸。
Lg,
/// 自定义尺寸。
Custom(u32),
}
impl GaugeSize {
/// 获取尺寸对应的宽高。
fn dimensions(self) -> (f32, f32) {
match self {
GaugeSize::Sm => (120.0, 80.0),
GaugeSize::Md => (200.0, 130.0),
GaugeSize::Lg => (300.0, 190.0),
GaugeSize::Custom(w) => (w as f32, w as f32 * 0.65),
}
}
/// 获取描边宽度。
fn stroke_width(self) -> f32 {
match self {
GaugeSize::Sm => 10.0,
GaugeSize::Md => 16.0,
GaugeSize::Lg => 22.0,
GaugeSize::Custom(w) => (w as f32 * 0.08).max(6.0),
}
}
}
/// 绘制状态。
struct PaintData {
value: f32,
color: Hsla,
track_color: Hsla,
stroke_width: f32,
}
/// 仪表盘组件。
#[derive(IntoElement)]
pub struct Gauge {
/// 组件 id(保留占位)。
_id: SharedString,
value: f32,
label: Option<SharedString>,
format_fn: Option<Rc<dyn Fn(f32) -> String>>,
size: GaugeSize,
color: Option<Hsla>,
track_color: Option<Hsla>,
style: StyleRefinement,
}
impl Gauge {
/// 创建仪表盘。
pub fn new(id: impl Into<SharedString>) -> Self {
Self {
_id: id.into(),
value: 0.0,
label: None,
format_fn: None,
size: GaugeSize::default(),
color: None,
track_color: None,
style: StyleRefinement::default(),
}
}
/// 设置进度值(0.0 - 1.0)。
pub fn value(mut self, value: f32) -> Self {
self.value = value.clamp(0.0, 1.0);
self
}
/// 设置标签。
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
/// 设置数值格式化函数。
pub fn format(mut self, f: impl Fn(f32) -> String + 'static) -> Self {
self.format_fn = Some(Rc::new(f));
self
}
/// 设置尺寸。
pub fn size(mut self, size: GaugeSize) -> Self {
self.size = size;
self
}
/// 设置进度颜色。
pub fn color(mut self, color: Hsla) -> Self {
self.color = Some(color);
self
}
/// 设置轨道颜色。
pub fn track_color(mut self, color: Hsla) -> Self {
self.track_color = Some(color);
self
}
}
impl Styled for Gauge {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl RenderOnce for Gauge {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let user_style = self.style;
let (width, height) = self.size.dimensions();
let stroke = self.size.stroke_width();
let gauge_color = self.color.unwrap_or_else(|| rgb(0x3b82f6).into());
let track = self.track_color.unwrap_or(theme.tokens.muted.color);
let formatted_value = if let Some(ref fmt) = self.format_fn {
fmt(self.value)
} else {
format!("{}%", (self.value * 100.0) as i32)
};
let text_color = theme.tokens.foreground;
let label_color = theme.tokens.muted_foreground;
let paint_data = PaintData {
value: self.value,
color: gauge_color,
track_color: track,
stroke_width: stroke,
};
div()
.flex()
.flex_col()
.items_center()
.gap(px(4.0))
.map(|this| {
let mut d = this;
d.style().refine(&user_style);
d
})
.child(
div()
.w(px(width))
.h(px(height))
.relative()
.child(
canvas(
move |_bounds, _window, _cx| paint_data,
move |bounds, data, window, _cx| {
if bounds.size.width <= px(0.0) || bounds.size.height <= px(0.0) {
return;
}
let w = bounds.size.width;
let center_x = bounds.left() + w * 0.5;
let arc_radius = w * 0.5 - px(data.stroke_width * 0.5);
if arc_radius <= px(0.0) {
return;
}
let center_y = bounds.bottom() - px(data.stroke_width * 0.5);
let segments = 60_usize;
let start_angle: f32 = std::f32::consts::PI;
let end_angle: f32 = 0.0;
{
let mut builder = PathBuilder::stroke(px(data.stroke_width));
for i in 0..=segments {
let t = i as f32 / segments as f32;
let angle = start_angle + (end_angle - start_angle) * t;
let pt = point(
center_x + arc_radius * angle.cos(),
center_y + arc_radius * angle.sin(),
);
if i == 0 {
builder.move_to(pt);
} else {
builder.line_to(pt);
}
}
if let Ok(path) = builder.build() {
window.paint_path(path, data.track_color);
}
}
if data.value > 0.0 {
let value_end =
start_angle + (end_angle - start_angle) * data.value;
let value_segments =
((segments as f32 * data.value) as usize).max(2);
let mut builder = PathBuilder::stroke(px(data.stroke_width));
for i in 0..=value_segments {
let t = i as f32 / value_segments as f32;
let angle = start_angle + (value_end - start_angle) * t;
let pt = point(
center_x + arc_radius * angle.cos(),
center_y + arc_radius * angle.sin(),
);
if i == 0 {
builder.move_to(pt);
} else {
builder.line_to(pt);
}
}
if let Ok(path) = builder.build() {
window.paint_path(path, data.color);
}
}
},
)
.size_full(),
)
.child(
div()
.absolute()
.bottom(px(4.0))
.left_0()
.right_0()
.flex()
.justify_center()
.child(
div()
.text_lg()
.font_weight(FontWeight::BOLD)
.text_color(text_color)
.child(formatted_value),
),
),
)
.when_some(self.label, |this, lbl| {
this.child(div().text_sm().text_color(label_color).child(lbl))
})
}
}