leftwm_core/models/
dock_area.rs

1use crate::models::Xyhw;
2use crate::models::XyhwBuilder;
3
4use super::Handle;
5use super::Screen;
6
7#[derive(Copy, Clone, Debug, Default)]
8pub struct DockArea {
9    pub top: i32,
10    pub top_start_x: i32,
11    pub top_end_x: i32,
12
13    pub bottom: i32,
14    pub bottom_start_x: i32,
15    pub bottom_end_x: i32,
16
17    pub right: i32,
18    pub right_start_y: i32,
19    pub right_end_y: i32,
20
21    pub left: i32,
22    pub left_start_y: i32,
23    pub left_end_y: i32,
24}
25
26impl DockArea {
27    #[must_use]
28    pub fn as_xyhw<H: Handle>(
29        &self,
30        screens_height: i32,
31        screens_width: i32,
32        screen: &Screen<H>,
33    ) -> Option<Xyhw> {
34        if self.top > 0 {
35            return Some(self.xyhw_from_top(screen.bbox.y));
36        }
37        if self.bottom > 0 {
38            return Some(self.xyhw_from_bottom(screens_height, screen.bbox.y + screen.bbox.height));
39        }
40        if self.left > 0 {
41            return Some(self.xyhw_from_left(screen.bbox.x));
42        }
43        if self.right > 0 {
44            return Some(self.xyhw_from_right(screens_width, screen.bbox.x + screen.bbox.width));
45        }
46        None
47    }
48
49    fn xyhw_from_top(&self, screen_y: i32) -> Xyhw {
50        XyhwBuilder {
51            x: self.top_start_x,
52            y: screen_y,
53            h: self.top - screen_y,
54            w: self.top_end_x - self.top_start_x,
55            ..XyhwBuilder::default()
56        }
57        .into()
58    }
59
60    fn xyhw_from_bottom(&self, screens_height: i32, screen_bottom: i32) -> Xyhw {
61        XyhwBuilder {
62            x: self.bottom_start_x,
63            y: screens_height - self.bottom,
64            h: self.bottom - (screens_height - screen_bottom),
65            w: self.bottom_end_x - self.bottom_start_x,
66            ..XyhwBuilder::default()
67        }
68        .into()
69    }
70
71    fn xyhw_from_left(&self, screen_x: i32) -> Xyhw {
72        XyhwBuilder {
73            x: screen_x,
74            y: self.left_start_y,
75            h: self.left_end_y - self.left_start_y,
76            w: self.left - screen_x,
77            ..XyhwBuilder::default()
78        }
79        .into()
80    }
81
82    fn xyhw_from_right(&self, screens_width: i32, screen_right: i32) -> Xyhw {
83        XyhwBuilder {
84            x: screens_width - self.right,
85            y: self.right_start_y,
86            h: self.right_end_y - self.right_start_y,
87            w: self.right - (screens_width - screen_right),
88            ..XyhwBuilder::default()
89        }
90        .into()
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn should_be_able_to_build_from_top() {
100        let area = DockArea {
101            top: 2,
102            top_start_x: 10,
103            top_end_x: 200,
104            ..DockArea::default()
105        };
106        let expected: Xyhw = XyhwBuilder {
107            h: 2,
108            w: 190,
109            x: 10,
110            y: 0,
111            ..XyhwBuilder::default()
112        }
113        .into();
114        assert_eq!(area.xyhw_from_top(0), expected);
115    }
116
117    #[test]
118    fn should_be_able_to_build_from_bottom() {
119        let area = DockArea {
120            bottom: 2,
121            bottom_start_x: 10,
122            bottom_end_x: 200,
123            ..DockArea::default()
124        };
125        let expected: Xyhw = XyhwBuilder {
126            h: 2,
127            w: 190,
128            x: 10,
129            y: 998,
130            ..XyhwBuilder::default()
131        }
132        .into();
133        assert_eq!(area.xyhw_from_bottom(1000, 1000), expected);
134    }
135
136    #[test]
137    fn should_be_able_to_build_from_left() {
138        let area = DockArea {
139            left: 2,
140            left_start_y: 10,
141            left_end_y: 200,
142            ..DockArea::default()
143        };
144        let expected: Xyhw = XyhwBuilder {
145            h: 190,
146            w: 2,
147            x: 0,
148            y: 10,
149            ..XyhwBuilder::default()
150        }
151        .into();
152        assert_eq!(area.xyhw_from_left(0), expected);
153    }
154
155    #[test]
156    fn should_be_able_to_build_from_right() {
157        let area = DockArea {
158            right: 2,
159            right_start_y: 10,
160            right_end_y: 200,
161            ..DockArea::default()
162        };
163        let expected: Xyhw = XyhwBuilder {
164            h: 190,
165            w: 2,
166            x: 1998,
167            y: 10,
168            ..XyhwBuilder::default()
169        }
170        .into();
171        assert_eq!(area.xyhw_from_right(2000, 2000), expected);
172    }
173}