leftwm_core/models/
screen.rs

1use super::{window::Handle, DockArea, WindowHandle, WorkspaceId};
2use crate::config::Workspace;
3use serde::{Deserialize, Serialize};
4use std::convert::From;
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7pub struct Screen<H: Handle> {
8    #[serde(bound = "")]
9    pub root: WindowHandle<H>,
10    pub output: String,
11    pub id: Option<WorkspaceId>,
12    pub bbox: BBox,
13}
14
15/// Screen Bounding Box
16#[derive(Serialize, Deserialize, Debug, Clone, Copy)]
17pub struct BBox {
18    pub x: i32,
19    pub y: i32,
20    pub width: i32,
21    pub height: i32,
22}
23
24impl<H: Handle> Screen<H> {
25    #[must_use]
26    pub fn new(bbox: BBox, output: String) -> Self {
27        Self {
28            root: WindowHandle::<H>(H::default()),
29            output,
30            bbox,
31            id: None,
32        }
33    }
34
35    #[must_use]
36    pub const fn contains_point(&self, x: i32, y: i32) -> bool {
37        let bbox = &self.bbox;
38        let max_x = bbox.x + bbox.width;
39        let max_y = bbox.y + bbox.height;
40        (bbox.x <= x && x <= max_x) && (bbox.y <= y && y <= max_y)
41    }
42
43    #[must_use]
44    pub const fn contains_dock_area(&self, dock_area: DockArea, screens_area: (i32, i32)) -> bool {
45        if dock_area.top > 0 {
46            return self.contains_point(dock_area.top_start_x, dock_area.top);
47        }
48        if dock_area.bottom > 0 {
49            return self
50                .contains_point(dock_area.bottom_start_x, screens_area.0 - dock_area.bottom);
51        }
52        if dock_area.left > 0 {
53            return self.contains_point(dock_area.left, dock_area.left_start_y);
54        }
55        if dock_area.right > 0 {
56            return self.contains_point(screens_area.1 - dock_area.right, dock_area.right_start_y);
57        }
58        false
59    }
60}
61
62impl BBox {
63    pub fn add(&mut self, bbox: BBox) {
64        self.x += bbox.x;
65        self.y += bbox.y;
66        self.width += bbox.width;
67        self.height += bbox.height;
68    }
69}
70
71impl<H: Handle> From<&Workspace> for Screen<H> {
72    fn from(wsc: &Workspace) -> Self {
73        Self {
74            bbox: BBox {
75                height: wsc.height,
76                width: wsc.width,
77                x: wsc.x,
78                y: wsc.y,
79            },
80            output: wsc.output.clone(),
81            ..Default::default()
82        }
83    }
84}
85
86impl<H: Handle> Default for Screen<H> {
87    fn default() -> Self {
88        Self {
89            root: WindowHandle::<H>(H::default()),
90            output: String::default(),
91            id: None,
92            bbox: BBox {
93                height: 600,
94                width: 800,
95                x: 0,
96                y: 0,
97            },
98        }
99    }
100}