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
//! 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`).
/// `RangeBase::GetDefaultValue2`: `Maximum` defaults to 1; `Minimum` and `Value` to 0.
pub const RANGE_BASE_DEFAULT_MAXIMUM: f64 = 1.0;
/// `RangeBase::GetDefaultValue2`: `SmallChange` defaults to 0.1.
pub const RANGE_BASE_DEFAULT_SMALL_CHANGE: f64 = 0.1;
/// `RangeBase::GetDefaultValue2`: `LargeChange` defaults to 1.
pub const RANGE_BASE_DEFAULT_LARGE_CHANGE: f64 = 1.0;
/// `DoubleUtil::Epsilon`.
const EPSILON: f64 = 1.1102230246251567e-016;
/// `DoubleUtil::AreClose`: within an epsilon proportional to the values.
pub fn are_close(value1: f64, value2: f64) -> bool {
if value1 == value2 {
return true;
}
let epsilon = (value1.abs() + value2.abs() + 10.0) * EPSILON;
let delta = value1 - value2;
-epsilon < delta && epsilon > delta
}
/// `DoubleUtil::LessThan`: strictly less and not close.
pub fn less_than(value1: f64, value2: f64) -> bool {
value1 < value2 && !are_close(value1, value2)
}
/// `DoubleUtil::LessThanOrClose`.
pub fn less_than_or_close(value1: f64, value2: f64) -> bool {
value1 < value2 || are_close(value1, value2)
}
/// `DoubleUtil::Fractional`: the part after the decimal point, toward zero.
pub fn fractional(value: f64) -> f64 {
if value > 0.0 {
value - value.floor()
} else {
value - value.ceil()
}
}
/// `RangeBase::EnsureValidDoubleValue`: a range property rejects NaN and the infinities (`E_INVALIDARG`, `ERROR_INVALID_DOUBLE_VALUE`).
pub fn ensure_valid_double(value: f64, property: &str) {
assert!(
value.is_finite(),
"RangeBase.{property}: {value} is not a valid double value"
);
}
/// `Minimum`, `Maximum` and `Value` of a `RangeBase` control.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RangeBase {
pub minimum: f64,
pub maximum: f64,
pub value: f64,
}
impl RangeBase {
pub fn new(minimum: f64, maximum: f64, value: f64) -> RangeBase {
ensure_valid_double(minimum, "Minimum");
ensure_valid_double(maximum, "Maximum");
ensure_valid_double(value, "Value");
RangeBase {
minimum,
maximum,
value,
}
}
/// `CRangeBase::SetValue` for the three properties together: `Maximum` is kept at or above `Minimum`, and `Value` is clamped between them (`CoerceAndSetValue`).
pub fn coerced(self) -> RangeBase {
let maximum = self.maximum.max(self.minimum);
RangeBase {
minimum: self.minimum,
maximum,
value: self.value.min(maximum).max(self.minimum),
}
}
/// `CRangeBase::CoerceAndSetValue`: what `put_Value(value)` stores.
pub fn coerce_value(&self, value: f64) -> f64 {
value.min(self.maximum).max(self.minimum)
}
/// `Maximum − Minimum`.
pub fn span(&self) -> f64 {
self.maximum - self.minimum
}
/// `Slider::UpdateTrackLayout`'s `multiplier`: the share of the track before `value`, 0 for an empty range.
pub fn fraction_of(&self, value: f64) -> f64 {
let range = self.span();
if range <= 0.0 {
0.0
} else {
1.0 - (self.maximum - value) / range
}
}
}