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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use crate::config::Config;
use crate::display_action::DisplayAction;
use crate::display_servers::DisplayServer;
use crate::models::Window;
use crate::models::WindowHandle;
use crate::models::Workspace;
use crate::state::State;
use crate::utils::child_process::Children;
use std::sync::{atomic::AtomicBool, Arc};
#[derive(Debug)]
pub struct Manager<C, SERVER> {
pub state: State<C>,
pub(crate) children: Children,
pub(crate) reap_requested: Arc<AtomicBool>,
pub(crate) reload_requested: bool,
pub display_server: SERVER,
}
impl<C, SERVER> Manager<C, SERVER>
where
C: Config,
SERVER: DisplayServer,
{
pub fn new(config: C) -> Self {
let display_server = SERVER::new(&config);
Self {
state: State::new(config),
children: Default::default(),
reap_requested: Default::default(),
reload_requested: false,
display_server,
}
}
pub fn register_child_hook(&self) {
crate::child_process::register_child_hook(self.reap_requested.clone());
}
#[must_use]
pub fn focused_workspace(&self) -> Option<&Workspace> {
self.state.focus_manager.workspace(self)
}
pub fn focused_workspace_mut(&mut self) -> Option<&mut Workspace> {
self.state
.focus_manager
.workspace_mut(&mut self.state.workspaces)
}
#[must_use]
pub fn focused_tag(&self, offset: usize) -> Option<String> {
self.state.focus_manager.tag(offset)
}
#[must_use]
pub fn tag_index(&self, tag: &str) -> Option<usize> {
Some(self.state.tags.iter().position(|t| t.id == tag)).unwrap_or(None)
}
#[must_use]
pub fn focused_window(&self) -> Option<&Window> {
self.state.focus_manager.window(self)
}
pub fn focused_window_mut(&mut self) -> Option<&mut Window> {
self.state.focus_manager.window_mut(&mut self.state.windows)
}
pub fn update_static(&mut self) {
let workspaces = self.state.workspaces.clone();
self.state
.windows
.iter_mut()
.filter(|w| w.strut.is_some() || w.is_sticky())
.for_each(|w| {
let (x, y) = match w.strut {
Some(strut) => strut.center(),
None => w.calculated_xyhw().center(),
};
if let Some(ws) = workspaces.iter().find(|ws| ws.contains_point(x, y)) {
w.tags = ws.tags.clone();
}
});
}
pub fn sort_windows(&mut self) {
use crate::models::WindowType;
let (level1, other): (Vec<&Window>, Vec<&Window>) =
self.state.windows.iter().partition(|w| {
w.type_ == WindowType::Dialog
|| w.type_ == WindowType::Splash
|| w.type_ == WindowType::Utility
|| w.type_ == WindowType::Menu
});
let (level2, other): (Vec<&Window>, Vec<&Window>) = other
.iter()
.partition(|w| w.type_ == WindowType::Normal && w.floating());
let (level3, other): (Vec<&Window>, Vec<&Window>) =
other.iter().partition(|w| w.type_ == WindowType::Normal);
let windows: Vec<Window> = level1
.iter()
.chain(level2.iter())
.chain(level3.iter())
.chain(other.iter())
.map(|&w| w.clone())
.collect();
self.state.windows = windows;
let order: Vec<_> = self.state.windows.iter().map(|w| w.handle).collect();
let act = DisplayAction::SetWindowOrder(order);
self.state.actions.push_back(act);
}
pub fn move_to_top(&mut self, handle: &WindowHandle) -> Option<()> {
let index = self
.state
.windows
.iter()
.position(|w| &w.handle == handle)?;
let window = self.state.windows.remove(index);
self.state.windows.insert(0, window);
self.sort_windows();
Some(())
}
#[must_use]
pub fn workspaces_display(&self) -> String {
let mut focused_id = None;
if let Some(f) = self.focused_workspace() {
focused_id = f.id;
}
let list: Vec<String> = self
.state
.workspaces
.iter()
.map(|w| {
let tags = w.tags.join(",");
if w.id == focused_id {
format!("({})", tags)
} else {
format!(" {} ", tags)
}
})
.collect();
list.join(" ")
}
#[must_use]
pub fn windows_display(&self) -> String {
let list: Vec<String> = self
.state
.windows
.iter()
.map(|w| {
let tags = w.tags.join(",");
format!("[{:?}:{}]", w.handle, tags)
})
.collect();
list.join(" ")
}
pub fn hard_reload(&mut self) {
self.reload_requested = true;
}
pub fn update_for_theme(&mut self) -> bool {
for win in &mut self.state.windows {
win.update_for_theme(&self.state.config);
}
for ws in &mut self.state.workspaces {
ws.update_for_theme(&self.state.config);
}
true
}
}
#[cfg(test)]
impl Manager<crate::config::TestConfig, crate::display_servers::MockDisplayServer> {
pub fn new_test(tags: Vec<String>) -> Self {
Self::new(crate::config::TestConfig { tags })
}
}