Skip to main content

a7n_core/input/
geometry.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2
3use a7n_proto::v1::{Edge, Landing};
4
5use super::clamp_position;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct Bounds {
9    pub x: i32,
10    pub y: i32,
11    pub width: u32,
12    pub height: u32,
13}
14
15impl Bounds {
16    #[must_use]
17    pub const fn rect(self) -> (i32, i32, u32, u32) {
18        (self.x, self.y, self.width, self.height)
19    }
20
21    pub(super) fn contains(self, x: i64, y: i64) -> bool {
22        x >= i64::from(self.x)
23            && y >= i64::from(self.y)
24            && x < i64::from(self.x) + i64::from(self.width)
25            && y < i64::from(self.y) + i64::from(self.height)
26    }
27}
28
29impl From<(i32, i32, u32, u32)> for Bounds {
30    fn from((x, y, width, height): (i32, i32, u32, u32)) -> Self {
31        Self {
32            x,
33            y,
34            width,
35            height,
36        }
37    }
38}
39
40pub(super) fn nearest_monitor(
41    bounds: (i32, i32, u32, u32),
42    monitors: &[Bounds],
43    x: i32,
44    y: i32,
45) -> Bounds {
46    monitors
47        .iter()
48        .filter(|m| m.width > 0 && m.height > 0)
49        .min_by_key(|m| {
50            let (px, py) = clamp_position(m.rect(), x, y);
51            let dx = i128::from(x) - i128::from(px);
52            let dy = i128::from(y) - i128::from(py);
53            dx * dx + dy * dy
54        })
55        .copied()
56        .unwrap_or_else(|| bounds.into())
57}
58
59/// Project a bbox-relative landing onto the outer monitor edge in that direction.
60#[must_use]
61#[allow(clippy::cast_possible_truncation)] // Offsets are clamped and float-to-int casts saturate.
62pub fn landing_point(bounds: Bounds, monitors: &[Bounds], landing: &Landing) -> (i32, i32) {
63    let edge = Edge::try_from(landing.edge).unwrap_or(Edge::Unspecified);
64    let offset = f64::from(landing.offset.clamp(0.0, 1.0));
65    let right = bounds
66        .x
67        .saturating_add_unsigned(bounds.width.saturating_sub(1));
68    let bottom = bounds
69        .y
70        .saturating_add_unsigned(bounds.height.saturating_sub(1));
71    let x = match edge {
72        Edge::Left => bounds.x,
73        Edge::Right => right,
74        _ => (f64::from(bounds.x) + f64::from(bounds.width.saturating_sub(1)) * offset) as i32,
75    };
76    let y = match edge {
77        Edge::Top => bounds.y,
78        Edge::Bottom => bottom,
79        _ => (f64::from(bounds.y) + f64::from(bounds.height.saturating_sub(1)) * offset) as i32,
80    };
81    let horizontal = matches!(edge, Edge::Left | Edge::Right);
82    let monitor = monitors
83        .iter()
84        .filter(|m| m.width > 0 && m.height > 0)
85        .min_by_key(|m| {
86            let (px, py) = clamp_position(m.rect(), x, y);
87            let distance = if horizontal {
88                i64::from(y) - i64::from(py)
89            } else {
90                i64::from(x) - i64::from(px)
91            };
92            let boundary = match edge {
93                Edge::Left => i64::from(m.x),
94                Edge::Right => -(i64::from(m.x) + i64::from(m.width) - 1),
95                Edge::Top => i64::from(m.y),
96                Edge::Bottom => -(i64::from(m.y) + i64::from(m.height) - 1),
97                Edge::Unspecified => 0,
98            };
99            (distance.abs(), boundary)
100        })
101        .copied()
102        .unwrap_or(bounds);
103    let (px, py) = clamp_position(monitor.rect(), x, y);
104    match edge {
105        Edge::Left => (monitor.x, py),
106        Edge::Right => (
107            monitor
108                .x
109                .saturating_add_unsigned(monitor.width.saturating_sub(1)),
110            py,
111        ),
112        Edge::Top => (px, monitor.y),
113        Edge::Bottom => (
114            px,
115            monitor
116                .y
117                .saturating_add_unsigned(monitor.height.saturating_sub(1)),
118        ),
119        Edge::Unspecified => (x, y),
120    }
121}