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
#[derive(Clone, Debug, Copy)]
pub enum Size {
Absolute { initial: f32, range: (f32, f32) },
Relative { fraction: f32, range: (f32, f32) },
Remainder { range: (f32, f32) },
}
impl Size {
pub fn exact(points: f32) -> Self {
Self::Absolute {
initial: points,
range: (points, points),
}
}
pub fn initial(points: f32) -> Self {
Self::Absolute {
initial: points,
range: (0.0, f32::INFINITY),
}
}
pub fn relative(fraction: f32) -> Self {
egui::egui_assert!(0.0 <= fraction && fraction <= 1.0);
Self::Relative {
fraction,
range: (0.0, f32::INFINITY),
}
}
pub fn remainder() -> Self {
Self::Remainder {
range: (0.0, f32::INFINITY),
}
}
pub fn at_least(mut self, minimum: f32) -> Self {
match &mut self {
Self::Absolute { range, .. }
| Self::Relative { range, .. }
| Self::Remainder { range, .. } => {
range.0 = minimum;
}
}
self
}
pub fn at_most(mut self, maximum: f32) -> Self {
match &mut self {
Self::Absolute { range, .. }
| Self::Relative { range, .. }
| Self::Remainder { range, .. } => {
range.1 = maximum;
}
}
self
}
pub fn range(self) -> (f32, f32) {
match self {
Self::Absolute { range, .. }
| Self::Relative { range, .. }
| Self::Remainder { range, .. } => range,
}
}
}
#[derive(Clone, Default)]
pub struct Sizing {
pub(crate) sizes: Vec<Size>,
}
impl Sizing {
pub fn add(&mut self, size: Size) {
self.sizes.push(size);
}
pub fn to_lengths(&self, length: f32, spacing: f32) -> Vec<f32> {
if self.sizes.is_empty() {
return vec![];
}
let mut remainders = 0;
let sum_non_remainder = self
.sizes
.iter()
.map(|&size| match size {
Size::Absolute { initial, .. } => initial,
Size::Relative {
fraction,
range: (min, max),
} => {
assert!(0.0 <= fraction && fraction <= 1.0);
(length * fraction).clamp(min, max)
}
Size::Remainder { .. } => {
remainders += 1;
0.0
}
})
.sum::<f32>()
+ spacing * (self.sizes.len() - 1) as f32;
let avg_remainder_length = if remainders == 0 {
0.0
} else {
let mut remainder_length = length - sum_non_remainder;
let avg_remainder_length = 0.0f32.max(remainder_length / remainders as f32).floor();
self.sizes.iter().for_each(|&size| {
if let Size::Remainder { range: (min, _max) } = size {
if avg_remainder_length < min {
remainder_length -= min;
remainders -= 1;
}
}
});
if remainders > 0 {
0.0f32.max(remainder_length / remainders as f32)
} else {
0.0
}
};
self.sizes
.iter()
.map(|&size| match size {
Size::Absolute { initial, .. } => initial,
Size::Relative {
fraction,
range: (min, max),
} => (length * fraction).clamp(min, max),
Size::Remainder { range: (min, max) } => avg_remainder_length.clamp(min, max),
})
.collect()
}
}
impl From<Vec<Size>> for Sizing {
fn from(sizes: Vec<Size>) -> Self {
Self { sizes }
}
}
#[test]
fn test_sizing() {
let sizing: Sizing = vec![].into();
assert_eq!(sizing.to_lengths(50.0, 0.0), vec![]);
let sizing: Sizing = vec![Size::remainder().at_least(20.0), Size::remainder()].into();
assert_eq!(sizing.to_lengths(50.0, 0.0), vec![25.0, 25.0]);
assert_eq!(sizing.to_lengths(30.0, 0.0), vec![20.0, 10.0]);
assert_eq!(sizing.to_lengths(20.0, 0.0), vec![20.0, 0.0]);
assert_eq!(sizing.to_lengths(10.0, 0.0), vec![20.0, 0.0]);
assert_eq!(sizing.to_lengths(20.0, 10.0), vec![20.0, 0.0]);
assert_eq!(sizing.to_lengths(30.0, 10.0), vec![20.0, 0.0]);
assert_eq!(sizing.to_lengths(40.0, 10.0), vec![20.0, 10.0]);
assert_eq!(sizing.to_lengths(110.0, 10.0), vec![50.0, 50.0]);
let sizing: Sizing = vec![Size::relative(0.5).at_least(10.0), Size::exact(10.0)].into();
assert_eq!(sizing.to_lengths(50.0, 0.0), vec![25.0, 10.0]);
assert_eq!(sizing.to_lengths(30.0, 0.0), vec![15.0, 10.0]);
assert_eq!(sizing.to_lengths(20.0, 0.0), vec![10.0, 10.0]);
assert_eq!(sizing.to_lengths(10.0, 0.0), vec![10.0, 10.0]);
}