3
3
use crate :: { layers:: ShapeIdx , * } ;
4
4
use epaint:: * ;
5
5
6
+ #[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
7
+ #[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
8
+ pub struct Margin {
9
+ pub left : f32 ,
10
+ pub right : f32 ,
11
+ pub top : f32 ,
12
+ pub bottom : f32 ,
13
+ }
14
+
15
+ impl Margin {
16
+ #[ inline]
17
+ pub fn same ( margin : f32 ) -> Self {
18
+ Self {
19
+ left : margin,
20
+ right : margin,
21
+ top : margin,
22
+ bottom : margin,
23
+ }
24
+ }
25
+
26
+ /// Margins with the same size on opposing sides
27
+ #[ inline]
28
+ pub fn symmetric ( x : f32 , y : f32 ) -> Self {
29
+ Self {
30
+ left : x,
31
+ right : x,
32
+ top : y,
33
+ bottom : y,
34
+ }
35
+ }
36
+
37
+ /// Total margins on both sides
38
+ pub fn sum ( & self ) -> Vec2 {
39
+ Vec2 :: new ( self . left + self . right , self . top + self . bottom )
40
+ }
41
+ }
42
+
43
+ impl From < Vec2 > for Margin {
44
+ fn from ( v : Vec2 ) -> Self {
45
+ Self :: symmetric ( v. x , v. y )
46
+ }
47
+ }
48
+
6
49
/// Color and margin of a rectangular background of a [`Ui`].
7
50
#[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
8
51
#[ must_use = "You should call .show()" ]
9
52
pub struct Frame {
10
53
/// On each side
11
- pub margin : Vec2 ,
54
+ pub margin : Margin ,
12
55
pub rounding : Rounding ,
13
56
pub shadow : Shadow ,
14
57
pub fill : Color32 ,
@@ -23,7 +66,7 @@ impl Frame {
23
66
/// For when you want to group a few widgets together within a frame.
24
67
pub fn group ( style : & Style ) -> Self {
25
68
Self {
26
- margin : Vec2 :: splat ( 6.0 ) , // symmetric looks best in corners when nesting
69
+ margin : Margin :: same ( 6.0 ) , // symmetric looks best in corners when nesting
27
70
rounding : style. visuals . widgets . noninteractive . rounding ,
28
71
stroke : style. visuals . widgets . noninteractive . bg_stroke ,
29
72
..Default :: default ( )
@@ -32,7 +75,7 @@ impl Frame {
32
75
33
76
pub ( crate ) fn side_top_panel ( style : & Style ) -> Self {
34
77
Self {
35
- margin : Vec2 :: new ( 8.0 , 2.0 ) ,
78
+ margin : Margin :: symmetric ( 8.0 , 2.0 ) ,
36
79
rounding : Rounding :: none ( ) ,
37
80
fill : style. visuals . window_fill ( ) ,
38
81
stroke : style. visuals . window_stroke ( ) ,
@@ -42,7 +85,7 @@ impl Frame {
42
85
43
86
pub ( crate ) fn central_panel ( style : & Style ) -> Self {
44
87
Self {
45
- margin : Vec2 :: new ( 8.0 , 8.0 ) ,
88
+ margin : Margin :: symmetric ( 8.0 , 8.0 ) ,
46
89
rounding : Rounding :: none ( ) ,
47
90
fill : style. visuals . window_fill ( ) ,
48
91
stroke : Default :: default ( ) ,
@@ -52,7 +95,7 @@ impl Frame {
52
95
53
96
pub fn window ( style : & Style ) -> Self {
54
97
Self {
55
- margin : style. spacing . window_padding ,
98
+ margin : style. spacing . window_margin ,
56
99
rounding : style. visuals . window_rounding ,
57
100
shadow : style. visuals . window_shadow ,
58
101
fill : style. visuals . window_fill ( ) ,
@@ -62,7 +105,7 @@ impl Frame {
62
105
63
106
pub fn menu ( style : & Style ) -> Self {
64
107
Self {
65
- margin : Vec2 :: splat ( 1.0 ) ,
108
+ margin : Margin :: same ( 1.0 ) ,
66
109
rounding : style. visuals . widgets . noninteractive . rounding ,
67
110
shadow : style. visuals . popup_shadow ,
68
111
fill : style. visuals . window_fill ( ) ,
@@ -72,7 +115,7 @@ impl Frame {
72
115
73
116
pub fn popup ( style : & Style ) -> Self {
74
117
Self {
75
- margin : style. spacing . window_padding ,
118
+ margin : style. spacing . window_margin ,
76
119
rounding : style. visuals . widgets . noninteractive . rounding ,
77
120
shadow : style. visuals . popup_shadow ,
78
121
fill : style. visuals . window_fill ( ) ,
@@ -83,7 +126,7 @@ impl Frame {
83
126
/// dark canvas to draw on
84
127
pub fn dark_canvas ( style : & Style ) -> Self {
85
128
Self {
86
- margin : Vec2 :: new ( 10.0 , 10.0 ) ,
129
+ margin : Margin :: symmetric ( 10.0 , 10.0 ) ,
87
130
rounding : style. visuals . widgets . noninteractive . rounding ,
88
131
fill : Color32 :: from_black_alpha ( 250 ) ,
89
132
stroke : style. visuals . window_stroke ( ) ,
@@ -109,7 +152,7 @@ impl Frame {
109
152
}
110
153
111
154
/// Margin on each side of the frame.
112
- pub fn margin ( mut self , margin : impl Into < Vec2 > ) -> Self {
155
+ pub fn margin ( mut self , margin : impl Into < Margin > ) -> Self {
113
156
self . margin = margin. into ( ) ;
114
157
self
115
158
}
@@ -137,7 +180,10 @@ impl Frame {
137
180
pub fn begin ( self , ui : & mut Ui ) -> Prepared {
138
181
let where_to_put_background = ui. painter ( ) . add ( Shape :: Noop ) ;
139
182
let outer_rect_bounds = ui. available_rect_before_wrap ( ) ;
140
- let mut inner_rect = outer_rect_bounds. shrink2 ( self . margin ) ;
183
+
184
+ let mut inner_rect = outer_rect_bounds;
185
+ inner_rect. min += Vec2 :: new ( self . margin . left , self . margin . top ) ;
186
+ inner_rect. max -= Vec2 :: new ( self . margin . right , self . margin . bottom ) ;
141
187
142
188
// Make sure we don't shrink to the negative:
143
189
inner_rect. max . x = inner_rect. max . x . max ( inner_rect. min . x ) ;
@@ -197,7 +243,10 @@ impl Frame {
197
243
198
244
impl Prepared {
199
245
pub fn outer_rect ( & self ) -> Rect {
200
- self . content_ui . min_rect ( ) . expand2 ( self . frame . margin )
246
+ let mut rect = self . content_ui . min_rect ( ) ;
247
+ rect. min -= Vec2 :: new ( self . frame . margin . left , self . frame . margin . top ) ;
248
+ rect. max += Vec2 :: new ( self . frame . margin . right , self . frame . margin . bottom ) ;
249
+ rect
201
250
}
202
251
203
252
pub fn end ( self , ui : & mut Ui ) -> Response {
0 commit comments