leftwm_core/models/
dock_area.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::models::Xyhw;
use crate::models::XyhwBuilder;

use super::Handle;
use super::Screen;

#[derive(Copy, Clone, Debug, Default)]
pub struct DockArea {
    pub top: i32,
    pub top_start_x: i32,
    pub top_end_x: i32,

    pub bottom: i32,
    pub bottom_start_x: i32,
    pub bottom_end_x: i32,

    pub right: i32,
    pub right_start_y: i32,
    pub right_end_y: i32,

    pub left: i32,
    pub left_start_y: i32,
    pub left_end_y: i32,
}

impl DockArea {
    #[must_use]
    pub fn as_xyhw<H: Handle>(
        &self,
        screens_height: i32,
        screens_width: i32,
        screen: &Screen<H>,
    ) -> Option<Xyhw> {
        if self.top > 0 {
            return Some(self.xyhw_from_top(screen.bbox.y));
        }
        if self.bottom > 0 {
            return Some(self.xyhw_from_bottom(screens_height, screen.bbox.y + screen.bbox.height));
        }
        if self.left > 0 {
            return Some(self.xyhw_from_left(screen.bbox.x));
        }
        if self.right > 0 {
            return Some(self.xyhw_from_right(screens_width, screen.bbox.x + screen.bbox.width));
        }
        None
    }

    fn xyhw_from_top(&self, screen_y: i32) -> Xyhw {
        XyhwBuilder {
            x: self.top_start_x,
            y: screen_y,
            h: self.top - screen_y,
            w: self.top_end_x - self.top_start_x,
            ..XyhwBuilder::default()
        }
        .into()
    }

    fn xyhw_from_bottom(&self, screens_height: i32, screen_bottom: i32) -> Xyhw {
        XyhwBuilder {
            x: self.bottom_start_x,
            y: screens_height - self.bottom,
            h: self.bottom - (screens_height - screen_bottom),
            w: self.bottom_end_x - self.bottom_start_x,
            ..XyhwBuilder::default()
        }
        .into()
    }

    fn xyhw_from_left(&self, screen_x: i32) -> Xyhw {
        XyhwBuilder {
            x: screen_x,
            y: self.left_start_y,
            h: self.left_end_y - self.left_start_y,
            w: self.left - screen_x,
            ..XyhwBuilder::default()
        }
        .into()
    }

    fn xyhw_from_right(&self, screens_width: i32, screen_right: i32) -> Xyhw {
        XyhwBuilder {
            x: screens_width - self.right,
            y: self.right_start_y,
            h: self.right_end_y - self.right_start_y,
            w: self.right - (screens_width - screen_right),
            ..XyhwBuilder::default()
        }
        .into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn should_be_able_to_build_from_top() {
        let area = DockArea {
            top: 2,
            top_start_x: 10,
            top_end_x: 200,
            ..DockArea::default()
        };
        let expected: Xyhw = XyhwBuilder {
            h: 2,
            w: 190,
            x: 10,
            y: 0,
            ..XyhwBuilder::default()
        }
        .into();
        assert_eq!(area.xyhw_from_top(0), expected);
    }

    #[test]
    fn should_be_able_to_build_from_bottom() {
        let area = DockArea {
            bottom: 2,
            bottom_start_x: 10,
            bottom_end_x: 200,
            ..DockArea::default()
        };
        let expected: Xyhw = XyhwBuilder {
            h: 2,
            w: 190,
            x: 10,
            y: 998,
            ..XyhwBuilder::default()
        }
        .into();
        assert_eq!(area.xyhw_from_bottom(1000, 1000), expected);
    }

    #[test]
    fn should_be_able_to_build_from_left() {
        let area = DockArea {
            left: 2,
            left_start_y: 10,
            left_end_y: 200,
            ..DockArea::default()
        };
        let expected: Xyhw = XyhwBuilder {
            h: 190,
            w: 2,
            x: 0,
            y: 10,
            ..XyhwBuilder::default()
        }
        .into();
        assert_eq!(area.xyhw_from_left(0), expected);
    }

    #[test]
    fn should_be_able_to_build_from_right() {
        let area = DockArea {
            right: 2,
            right_start_y: 10,
            right_end_y: 200,
            ..DockArea::default()
        };
        let expected: Xyhw = XyhwBuilder {
            h: 190,
            w: 2,
            x: 1998,
            y: 10,
            ..XyhwBuilder::default()
        }
        .into();
        assert_eq!(area.xyhw_from_right(2000, 2000), expected);
    }
}