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
use std::ops::Deref;

pub use lyon::math::{Box2D as Bounds, Point, Size, Transform};

pub use layout::Spacer;
pub use modifiers::fixed::{Fixed, FixedHeight, FixedWidth};
pub use modifiers::padding::Padding;
pub use output::{gpu, svg, Output};
pub use shapes::{Circle, ContinuousRoundedRectangle, Ellipse, Rectangle, RoundedRectangle};
pub use shapes::{Path, Shape};
#[doc(inline)]
pub use text::Text;

use crate::{From, TryInto};

/// Alias for `euclid::default::SideOffsets2D<f32>`
pub type Offsets = lyon::geom::euclid::default::SideOffsets2D<f32>;

mod output;
/// Text handling for `View` construction.
pub mod text;

pub mod gesture;
mod layout;
mod modifiers;

mod shapes;
#[cfg(feature = "default_ui")]
pub mod ui;

/// User interface element and modifiers to re-configure it.
pub trait View: Sized {
    /// The intrinsic size of the `View`
    fn size(&self) -> Size;
    /// User-interface [`Event`] handling of the `View`
    #[allow(unused_variables)]
    fn event(&self, event: Event, offset: Point, bounds: Bounds) {}
    /// How the `View` is drawn
    fn draw(&self, bounds: Bounds, onto: &mut impl Output);

    /// Add padding to all sides of the `View`
    fn padding(self, top: f32, right: f32, bottom: f32, left: f32) -> Padding<Self> {
        Padding {
            offsets: Offsets::new(top, right, bottom, left),
            view: self,
        }
    }

    /// Add padding to the top of the `View`
    fn padding_top(self, pad: f32) -> Padding<Self> {
        self.padding(pad, 0.0, 0.0, 0.0)
    }

    /// Add padding to the right side of the `View`
    fn padding_right(self, pad: f32) -> Padding<Self> {
        self.padding(0.0, pad, 0.0, 0.0)
    }

    /// Add padding to the bottom of the `View`
    fn padding_bottom(self, pad: f32) -> Padding<Self> {
        self.padding(0.0, 0.0, pad, 0.0)
    }

    /// Add padding to the left side of the `View`
    fn padding_left(self, pad: f32) -> Padding<Self> {
        self.padding(0.0, 0.0, 0.0, pad)
    }

    /// Add padding to the horizontal sides of the `View`
    fn padding_horizontal(self, pad: f32) -> Padding<Self> {
        self.padding(0.0, pad, 0.0, pad)
    }

    /// Add padding to the vertical sides of the `View`
    fn padding_vertical(self, pad: f32) -> Padding<Self> {
        self.padding(0.0, 0.0, pad, 0.0)
    }

    /// Add different padding to the horizontal and vertical sides of the `View`
    fn padding_both(self, horizontal: f32, vertical: f32) -> Padding<Self> {
        self.padding(vertical, horizontal, vertical, horizontal)
    }

    /// Add the same padding to all sides of the `View`
    fn padding_all(self, pad: f32) -> Padding<Self> {
        self.padding(pad, pad, pad, pad)
    }

    /// Set the size of the `View` to a fixed value.
    fn fixed(self, width: f32, height: f32) -> impl View {
        let size = self.size();
        if size.is_empty() {
            return Err(Fixed {
                size: Size::new(width, height),
                view: self,
            });
        }

        let horizontal = (width - size.width) / 2.0;
        let vertical = (height - size.height) / 2.0;
        Ok(self.padding_both(horizontal, vertical))
    }

    fn width(self, width: f32) -> impl View {
        let size = self.size();
        if size.is_empty() {
            return Err(FixedWidth { width, view: self });
        }

        let horizontal = (width - size.width) / 2.0;
        Ok(self.padding_horizontal(horizontal))
    }

    fn height(self, height: f32) -> impl View {
        let size = self.size();
        if size.is_empty() {
            return Err(FixedHeight { height, view: self });
        }

        let vertical = (height - size.height) / 2.0;
        Ok(self.padding_vertical(vertical))
    }

    #[doc(hidden)]
    #[inline(always)]
    fn needs_layout(&self) -> bool {
        false
    }

    #[doc(hidden)]
    #[inline(always)]
    fn update_layout(&self, _size: Size, _bounds: Bounds) {}

    /// Causes a tuple of `View`s to cascade horizontally, rather than vertically.
    /// ## Note
    /// For other views, nothing changes
    fn across(self) -> impl View {
        self
    }
}

impl<T: View> View for Box<T> {
    #[inline(always)]
    fn size(&self) -> Size {
        self.deref().size()
    }

    #[inline(always)]
    fn event(&self, event: Event, offset: Point, bounds: Bounds) {
        self.deref().event(event, offset, bounds)
    }

    #[inline(always)]
    fn draw(&self, bounds: Bounds, onto: &mut impl Output) {
        self.deref().draw(bounds, onto)
    }
}

impl<T: View> View for Option<T> {
    fn size(&self) -> Size {
        if let Some(view) = self {
            return view.size();
        }

        Size::zero()
    }

    fn event(&self, event: Event, offset: Point, bounds: Bounds) {
        if let Some(view) = self {
            view.event(event, offset, bounds)
        }
    }

    fn draw(&self, bounds: Bounds, onto: &mut impl Output) {
        if let Some(view) = self {
            view.draw(bounds, onto)
        }
    }
}

impl<T: View, E: View> View for Result<T, E> {
    fn size(&self) -> Size {
        match self {
            Ok(view) => view.size(),
            Err(view) => view.size(),
        }
    }

    fn event(&self, event: Event, offset: Point, bounds: Bounds) {
        match self {
            Ok(view) => view.event(event, offset, bounds),
            Err(view) => view.event(event, offset, bounds),
        }
    }

    fn draw(&self, bounds: Bounds, onto: &mut impl Output) {
        match self {
            Ok(view) => view.draw(bounds, onto),
            Err(view) => view.draw(bounds, onto),
        }
    }
}

/// [`View`] events.
#[allow(missing_docs)]
#[derive(Copy, Clone, From, TryInto)]
pub enum Event {
    Gesture(Gesture),
    Resize { width: u32, height: u32 },
    Redraw,
}

/// touches… buttons…
#[derive(Copy, Clone)]
pub enum Gesture {
    Began { n: u8 },
    Moved { n: u8 },
    Ended { n: u8 },
}