buoyant 0.4.4

SwiftUI-like UIs in Rust for embedded devices
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
use crate::primitives::Interpolate;

use super::Size;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProposedDimensions {
    pub width: ProposedDimension,
    pub height: ProposedDimension,
}

impl ProposedDimensions {
    #[must_use]
    pub fn resolve_most_flexible(self, minimum: u16, ideal: u16) -> Dimensions {
        Dimensions {
            width: self.width.resolve_most_flexible(minimum, ideal),
            height: self.height.resolve_most_flexible(minimum, ideal),
        }
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ProposedDimension {
    /// An exactly sized offer
    Exact(u16),
    /// A request for the most compact size a view can manage
    Compact,
    /// An offer of infinite size
    Infinite,
}

impl From<Size> for ProposedDimensions {
    fn from(size: Size) -> Self {
        Self {
            width: ProposedDimension::Exact(size.width),
            height: ProposedDimension::Exact(size.height),
        }
    }
}

#[cfg(feature = "embedded-graphics")]
impl From<embedded_graphics_core::geometry::Size> for ProposedDimensions {
    fn from(size: embedded_graphics_core::geometry::Size) -> Self {
        Self {
            width: ProposedDimension::Exact(size.width as u16),
            height: ProposedDimension::Exact(size.height as u16),
        }
    }
}

impl ProposedDimension {
    /// Returns the most flexible dimension within the proposal
    /// Magic size of 10 points is applied to views that have no implicit size
    #[must_use]
    pub fn resolve_most_flexible(self, minimum: u16, ideal: u16) -> Dimension {
        match self {
            Self::Compact => Dimension(ideal),
            Self::Exact(d) => Dimension(d.max(minimum)),
            Self::Infinite => Dimension::infinite(),
        }
    }
}

impl From<u16> for ProposedDimension {
    fn from(value: u16) -> Self {
        Self::Exact(value)
    }
}

impl core::ops::Add<u16> for ProposedDimension {
    type Output = Self;

    fn add(self, rhs: u16) -> Self::Output {
        match self {
            Self::Compact => Self::Compact,
            Self::Exact(d) => Self::Exact(d + rhs),
            Self::Infinite => Self::Infinite,
        }
    }
}

impl core::ops::Sub<u16> for ProposedDimension {
    type Output = Self;

    fn sub(self, rhs: u16) -> Self::Output {
        match self {
            Self::Compact => Self::Compact,
            Self::Exact(d) => Self::Exact(d.saturating_sub(rhs)),
            Self::Infinite => Self::Infinite,
        }
    }
}

impl core::ops::Mul<u16> for ProposedDimension {
    type Output = Self;

    fn mul(self, rhs: u16) -> Self::Output {
        match self {
            Self::Compact => Self::Compact,
            Self::Exact(d) => Self::Exact(d.saturating_mul(rhs)),
            Self::Infinite => Self::Infinite,
        }
    }
}

impl core::ops::Div<u16> for ProposedDimension {
    type Output = Self;

    fn div(self, rhs: u16) -> Self::Output {
        match self {
            Self::Compact => Self::Compact,
            Self::Exact(d) => Self::Exact(d.saturating_div(rhs)),
            Self::Infinite => Self::Infinite,
        }
    }
}

/// The dimension of a single axis
/// `u16::MAX` is treated as infinity, and this type mostly exists to prevent accidental panics from
/// operations overflowing
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Dimension(pub u16);

impl Dimension {
    #[must_use]
    pub const fn infinite() -> Self {
        Self(u16::MAX)
    }

    #[must_use]
    pub const fn is_infinite(self) -> bool {
        self.0 == u16::MAX
    }
}

impl From<Dimension> for u16 {
    fn from(value: Dimension) -> Self {
        value.0
    }
}

impl From<Dimension> for i16 {
    fn from(value: Dimension) -> Self {
        value.0.try_into().unwrap_or(Self::MAX)
    }
}

impl From<Dimension> for u32 {
    fn from(value: Dimension) -> Self {
        Self::from(value.0)
    }
}

impl From<Dimension> for i32 {
    fn from(value: Dimension) -> Self {
        Self::from(value.0)
    }
}

impl From<Dimension> for f32 {
    fn from(value: Dimension) -> Self {
        Self::from(value.0)
    }
}

impl From<u16> for Dimension {
    fn from(value: u16) -> Self {
        Self(value)
    }
}

impl core::ops::Add for Dimension {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_add(rhs.0))
    }
}

impl core::ops::Sub for Dimension {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_sub(rhs.0))
    }
}

impl core::ops::Mul for Dimension {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_mul(rhs.0))
    }
}

impl core::ops::Div for Dimension {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        Self(self.0.saturating_div(rhs.0))
    }
}

impl core::ops::AddAssign for Dimension {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl core::ops::SubAssign for Dimension {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl core::ops::Add<u16> for Dimension {
    type Output = Self;

    fn add(self, rhs: u16) -> Self::Output {
        Self(self.0.saturating_add(rhs))
    }
}

impl core::ops::Sub<u16> for Dimension {
    type Output = Self;

    fn sub(self, rhs: u16) -> Self::Output {
        Self(self.0.saturating_sub(rhs))
    }
}

impl core::ops::Mul<u16> for Dimension {
    type Output = Self;
    fn mul(self, rhs: u16) -> Self::Output {
        Self(self.0.saturating_mul(rhs))
    }
}

impl core::ops::Div<u16> for Dimension {
    type Output = Self;

    fn div(self, rhs: u16) -> Self::Output {
        Self(self.0.saturating_div(rhs))
    }
}

impl core::ops::AddAssign<u16> for Dimension {
    fn add_assign(&mut self, rhs: u16) {
        *self = *self + rhs;
    }
}

impl core::ops::SubAssign<u16> for Dimension {
    fn sub_assign(&mut self, rhs: u16) {
        *self = *self - rhs;
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Dimensions {
    pub width: Dimension,
    pub height: Dimension,
}

impl Dimensions {
    #[must_use]
    pub const fn new(width: u16, height: u16) -> Self {
        Self {
            width: Dimension(width),
            height: Dimension(height),
        }
    }

    #[must_use]
    pub const fn zero() -> Self {
        Self {
            width: Dimension(0),
            height: Dimension(0),
        }
    }

    #[must_use]
    pub fn union(self, other: Self) -> Self {
        Self {
            width: self.width.max(other.width),
            height: self.height.max(other.height),
        }
    }

    #[must_use]
    pub fn intersection(self, other: Self) -> Self {
        Self {
            width: self.width.min(other.width),
            height: self.height.min(other.height),
        }
    }

    #[must_use]
    pub fn intersecting_proposal(self, offer: &ProposedDimensions) -> Self {
        Self {
            width: match offer.width {
                ProposedDimension::Exact(d) => Dimension(self.width.0.min(d)),
                ProposedDimension::Infinite | ProposedDimension::Compact => self.width,
            },
            height: match offer.height {
                ProposedDimension::Exact(d) => Dimension(self.height.0.min(d)),
                ProposedDimension::Infinite | ProposedDimension::Compact => self.height,
            },
        }
    }

    #[must_use]
    pub fn area(self) -> u16 {
        (self.width * self.height).0
    }
}

impl core::ops::Add for Dimensions {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            width: self.width + rhs.width,
            height: self.height + rhs.height,
        }
    }
}

impl core::ops::Add<Size> for Dimensions {
    type Output = Self;

    fn add(self, rhs: Size) -> Self::Output {
        Self {
            width: self.width + rhs.width,
            height: self.height + rhs.height,
        }
    }
}

impl From<Size> for Dimensions {
    fn from(value: Size) -> Self {
        Self {
            width: Dimension(value.width),
            height: Dimension(value.height),
        }
    }
}

impl From<Dimensions> for Size {
    fn from(value: Dimensions) -> Self {
        Self {
            width: value.width.into(),
            height: value.height.into(),
        }
    }
}

#[cfg(feature = "embedded-graphics")]
impl From<embedded_graphics_core::geometry::Size> for Dimensions {
    fn from(value: embedded_graphics_core::geometry::Size) -> Self {
        Self {
            width: Dimension(value.width.try_into().unwrap_or(u16::MAX - 1)),
            height: Dimension(value.height.try_into().unwrap_or(u16::MAX - 1)),
        }
    }
}

impl Interpolate for Dimensions {
    fn interpolate(from: Self, to: Self, amount: u8) -> Self {
        Self {
            width: Dimension::interpolate(from.width, to.width, amount),
            height: Dimension::interpolate(from.height, to.height, amount),
        }
    }
}

impl Interpolate for Dimension {
    fn interpolate(from: Self, to: Self, amount: u8) -> Self {
        Self(
            (((u32::from(amount) * u32::from(to.0))
                + (u32::from(255 - amount) * u32::from(from.0)))
                / 255) as u16,
        )
    }
}

#[cfg(feature = "embedded-graphics")]
impl From<Dimensions> for embedded_graphics_core::geometry::Size {
    fn from(value: Dimensions) -> Self {
        Self::new(
            u32::from(value.width.0),
            u32::from(value.height.0),
        )
    }
}

#[cfg(test)]
mod tests {
    use crate::primitives::Interpolate as _;

    use super::ProposedDimension;

    #[test]
    fn interpolate_dimensions() {
        let from = super::Dimensions::new(10, 20);
        let to = super::Dimensions::new(20, 30);
        let result = super::Dimensions::interpolate(from, to, 128);
        assert_eq!(result.width.0, 15);
        assert_eq!(result.height.0, 25);
    }

    #[test]
    fn interpolate_dimension_min() {
        let from = super::Dimension::from(10);
        let to = super::Dimension::from(30);
        let result = super::Dimension::interpolate(from, to, 0);
        assert_eq!(result.0, 10);
    }

    #[test]
    fn interpolate_dimension_max() {
        let from = super::Dimension::from(10);
        let to = super::Dimension::from(30);
        let result = super::Dimension::interpolate(from, to, 255);
        assert_eq!(result.0, 30);
    }

    #[test]
    fn proposed_dimension_order() {
        assert_eq!(ProposedDimension::Compact, ProposedDimension::Compact);
        assert_eq!(ProposedDimension::Exact(0), ProposedDimension::Exact(0));
        assert_eq!(ProposedDimension::Exact(10), ProposedDimension::Exact(10));
        assert_eq!(ProposedDimension::Infinite, ProposedDimension::Infinite);
        assert!(ProposedDimension::Compact > ProposedDimension::Exact(0));
        assert!(ProposedDimension::Compact > ProposedDimension::Exact(100));
        assert!(ProposedDimension::Compact < ProposedDimension::Infinite);
        assert!(ProposedDimension::Exact(0) < ProposedDimension::Infinite);
        assert!(ProposedDimension::Exact(u16::MAX) < ProposedDimension::Infinite);
    }
}