leftwm_core/models/
xyhw_change.rs

1use crate::models::Window;
2use crate::models::Xyhw;
3use serde::{Deserialize, Serialize};
4
5use super::Handle;
6
7#[derive(Default, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Copy)]
8pub struct XyhwChange {
9    pub x: Option<i32>,
10    pub y: Option<i32>,
11    pub h: Option<i32>,
12    pub w: Option<i32>,
13    pub minw: Option<i32>,
14    pub maxw: Option<i32>,
15    pub minh: Option<i32>,
16    pub maxh: Option<i32>,
17}
18
19impl From<Xyhw> for XyhwChange {
20    fn from(xywh: Xyhw) -> Self {
21        Self {
22            x: Some(xywh.x()),
23            y: Some(xywh.y()),
24            w: Some(xywh.w()),
25            h: Some(xywh.h()),
26            minw: Some(xywh.minw()),
27            maxw: Some(xywh.maxw()),
28            minh: Some(xywh.minh()),
29            maxh: Some(xywh.maxh()),
30        }
31    }
32}
33
34impl XyhwChange {
35    pub fn update(&self, xyhw: &mut Xyhw) -> bool {
36        let mut changed = false;
37        if let Some(x) = self.x {
38            if xyhw.x() != x {
39                xyhw.set_x(x);
40                changed = true;
41            }
42        }
43        if let Some(y) = self.y {
44            if xyhw.y() != y {
45                xyhw.set_y(y);
46                changed = true;
47            }
48        }
49        if let Some(w) = self.w {
50            if xyhw.w() != w {
51                xyhw.set_w(w);
52                changed = true;
53            }
54        }
55        if let Some(h) = self.h {
56            if xyhw.h() != h {
57                xyhw.set_h(h);
58                changed = true;
59            }
60        }
61        if let Some(minw) = self.minw {
62            if xyhw.minw() != minw {
63                xyhw.set_minw(minw);
64                changed = true;
65            }
66        }
67        if let Some(maxw) = self.maxw {
68            if xyhw.maxw() != maxw {
69                xyhw.set_maxw(maxw);
70                changed = true;
71            }
72        }
73        if let Some(minh) = self.minh {
74            if xyhw.minh() != minh {
75                xyhw.set_minh(minh);
76                changed = true;
77            }
78        }
79        if let Some(maxh) = self.maxh {
80            if xyhw.maxh() != maxh {
81                xyhw.set_maxh(maxh);
82                changed = true;
83            }
84        }
85        changed
86    }
87
88    pub fn update_window_floating<H: Handle>(&self, window: &mut Window<H>) -> bool {
89        let mut changed = false;
90        if window.floating() {
91            let mut current = window.calculated_xyhw();
92            changed = self.update(&mut current);
93            window.set_floating_exact(current);
94        }
95        changed
96    }
97
98    /// # Panics
99    ///
100    /// Souldn't panic; `window.strut` is modified to be a `Some` by
101    /// the time of `unwrap()`
102    pub fn update_window_strut<H: Handle>(&self, window: &mut Window<H>) -> bool {
103        let mut changed = if window.strut.is_none() {
104            window.strut = Some(Xyhw::default());
105            true
106        } else {
107            false
108        };
109        let Some(mut xyhw) = window.strut else {
110            return false;
111        };
112        changed = self.update(&mut xyhw) || changed;
113        window.strut = Some(xyhw);
114        changed
115    }
116}