1- use crate :: {
2- node:: Node ,
3- prelude:: {
4- Alignment ,
5- DirectionMode ,
6- Gaps ,
7- Size ,
8- } ,
1+ use crate :: prelude:: {
2+ DirectionMode ,
3+ Gaps ,
4+ Node ,
5+ Size ,
96} ;
107
118#[ derive( PartialEq ) ]
@@ -18,157 +15,37 @@ pub type CursorPoint = euclid::Point2D<f64, Measure>;
1815pub type Length = euclid:: Length < f32 , Measure > ;
1916
2017pub trait AreaModel {
21- // The area without any outer gap (e.g margin)
22- fn after_gaps ( & self , margin : & Gaps ) -> Area ;
18+ /// The area without any outer gap (e.g margin)
19+ fn without_gaps ( self , gap : & Gaps ) -> Area ;
2320
24- // Adjust the available area with the node offsets (mainly used by scrollviews)
21+ /// Adjust the available area with the node offsets (mainly used by scrollviews)
2522 fn move_with_offsets ( & mut self , offset_x : & Length , offset_y : & Length ) ;
2623
27- // Align the content of this node.
28- fn align_content (
29- & mut self ,
30- available_area : & Area ,
31- contents_area : & Size2D ,
32- alignment : & Alignment ,
33- direction : & DirectionMode ,
34- alignment_direction : AlignmentDirection ,
35- ) ;
36-
37- // Align the position of this node.
38- #[ allow( clippy:: too_many_arguments) ]
39- fn align_position (
40- & mut self ,
41- initial_available_area : & Area ,
42- inner_sizes : & Size2D ,
43- alignment : & Alignment ,
44- direction : & DirectionMode ,
45- alignment_direction : AlignmentDirection ,
46- siblings_len : usize ,
47- child_position : usize ,
48- ) ;
49-
24+ /// Adjust the size given the Node data
5025 fn adjust_size ( & mut self , node : & Node ) ;
5126}
5227
5328impl AreaModel for Area {
54- /// Get the area inside after including the gaps (margins or paddings)
55- fn after_gaps ( & self , margin : & Gaps ) -> Area {
29+ # [ inline ]
30+ fn without_gaps ( self , gaps : & Gaps ) -> Area {
5631 let origin = self . origin ;
5732 let size = self . size ;
5833 Area :: new (
59- Point2D :: new ( origin. x + margin . left ( ) , origin. y + margin . top ( ) ) ,
34+ Point2D :: new ( origin. x + gaps . left ( ) , origin. y + gaps . top ( ) ) ,
6035 Size2D :: new (
61- size. width - margin . horizontal ( ) ,
62- size. height - margin . vertical ( ) ,
36+ size. width - gaps . horizontal ( ) ,
37+ size. height - gaps . vertical ( ) ,
6338 ) ,
6439 )
6540 }
6641
67- /// Get the area inside after including the gaps (margins or paddings)
42+ # [ inline ]
6843 fn move_with_offsets ( & mut self , offset_x : & Length , offset_y : & Length ) {
6944 self . origin . x += offset_x. get ( ) ;
7045 self . origin . y += offset_y. get ( ) ;
7146 }
7247
73- fn align_content (
74- & mut self ,
75- available_area : & Area ,
76- contents_size : & Size2D ,
77- alignment : & Alignment ,
78- direction : & DirectionMode ,
79- alignment_direction : AlignmentDirection ,
80- ) {
81- let axis = get_align_axis ( direction, alignment_direction) ;
82-
83- match axis {
84- AlignAxis :: Height => match alignment {
85- Alignment :: Center => {
86- let new_origin_y =
87- ( available_area. height ( ) / 2.0 ) - ( contents_size. height / 2.0 ) ;
88-
89- self . origin . y = available_area. min_y ( ) + new_origin_y;
90- }
91- Alignment :: End => {
92- self . origin . y = available_area. max_y ( ) - contents_size. height ;
93- }
94- _ => { }
95- } ,
96- AlignAxis :: Width => match alignment {
97- Alignment :: Center => {
98- let new_origin_x = ( available_area. width ( ) / 2.0 ) - ( contents_size. width / 2.0 ) ;
99-
100- self . origin . x = available_area. min_x ( ) + new_origin_x;
101- }
102- Alignment :: End => {
103- self . origin . x = available_area. max_x ( ) - contents_size. width ;
104- }
105- _ => { }
106- } ,
107- }
108- }
109-
110- fn align_position (
111- & mut self ,
112- initial_available_area : & Area ,
113- inner_sizes : & Size2D ,
114- alignment : & Alignment ,
115- direction : & DirectionMode ,
116- alignment_direction : AlignmentDirection ,
117- siblings_len : usize ,
118- child_position : usize ,
119- ) {
120- let axis = get_align_axis ( direction, alignment_direction) ;
121-
122- match axis {
123- AlignAxis :: Height => match alignment {
124- Alignment :: SpaceBetween if child_position > 0 => {
125- let all_gaps_sizes = initial_available_area. height ( ) - inner_sizes. height ;
126- let gap_size = all_gaps_sizes / ( siblings_len - 1 ) as f32 ;
127- self . origin . y += gap_size;
128- }
129- Alignment :: SpaceEvenly => {
130- let all_gaps_sizes = initial_available_area. height ( ) - inner_sizes. height ;
131- let gap_size = all_gaps_sizes / ( siblings_len + 1 ) as f32 ;
132- self . origin . y += gap_size;
133- }
134- Alignment :: SpaceAround => {
135- let all_gaps_sizes = initial_available_area. height ( ) - inner_sizes. height ;
136- let one_gap_size = all_gaps_sizes / siblings_len as f32 ;
137- let gap_size = if child_position == 0 || child_position == siblings_len {
138- one_gap_size / 2.
139- } else {
140- one_gap_size
141- } ;
142- self . origin . y += gap_size;
143- }
144- _ => { }
145- } ,
146- AlignAxis :: Width => match alignment {
147- Alignment :: SpaceBetween if child_position > 0 => {
148- let all_gaps_sizes = initial_available_area. width ( ) - inner_sizes. width ;
149- let gap_size = all_gaps_sizes / ( siblings_len - 1 ) as f32 ;
150- self . origin . x += gap_size;
151- }
152- Alignment :: SpaceEvenly => {
153- let all_gaps_sizes = initial_available_area. width ( ) - inner_sizes. width ;
154- let gap_size = all_gaps_sizes / ( siblings_len + 1 ) as f32 ;
155- self . origin . x += gap_size;
156- }
157- Alignment :: SpaceAround => {
158- let all_gaps_sizes = initial_available_area. width ( ) - inner_sizes. width ;
159- let one_gap_size = all_gaps_sizes / siblings_len as f32 ;
160- let gap_size = if child_position == 0 || child_position == siblings_len {
161- one_gap_size / 2.
162- } else {
163- one_gap_size
164- } ;
165- self . origin . x += gap_size;
166- }
167- _ => { }
168- } ,
169- }
170- }
171-
48+ #[ inline( always) ]
17249 fn adjust_size ( & mut self , node : & Node ) {
17350 if let Size :: InnerPercentage ( p) = node. width {
17451 self . size . width *= p. get ( ) / 100. ;
@@ -179,22 +56,6 @@ impl AreaModel for Area {
17956 }
18057}
18158
182- pub fn get_align_axis (
183- direction : & DirectionMode ,
184- alignment_direction : AlignmentDirection ,
185- ) -> AlignAxis {
186- match direction {
187- DirectionMode :: Vertical => match alignment_direction {
188- AlignmentDirection :: Main => AlignAxis :: Height ,
189- AlignmentDirection :: Cross => AlignAxis :: Width ,
190- } ,
191- DirectionMode :: Horizontal => match alignment_direction {
192- AlignmentDirection :: Main => AlignAxis :: Width ,
193- AlignmentDirection :: Cross => AlignAxis :: Height ,
194- } ,
195- }
196- }
197-
19859pub enum AlignmentDirection {
19960 Main ,
20061 Cross ,
@@ -205,3 +66,31 @@ pub enum AlignAxis {
20566 Height ,
20667 Width ,
20768}
69+
70+ impl AlignAxis {
71+ #[ inline]
72+ pub fn new ( direction : & DirectionMode , alignment_direction : AlignmentDirection ) -> Self {
73+ match direction {
74+ DirectionMode :: Vertical => match alignment_direction {
75+ AlignmentDirection :: Main => AlignAxis :: Height ,
76+ AlignmentDirection :: Cross => AlignAxis :: Width ,
77+ } ,
78+ DirectionMode :: Horizontal => match alignment_direction {
79+ AlignmentDirection :: Main => AlignAxis :: Width ,
80+ AlignmentDirection :: Cross => AlignAxis :: Height ,
81+ } ,
82+ }
83+ }
84+ }
85+
86+ pub trait SizeModel {
87+ /// Get the size with the given gap, e.g padding.
88+ fn with_gaps ( self , gap : & Gaps ) -> Size2D ;
89+ }
90+
91+ impl SizeModel for Size2D {
92+ #[ inline]
93+ fn with_gaps ( self , gap : & Gaps ) -> Size2D {
94+ Size2D :: new ( self . width + gap. horizontal ( ) , self . height + gap. vertical ( ) )
95+ }
96+ }
0 commit comments