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
|
//! Layouting primitives.
use std::fmt::{self, Display, Formatter};
/// Specifies the directions into which content is laid out.
///
/// The primary component defines into which direction text and lines flow and the
/// secondary into which paragraphs and pages grow.
pub type LayoutSystem = Gen2<Dir>;
impl Default for LayoutSystem {
fn default() -> Self {
Self::new(Dir::LTR, Dir::TTB)
}
}
/// Specifies where to align a layout in a parent container.
pub type LayoutAlign = Gen2<GenAlign>;
impl Default for LayoutAlign {
fn default() -> Self {
Self::new(GenAlign::Start, GenAlign::Start)
}
}
/// Whether to expand a layout to an area's full size or shrink it to fit its content.
pub type LayoutExpansion = Spec2<bool>;
/// The four directions into which content can be laid out.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Dir {
/// Left to right.
LTR,
/// Right to left.
RTL,
/// Top to bottom.
TTB,
/// Bottom to top.
BTT,
}
impl Dir {
/// The side this direction starts at.
pub fn start(self) -> Side {
match self {
Self::LTR => Side::Left,
Self::RTL => Side::Right,
Self::TTB => Side::Top,
Self::BTT => Side::Bottom,
}
}
/// The side this direction ends at.
pub fn end(self) -> Side {
match self {
Self::LTR => Side::Right,
Self::RTL => Side::Left,
Self::TTB => Side::Bottom,
Self::BTT => Side::Top,
}
}
/// The specific axis this direction belongs to.
pub fn axis(self) -> SpecAxis {
match self {
Self::LTR | Self::RTL => SpecAxis::Horizontal,
Self::TTB | Self::BTT => SpecAxis::Vertical,
}
}
/// Whether this direction points into the positive coordinate direction.
///
/// The positive directions are left-to-right and top-to-bottom.
pub fn is_positive(self) -> bool {
match self {
Self::LTR | Self::TTB => true,
Self::RTL | Self::BTT => false,
}
}
/// The factor for this direction.
///
/// - `1.0` if the direction is positive.
/// - `-1.0` if the direction is negative.
pub fn factor(self) -> f64 {
if self.is_positive() { 1.0 } else { -1.0 }
}
/// The inverse direction.
pub fn inv(self) -> Self {
match self {
Self::LTR => Self::RTL,
Self::RTL => Self::LTR,
Self::TTB => Self::BTT,
Self::BTT => Self::TTB,
}
}
}
impl Display for Dir {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
Self::LTR => "ltr",
Self::RTL => "rtl",
Self::TTB => "ttb",
Self::BTT => "btt",
})
}
}
/// The two generic layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum GenAxis {
/// The primary layouting direction into which text and lines flow.
Primary,
/// The secondary layouting direction into which paragraphs grow.
Secondary,
}
impl GenAxis {
/// The specific version of this axis in the given layout system.
pub fn to_spec(self, sys: LayoutSystem) -> SpecAxis {
sys.get(self).axis()
}
}
impl Display for GenAxis {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
Self::Primary => "primary",
Self::Secondary => "secondary",
})
}
}
/// The two specific layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum SpecAxis {
/// The horizontal layouting axis.
Horizontal,
/// The vertical layouting axis.
Vertical,
}
impl SpecAxis {
/// The generic version of this axis in the given layout system.
pub fn to_gen(self, sys: LayoutSystem) -> GenAxis {
if self == sys.primary.axis() {
GenAxis::Primary
} else {
GenAxis::Secondary
}
}
}
impl Display for SpecAxis {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
Self::Horizontal => "horizontal",
Self::Vertical => "vertical",
})
}
}
/// A side of a container.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Side {
Left,
Top,
Right,
Bottom,
}
/// Where to align content along an axis in a generic context.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum GenAlign {
Start,
Center,
End,
}
impl GenAlign {
/// The inverse alignment.
pub fn inv(self) -> Self {
match self {
Self::Start => Self::End,
Self::Center => Self::Center,
Self::End => Self::Start,
}
}
}
impl Display for GenAlign {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
Self::Start => "start",
Self::Center => "center",
Self::End => "end",
})
}
}
/// Where to align content along an axis in a specific context.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum SpecAlign {
Left,
Right,
Top,
Bottom,
Center,
}
impl SpecAlign {
/// The specific axis this alignment refers to.
///
/// Returns `None` if this is `Center` since the axis is unknown.
pub fn axis(self) -> Option<SpecAxis> {
match self {
Self::Left => Some(SpecAxis::Horizontal),
Self::Right => Some(SpecAxis::Horizontal),
Self::Top => Some(SpecAxis::Vertical),
Self::Bottom => Some(SpecAxis::Vertical),
Self::Center => None,
}
}
/// The generic version of this alignment in the given layout system.
pub fn to_gen(self, sys: LayoutSystem) -> GenAlign {
let get = |spec: SpecAxis, positive: GenAlign| {
if sys.get(spec.to_gen(sys)).is_positive() {
positive
} else {
positive.inv()
}
};
match self {
Self::Left => get(SpecAxis::Horizontal, GenAlign::Start),
Self::Right => get(SpecAxis::Horizontal, GenAlign::End),
Self::Top => get(SpecAxis::Vertical, GenAlign::Start),
Self::Bottom => get(SpecAxis::Vertical, GenAlign::End),
Self::Center => GenAlign::Center,
}
}
}
impl Display for SpecAlign {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self {
Self::Left => "left",
Self::Right => "right",
Self::Top => "top",
Self::Bottom => "bottom",
Self::Center => "center",
})
}
}
/// A generic container with two components for the two generic axes.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Gen2<T> {
/// The primary component.
pub primary: T,
/// The secondary component.
pub secondary: T,
}
impl<T> Gen2<T> {
/// Create a new instance from the two components.
pub fn new(primary: T, secondary: T) -> Self {
Self { primary, secondary }
}
/// Return the component for the specified generic axis.
pub fn get(self, axis: GenAxis) -> T {
match axis {
GenAxis::Primary => self.primary,
GenAxis::Secondary => self.secondary,
}
}
/// Borrow the component for the specified generic axis.
pub fn get_ref(&mut self, axis: GenAxis) -> &T {
match axis {
GenAxis::Primary => &mut self.primary,
GenAxis::Secondary => &mut self.secondary,
}
}
/// Borrow the component for the specified generic axis mutably.
pub fn get_mut(&mut self, axis: GenAxis) -> &mut T {
match axis {
GenAxis::Primary => &mut self.primary,
GenAxis::Secondary => &mut self.secondary,
}
}
}
/// A generic container with two components for the two specific axes.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Spec2<T> {
/// The horizontal component.
pub horizontal: T,
/// The vertical component.
pub vertical: T,
}
impl<T> Spec2<T> {
/// Create a new instance from the two components.
pub fn new(horizontal: T, vertical: T) -> Self {
Self { horizontal, vertical }
}
/// Return the component for the given specific axis.
pub fn get(self, axis: SpecAxis) -> T {
match axis {
SpecAxis::Horizontal => self.horizontal,
SpecAxis::Vertical => self.vertical,
}
}
/// Borrow the component for the given specific axis.
pub fn get_ref(&mut self, axis: SpecAxis) -> &T {
match axis {
SpecAxis::Horizontal => &mut self.horizontal,
SpecAxis::Vertical => &mut self.vertical,
}
}
/// Borrow the component for the given specific axis mutably.
pub fn get_mut(&mut self, axis: SpecAxis) -> &mut T {
match axis {
SpecAxis::Horizontal => &mut self.horizontal,
SpecAxis::Vertical => &mut self.vertical,
}
}
}
|