zaxis 0.0.5

An event-driven immediate mode GUI for desktop tools
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::{hash::Hash, ops::RangeInclusive, panic::Location};

use winit::keyboard::KeyCode;

use crate::{
    context::{HitAction, HitRegion, Paint, SliderInput},
    Border, Color, CornerRadius, Id, Rect, Vec2,
};

use super::{font_size, visible_label, HoverStyle, Numeric, Response, SemanticStatus, Ui, Widget};
mod paint;

/// Space between a caption above the track and the track.
const CAPTION_GAP: f32 = 4.0;

/// Semantic accent for a slider; the same type every control uses for validation.
/// An explicit [`Slider::color`] takes precedence.
pub type SliderStatus = SemanticStatus;

/// A horizontal slider bound to a number: `f32` by default, any [`Numeric`] type works.
/// Integers snap to whole numbers and show no decimals without further configuration.
/// Enabled values are clamped and snapped on every UI pass; disabled values are untouched.
/// Pointer capture continues outside the control. Tab focuses it; arrows adjust it,
/// PageUp/PageDown adjust by ten steps, and Home/End select the endpoints.
pub struct Slider<'a, T: Numeric = f32> {
    value: &'a mut T,
    range: RangeInclusive<T>,
    step: Option<T>,
    id: Option<Id>,
    enabled: bool,
    width: Option<f32>,
    color: Option<Color>,
    status: SemanticStatus,
    source: &'static Location<'static>,
    text: Option<String>,
    caption_above: bool,
    suffix: String,
    precision: Option<usize>,
    blur: Option<f32>,
    hover_style: Option<HoverStyle>,
    style: super::theme::SliderStyle,
    painter: Option<super::theme::painter::PaintHook<'a>>,
}

impl<'a, T: Numeric> Slider<'a, T> {
    /// Equal endpoints are allowed. Descending endpoints are swapped; a range with a
    /// non-finite end becomes `0..=1`.
    #[track_caller]
    pub fn new(value: &'a mut T, range: RangeInclusive<T>) -> Self {
        let (start, end) = (*range.start(), *range.end());
        let range = if !start.finite() || !end.finite() {
            crate::context::invalid_value(
                "Slider::new",
                format!("expected a finite range, got {start}..={end}; using 0..=1"),
            );
            T::from_f64(0.0)..=T::from_f64(1.0)
        } else if start > end {
            crate::context::invalid_value(
                "Slider::new",
                format!("expected an ascending range, got {start}..={end}; ends swapped"),
            );
            end..=start
        } else {
            range
        };
        Self {
            value,
            range,
            step: None,
            id: None,
            enabled: true,
            width: None,
            color: None,
            status: SemanticStatus::Normal,
            source: Location::caller(),
            text: None,
            caption_above: false,
            suffix: String::new(),
            precision: None,
            blur: None,
            hover_style: None,
            style: Default::default(),
            painter: None,
        }
    }

    pub fn style(mut self, style: super::theme::SliderStyle) -> Self {
        self.style = style;
        self
    }
    #[track_caller]
    pub fn height(mut self, height: f32) -> Self {
        self.style.height =
            super::sanitize::positive("Slider::height", height).or(self.style.height);
        self
    }
    /// Called for SliderTrack (including its fill) and SliderThumb.
    pub fn painter(
        mut self,
        mode: super::theme::PaintMode,
        paint: impl Fn(&mut super::theme::Painter<'_>, super::theme::ControlPaint) + 'a,
    ) -> Self {
        self.painter = Some(super::theme::painter::PaintHook {
            mode,
            callback: Box::new(paint),
        });
        self
    }
    pub fn id_source(mut self, source: impl Hash) -> Self {
        self.id = Some(Id::new(source));
        self
    }

    /// Display a caption and the current value below the track.
    pub fn text(mut self, text: impl Into<String>) -> Self {
        self.text = Some(text.into());
        self
    }
    /// Put the caption on a line above the track: the text left-aligned and the value with
    /// its suffix right-aligned, instead of one `caption: value` line below it.
    pub fn caption_above(mut self, above: bool) -> Self {
        self.caption_above = above;
        self
    }
    pub fn suffix(mut self, suffix: impl Into<String>) -> Self {
        self.suffix = suffix.into();
        self
    }
    /// Decimals shown for floats (default 2, at most 16). Integers never show decimals.
    pub fn precision(mut self, precision: usize) -> Self {
        self.precision = Some(precision.min(16));
        self
    }

    /// Snap to multiples of a positive, finite step relative to the range minimum;
    /// integers default to one. Both endpoints remain selectable even when the step
    /// does not divide the range. A step that is not positive is ignored.
    #[track_caller]
    pub fn step(mut self, step: T) -> Self {
        if step.positive() {
            self.step = Some(step);
        } else {
            crate::context::invalid_value(
                "Slider::step",
                format!("expected a finite step > 0, got {step}; ignored"),
            );
        }
        self
    }

    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    #[deprecated(
        note = "use `.enabled(!disabled)`; `enabled` is the one way to set a control's availability"
    )]
    pub fn disabled(self, disabled: bool) -> Self {
        self.enabled(!disabled)
    }

    /// Preferred width in logical pixels, limited by the available layout width.
    #[track_caller]
    pub fn width(mut self, width: f32) -> Self {
        self.width = super::sanitize::positive("Slider::width", width).or(self.width);
        self
    }

    /// Override the status accent on the filled track and thumb.
    pub fn color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    pub fn status(mut self, status: SemanticStatus) -> Self {
        self.status = status;
        self
    }
    pub fn blur(mut self, radius: f32) -> Self {
        self.blur = Some(super::blur::normalize_radius(radius));
        self
    }
    /// Apply the hover preset to the thumb, keeping the track's status accent.
    pub fn hover_style(mut self, style: HoverStyle) -> Self {
        self.hover_style = Some(style);
        self
    }

    /// The explicit step, or one for integer types.
    fn step_f64(&self) -> Option<f64> {
        self.step.map(T::to_f64).or(T::INTEGER.then_some(1.0))
    }

    fn normalized(&self, value: f64) -> T {
        let (start, end) = (*self.range.start(), *self.range.end());
        let (min, max) = (start.to_f64(), end.to_f64());
        let value = if value.is_nan() {
            min
        } else {
            value.clamp(min, max)
        };
        if value == min {
            return start;
        }
        if value == max {
            return end;
        }
        let value = if let Some(step) = self.step_f64() {
            min + ((value - min) / step).round() * step
        } else {
            value
        };
        T::from_f64(value).normalized(start, end)
    }
}

impl<T: Numeric> Widget for Slider<'_, T> {
    fn ui(mut self, ui: &mut Ui<'_>) -> Response {
        self.enabled &= ui.is_enabled();
        let style = ui.style().clone();
        let mut component = style.slider;
        component.merge(self.style);
        let height = component.height.unwrap_or(28.0).max(0.0);
        let id = match self.id {
            Some(id) => ui.scope.with(("slider", id)),
            None => ui.auto_id(("slider", self.source)),
        };
        let size = font_size(style.font_size);
        let weight = ui.style().typography.weights.control;
        let width = self
            .width
            .or(component.width)
            .unwrap_or(200.0)
            .min(ui.available_width());
        let precision = self.precision.unwrap_or(if T::INTEGER { 0 } else { 2 });
        // The caption fixes the line height; the value is formatted after input.
        let header = self
            .text
            .as_ref()
            .filter(|_| self.caption_above)
            .map(|text| {
                let caption = visible_label(text).to_owned();
                let line = ui.context.measure_text(&caption, size, weight, width).y;
                (caption, line)
            });
        let header_height = header.as_ref().map_or(0.0, |(_, line)| line + CAPTION_GAP);
        let rect = Rect::from_min_size(
            ui.layout.cursor + Vec2::new(0.0, header_height),
            Vec2::new(width, height),
        );
        let mut response = ui.response(id, rect, self.enabled);
        let radius = component
            .thumb_radius
            .unwrap_or(8.0)
            .max(0.0)
            .min(rect.size().x * 0.5)
            .min(height * 0.5);
        let left = rect.min.x + radius;
        let right = rect.max.x - radius;
        let min = self.range.start().to_f64();
        let max = self.range.end().to_f64();
        let span = max - min;
        let mut value = self.normalized(self.value.to_f64());
        let mut input = ui.context.take_slider_input(id);
        // Requests from assistive technology go through the same steps as the keys.
        for action in ui.context.take_access_actions(id) {
            input.push(match action {
                crate::AccessAction::Increment => SliderInput::Key(KeyCode::ArrowRight),
                crate::AccessAction::Decrement => SliderInput::Key(KeyCode::ArrowLeft),
                crate::AccessAction::SetNumericValue(value) => SliderInput::Set(value),
                _ => continue,
            });
        }
        if self.enabled {
            for event in input {
                let next = match event {
                    SliderInput::Pointer(pointer) if right > left => {
                        let t = f64::from(((pointer.x - left) / (right - left)).clamp(0.0, 1.0));
                        min + span * t
                    }
                    SliderInput::Set(target) if target.is_finite() => target,
                    SliderInput::Set(_) => value.to_f64(),
                    SliderInput::Key(KeyCode::Home) => min,
                    SliderInput::Key(KeyCode::End) => max,
                    SliderInput::Key(key) => {
                        let direction = match key {
                            KeyCode::ArrowRight | KeyCode::ArrowUp => 1.0,
                            KeyCode::ArrowLeft | KeyCode::ArrowDown => -1.0,
                            KeyCode::PageUp => 10.0,
                            KeyCode::PageDown => -10.0,
                            _ => 0.0,
                        };
                        let step = self.step_f64().unwrap_or(span / 100.0);
                        if self.step_f64().is_some() {
                            let offset = (value.to_f64() - min) / step;
                            // Avoid skipping a tick because the bound f32 rounded slightly.
                            let nearest = offset.round();
                            let offset = if (offset - nearest).abs() < 1e-6 {
                                nearest
                            } else {
                                offset
                            };
                            let tick = if direction > 0.0 {
                                offset.floor()
                            } else {
                                offset.ceil()
                            };
                            min + (tick + direction) * step
                        } else {
                            value.to_f64() + direction * step
                        }
                    }
                    _ => value.to_f64(),
                };
                value = self.normalized(next);
            }
            response.changed = !self.value.same(value);
            *self.value = value;
        }
        let label = self
            .text
            .as_ref()
            .filter(|_| !self.caption_above)
            .map(|text| {
                let caption = visible_label(text);
                let separator = if caption.is_empty() { "" } else { ": " };
                let text = format!(
                    "{caption}{separator}{}{}",
                    self.value.formatted(Some(precision)),
                    self.suffix
                );
                let text_size = ui.context.measure_text(&text, size, weight, width);
                (text, text_size)
            });
        let label_height = label
            .as_ref()
            .map_or(0.0, |(_, text_size)| ui.layout.spacing + text_size.y);
        ui.allocate_space(Vec2::new(width, header_height + height + label_height));
        ui.context.register_hit(HitRegion {
            id,
            window: ui.window,
            rect,
            clip: ui.clip,
            action: if self.enabled {
                HitAction::Slider
            } else {
                HitAction::Block
            },
        });
        ui.a11y(id, rect, crate::AccessRole::Slider, |node| {
            use crate::AccessActionKind as Kind;
            let caption = self.text.as_deref().map(visible_label).unwrap_or_default();
            node.label(caption)
                .value(format!(
                    "{}{}",
                    value.formatted(Some(precision)),
                    self.suffix
                ))
                .numeric(value.to_f64(), min, max)
                .step(self.step_f64().unwrap_or(span / 100.0))
                .jump(self.step_f64().unwrap_or(span / 100.0) * 10.0)
                .orientation(crate::AccessOrientation::Horizontal)
                .disabled(!self.enabled)
                .action(Kind::Increment)
                .action(Kind::Decrement)
                .action(Kind::SetValue);
        });
        let t = if span > 0.0 {
            (value.to_f64() - min) / span
        } else {
            0.0
        };
        let x = left + (right - left) * t as f32;
        let foreground = self.paint_parts(ui, response, &style, component, left, right, x, radius);
        if let Some((caption, _)) = header {
            let value = format!("{}{}", self.value.formatted(Some(precision)), self.suffix);
            let value_size = ui.context.measure_text(&value, size, weight, width);
            let top = rect.min - Vec2::new(0.0, header_height);
            ui.context.paint(
                id.with("header"),
                ui.window,
                ui.clip,
                vec![
                    Paint::Text {
                        text: caption,
                        position: top,
                        size,
                        weight,
                        wrap_width: width,
                        color: foreground,
                    },
                    Paint::Text {
                        text: value,
                        position: top + Vec2::new((width - value_size.x).max(0.0), 0.0),
                        size,
                        weight,
                        wrap_width: width,
                        color: foreground,
                    },
                ],
            );
        }
        if let Some((text, _)) = label {
            ui.context.paint(
                id.with("label"),
                ui.window,
                ui.clip,
                vec![Paint::Text {
                    text,
                    position: rect.min + Vec2::new(0.0, height + ui.layout.spacing),
                    size,
                    weight,
                    wrap_width: width,
                    color: foreground,
                }],
            );
        }
        response
    }
}

impl Ui<'_> {
    #[track_caller]
    #[deprecated(note = "use `ui.add(Slider::new(value, range).enabled(enabled))`")]
    pub fn slider_enabled<T: Numeric>(
        &mut self,
        enabled: bool,
        value: &mut T,
        range: RangeInclusive<T>,
    ) -> Response {
        self.add(Slider::new(value, range).enabled(enabled))
    }
    #[track_caller]
    pub fn slider_labeled<T: Numeric>(
        &mut self,
        value: &mut T,
        range: RangeInclusive<T>,
        text: impl Into<String>,
    ) -> Response {
        self.add(Slider::new(value, range).text(text))
    }
    #[track_caller]
    pub fn slider<T: Numeric>(&mut self, value: &mut T, range: RangeInclusive<T>) -> Response {
        self.add(Slider::new(value, range))
    }
}