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
use crate::prelude::{
    DirectionMode,
    Gaps,
    Node,
    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 without_gaps(self, gap: &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);

    /// Adjust the size given the Node data
    fn adjust_size(&mut self, node: &Node);
}

impl AreaModel for Area {
    fn without_gaps(self, gaps: &Gaps) -> Area {
        let origin = self.origin;
        let size = self.size;
        Area::new(
            Point2D::new(origin.x + gaps.left(), origin.y + gaps.top()),
            Size2D::new(
                size.width - gaps.horizontal(),
                size.height - gaps.vertical(),
            ),
        )
    }

    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 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.;
        }
    }
}

pub enum AlignmentDirection {
    Main,
    Cross,
}

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

impl AlignAxis {
    pub fn new(direction: &DirectionMode, alignment_direction: AlignmentDirection) -> Self {
        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 trait SizeModel {
    /// Get the size with the given gap, e.g padding.
    fn with_gaps(self, gap: &Gaps) -> Size2D;
}

impl SizeModel for Size2D {
    fn with_gaps(self, gap: &Gaps) -> Size2D {
        Size2D::new(self.width + gap.horizontal(), self.height + gap.vertical())
    }
}