agg-gui 0.3.0

Immediate-mode Rust GUI library with AGG rendering, Y-up layout, widgets, text, SVG, and native/WASM adapters
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//! Pure value-mapping math for [`crate::widgets::slider::Slider`].
//!
//! This module holds the numeric core of the slider — everything that turns a
//! value into a normalized `[0, 1]` position and back — kept separate from the
//! widget's paint/event code so the mapping can be unit-tested in isolation and
//! so `slider.rs` stays under the project's 800-line limit.
//!
//! The logarithmic mapping (including ranges that span zero and infinity) and
//! the "smart aim" round-number finder are ported from egui's
//! `crates/egui/src/widgets/slider.rs` and `crates/emath/src/smart_aim.rs`
//! respectively (see `egui-reference/`).  Semantics are preserved so the
//! widget behaves like egui's; only the API shape (scalar `min`/`max` instead
//! of `RangeInclusive`) differs to avoid dragging range types through the
//! widget.

use std::cmp::Ordering;

/// Configuration for how a slider maps values to positions.
///
/// Mirrors egui's private `SliderSpec`.  For linear sliders only
/// `logarithmic == false` matters; the other two fields shape how logarithmic
/// sliders treat the special endpoints `0` and `∞`.
#[derive(Clone, Copy, Debug)]
pub struct SliderSpec {
    pub logarithmic: bool,
    /// For logarithmic sliders, the smallest positive value we care about.
    /// `1` for integer sliders, `1e-6` for reals by default.
    pub smallest_positive: f64,
    /// For logarithmic sliders, the largest positive value before the slider
    /// switches to `INFINITY` (when infinity is the high end). Default: `∞`.
    pub largest_finite: f64,
}

impl Default for SliderSpec {
    fn default() -> Self {
        Self {
            logarithmic: false,
            smallest_positive: 1e-6,
            largest_finite: f64::INFINITY,
        }
    }
}

const INFINITY: f64 = f64::INFINITY;

/// For an infinitely large logarithmic range (e.g. from zero), span this many
/// orders of magnitude.
const INF_RANGE_MAGNITUDE: f64 = 10.0;

#[inline]
fn lerp(a: f64, b: f64, t: f64) -> f64 {
    a + (b - a) * t
}

#[inline]
fn remap(x: f64, from_lo: f64, from_hi: f64, to_lo: f64, to_hi: f64) -> f64 {
    let t = (x - from_lo) / (from_hi - from_lo);
    lerp(to_lo, to_hi, t)
}

#[inline]
fn remap_clamp(x: f64, from_lo: f64, from_hi: f64, to_lo: f64, to_hi: f64) -> f64 {
    if x <= from_lo.min(from_hi) {
        if from_lo <= from_hi {
            to_lo
        } else {
            to_hi
        }
    } else if x >= from_lo.max(from_hi) {
        if from_lo <= from_hi {
            to_hi
        } else {
            to_lo
        }
    } else {
        remap(x, from_lo, from_hi, to_lo, to_hi)
    }
}

/// Clamp `x` into `[min, max]`, tolerating a reversed (high-to-low) range and
/// NaN endpoints the same way egui's `clamp_value_to_range` does.
pub fn clamp_value_to_range(x: f64, min: f64, max: f64) -> f64 {
    let (mut lo, mut hi) = (min, max);
    if lo.total_cmp(&hi) == Ordering::Greater {
        std::mem::swap(&mut lo, &mut hi);
    }
    match x.total_cmp(&lo) {
        Ordering::Less | Ordering::Equal => lo,
        Ordering::Greater => match x.total_cmp(&hi) {
            Ordering::Greater | Ordering::Equal => hi,
            Ordering::Less => x,
        },
    }
}

/// Map a value to its normalized `[0, 1]` slider position.
pub fn normalized_from_value(value: f64, min: f64, max: f64, spec: &SliderSpec) -> f64 {
    if min.is_nan() || max.is_nan() {
        f64::NAN
    } else if min == max {
        0.5 // empty range, show center of slider
    } else if min > max {
        1.0 - normalized_from_value(value, max, min, spec)
    } else if value <= min {
        0.0
    } else if value >= max {
        1.0
    } else if spec.logarithmic {
        if max <= 0.0 {
            // non-positive range
            normalized_from_value(-value, -min, -max, spec)
        } else if 0.0 <= min {
            let (min_log, max_log) = range_log10(min, max, spec);
            let value_log = value.log10();
            remap_clamp(value_log, min_log, max_log, 0.0, 1.0)
        } else {
            let zero_cutoff = logarithmic_zero_cutoff(min, max);
            if value < 0.0 {
                remap(
                    normalized_from_value(value, min, 0.0, spec),
                    0.0,
                    1.0,
                    0.0,
                    zero_cutoff,
                )
            } else {
                remap(
                    normalized_from_value(value, 0.0, max, spec),
                    0.0,
                    1.0,
                    zero_cutoff,
                    1.0,
                )
            }
        }
    } else {
        remap_clamp(value, min, max, 0.0, 1.0)
    }
}

/// Inverse of [`normalized_from_value`]: map a normalized `[0, 1]` position
/// back to a value.
pub fn value_from_normalized(normalized: f64, min: f64, max: f64, spec: &SliderSpec) -> f64 {
    if min.is_nan() || max.is_nan() {
        f64::NAN
    } else if min == max {
        min
    } else if min > max {
        value_from_normalized(1.0 - normalized, max, min, spec)
    } else if normalized <= 0.0 {
        min
    } else if normalized >= 1.0 {
        max
    } else if spec.logarithmic {
        if max <= 0.0 {
            // non-positive range
            -value_from_normalized(normalized, -min, -max, spec)
        } else if 0.0 <= min {
            let (min_log, max_log) = range_log10(min, max, spec);
            let log = lerp(min_log, max_log, normalized);
            10.0_f64.powf(log)
        } else {
            let zero_cutoff = logarithmic_zero_cutoff(min, max);
            if normalized < zero_cutoff {
                // negative
                value_from_normalized(remap(normalized, 0.0, zero_cutoff, 0.0, 1.0), min, 0.0, spec)
            } else {
                // positive
                value_from_normalized(remap(normalized, zero_cutoff, 1.0, 0.0, 1.0), 0.0, max, spec)
            }
        }
    } else {
        lerp(min, max, normalized.clamp(0.0, 1.0))
    }
}

fn range_log10(min: f64, max: f64, spec: &SliderSpec) -> (f64, f64) {
    debug_assert!(spec.logarithmic, "spec must be logarithmic");
    debug_assert!(min <= max, "min must be <= max, got min={min} max={max}");

    if min == 0.0 && max == INFINITY {
        (spec.smallest_positive.log10(), INF_RANGE_MAGNITUDE)
    } else if min == 0.0 {
        if spec.smallest_positive < max {
            (spec.smallest_positive.log10(), max.log10())
        } else {
            (max.log10() - INF_RANGE_MAGNITUDE, max.log10())
        }
    } else if max == INFINITY {
        if min < spec.largest_finite {
            (min.log10(), spec.largest_finite.log10())
        } else {
            (min.log10(), min.log10() + INF_RANGE_MAGNITUDE)
        }
    } else {
        (min.log10(), max.log10())
    }
}

/// Where to place the zero crossing for a logarithmic slider whose range spans
/// zero (negative min, positive max).
fn logarithmic_zero_cutoff(min: f64, max: f64) -> f64 {
    debug_assert!(
        min < 0.0 && 0.0 < max,
        "min must be negative and max positive, got min={min} max={max}"
    );

    let min_magnitude = if min == -INFINITY {
        INF_RANGE_MAGNITUDE
    } else {
        min.abs().log10().abs()
    };
    let max_magnitude = if max == INFINITY {
        INF_RANGE_MAGNITUDE
    } else {
        max.log10().abs()
    };

    let cutoff = min_magnitude / (min_magnitude + max_magnitude);
    debug_assert!(
        (0.0..=1.0).contains(&cutoff),
        "Bad cutoff {cutoff:?} for min {min:?} and max {max:?}"
    );
    cutoff
}

// ---------------------------------------------------------------------------
// Smart aim — find the "roundest" number in a range so dragging snaps to nice
// values.  Ported from egui's `emath::smart_aim`.
// ---------------------------------------------------------------------------

const NUM_DECIMALS: usize = 16;

#[inline]
fn fast_midpoint(a: f64, b: f64) -> f64 {
    (a + b) / 2.0
}

#[inline]
fn u8_midpoint(a: u8, b: u8) -> u8 {
    ((a as u16 + b as u16) / 2) as u8
}

/// Find the "simplest" number in the closed range `[min, max]` — the one with
/// the fewest decimal digits.  E.g. `[0.83, 1.354] -> 1.0`, `[0.37, 0.48] -> 0.4`.
pub fn best_in_range_f64(min: f64, max: f64) -> f64 {
    // Avoid NaN if we can:
    if min.is_nan() {
        return max;
    }
    if max.is_nan() {
        return min;
    }

    if max < min {
        return best_in_range_f64(max, min);
    }
    if min == max {
        return min;
    }
    if min <= 0.0 && 0.0 <= max {
        return 0.0; // always prefer zero
    }
    if min < 0.0 {
        return -best_in_range_f64(-max, -min);
    }

    debug_assert!(0.0 < min && min < max, "Logic bug");

    // Prefer finite numbers:
    if !max.is_finite() {
        return min;
    }

    let min_exponent = min.log10();
    let max_exponent = max.log10();

    if min_exponent.floor() != max_exponent.floor() {
        // Different orders of magnitude — pick the geometric center of the two:
        let exponent = fast_midpoint(min_exponent, max_exponent);
        return 10.0_f64.powi(exponent.round() as i32);
    }

    if is_integer(min_exponent) {
        return 10.0_f64.powf(min_exponent);
    }
    if is_integer(max_exponent) {
        return 10.0_f64.powf(max_exponent);
    }

    // Find the proper scale, then convert to integers:
    let scale = NUM_DECIMALS as i32 - max_exponent.floor() as i32 - 1;
    let scale_factor = 10.0_f64.powi(scale);

    let min_str = to_decimal_string((min * scale_factor).round() as u64);
    let max_str = to_decimal_string((max * scale_factor).round() as u64);

    // Two positive integers of the same length. Find the first non-matching
    // ("deciding") digit; everything before it matches, everything after is
    // zero, and the deciding digit is a "smart average".
    let mut ret_str = [0u8; NUM_DECIMALS];

    for i in 0..NUM_DECIMALS {
        if min_str[i] == max_str[i] {
            ret_str[i] = min_str[i];
        } else {
            let mut deciding_digit_min = min_str[i];
            let deciding_digit_max = max_str[i];

            debug_assert!(deciding_digit_min < deciding_digit_max, "Bug in smart aim");

            let rest_of_min_is_zeroes = min_str[i + 1..].iter().all(|&c| c == 0);

            if !rest_of_min_is_zeroes {
                // There are more digits after `deciding_digit_min`, so we can't
                // pick it — the true selectable min is one greater:
                deciding_digit_min += 1;
            }

            let deciding_digit = if deciding_digit_min == 0 {
                0
            } else if deciding_digit_min <= 5 && 5 <= deciding_digit_max {
                5 // 5 is the roundest number in the range
            } else {
                u8_midpoint(deciding_digit_min, deciding_digit_max)
            };

            ret_str[i] = deciding_digit;

            return from_decimal_string(ret_str) as f64 / scale_factor;
        }
    }

    min // All digits the same (already handled earlier, but be safe).
}

fn is_integer(f: f64) -> bool {
    f.round() == f
}

fn to_decimal_string(v: u64) -> [u8; NUM_DECIMALS] {
    let mut ret = [0u8; NUM_DECIMALS];
    let mut value = v;
    for i in (0..NUM_DECIMALS).rev() {
        ret[i] = (value % 10) as u8;
        value /= 10;
    }
    ret
}

fn from_decimal_string(s: [u8; NUM_DECIMALS]) -> u64 {
    let mut value = 0u64;
    for &c in &s {
        debug_assert!(c <= 9, "Bad number");
        value = value * 10 + c as u64;
    }
    value
}

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

    fn log_spec() -> SliderSpec {
        SliderSpec {
            logarithmic: true,
            smallest_positive: 1e-6,
            largest_finite: f64::INFINITY,
        }
    }

    #[test]
    fn linear_mapping_roundtrips() {
        let spec = SliderSpec::default();
        for &(v, n) in &[(0.0, 0.0), (5.0, 0.5), (10.0, 1.0), (2.5, 0.25)] {
            assert!((normalized_from_value(v, 0.0, 10.0, &spec) - n).abs() < 1e-9);
            assert!((value_from_normalized(n, 0.0, 10.0, &spec) - v).abs() < 1e-9);
        }
    }

    #[test]
    fn linear_clamps_outside_range() {
        let spec = SliderSpec::default();
        assert_eq!(normalized_from_value(-5.0, 0.0, 10.0, &spec), 0.0);
        assert_eq!(normalized_from_value(50.0, 0.0, 10.0, &spec), 1.0);
    }

    #[test]
    fn logarithmic_midpoint_is_geometric_mean() {
        let spec = log_spec();
        // For 1..=100 log slider, the halfway position should be ~10.
        let mid = value_from_normalized(0.5, 1.0, 100.0, &spec);
        assert!((mid - 10.0).abs() < 1e-6, "got {mid}");
        // And the inverse holds.
        let n = normalized_from_value(10.0, 1.0, 100.0, &spec);
        assert!((n - 0.5).abs() < 1e-9, "got {n}");
    }

    #[test]
    fn logarithmic_reversed_range() {
        let spec = log_spec();
        // High-to-low range: normalized 0 -> max value.
        let v = value_from_normalized(0.0, 100.0, 1.0, &spec);
        assert!((v - 100.0).abs() < 1e-9, "got {v}");
    }

    #[test]
    fn logarithmic_spanning_zero_puts_zero_at_cutoff() {
        let spec = log_spec();
        // Symmetric range around zero -> cutoff at 0.5, value there is ~0.
        let cutoff = logarithmic_zero_cutoff(-1000.0, 1000.0);
        assert!((cutoff - 0.5).abs() < 1e-9, "got {cutoff}");
        let v = value_from_normalized(0.5, -1000.0, 1000.0, &spec);
        assert!(v.abs() < 1.0, "expected near zero, got {v}");
    }

    #[test]
    fn logarithmic_spanning_infinity() {
        let spec = log_spec();
        // 0..=∞ maps normalized 1.0 to ∞ and 0.0 to 0.0.
        assert_eq!(value_from_normalized(1.0, 0.0, f64::INFINITY, &spec), f64::INFINITY);
        assert_eq!(value_from_normalized(0.0, 0.0, f64::INFINITY, &spec), 0.0);
        // A midpoint stays finite and positive.
        let mid = value_from_normalized(0.5, 0.0, f64::INFINITY, &spec);
        assert!(mid.is_finite() && mid > 0.0, "got {mid}");
    }

    /// Regression: the Sliders demo's range sliders span `-∞..=∞` (logarithmic)
    /// and the demo slider itself may be rebuilt with an infinite bound. The
    /// initial displayed value/position must be finite — never `NaN` — for the
    /// exact values the demo starts with. (egui's Sliders demo shows finite
    /// values, never NaN.)
    #[test]
    fn infinite_range_initial_mapping_is_finite() {
        let spec = log_spec();
        // Demo slider default: value 10 in 0..=10000 (log).
        let n = normalized_from_value(10.0, 0.0, 10000.0, &spec);
        assert!(n.is_finite() && (0.0..=1.0).contains(&n), "demo n={n}");

        // Range sliders: both bounds infinite. The current min (0) and max
        // (10000) must map to a finite, in-range position.
        for &v in &[0.0, 10000.0] {
            let n = normalized_from_value(v, -f64::INFINITY, f64::INFINITY, &spec);
            assert!(n.is_finite() && (0.0..=1.0).contains(&n), "v={v} n={n}");
        }
        // A finite normalized position maps back to a finite value.
        let mid = value_from_normalized(0.5, -f64::INFINITY, f64::INFINITY, &spec);
        assert!(mid.is_finite(), "mid={mid}");
    }

    /// One infinite bound (e.g. `min..=∞` after the user drags a range slider):
    /// a finite value in the range still maps to a finite, in-range position,
    /// and round-trips.
    #[test]
    fn one_infinite_bound_roundtrips_finite() {
        let spec = log_spec();
        let n = normalized_from_value(10.0, -f64::INFINITY, 10000.0, &spec);
        assert!(n.is_finite() && (0.0..=1.0).contains(&n), "n={n}");
        let v = value_from_normalized(n, -f64::INFINITY, 10000.0, &spec);
        assert!(v.is_finite(), "v={v}");

        let n = normalized_from_value(10.0, 0.0, f64::INFINITY, &spec);
        assert!(n.is_finite() && (0.0..=1.0).contains(&n), "n={n}");
    }

    #[test]
    fn clamp_handles_reversed_range() {
        assert_eq!(clamp_value_to_range(5.0, 10.0, 0.0), 5.0);
        assert_eq!(clamp_value_to_range(-1.0, 10.0, 0.0), 0.0);
        assert_eq!(clamp_value_to_range(11.0, 10.0, 0.0), 10.0);
    }

    // Smart-aim cases ported from egui's `smart_aim::test_aim`.
    #[test]
    fn smart_aim_round_numbers() {
        assert_eq!(best_in_range_f64(-0.2, 0.0), 0.0);
        assert_eq!(best_in_range_f64(-10_004.23, 3.14), 0.0);
        assert_eq!(best_in_range_f64(7.8, 17.8), 10.0);
        assert_eq!(best_in_range_f64(99.0, 300.0), 100.0);
        assert_eq!(best_in_range_f64(-99.0, -300.0), -100.0);
        assert_eq!(best_in_range_f64(0.4, 0.9), 0.5);
        assert_eq!(best_in_range_f64(14.1, 19.99), 15.0);
        assert_eq!(best_in_range_f64(12.3, 65.9), 50.0);
        assert_eq!(best_in_range_f64(493.0, 879.0), 500.0);
        assert_eq!(best_in_range_f64(0.37, 0.48), 0.40);
        assert_eq!(best_in_range_f64(7.5, 16.3), 10.0);
        assert_eq!(best_in_range_f64(7.5, 763.3), 100.0);
        assert_eq!(best_in_range_f64(7.5, 123_456.0), 1000.0);
        assert_eq!(best_in_range_f64(9.9999, 99.999), 10.0);
        assert_eq!(best_in_range_f64(10.001, 99.999), 50.0);
    }

    #[test]
    fn smart_aim_integers() {
        assert_eq!(best_in_range_f64(99.0, 300.0), 100.0);
        assert_eq!(best_in_range_f64(4.0, 9.0), 5.0);
        assert_eq!(best_in_range_f64(14.0, 19.0), 15.0);
        assert_eq!(best_in_range_f64(12.0, 65.0), 50.0);
        assert_eq!(best_in_range_f64(37.0, 48.0), 40.0);
        assert_eq!(best_in_range_f64(12345.0, 12780.0), 12500.0);
    }

    #[test]
    fn smart_aim_nan_and_infinity() {
        assert!(best_in_range_f64(f64::NAN, f64::NAN).is_nan());
        assert_eq!(best_in_range_f64(f64::NAN, 1.2), 1.2);
        assert_eq!(best_in_range_f64(1.2, f64::INFINITY), 1.2);
        assert_eq!(best_in_range_f64(f64::NEG_INFINITY, 1.2), 0.0);
        assert_eq!(best_in_range_f64(f64::NEG_INFINITY, -2.7), -2.7);
        assert_eq!(best_in_range_f64(f64::NEG_INFINITY, f64::INFINITY), 0.0);
    }
}