Skip to main content

xrack_runtime/
range.rs

1use crate::Node;
2
3#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
4pub enum DialInputMode {
5    #[default]
6    Circular,
7    Horizontal,
8    Vertical,
9}
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
12pub enum ScrollSnapMode {
13    #[default]
14    None,
15    Always,
16    OnRelease,
17}
18
19/// The normalized input understood by range controls, independent of the backend.
20#[derive(Clone, Copy, Debug, PartialEq)]
21pub enum RangeAction {
22    SetFraction(f64),
23    Decrease,
24    Increase,
25    Minimum,
26    Maximum,
27    Release,
28}
29
30#[derive(Clone, Copy, Debug, PartialEq)]
31pub struct SliderRange {
32    min: f64,
33    max: f64,
34    value: f64,
35    step: f64,
36    wrap: bool,
37}
38
39impl SliderRange {
40    pub fn from_node(node: &Node) -> Self {
41        let min = node.number("minimum").unwrap_or(0.0);
42        let max = node.number("maximum").unwrap_or(100.0).max(min);
43        Self {
44            min,
45            max,
46            value: node.number("value").unwrap_or(0.0).clamp(min, max),
47            step: node.number("step").unwrap_or(1.0).max(0.0),
48            wrap: node.kind == "Dial" && node.boolean("wrap", false),
49        }
50    }
51
52    pub fn value(self) -> f64 {
53        self.value
54    }
55
56    pub fn fraction(self) -> f64 {
57        if self.max == self.min {
58            return 0.0;
59        }
60        let span = self.max - self.min;
61        if span.is_finite() {
62            (self.value - self.min) / span
63        } else {
64            // Halve first so even a range spanning both finite extremes works.
65            (self.value * 0.5 - self.min * 0.5) / (self.max * 0.5 - self.min * 0.5)
66        }
67    }
68
69    pub fn apply(self, action: RangeAction) -> Option<f64> {
70        let next = match action {
71            RangeAction::SetFraction(fraction) if fraction.is_finite() => {
72                let t = fraction.clamp(0.0, 1.0);
73                self.min * (1.0 - t) + self.max * t
74            }
75            RangeAction::Decrease => self.value - self.step,
76            RangeAction::Increase => self.value + self.step,
77            RangeAction::Minimum => self.min,
78            RangeAction::Maximum => self.max,
79            RangeAction::SetFraction(_) | RangeAction::Release => return None,
80        };
81        let next = match action {
82            RangeAction::Increase if self.wrap && next > self.max => self.min,
83            RangeAction::Decrease if self.wrap && next < self.min => self.max,
84            _ => next.clamp(self.min, self.max),
85        };
86        (next != self.value).then_some(next)
87    }
88}
89
90pub fn progress_value(node: &Node) -> f64 {
91    node.number("progress").unwrap_or(0.0).clamp(0.0, 1.0)
92}
93
94#[derive(Clone, Copy, Debug, PartialEq)]
95pub struct ScrollBarRange {
96    pub position: f64,
97    pub size: f64,
98    pub step: f64,
99}
100
101impl ScrollBarRange {
102    pub const DEFAULT_SIZE: f64 = 0.0;
103    pub const DEFAULT_STEP: f64 = 0.1;
104
105    pub fn from_node(node: &Node) -> Self {
106        Self::new(
107            node.number("position").unwrap_or_default(),
108            node.number("size").unwrap_or(Self::DEFAULT_SIZE),
109            node.number("step-size").unwrap_or(Self::DEFAULT_STEP),
110        )
111    }
112
113    pub fn new(position: f64, size: f64, step: f64) -> Self {
114        let unit = |value: f64, default| {
115            if value.is_finite() {
116                value.clamp(0.0, 1.0)
117            } else {
118                default
119            }
120        };
121        let size = unit(size, Self::DEFAULT_SIZE);
122        Self {
123            position: unit(position, 0.0).min(1.0 - size),
124            size,
125            step: unit(step, Self::DEFAULT_STEP),
126        }
127    }
128
129    pub fn maximum(self) -> f64 {
130        1.0 - self.size
131    }
132
133    pub fn apply(self, action: RangeAction) -> Option<f64> {
134        let next = match action {
135            RangeAction::SetFraction(fraction) if fraction.is_finite() => fraction * self.maximum(),
136            RangeAction::Decrease => self.position - self.step,
137            RangeAction::Increase => self.position + self.step,
138            RangeAction::Minimum => 0.0,
139            RangeAction::Maximum => self.maximum(),
140            _ => return None,
141        }
142        .clamp(0.0, self.maximum());
143        (next != self.position).then_some(next)
144    }
145}
146
147#[derive(Clone, Copy, Debug, PartialEq)]
148pub enum IndicatorState {
149    Hidden,
150    Determinate(f64),
151    Indeterminate,
152}
153
154pub fn indicator_state(node: &Node) -> Option<IndicatorState> {
155    Some(match node.kind {
156        "BusyIndicator" if node.boolean("running", true) => IndicatorState::Indeterminate,
157        "BusyIndicator" => IndicatorState::Hidden,
158        "Spinner" | "ProgressIndicator" if node.boolean("indeterminate", false) => {
159            IndicatorState::Indeterminate
160        }
161        "Spinner" | "ProgressIndicator" => IndicatorState::Determinate(progress_value(node)),
162        _ => return None,
163    })
164}
165
166#[derive(Clone, Copy, Debug)]
167pub struct SliderInterval {
168    axis: SliderRange,
169    values: [f64; 2],
170}
171
172impl SliderInterval {
173    pub fn from_node(node: &Node) -> Self {
174        let axis = SliderRange::from_node(node);
175        Self::from_values(
176            node,
177            node.number("lower-value").unwrap_or(axis.min),
178            node.number("upper-value").unwrap_or(axis.max),
179        )
180    }
181
182    pub fn from_values(node: &Node, lower: f64, upper: f64) -> Self {
183        let axis = SliderRange::from_node(node);
184        let upper = if upper.is_finite() {
185            upper.clamp(axis.min, axis.max)
186        } else {
187            axis.max
188        };
189        let lower = if lower.is_finite() {
190            lower.clamp(axis.min, upper)
191        } else {
192            axis.min
193        };
194        Self {
195            axis,
196            values: [lower, upper],
197        }
198    }
199
200    pub fn values(self) -> [f64; 2] {
201        self.values
202    }
203
204    pub fn fractions(self) -> [f64; 2] {
205        self.values
206            .map(|value| SliderRange { value, ..self.axis }.fraction())
207    }
208
209    pub fn apply(self, handle: usize, action: RangeAction) -> Option<[f64; 2]> {
210        let value = *self.values.get(handle)?;
211        let mut values = self.values;
212        let next = SliderRange { value, ..self.axis }.apply(action)?;
213        values[handle] = if handle == 0 {
214            next.min(values[1])
215        } else {
216            next.max(values[0])
217        };
218        (values != self.values).then_some(values)
219    }
220}