Skip to main content

strata_platform/
csd.rs

1//! Client-Side Decorations (CSD): Titlebar geometry, control buttons, and resize border hit-testing.
2
3use crate::window::CursorIcon;
4
5/// The region of a Client-Side Decorated window under a given coordinate.
6#[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    /// Returns true if this hit zone represents an interactive window control button.
25    pub fn is_button(&self) -> bool {
26        matches!(
27            self,
28            CsdHitZone::MinimizeButton | CsdHitZone::MaximizeButton | CsdHitZone::CloseButton
29        )
30    }
31
32    /// Returns true if this hit zone is the window titlebar.
33    pub fn is_titlebar(&self) -> bool {
34        matches!(self, CsdHitZone::Titlebar)
35    }
36
37    /// Returns true if this hit zone triggers window resizing.
38    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    /// Converts the zone to an X11 `_NET_WM_MOVERESIZE` direction index.
53    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), // _NET_WM_MOVERESIZE_MOVE
64            _ => None,
65        }
66    }
67
68    /// Converts the zone to a Wayland `xdg_toplevel` resize edge bitmask.
69    pub fn to_wayland_resize_edge(&self) -> Option<u32> {
70        match self {
71            CsdHitZone::ResizeBorderTop => Some(1),          // TOP
72            CsdHitZone::ResizeBorderBottom => Some(2),       // BOTTOM
73            CsdHitZone::ResizeBorderLeft => Some(4),         // LEFT
74            CsdHitZone::ResizeCornerTopLeft => Some(5),      // TOP | LEFT
75            CsdHitZone::ResizeCornerBottomLeft => Some(6),   // BOTTOM | LEFT
76            CsdHitZone::ResizeBorderRight => Some(8),        // RIGHT
77            CsdHitZone::ResizeCornerTopRight => Some(9),     // TOP | RIGHT
78            CsdHitZone::ResizeCornerBottomRight => Some(10), // BOTTOM | RIGHT
79            _ => None,
80        }
81    }
82
83    /// Converts the zone to a Win32 `WM_NCHITTEST` result code.
84    pub fn to_win32_nchittest(&self) -> isize {
85        match self {
86            CsdHitZone::None => 1,                     // HTCLIENT
87            CsdHitZone::Titlebar => 2,                 // HTCAPTION
88            CsdHitZone::MinimizeButton => 8,           // HTMINBUTTON
89            CsdHitZone::MaximizeButton => 9,           // HTMAXBUTTON
90            CsdHitZone::CloseButton => 20,             // HTCLOSE
91            CsdHitZone::ResizeBorderLeft => 10,        // HTLEFT
92            CsdHitZone::ResizeBorderRight => 11,       // HTRIGHT
93            CsdHitZone::ResizeBorderTop => 12,         // HTTOP
94            CsdHitZone::ResizeCornerTopLeft => 13,     // HTTOPLEFT
95            CsdHitZone::ResizeCornerTopRight => 14,    // HTTOPRIGHT
96            CsdHitZone::ResizeBorderBottom => 15,      // HTBOTTOM
97            CsdHitZone::ResizeCornerBottomLeft => 16,  // HTBOTTOMLEFT
98            CsdHitZone::ResizeCornerBottomRight => 17, // HTBOTTOMRIGHT
99        }
100    }
101
102    /// Determines the appropriate cursor icon for this hit zone.
103    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/// Placement orientation for titlebar control buttons (close, minimize, maximize).
123#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
124pub enum ControlPlacement {
125    #[default]
126    Right, // Windows / Linux default
127    Left, // macOS default
128}
129
130/// Geometry parameters defining CSD titlebar and resize borders.
131#[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    /// Creates custom CSD geometry parameters.
154    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    /// Creates a macOS-styled CSD geometry layout with left-aligned buttons.
171    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    /// Resolves the CsdHitZone for a coordinate `(x, y)` given the window dimensions.
182    pub fn hit_test(&self, window_width: f32, window_height: f32, x: f32, y: f32) -> CsdHitZone {
183        // Outside the window bounds
184        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        // 1. Resize Corners
191        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        // 2. Resize Borders
205        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        // 3. Titlebar & Control Buttons
219        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/// High-level controller managing CSD geometry, hit-testing, and native drag tracking.
255#[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    /// Creates a new CsdController with the given geometry.
276    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    /// Performs hit-testing against the managed geometry.
286    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    /// Processes a pointer down event and records drag start candidate coordinates.
291    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    /// Resets drag tracking state on pointer up.
309    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    /// Returns true if the pointer has travelled past the minimum drag threshold.
316    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}