-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric_input.rs
More file actions
272 lines (252 loc) · 7.49 KB
/
numeric_input.rs
File metadata and controls
272 lines (252 loc) · 7.49 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use iced::{
Alignment, Background, Border, Color, Element, Length, Theme,
widget::{button, column, container, row, text_input},
};
use iced_fonts::lucide::{chevron_down, chevron_up};
use std::{
fmt::Display,
ops::{Add, RangeInclusive, Sub},
str::FromStr,
};
fn spinner_button_style(theme: &Theme, status: button::Status) -> button::Style {
let palette = theme.extended_palette();
let active_bg = palette.primary.strong.color;
let hovered_bg = palette.primary.base.color;
let disabled_bg = Color {
a: active_bg.a * 0.4,
..active_bg
};
let mut style = button::Style {
background: Some(Background::Color(match status {
button::Status::Hovered | button::Status::Pressed => hovered_bg,
button::Status::Disabled => disabled_bg,
_ => active_bg,
})),
text_color: match status {
button::Status::Disabled => Color {
a: palette.primary.strong.text.a * 0.45,
..palette.primary.strong.text
},
_ => palette.primary.strong.text,
},
..button::Style::default()
};
style.border = Border {
color: Color::from_rgba(0.0, 0.0, 0.0, 0.0),
width: 1.0,
radius: 3.0.into(),
};
style
}
fn shell_style(_theme: &Theme) -> container::Style {
container::Style {
text_color: Some(Color::from_rgb(0.92, 0.92, 0.92)),
background: Some(Background::Color(Color::from_rgba(0.10, 0.10, 0.10, 1.0))),
border: Border {
color: Color::from_rgba(0.28, 0.28, 0.28, 1.0),
width: 1.0,
radius: 2.0.into(),
},
..container::Style::default()
}
}
fn input_style(theme: &Theme, status: text_input::Status) -> text_input::Style {
let mut style = text_input::default(theme, status);
style.background = Background::Color(Color::TRANSPARENT);
style.border = Border {
color: Color::TRANSPARENT,
width: 0.0,
radius: 0.0.into(),
};
style
}
pub fn number_input<'a, T, Message>(
value: &'a T,
bounds: RangeInclusive<T>,
on_change: impl Fn(T) -> Message + 'a + Copy,
) -> Element<'a, Message>
where
T: Copy + Display + FromStr + PartialOrd + Add<Output = T> + Sub<Output = T> + From<u8> + 'a,
Message: Clone + 'a,
{
let min = *bounds.start();
let max = *bounds.end();
let current = *value;
let step = T::from(1_u8);
let dec_value = if current > min + step {
current - step
} else {
min
};
let inc_value = if current < max - step {
current + step
} else {
max
};
let input = text_input("", ¤t.to_string())
.on_input(move |raw| {
raw.parse::<T>()
.map(|parsed| {
let clamped = if parsed < min {
min
} else if parsed > max {
max
} else {
parsed
};
on_change(clamped)
})
.unwrap_or_else(|_| on_change(current))
})
.style(input_style)
.padding([5, 8])
.width(Length::Fixed(72.0))
.size(14);
let decrement = button(
container(chevron_down().size(14))
.center_x(Length::Fill)
.center_y(Length::Fill),
)
.style(spinner_button_style)
.padding(0)
.width(Length::Fixed(22.0))
.height(Length::Fixed(15.0));
let decrement = if current > min {
decrement.on_press(on_change(dec_value))
} else {
decrement
};
let increment = button(
container(chevron_up().size(14))
.center_x(Length::Fill)
.center_y(Length::Fill),
)
.style(spinner_button_style)
.padding(0)
.width(Length::Fixed(22.0))
.height(Length::Fixed(15.0));
let increment = if current < max {
increment.on_press(on_change(inc_value))
} else {
increment
};
container(
row![
container(input)
.width(Length::Fixed(72.0))
.center_y(Length::Fixed(30.0)),
column![increment, decrement]
.spacing(0)
.width(Length::Fixed(22.0))
.align_x(Alignment::Center),
]
.spacing(0)
.align_y(Alignment::Center),
)
.style(shell_style)
.into()
}
fn format_decimal_value(value: f32) -> String {
let mut formatted = format!("{value:.3}");
while formatted.contains('.') && formatted.ends_with('0') {
formatted.pop();
}
if formatted.ends_with('.') {
formatted.pop();
}
formatted
}
pub fn number_input_f32<'a, Message>(
value: &'a str,
bounds: RangeInclusive<f32>,
step: f32,
on_change: impl Fn(String) -> Message + 'a + Copy,
) -> Element<'a, Message>
where
Message: Clone + 'a,
{
let min = *bounds.start();
let max = *bounds.end();
let parsed_current = value.trim().parse::<f32>().ok();
let current = parsed_current.unwrap_or(min).clamp(min, max);
let dec_value = (current - step).clamp(min, max);
let inc_value = (current + step).clamp(min, max);
let input = text_input("", value)
.on_input(on_change)
.style(input_style)
.padding([5, 8])
.width(Length::Fixed(72.0))
.size(14);
let decrement = button(
container(chevron_down().size(14))
.center_x(Length::Fill)
.center_y(Length::Fill),
)
.style(spinner_button_style)
.padding(0)
.width(Length::Fixed(22.0))
.height(Length::Fixed(15.0));
let decrement = if current > min {
decrement.on_press(on_change(format_decimal_value(dec_value)))
} else {
decrement
};
let increment = button(
container(chevron_up().size(14))
.center_x(Length::Fill)
.center_y(Length::Fill),
)
.style(spinner_button_style)
.padding(0)
.width(Length::Fixed(22.0))
.height(Length::Fixed(15.0));
let increment = if current < max {
increment.on_press(on_change(format_decimal_value(inc_value)))
} else {
increment
};
container(
row![
container(input)
.width(Length::Fixed(72.0))
.center_y(Length::Fixed(30.0)),
column![increment, decrement]
.spacing(0)
.width(Length::Fixed(22.0))
.align_x(Alignment::Center),
]
.spacing(0)
.align_y(Alignment::Center),
)
.style(shell_style)
.into()
}
#[cfg(test)]
mod tests {
use super::format_decimal_value;
#[test]
fn format_decimal_value_trims_trailing_zeroes() {
assert_eq!(format_decimal_value(6.1), "6.1");
assert_eq!(format_decimal_value(6.0), "6");
assert_eq!(format_decimal_value(6.125), "6.125");
}
#[test]
fn format_decimal_value_handles_negative() {
assert_eq!(format_decimal_value(-6.5), "-6.5");
assert_eq!(format_decimal_value(-6.0), "-6");
}
#[test]
fn format_decimal_value_handles_zero() {
assert_eq!(format_decimal_value(0.0), "0");
}
#[test]
fn format_decimal_value_handles_large_numbers() {
assert_eq!(format_decimal_value(1234.567), "1234.567");
assert_eq!(format_decimal_value(1000.0), "1000");
}
#[test]
fn format_decimal_value_handles_small_decimals() {
assert_eq!(format_decimal_value(0.001), "0.001");
assert_eq!(format_decimal_value(0.000), "0");
}
}