1use crate::*;
3
4#[derive(Clone, Copy, Debug)]
6pub struct WindowFrameState {
7 pub bounds: Rect,
8 pub min_size: [f32; 2],
9 dragging: bool,
10 resizing: bool,
11 grab_offset: [f32; 2],
12}
13
14impl Default for WindowFrameState {
15 fn default() -> Self {
16 Self {
17 bounds: Rect { x: 40.0, y: 40.0, width: 320.0, height: 360.0 },
18 min_size: [200.0, 160.0],
19 dragging: false,
20 resizing: false,
21 grab_offset: [0.0, 0.0],
22 }
23 }
24}
25
26impl WindowFrameState {
27 pub fn new(bounds: Rect) -> Self {
28 Self { bounds, ..Self::default() }
29 }
30 pub fn is_dragging(&self) -> bool {
31 self.dragging
32 }
33 pub fn is_resizing(&self) -> bool {
34 self.resizing
35 }
36}
37
38#[derive(Clone, Copy, Debug)]
40pub struct WindowFrameOptions {
41 pub caption_height: f32,
42 pub corner_radius: f32,
43 pub caption_color: Option<Color>,
44 pub caption_text_color: Option<Color>,
45 pub surface_color: Option<Color>,
46 pub clamp_to: Option<Rect>,
47 pub show_maximize: bool,
48 pub show_close: bool,
49 pub resizable: bool,
50}
51
52impl Default for WindowFrameOptions {
53 fn default() -> Self {
54 Self {
55 caption_height: 36.0,
56 corner_radius: 10.0,
57 caption_color: None,
58 caption_text_color: None,
59 surface_color: None,
60 clamp_to: None,
61 show_maximize: true,
62 show_close: true,
63 resizable: true,
64 }
65 }
66}
67
68#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
70pub struct WindowFrameAction {
71 pub moved: bool,
72 pub resized: bool,
73 pub maximize_clicked: bool,
74 pub close_clicked: bool,
75}
76
77impl Frame<'_> {
78 pub fn floating_window(
81 &mut self,
82 id: u64,
83 title: &str,
84 state: &mut WindowFrameState,
85 options: WindowFrameOptions,
86 body: impl FnOnce(&mut Self, Rect),
87 ) -> WindowFrameAction {
88 let t = self.theme();
89 let cap_h = options.caption_height.max(28.0);
90 let min_w = state.min_size[0].max(140.0);
91 let min_h = state.min_size[1].max(cap_h + 64.0);
92 state.bounds.width = state.bounds.width.max(min_w);
93 state.bounds.height = state.bounds.height.max(min_h);
94
95 let mut action = WindowFrameAction::default();
96 let btn_count = usize::from(options.show_maximize) + usize::from(options.show_close);
97 let btn_area = btn_count as f32 * 36.0 + 4.0;
98 let drag_bar = Rect {
99 x: state.bounds.x,
100 y: state.bounds.y,
101 width: (state.bounds.width - btn_area).max(40.0),
102 height: cap_h,
103 };
104 let grip = Rect {
105 x: state.bounds.x + state.bounds.width - 24.0,
106 y: state.bounds.y + state.bounds.height - 24.0,
107 width: 24.0,
108 height: 24.0,
109 };
110
111 if self.input.released || self.input.cancelled || self.input.blurred {
112 state.dragging = false;
113 state.resizing = false;
114 }
115 if let Some(p) = self.input.pointer {
116 if self.input.pressed {
117 if options.resizable && grip.contains(p) {
118 state.resizing = true;
119 state.grab_offset = [
120 state.bounds.x + state.bounds.width - p[0],
121 state.bounds.y + state.bounds.height - p[1],
122 ];
123 } else if drag_bar.contains(p) {
124 state.dragging = true;
125 state.grab_offset = [p[0] - state.bounds.x, p[1] - state.bounds.y];
126 }
127 }
128 if state.dragging {
129 let nx = p[0] - state.grab_offset[0];
130 let ny = p[1] - state.grab_offset[1];
131 if (nx - state.bounds.x).abs() > 0.01 || (ny - state.bounds.y).abs() > 0.01 {
132 state.bounds.x = nx;
133 state.bounds.y = ny;
134 action.moved = true;
135 }
136 } else if state.resizing {
137 let nw = (p[0] + state.grab_offset[0] - state.bounds.x).max(min_w);
138 let nh = (p[1] + state.grab_offset[1] - state.bounds.y).max(min_h);
139 if (nw - state.bounds.width).abs() > 0.01 || (nh - state.bounds.height).abs() > 0.01 {
140 state.bounds.width = nw;
141 state.bounds.height = nh;
142 action.resized = true;
143 }
144 }
145 }
146 if let Some(limit) = options.clamp_to {
147 state.bounds.width = state.bounds.width.min(limit.width.max(min_w));
148 state.bounds.height = state.bounds.height.min(limit.height.max(min_h));
149 state.bounds.x = state.bounds.x.clamp(limit.x, (limit.x + limit.width - state.bounds.width).max(limit.x));
150 state.bounds.y = state.bounds.y.clamp(limit.y, (limit.y + limit.height - cap_h).max(limit.y));
151 }
152
153 let frame = state.bounds;
154 let cap = Rect { height: cap_h, ..frame };
155 let body_rect = Rect {
156 x: frame.x,
157 y: frame.y + cap_h,
158 width: frame.width,
159 height: (frame.height - cap_h).max(0.0),
160 };
161 let cap_bg = options.caption_color.unwrap_or(t.primary_container);
162 let cap_fg = options.caption_text_color.unwrap_or(t.on_primary_container);
163 let surf_bg = options.surface_color.unwrap_or(t.surface_low);
164
165 self.shadow(frame, options.corner_radius + 2.0, [0.0, 10.0], 26.0, 0.0, Color(0, 0, 0, 95));
166 self.paint_rect(frame, options.corner_radius, surf_bg);
167 self.paint_asym_rect(cap, options.corner_radius, 0.0, cap_bg);
168
169 let drag_bar = Rect { x: cap.x, y: cap.y, width: (cap.width - btn_area).max(40.0), height: cap_h };
170 let _ = self.target(id, drag_bar, "Move window", true, Role::Button);
171 self.styled_text(
172 Rect { x: cap.x + 12.0, y: cap.y, width: (cap.width - btn_area - 16.0).max(0.0), height: cap_h },
173 title,
174 cap_fg,
175 TextStyle { size: 13.5, weight: 500, ..TextStyle::default() },
176 );
177
178 let mut btn_x = cap.x + cap.width - 36.0;
179 if options.show_close {
180 let close_btn = Rect { x: btn_x, y: cap.y + (cap_h - 32.0) * 0.5, width: 32.0, height: 32.0 };
181 let r_cls = self.target(id.wrapping_add(2), close_btn, "Close window", true, Role::Button);
182 self.state_layer(r_cls, 16.0, cap_fg);
183 self.paint_system_icon(close_btn.inset(7.0), SystemIcon::Close, cap_fg);
184 action.close_clicked = r_cls.clicked;
185 btn_x -= 36.0;
186 }
187 if options.show_maximize {
188 let max_btn = Rect { x: btn_x, y: cap.y + (cap_h - 32.0) * 0.5, width: 32.0, height: 32.0 };
189 let r_max = self.target(id.wrapping_add(1), max_btn, "Maximize window", true, Role::Button);
190 self.state_layer(r_max, 16.0, cap_fg);
191 self.paint_system_icon(max_btn.inset(7.0), SystemIcon::Maximize, cap_fg);
192 action.maximize_clicked = r_max.clicked;
193 }
194
195 self.within(body_rect, |f| body(f, body_rect));
196
197 if options.resizable {
198 let grip = Rect {
199 x: frame.x + frame.width - 24.0,
200 y: frame.y + frame.height - 24.0,
201 width: 24.0,
202 height: 24.0,
203 };
204 let _ = self.target(id.wrapping_add(3), grip, "Resize window", true, Role::Button);
205 self.paint_line([grip.x + 8.0, grip.y + 19.0], [grip.x + 19.0, grip.y + 8.0], 1.6, t.outline);
206 self.paint_line([grip.x + 13.0, grip.y + 19.0], [grip.x + 19.0, grip.y + 13.0], 1.6, t.outline);
207 }
208
209 action
210 }
211}
212
213#[cfg(test)]
214#[path = "window_frame.spec.rs"]
215mod tests;