Skip to main content

xrack_runtime/
window_status.rs

1use crate::Property;
2use std::cell::{Cell, RefCell};
3
4/// Native state reported by the host. Some window managers do not report minimization.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub struct WindowStatus {
7    pub active: bool,
8    pub minimized: Option<bool>,
9    pub maximized: bool,
10    pub full_screen: bool,
11}
12
13/// An ordered request from a component to the host of its native window.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum WindowCommand {
16    Minimize,
17    Maximize,
18    Restore,
19    ToggleMaximized,
20    SetFullScreen(bool),
21    ToggleFullScreen,
22}
23
24#[doc(hidden)]
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum WindowCommandMethod {
27    Minimize,
28    Maximize,
29    Restore,
30    ToggleMaximized,
31    SetFullScreen,
32    ToggleFullScreen,
33}
34
35impl WindowCommandMethod {
36    pub fn parse(name: &str) -> Option<Self> {
37        Some(match name {
38            "minimize" => Self::Minimize,
39            "maximize" => Self::Maximize,
40            "restore" => Self::Restore,
41            "toggle-maximized" => Self::ToggleMaximized,
42            "set-full-screen" => Self::SetFullScreen,
43            "toggle-full-screen" => Self::ToggleFullScreen,
44            _ => return None,
45        })
46    }
47
48    pub fn action(self, enabled: Option<bool>) -> Option<WindowCommand> {
49        Some(match (self, enabled) {
50            (Self::Minimize, None) => WindowCommand::Minimize,
51            (Self::Maximize, None) => WindowCommand::Maximize,
52            (Self::Restore, None) => WindowCommand::Restore,
53            (Self::ToggleMaximized, None) => WindowCommand::ToggleMaximized,
54            (Self::SetFullScreen, Some(enabled)) => WindowCommand::SetFullScreen(enabled),
55            (Self::ToggleFullScreen, None) => WindowCommand::ToggleFullScreen,
56            _ => return None,
57        })
58    }
59}
60
61#[doc(hidden)]
62#[derive(Clone, Copy, Debug)]
63pub enum WindowMetric {
64    Active,
65    Minimized,
66    MinimizedKnown,
67    Maximized,
68    FullScreen,
69}
70
71impl WindowMetric {
72    pub fn parse(name: &str) -> Option<Self> {
73        Some(match name {
74            "window-active" => Self::Active,
75            "minimized" => Self::Minimized,
76            "minimized-known" => Self::MinimizedKnown,
77            "maximized" => Self::Maximized,
78            "full-screen" => Self::FullScreen,
79            _ => return None,
80        })
81    }
82}
83
84#[doc(hidden)]
85#[derive(Default)]
86pub struct WindowStatusBinding([Property<bool>; 5]);
87
88impl WindowStatusBinding {
89    pub fn get(&self, metric: WindowMetric) -> bool {
90        self.0[metric as usize].get(|| false)
91    }
92
93    pub fn publish(&self, status: WindowStatus) {
94        for (property, value) in self.0.iter().zip([
95            status.active,
96            status.minimized.unwrap_or(false),
97            status.minimized.is_some(),
98            status.maximized,
99            status.full_screen,
100        ]) {
101            property.set_if_changed(value);
102        }
103    }
104}
105
106#[doc(hidden)]
107#[derive(Default)]
108pub struct WindowLifecycle {
109    status: WindowStatusBinding,
110    close: Cell<bool>,
111    commands: RefCell<Vec<WindowCommand>>,
112}
113
114impl WindowLifecycle {
115    pub fn get(&self, metric: WindowMetric) -> bool {
116        self.status.get(metric)
117    }
118
119    pub fn publish(&self, status: WindowStatus) {
120        self.status.publish(status);
121    }
122
123    pub fn request_close(&self) {
124        self.close.set(true);
125    }
126
127    pub fn take_close_request(&self) -> bool {
128        self.close.replace(false)
129    }
130
131    pub fn request(&self, command: WindowCommand) {
132        self.commands.borrow_mut().push(command);
133    }
134
135    pub fn take_commands(&self) -> Vec<WindowCommand> {
136        self.commands.take()
137    }
138
139    pub fn has_commands(&self) -> bool {
140        !self.commands.borrow().is_empty()
141    }
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    #[test]
149    fn learning_minimization_status_does_not_invalidate_unchanged_flags() {
150        let status = WindowStatusBinding::default();
151        let minimized = Property::default();
152        let known = Property::default();
153        let read_minimized = || minimized.get(|| status.get(WindowMetric::Minimized));
154        let read_known = || known.get(|| status.get(WindowMetric::MinimizedKnown));
155        assert!(!read_minimized());
156        assert!(!read_known());
157        status.publish(WindowStatus {
158            active: true,
159            minimized: Some(false),
160            ..WindowStatus::default()
161        });
162        assert!(!minimized.is_dirty());
163        assert!(known.is_dirty());
164        assert!(read_known());
165        status.publish(WindowStatus {
166            minimized: Some(true),
167            ..WindowStatus::default()
168        });
169        assert!(!known.is_dirty());
170        assert!(read_minimized());
171    }
172}