xrack-runtime 0.2.0

Runtime types, properties, models, and controls for generated XRack UI
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use crate::{Node, Value};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum DialInputMode {
    #[default]
    Circular,
    Horizontal,
    Vertical,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ScrollSnapMode {
    #[default]
    None,
    Always,
    OnRelease,
}

/// The normalized input understood by range controls, independent of the backend.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RangeAction {
    SetFraction(f64),
    CommitFraction(f64),
    SetValue(f64),
    SetPoint { x: f64, y: f64 },
    Decrease,
    Increase,
    Step(i64),
    Minimum,
    Maximum,
    Release,
}

impl RangeAction {
    pub fn is_release(self) -> bool {
        matches!(self, Self::Release | Self::CommitFraction(_))
    }
}

/// Step by a wheel batch without iterating once per detent.
pub(crate) fn step_float(
    value: f64,
    limits: (f64, f64),
    step: f64,
    steps: i64,
    wrap: bool,
    normalize: impl Fn(f64) -> f64,
) -> f64 {
    let (min, max) = limits;
    if step == 0.0 || steps == 0 || min == max {
        return value;
    }
    let forward = steps > 0;
    let amount = steps.unsigned_abs() as f64;
    let signed_step = if forward { step } else { -step };
    let advance = |from, count| normalize(signed_step.mul_add(count, from));
    let limit = if forward { max } else { min };
    let fits = |candidate| {
        if forward {
            candidate <= limit
        } else {
            candidate >= limit
        }
    };
    let next = advance(value, amount);
    if !wrap || fits(next) {
        return next.clamp(min, max);
    }
    let steps_in_range = |from: f64| {
        let distance = limit - from;
        let estimate = if distance.is_finite() {
            distance / signed_step
        } else {
            (limit * 0.5 - from * 0.5) / (signed_step * 0.5)
        }
        .floor()
        .min(amount);
        if estimate < amount && fits(advance(from, estimate + 1.0)) {
            estimate + 1.0
        } else if estimate > 0.0 && !fits(advance(from, estimate)) {
            estimate - 1.0
        } else {
            estimate
        }
    };
    let start = if forward { min } else { max };
    let remaining = (amount - steps_in_range(value) - 1.0).max(0.0);
    let cycle = steps_in_range(start) + 1.0;
    advance(start, remaining % cycle).clamp(min, max)
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SliderRange {
    min: f64,
    max: f64,
    value: f64,
    step: f64,
    wrap: bool,
    snap: ScrollSnapMode,
}

impl SliderRange {
    pub fn from_node(node: &Node) -> Self {
        let min = node.number("minimum").unwrap_or(0.0);
        let max = node.number("maximum").unwrap_or(100.0).max(min);
        Self {
            min,
            max,
            value: node.number("value").unwrap_or(0.0).clamp(min, max),
            step: node.number("step").unwrap_or(1.0).max(0.0),
            wrap: node.kind == "Dial" && node.boolean("wrap", false),
            snap: match node.properties.get("snap-mode") {
                Some(Value::ScrollSnapMode(mode)) => *mode,
                _ => ScrollSnapMode::None,
            },
        }
    }

    pub fn with_value(mut self, value: f64) -> Self {
        if value.is_finite() {
            self.value = value.clamp(self.min, self.max);
        }
        self
    }

    pub fn snap_mode(self) -> ScrollSnapMode {
        self.snap
    }

    pub fn preview_fraction(self, fraction: f64) -> f64 {
        self.with_value(
            self.apply(RangeAction::SetFraction(fraction))
                .unwrap_or(self.value),
        )
        .fraction()
    }

    fn snap_value(self, value: f64) -> f64 {
        let value = value.clamp(self.min, self.max);
        if self.step == 0.0 || value == self.min || value == self.max {
            return value;
        }
        let steps = ((value - self.min) / self.step).round();
        if !steps.is_finite() {
            return value;
        }
        let snapped = steps.mul_add(self.step, self.min).clamp(self.min, self.max);
        if self.max - value < (snapped - value).abs() {
            self.max
        } else {
            snapped
        }
    }

    pub fn value(self) -> f64 {
        self.value
    }

    pub fn fraction(self) -> f64 {
        if self.max == self.min {
            return 0.0;
        }
        let span = self.max - self.min;
        if span.is_finite() {
            (self.value - self.min) / span
        } else {
            // Halve first so even a range spanning both finite extremes works.
            (self.value * 0.5 - self.min * 0.5) / (self.max * 0.5 - self.min * 0.5)
        }
    }

    pub fn apply(self, action: RangeAction) -> Option<f64> {
        let next = match action {
            RangeAction::SetFraction(fraction) | RangeAction::CommitFraction(fraction)
                if fraction.is_finite() =>
            {
                let t = fraction.clamp(0.0, 1.0);
                let value = self.min * (1.0 - t) + self.max * t;
                if self.snap == ScrollSnapMode::Always
                    || self.snap == ScrollSnapMode::OnRelease && action.is_release()
                {
                    self.snap_value(value)
                } else {
                    value
                }
            }
            RangeAction::SetValue(value) if value.is_finite() => {
                if self.snap == ScrollSnapMode::None {
                    value
                } else {
                    self.snap_value(value)
                }
            }
            RangeAction::Decrease => self.value - self.step,
            RangeAction::Increase => self.value + self.step,
            RangeAction::Step(steps) => step_float(
                self.value,
                (self.min, self.max),
                self.step,
                steps,
                self.wrap,
                |value| value,
            ),
            RangeAction::Minimum => self.min,
            RangeAction::Maximum => self.max,
            _ => return None,
        };
        let next = match action {
            RangeAction::Increase if self.wrap && next > self.max => self.min,
            RangeAction::Decrease if self.wrap && next < self.min => self.max,
            _ => next.clamp(self.min, self.max),
        };
        (next != self.value).then_some(next)
    }
}

pub fn progress_value(node: &Node) -> f64 {
    node.number("progress").unwrap_or(0.0).clamp(0.0, 1.0)
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScrollBarRange {
    pub position: f64,
    pub size: f64,
    pub step: f64,
}

impl ScrollBarRange {
    pub const DEFAULT_SIZE: f64 = 0.0;
    pub const DEFAULT_STEP: f64 = 0.1;

    pub fn from_node(node: &Node) -> Self {
        Self::new(
            node.number("position").unwrap_or_default(),
            node.number("size").unwrap_or(Self::DEFAULT_SIZE),
            node.number("step-size").unwrap_or(Self::DEFAULT_STEP),
        )
    }

    pub fn new(position: f64, size: f64, step: f64) -> Self {
        let unit = |value: f64, default| {
            if value.is_finite() {
                value.clamp(0.0, 1.0)
            } else {
                default
            }
        };
        let size = unit(size, Self::DEFAULT_SIZE);
        Self {
            position: unit(position, 0.0).min(1.0 - size),
            size,
            step: unit(step, Self::DEFAULT_STEP),
        }
    }

    pub fn maximum(self) -> f64 {
        1.0 - self.size
    }

    pub fn apply(self, action: RangeAction) -> Option<f64> {
        let next = match action {
            RangeAction::SetFraction(fraction) if fraction.is_finite() => fraction * self.maximum(),
            RangeAction::Decrease => self.position - self.step,
            RangeAction::Increase => self.position + self.step,
            RangeAction::Minimum => 0.0,
            RangeAction::Maximum => self.maximum(),
            _ => return None,
        }
        .clamp(0.0, self.maximum());
        (next != self.position).then_some(next)
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum IndicatorState {
    Hidden,
    Determinate(f64),
    Indeterminate,
}

pub fn indicator_state(node: &Node) -> Option<IndicatorState> {
    Some(match node.kind {
        "BusyIndicator" if node.boolean("running", true) => IndicatorState::Indeterminate,
        "BusyIndicator" => IndicatorState::Hidden,
        "Spinner" | "ProgressIndicator" if node.boolean("indeterminate", false) => {
            IndicatorState::Indeterminate
        }
        "Spinner" | "ProgressIndicator" => IndicatorState::Determinate(progress_value(node)),
        _ => return None,
    })
}

#[derive(Clone, Copy, Debug)]
pub struct SliderInterval {
    axis: SliderRange,
    values: [f64; 2],
}

impl SliderInterval {
    pub fn from_node(node: &Node) -> Self {
        let axis = SliderRange::from_node(node);
        Self::from_values(
            node,
            node.number("lower-value").unwrap_or(axis.min),
            node.number("upper-value").unwrap_or(axis.max),
        )
    }

    pub fn from_values(node: &Node, lower: f64, upper: f64) -> Self {
        let axis = SliderRange::from_node(node);
        let upper = if upper.is_finite() {
            upper.clamp(axis.min, axis.max)
        } else {
            axis.max
        };
        let lower = if lower.is_finite() {
            lower.clamp(axis.min, upper)
        } else {
            axis.min
        };
        Self {
            axis,
            values: [lower, upper],
        }
    }

    pub fn values(self) -> [f64; 2] {
        self.values
    }

    pub fn fractions(self) -> [f64; 2] {
        self.values
            .map(|value| SliderRange { value, ..self.axis }.fraction())
    }

    pub fn nearest_handle(self, fraction: f64) -> usize {
        let [lower, upper] = self.fractions();
        usize::from(if lower == upper {
            fraction >= lower
        } else {
            (fraction - upper).abs() < (fraction - lower).abs()
        })
    }

    pub fn preview_fractions(self, handle: usize, fraction: f64) -> [f64; 2] {
        let values = self
            .apply(handle, RangeAction::SetFraction(fraction))
            .unwrap_or(self.values);
        Self { values, ..self }.fractions()
    }

    pub fn apply(self, handle: usize, action: RangeAction) -> Option<[f64; 2]> {
        let value = *self.values.get(handle)?;
        let mut values = self.values;
        let next = SliderRange { value, ..self.axis }.apply(action)?;
        values[handle] = if handle == 0 {
            next.min(values[1])
        } else {
            next.max(values[0])
        };
        (values != self.values).then_some(values)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn batched_steps_keep_remaining_detents_after_wrap() {
        let mut dial = Node::new("Dial", None);
        dial.properties.insert("minimum", (-2.0_f64).into());
        dial.properties.insert("maximum", 3.0_f64.into());
        dial.properties.insert("value", 2.0_f64.into());
        dial.properties.insert("wrap", true.into());
        let range = SliderRange::from_node(&dial);
        assert_eq!(range.apply(RangeAction::Step(3)), Some(-1.0));
        assert_eq!(range.apply(RangeAction::Step(-6)), None);
        assert_eq!(
            step_float(
                -f64::MAX,
                (-f64::MAX, f64::MAX),
                f64::MAX,
                10,
                true,
                |value| value
            ),
            0.0,
        );
    }

    #[test]
    fn slider_snap_uses_the_minimum_and_keeps_a_partial_final_step_reachable() {
        let mut node = Node::new("Slider", None);
        node.properties.insert("minimum", 3.0_f64.into());
        node.properties.insert("maximum", 17.0_f64.into());
        node.properties.insert("step", 5.0_f64.into());
        node.properties
            .insert("snap-mode", ScrollSnapMode::OnRelease.into());
        let range = SliderRange::from_node(&node);
        assert_eq!(range.apply(RangeAction::SetFraction(0.5)), Some(10.0));
        assert_eq!(range.apply(RangeAction::CommitFraction(0.5)), Some(8.0));
        assert_eq!(range.apply(RangeAction::CommitFraction(0.96)), Some(17.0));
        assert_eq!(range.apply(RangeAction::SetValue(15.0)), Some(13.0));
        assert_eq!(range.apply(RangeAction::SetValue(f64::INFINITY)), None);
    }
}