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
use std::f32::consts::PI;

use crate::{
    node::Node,
    prelude::{
        Alignment,
        DirectionMode,
        Gaps,
        Size,
    },
};

#[derive(PartialEq)]
pub struct Measure;

pub type Area = euclid::Rect<f32, Measure>;
pub type Size2D = euclid::Size2D<f32, Measure>;
pub type Point2D = euclid::Point2D<f32, Measure>;
pub type CursorPoint = euclid::Point2D<f64, Measure>;
pub type Length = euclid::Length<f32, Measure>;

pub trait AreaModel {
    // The area without any outer gap (e.g margin)
    fn after_gaps(&self, margin: &Gaps) -> Area;

    // Adjust the available area with the node offsets (mainly used by scrollviews)
    fn move_with_offsets(&mut self, offset_x: &Length, offset_y: &Length);

    // Align the content of this node.
    fn align_content(
        &mut self,
        available_area: &Area,
        contents_area: &Size2D,
        alignment: &Alignment,
        direction: &DirectionMode,
        alignment_direction: AlignmentDirection,
    );

    // Align the position of this node.
    #[allow(clippy::too_many_arguments)]
    fn align_position(
        &mut self,
        initial_available_area: &Area,
        inner_sizes: &Size2D,
        alignment: &Alignment,
        direction: &DirectionMode,
        alignment_direction: AlignmentDirection,
        siblings_len: usize,
        child_position: usize,
    );

    fn adjust_size(&mut self, node: &Node);

    fn expand(&mut self, size: &Size2D);

    fn max_area_when_rotated(&self, center: Point2D) -> Area;

    fn clip(&mut self, other: &Self);
}

impl AreaModel for Area {
    /// Get the area inside after including the gaps (margins or paddings)
    fn after_gaps(&self, margin: &Gaps) -> Area {
        let origin = self.origin;
        let size = self.size;
        Area::new(
            Point2D::new(origin.x + margin.left(), origin.y + margin.top()),
            Size2D::new(
                size.width - margin.horizontal(),
                size.height - margin.vertical(),
            ),
        )
    }

    /// Get the area inside after including the gaps (margins or paddings)
    fn move_with_offsets(&mut self, offset_x: &Length, offset_y: &Length) {
        self.origin.x += offset_x.get();
        self.origin.y += offset_y.get();
    }

    fn align_content(
        &mut self,
        available_area: &Area,
        contents_size: &Size2D,
        alignment: &Alignment,
        direction: &DirectionMode,
        alignment_direction: AlignmentDirection,
    ) {
        let axis = get_align_axis(direction, alignment_direction);

        match axis {
            AlignAxis::Height => match alignment {
                Alignment::Center => {
                    let new_origin_y =
                        (available_area.height() / 2.0) - (contents_size.height / 2.0);

                    self.origin.y = available_area.min_y() + new_origin_y;
                }
                Alignment::End => {
                    self.origin.y = available_area.max_y() - contents_size.height;
                }
                _ => {}
            },
            AlignAxis::Width => match alignment {
                Alignment::Center => {
                    let new_origin_x = (available_area.width() / 2.0) - (contents_size.width / 2.0);

                    self.origin.x = available_area.min_x() + new_origin_x;
                }
                Alignment::End => {
                    self.origin.x = available_area.max_x() - contents_size.width;
                }
                _ => {}
            },
        }
    }

    fn align_position(
        &mut self,
        initial_available_area: &Area,
        inner_sizes: &Size2D,
        alignment: &Alignment,
        direction: &DirectionMode,
        alignment_direction: AlignmentDirection,
        siblings_len: usize,
        child_position: usize,
    ) {
        let axis = get_align_axis(direction, alignment_direction);

        match axis {
            AlignAxis::Height => match alignment {
                Alignment::SpaceBetween if child_position > 0 => {
                    let all_gaps_sizes = initial_available_area.height() - inner_sizes.height;
                    let gap_size = all_gaps_sizes / (siblings_len - 1) as f32;
                    self.origin.y += gap_size;
                }
                Alignment::SpaceEvenly => {
                    let all_gaps_sizes = initial_available_area.height() - inner_sizes.height;
                    let gap_size = all_gaps_sizes / (siblings_len + 1) as f32;
                    self.origin.y += gap_size;
                }
                Alignment::SpaceAround => {
                    let all_gaps_sizes = initial_available_area.height() - inner_sizes.height;
                    let one_gap_size = all_gaps_sizes / siblings_len as f32;
                    let gap_size = if child_position == 0 || child_position == siblings_len {
                        one_gap_size / 2.
                    } else {
                        one_gap_size
                    };
                    self.origin.y += gap_size;
                }
                _ => {}
            },
            AlignAxis::Width => match alignment {
                Alignment::SpaceBetween if child_position > 0 => {
                    let all_gaps_sizes = initial_available_area.width() - inner_sizes.width;
                    let gap_size = all_gaps_sizes / (siblings_len - 1) as f32;
                    self.origin.x += gap_size;
                }
                Alignment::SpaceEvenly => {
                    let all_gaps_sizes = initial_available_area.width() - inner_sizes.width;
                    let gap_size = all_gaps_sizes / (siblings_len + 1) as f32;
                    self.origin.x += gap_size;
                }
                Alignment::SpaceAround => {
                    let all_gaps_sizes = initial_available_area.width() - inner_sizes.width;
                    let one_gap_size = all_gaps_sizes / siblings_len as f32;
                    let gap_size = if child_position == 0 || child_position == siblings_len {
                        one_gap_size / 2.
                    } else {
                        one_gap_size
                    };
                    self.origin.x += gap_size;
                }
                _ => {}
            },
        }
    }

    fn adjust_size(&mut self, node: &Node) {
        if let Size::InnerPercentage(p) = node.width {
            self.size.width *= p.get() / 100.;
        }
        if let Size::InnerPercentage(p) = node.height {
            self.size.height *= p.get() / 100.;
        }
    }

    fn expand(&mut self, size: &Size2D) {
        self.origin.x -= size.width;
        self.origin.y -= size.height;
        self.size.width += size.width * 2.;
        self.size.height += size.height * 2.;
    }

    fn max_area_when_rotated(&self, center: Point2D) -> Area {
        let (top_left_extreme, bottom_right_extreme) = calculate_extreme_corners(self, center);

        Area::new(
            Point2D::new(top_left_extreme.x, top_left_extreme.y),
            Size2D::new(
                bottom_right_extreme.x - top_left_extreme.x,
                bottom_right_extreme.y - top_left_extreme.y,
            ),
        )
    }

    fn clip(&mut self, other: &Self) {
        self.origin.x = self.origin.x.max(other.origin.x);
        self.origin.y = self.origin.y.max(other.origin.y);
        self.size.width = self.size.width.min(other.size.width);
        self.size.height = self.size.height.min(other.size.height);
    }
}

pub fn get_align_axis(
    direction: &DirectionMode,
    alignment_direction: AlignmentDirection,
) -> AlignAxis {
    match direction {
        DirectionMode::Vertical => match alignment_direction {
            AlignmentDirection::Main => AlignAxis::Height,
            AlignmentDirection::Cross => AlignAxis::Width,
        },
        DirectionMode::Horizontal => match alignment_direction {
            AlignmentDirection::Main => AlignAxis::Width,
            AlignmentDirection::Cross => AlignAxis::Height,
        },
    }
}

pub enum AlignmentDirection {
    Main,
    Cross,
}

#[derive(Debug)]
pub enum AlignAxis {
    Height,
    Width,
}

fn rotate_point_around_center(point: Point2D, center: Point2D, angle_radians: f32) -> Point2D {
    let sin_theta = angle_radians.sin();
    let cos_theta = angle_radians.cos();

    let x_shifted = point.x - center.x;
    let y_shifted = point.y - center.y;

    let x_rotated = x_shifted * cos_theta - y_shifted * sin_theta;
    let y_rotated = x_shifted * sin_theta + y_shifted * cos_theta;

    Point2D::new(x_rotated + center.x, y_rotated + center.y)
}

fn calculate_extreme_corners(area: &Area, center: Point2D) -> (Point2D, Point2D) {
    let biggest_side_width = (center.x - area.min_x()).max(area.max_x() - center.x);
    let biggest_side_height = (center.y - area.min_y()).max(area.max_y() - center.y);

    let corners = [
        Point2D::new(
            center.x - biggest_side_width,
            center.y - biggest_side_height,
        ),
        Point2D::new(
            center.x - biggest_side_width,
            center.y + biggest_side_height,
        ),
        Point2D::new(
            center.x + biggest_side_width,
            center.y - biggest_side_height,
        ),
        Point2D::new(
            center.x + biggest_side_width,
            center.y + biggest_side_height,
        ),
    ];

    let angle_45_radians = 45.0 * PI / 180.0;

    let rotated_corners: Vec<Point2D> = corners
        .iter()
        .map(|&corner| rotate_point_around_center(corner, center, angle_45_radians))
        .collect();

    let min_x = rotated_corners
        .iter()
        .map(|p| p.x)
        .fold(f32::INFINITY, |a, b| a.min(b));
    let min_y = rotated_corners
        .iter()
        .map(|p| p.y)
        .fold(f32::INFINITY, |a, b| a.min(b));
    let max_x = rotated_corners
        .iter()
        .map(|p| p.x)
        .fold(f32::NEG_INFINITY, |a, b| a.max(b));
    let max_y = rotated_corners
        .iter()
        .map(|p| p.y)
        .fold(f32::NEG_INFINITY, |a, b| a.max(b));

    (Point2D::new(min_x, min_y), Point2D::new(max_x, max_y))
}