1use crate::{Direction, Error, KeyboardInput, Orientation, RangeCommand, RangeValue};
2
3#[derive(Clone, Debug, PartialEq)]
6pub struct SliderValue {
7 pub range: RangeValue,
8 pub axis: Orientation,
9 pub direction: Direction,
10 pub step: f64,
11 pub page_step: f64,
12 pub value_text: Option<String>,
13}
14impl SliderValue {
15 pub fn validate(&self) -> Result<(), Error> {
16 if !self.range.min.is_finite()
20 || !self.range.max.is_finite()
21 || !self.range.value.is_finite()
22 || self.range.min > self.range.max
23 || self.range.value < self.range.min
24 || self.range.value > self.range.max
25 {
26 return Err(Error::InvalidProps("slider requires a finite closed range"));
27 }
28 if [self.step, self.page_step]
29 .into_iter()
30 .any(|value| !value.is_finite() || value <= 0.0)
31 {
32 return Err(Error::InvalidProps(
33 "slider steps must be finite and positive",
34 ));
35 }
36 if self
37 .value_text
38 .as_ref()
39 .is_some_and(|text| text.len() > 1024 || text.trim().is_empty())
40 {
41 return Err(Error::InvalidProps(
42 "slider value text requires 1..=1024 bytes",
43 ));
44 }
45 Ok(())
46 }
47}
48impl RangeCommand {
49 pub fn slider_key(
50 axis: Orientation,
51 direction: Direction,
52 input: KeyboardInput<'_>,
53 ) -> Option<Self> {
54 if input.editable
55 || input.composing
56 || input.control
57 || input.meta
58 || input.alt
59 || input.shift
60 {
61 return None;
62 }
63 match (axis, direction, input.key) {
64 (Orientation::Horizontal, Direction::Ltr, "ArrowRight")
65 | (Orientation::Horizontal, Direction::Rtl, "ArrowLeft")
66 | (Orientation::Vertical, _, "ArrowUp") => Some(Self::Increase),
67 (Orientation::Horizontal, Direction::Ltr, "ArrowLeft")
68 | (Orientation::Horizontal, Direction::Rtl, "ArrowRight")
69 | (Orientation::Vertical, _, "ArrowDown") => Some(Self::Decrease),
70 (_, _, "PageUp") => Some(Self::PageIncrease),
71 (_, _, "PageDown") => Some(Self::PageDecrease),
72 (_, _, "Home") => Some(Self::Minimum),
73 (_, _, "End") => Some(Self::Maximum),
74 _ => None,
75 }
76 }
77}