1use crate::window::CursorIcon;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum CsdHitZone {
8 None,
9 Titlebar,
10 MinimizeButton,
11 MaximizeButton,
12 CloseButton,
13 ResizeBorderTop,
14 ResizeBorderBottom,
15 ResizeBorderLeft,
16 ResizeBorderRight,
17 ResizeCornerTopLeft,
18 ResizeCornerTopRight,
19 ResizeCornerBottomLeft,
20 ResizeCornerBottomRight,
21}
22
23impl CsdHitZone {
24 pub fn is_button(&self) -> bool {
26 matches!(
27 self,
28 CsdHitZone::MinimizeButton | CsdHitZone::MaximizeButton | CsdHitZone::CloseButton
29 )
30 }
31
32 pub fn is_titlebar(&self) -> bool {
34 matches!(self, CsdHitZone::Titlebar)
35 }
36
37 pub fn is_resize(&self) -> bool {
39 matches!(
40 self,
41 CsdHitZone::ResizeBorderTop
42 | CsdHitZone::ResizeBorderBottom
43 | CsdHitZone::ResizeBorderLeft
44 | CsdHitZone::ResizeBorderRight
45 | CsdHitZone::ResizeCornerTopLeft
46 | CsdHitZone::ResizeCornerTopRight
47 | CsdHitZone::ResizeCornerBottomLeft
48 | CsdHitZone::ResizeCornerBottomRight
49 )
50 }
51
52 pub fn to_x11_moveresize_direction(&self) -> Option<u32> {
54 match self {
55 CsdHitZone::ResizeCornerTopLeft => Some(0),
56 CsdHitZone::ResizeBorderTop => Some(1),
57 CsdHitZone::ResizeCornerTopRight => Some(2),
58 CsdHitZone::ResizeBorderRight => Some(3),
59 CsdHitZone::ResizeCornerBottomRight => Some(4),
60 CsdHitZone::ResizeBorderBottom => Some(5),
61 CsdHitZone::ResizeCornerBottomLeft => Some(6),
62 CsdHitZone::ResizeBorderLeft => Some(7),
63 CsdHitZone::Titlebar => Some(8), _ => None,
65 }
66 }
67
68 pub fn to_wayland_resize_edge(&self) -> Option<u32> {
70 match self {
71 CsdHitZone::ResizeBorderTop => Some(1), CsdHitZone::ResizeBorderBottom => Some(2), CsdHitZone::ResizeBorderLeft => Some(4), CsdHitZone::ResizeCornerTopLeft => Some(5), CsdHitZone::ResizeCornerBottomLeft => Some(6), CsdHitZone::ResizeBorderRight => Some(8), CsdHitZone::ResizeCornerTopRight => Some(9), CsdHitZone::ResizeCornerBottomRight => Some(10), _ => None,
80 }
81 }
82
83 pub fn to_win32_nchittest(&self) -> isize {
85 match self {
86 CsdHitZone::None => 1, CsdHitZone::Titlebar => 2, CsdHitZone::MinimizeButton => 8, CsdHitZone::MaximizeButton => 9, CsdHitZone::CloseButton => 20, CsdHitZone::ResizeBorderLeft => 10, CsdHitZone::ResizeBorderRight => 11, CsdHitZone::ResizeBorderTop => 12, CsdHitZone::ResizeCornerTopLeft => 13, CsdHitZone::ResizeCornerTopRight => 14, CsdHitZone::ResizeBorderBottom => 15, CsdHitZone::ResizeCornerBottomLeft => 16, CsdHitZone::ResizeCornerBottomRight => 17, }
100 }
101
102 pub fn cursor_icon(&self) -> CursorIcon {
104 match self {
105 CsdHitZone::None => CursorIcon::Default,
106 CsdHitZone::Titlebar => CursorIcon::Default,
107 CsdHitZone::MinimizeButton | CsdHitZone::MaximizeButton | CsdHitZone::CloseButton => {
108 CursorIcon::Pointer
109 }
110 CsdHitZone::ResizeBorderTop | CsdHitZone::ResizeBorderBottom => CursorIcon::ResizeN,
111 CsdHitZone::ResizeBorderLeft | CsdHitZone::ResizeBorderRight => CursorIcon::ResizeE,
112 CsdHitZone::ResizeCornerTopLeft | CsdHitZone::ResizeCornerBottomRight => {
113 CursorIcon::ResizeNw
114 }
115 CsdHitZone::ResizeCornerTopRight | CsdHitZone::ResizeCornerBottomLeft => {
116 CursorIcon::ResizeNe
117 }
118 }
119 }
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
124pub enum ControlPlacement {
125 #[default]
126 Right, Left, }
129
130#[derive(Debug, Clone, Copy, PartialEq)]
132pub struct CsdGeometry {
133 pub titlebar_height: f32,
134 pub button_width: f32,
135 pub button_height: f32,
136 pub resize_border_thickness: f32,
137 pub placement: ControlPlacement,
138}
139
140impl Default for CsdGeometry {
141 fn default() -> Self {
142 Self {
143 titlebar_height: 32.0,
144 button_width: 46.0,
145 button_height: 32.0,
146 resize_border_thickness: 6.0,
147 placement: ControlPlacement::Right,
148 }
149 }
150}
151
152impl CsdGeometry {
153 pub fn new(
155 titlebar_height: f32,
156 button_width: f32,
157 button_height: f32,
158 resize_border_thickness: f32,
159 placement: ControlPlacement,
160 ) -> Self {
161 Self {
162 titlebar_height,
163 button_width,
164 button_height,
165 resize_border_thickness,
166 placement,
167 }
168 }
169
170 pub fn macos_style() -> Self {
172 Self {
173 titlebar_height: 28.0,
174 button_width: 24.0,
175 button_height: 28.0,
176 resize_border_thickness: 5.0,
177 placement: ControlPlacement::Left,
178 }
179 }
180
181 pub fn hit_test(&self, window_width: f32, window_height: f32, x: f32, y: f32) -> CsdHitZone {
183 if x < 0.0 || y < 0.0 || x > window_width || y > window_height {
185 return CsdHitZone::None;
186 }
187
188 let t = self.resize_border_thickness;
189
190 if x < t && y < t {
192 return CsdHitZone::ResizeCornerTopLeft;
193 }
194 if x > window_width - t && y < t {
195 return CsdHitZone::ResizeCornerTopRight;
196 }
197 if x < t && y > window_height - t {
198 return CsdHitZone::ResizeCornerBottomLeft;
199 }
200 if x > window_width - t && y > window_height - t {
201 return CsdHitZone::ResizeCornerBottomRight;
202 }
203
204 if y < t {
206 return CsdHitZone::ResizeBorderTop;
207 }
208 if y > window_height - t {
209 return CsdHitZone::ResizeBorderBottom;
210 }
211 if x < t {
212 return CsdHitZone::ResizeBorderLeft;
213 }
214 if x > window_width - t {
215 return CsdHitZone::ResizeBorderRight;
216 }
217
218 if y <= self.titlebar_height {
220 match self.placement {
221 ControlPlacement::Right => {
222 let bw = self.button_width;
223 if x >= window_width - bw {
224 return CsdHitZone::CloseButton;
225 }
226 if x >= window_width - 2.0 * bw {
227 return CsdHitZone::MaximizeButton;
228 }
229 if x >= window_width - 3.0 * bw {
230 return CsdHitZone::MinimizeButton;
231 }
232 CsdHitZone::Titlebar
233 }
234 ControlPlacement::Left => {
235 let bw = self.button_width;
236 if x <= bw {
237 return CsdHitZone::CloseButton;
238 }
239 if x <= 2.0 * bw {
240 return CsdHitZone::MinimizeButton;
241 }
242 if x <= 3.0 * bw {
243 return CsdHitZone::MaximizeButton;
244 }
245 CsdHitZone::Titlebar
246 }
247 }
248 } else {
249 CsdHitZone::None
250 }
251 }
252}
253
254#[derive(Debug, Clone)]
256pub struct CsdController {
257 pub geometry: CsdGeometry,
258 pub is_dragging: bool,
259 pub active_zone: CsdHitZone,
260 pub drag_start_pos: Option<(f32, f32)>,
261}
262
263impl Default for CsdController {
264 fn default() -> Self {
265 Self {
266 geometry: CsdGeometry::default(),
267 is_dragging: false,
268 active_zone: CsdHitZone::None,
269 drag_start_pos: None,
270 }
271 }
272}
273
274impl CsdController {
275 pub fn new(geometry: CsdGeometry) -> Self {
277 Self {
278 geometry,
279 is_dragging: false,
280 active_zone: CsdHitZone::None,
281 drag_start_pos: None,
282 }
283 }
284
285 pub fn hit_test(&self, window_width: f32, window_height: f32, x: f32, y: f32) -> CsdHitZone {
287 self.geometry.hit_test(window_width, window_height, x, y)
288 }
289
290 pub fn on_pointer_down(
292 &mut self,
293 window_width: f32,
294 window_height: f32,
295 x: f32,
296 y: f32,
297 ) -> CsdHitZone {
298 let zone = self.hit_test(window_width, window_height, x, y);
299 self.active_zone = zone;
300 if zone == CsdHitZone::Titlebar || zone.is_resize() {
301 self.drag_start_pos = Some((x, y));
302 } else {
303 self.drag_start_pos = None;
304 }
305 zone
306 }
307
308 pub fn on_pointer_up(&mut self) {
310 self.is_dragging = false;
311 self.active_zone = CsdHitZone::None;
312 self.drag_start_pos = None;
313 }
314
315 pub fn should_start_drag(&mut self, x: f32, y: f32) -> bool {
317 if let Some((sx, sy)) = self.drag_start_pos {
318 if (x - sx).abs() > 4.0 || (y - sy).abs() > 4.0 {
319 self.is_dragging = true;
320 return true;
321 }
322 }
323 false
324 }
325}