Skip to main content

waft_winui/controls/
range_base.rs

1//! XAML `RangeBase` (`dxaml/xcp/core/core/elements/RangeBase.cpp`, `dxaml/xcp/dxaml/lib/RangeBase_Partial.cpp`): `Minimum`, `Maximum` and `Value`, and the coercion `CRangeBase::SetValue` applies whenever one of them is set. The comparisons the range controls make are `DoubleUtil`'s (`dxaml/xcp/components/base/DoubleUtil.cpp`).
2
3/// `RangeBase::GetDefaultValue2`: `Maximum` defaults to 1; `Minimum` and `Value` to 0.
4pub const RANGE_BASE_DEFAULT_MAXIMUM: f64 = 1.0;
5/// `RangeBase::GetDefaultValue2`: `SmallChange` defaults to 0.1.
6pub const RANGE_BASE_DEFAULT_SMALL_CHANGE: f64 = 0.1;
7/// `RangeBase::GetDefaultValue2`: `LargeChange` defaults to 1.
8pub const RANGE_BASE_DEFAULT_LARGE_CHANGE: f64 = 1.0;
9
10/// `DoubleUtil::Epsilon`.
11const EPSILON: f64 = 1.1102230246251567e-016;
12
13/// `DoubleUtil::AreClose`: within an epsilon proportional to the values.
14pub fn are_close(value1: f64, value2: f64) -> bool {
15    if value1 == value2 {
16        return true;
17    }
18    let epsilon = (value1.abs() + value2.abs() + 10.0) * EPSILON;
19    let delta = value1 - value2;
20    -epsilon < delta && epsilon > delta
21}
22
23/// `DoubleUtil::LessThan`: strictly less and not close.
24pub fn less_than(value1: f64, value2: f64) -> bool {
25    value1 < value2 && !are_close(value1, value2)
26}
27
28/// `DoubleUtil::LessThanOrClose`.
29pub fn less_than_or_close(value1: f64, value2: f64) -> bool {
30    value1 < value2 || are_close(value1, value2)
31}
32
33/// `DoubleUtil::Fractional`: the part after the decimal point, toward zero.
34pub fn fractional(value: f64) -> f64 {
35    if value > 0.0 {
36        value - value.floor()
37    } else {
38        value - value.ceil()
39    }
40}
41
42/// `RangeBase::EnsureValidDoubleValue`: a range property rejects NaN and the infinities (`E_INVALIDARG`, `ERROR_INVALID_DOUBLE_VALUE`).
43pub fn ensure_valid_double(value: f64, property: &str) {
44    assert!(
45        value.is_finite(),
46        "RangeBase.{property}: {value} is not a valid double value"
47    );
48}
49
50/// `Minimum`, `Maximum` and `Value` of a `RangeBase` control.
51#[derive(Clone, Copy, Debug, PartialEq)]
52pub struct RangeBase {
53    pub minimum: f64,
54    pub maximum: f64,
55    pub value: f64,
56}
57
58impl RangeBase {
59    pub fn new(minimum: f64, maximum: f64, value: f64) -> RangeBase {
60        ensure_valid_double(minimum, "Minimum");
61        ensure_valid_double(maximum, "Maximum");
62        ensure_valid_double(value, "Value");
63        RangeBase {
64            minimum,
65            maximum,
66            value,
67        }
68    }
69
70    /// `CRangeBase::SetValue` for the three properties together: `Maximum` is kept at or above `Minimum`, and `Value` is clamped between them (`CoerceAndSetValue`).
71    pub fn coerced(self) -> RangeBase {
72        let maximum = self.maximum.max(self.minimum);
73        RangeBase {
74            minimum: self.minimum,
75            maximum,
76            value: self.value.min(maximum).max(self.minimum),
77        }
78    }
79
80    /// `CRangeBase::CoerceAndSetValue`: what `put_Value(value)` stores.
81    pub fn coerce_value(&self, value: f64) -> f64 {
82        value.min(self.maximum).max(self.minimum)
83    }
84
85    /// `Maximum − Minimum`.
86    pub fn span(&self) -> f64 {
87        self.maximum - self.minimum
88    }
89
90    /// `Slider::UpdateTrackLayout`'s `multiplier`: the share of the track before `value`, 0 for an empty range.
91    pub fn fraction_of(&self, value: f64) -> f64 {
92        let range = self.span();
93        if range <= 0.0 {
94            0.0
95        } else {
96            1.0 - (self.maximum - value) / range
97        }
98    }
99}