azul_layout/
geometry.rs

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
#![allow(dead_code)]

// MIT License
//
// Copyright (c) 2018 Visly Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use std::ops::Add;
use azul_core::ui_solver::ResolvedOffsets;
use crate::{
    number::Number,
    style::{FlexDirection, Dimension},
};

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Rect {
    pub origin: RectOrigin,
    pub size: RectSize,
    pub margin: ResolvedOffsets,
    pub padding: ResolvedOffsets,
    pub border_widths: ResolvedOffsets,
}

impl Rect {
    pub const fn undefined() -> Self {
        Self {
            origin: RectOrigin::undefined(),
            size: RectSize::undefined(),
            margin: ResolvedOffsets::zero(),
            padding: ResolvedOffsets::zero(),
            border_widths: ResolvedOffsets::zero(),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct RectOrigin {
    pub x: Number,
    pub y: Number,
}

impl RectOrigin {
    pub const fn undefined() -> Self {
        Self {
            x: Number::Undefined,
            y: Number::Undefined,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct RectSize {
    pub width: Number,
    pub height: Number,
}

impl RectSize {
    pub const fn undefined() -> Self {
        Self {
            width: Number::Undefined,
            height: Number::Undefined,
        }
    }

    pub(crate) fn main(self, direction: FlexDirection) -> Number {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.width,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.height,
        }
    }

    pub(crate) fn cross(self, direction: FlexDirection) -> Number {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.height,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.width,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Offsets<T> {
    pub top: T,
    pub left: T,
    pub bottom: T,
    pub right: T,
}

pub(crate) const DEFAULT_OFFSETS: Offsets<Dimension> = Offsets {
    top: Dimension::DEFAULT,
    left: Dimension::DEFAULT,
    bottom: Dimension::DEFAULT,
    right: Dimension::DEFAULT,
};

impl<T> Offsets<T> {
    pub(crate) fn map<R, F: Fn(T) -> R>(self, f: F) -> Offsets<R> {
        Offsets { left: f(self.left), right: f(self.right), top: f(self.top), bottom: f(self.bottom) }
    }
}

impl<T: Add<Output = T> + Copy + Clone> Offsets<T> {
    pub(crate) fn horizontal(&self) -> T {
        self.left + self.right
    }

    pub(crate) fn vertical(&self) -> T {
        self.top + self.bottom
    }

    pub(crate) fn main(&self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.left + self.right,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.top + self.bottom,
        }
    }

    pub(crate) fn cross(&self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.top + self.bottom,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.left + self.right,
        }
    }
}

impl Add<RectSize> for RectSize {
    type Output = RectSize;

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

impl<T: Add<Output = T>> Add<Offsets<T>> for Offsets<T> {
    type Output = Offsets<T>;

    fn add(self, rhs: Offsets<T>) -> Offsets<T> {
        Offsets {
            left: self.left + rhs.left,
            right: self.right + rhs.right,
            top: self.top + rhs.top,
            bottom: self.bottom + rhs.bottom,
        }
    }
}

impl<T: Copy + Clone> Offsets<T> {
    pub(crate) fn main_start(&self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.left,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.top,
        }
    }

    pub(crate) fn main_end(&self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.right,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.bottom,
        }
    }

    pub(crate) fn cross_start(&self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.top,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.left,
        }
    }

    pub(crate) fn cross_end(&self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.bottom,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.right,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Size<T> {
    pub width: T,
    pub height: T,
}

pub(crate) const DEFAULT_SIZE: Size<Dimension> = Size {
    width: Dimension::DEFAULT,
    height: Dimension::DEFAULT,
};

impl<T> Size<T> {
    pub(crate) fn map<R, F>(self, f: F) -> Size<R>
    where
        F: Fn(T) -> R,
    {
        Size { width: f(self.width), height: f(self.height) }
    }

    pub(crate) fn set_main(&mut self, direction: FlexDirection, value: T) {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.width = value,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.height = value,
        }
    }

    pub(crate) fn set_cross(&mut self, direction: FlexDirection, value: T) {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.height = value,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.width = value,
        }
    }

    pub(crate) fn main(self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.width,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.height,
        }
    }

    pub(crate) fn cross(self, direction: FlexDirection) -> T {
        match direction {
            FlexDirection::Row | FlexDirection::RowReverse => self.height,
            FlexDirection::Column | FlexDirection::ColumnReverse => self.width,
        }
    }
}