ailloli_ui_widgets/chrome/resize_region.rs
1//! Geometry-only hit testing for client-side resize frames.
2
3use ailloli_ui_core::{Point, Rect};
4use ailloli_ui_runtime::input::ResizeEdge;
5
6/// Detects a resize edge on a logical-pixel frame inside `bounds`.
7///
8/// Disabled frames and points outside the inclusive bounds return `None`.
9/// `thickness` is clamped to at least one logical pixel; corners take priority
10/// over individual edges, and zero/negative bounds are not normalized.
11///
12/// # Examples
13///
14/// ```
15/// use ailloli_ui_core::{Point, Rect};
16/// use ailloli_ui_runtime::input::ResizeEdge;
17/// use ailloli_ui_widgets::chrome::hit_resize_frame;
18/// let bounds = Rect::new(0.0, 0.0, 100.0, 60.0);
19/// assert_eq!(hit_resize_frame(bounds, 8.0, Point::new(2.0, 30.0), true), Some(ResizeEdge::W));
20/// assert_eq!(hit_resize_frame(bounds, 8.0, Point::new(50.0, 30.0), true), None);
21/// assert_eq!(hit_resize_frame(bounds, 8.0, Point::new(2.0, 30.0), false), None);
22/// ```
23pub fn hit_resize_frame(
24 bounds: Rect,
25 thickness: f32,
26 p: Point,
27 enabled: bool,
28) -> Option<ResizeEdge> {
29 if !enabled {
30 return None;
31 }
32 let t = thickness.max(1.0);
33 let x = bounds.x;
34 let y = bounds.y;
35 let w = bounds.w;
36 let h = bounds.h;
37 let px = p.x;
38 let py = p.y;
39 if px < x || py < y || px > x + w || py > y + h {
40 return None;
41 }
42 let left = px < x + t;
43 let right = px > x + w - t;
44 let top = py < y + t;
45 let bottom = py > y + h - t;
46
47 if top && left {
48 return Some(ResizeEdge::NW);
49 }
50 if top && right {
51 return Some(ResizeEdge::NE);
52 }
53 if bottom && left {
54 return Some(ResizeEdge::SW);
55 }
56 if bottom && right {
57 return Some(ResizeEdge::SE);
58 }
59 if top {
60 return Some(ResizeEdge::N);
61 }
62 if bottom {
63 return Some(ResizeEdge::S);
64 }
65 if left {
66 return Some(ResizeEdge::W);
67 }
68 if right {
69 return Some(ResizeEdge::E);
70 }
71 None
72}