|
|
@@ -9,6 +9,15 @@ use x11rb::protocol::Event; |
| 9 |
9 |
use x11rb::protocol::xproto::*; |
| 10 |
10 |
use x11rb::rust_connection::RustConnection; |
| 11 |
11 |
|
|
12 |
+// TODO: move this to its own file? (tag.rs?) |
|
13 |
+pub type TagMask = u32; |
|
14 |
+ |
|
15 |
+pub const TAG_COUNT: usize = 9; |
|
16 |
+ |
|
17 |
+pub fn tag_mask(tag: usize) -> TagMask { |
|
18 |
+ 1 << tag |
|
19 |
+} |
|
20 |
+ |
| 12 |
21 |
pub struct WindowManager { |
| 13 |
22 |
connection: RustConnection, |
| 14 |
23 |
screen_number: usize, |
|
|
@@ -17,6 +26,8 @@ pub struct WindowManager { |
| 17 |
26 |
windows: Vec<Window>, |
| 18 |
27 |
focused_window: Option<Window>, |
| 19 |
28 |
layout: Box<dyn Layout>, |
|
29 |
+ window_tags: std::collections::HashMap<Window, TagMask>, |
|
30 |
+ selected_tags: TagMask, |
| 20 |
31 |
} |
| 21 |
32 |
|
| 22 |
33 |
impl WindowManager { |
|
|
@@ -45,6 +56,8 @@ impl WindowManager { |
| 45 |
56 |
windows: Vec::new(), |
| 46 |
57 |
focused_window: None, |
| 47 |
58 |
layout: Box::new(TilingLayout), |
|
59 |
+ window_tags: std::collections::HashMap::new(), |
|
60 |
+ selected_tags: tag_mask(0), |
| 48 |
61 |
}); |
| 49 |
62 |
} |
| 50 |
63 |
|
|
|
@@ -95,6 +108,16 @@ impl WindowManager { |
| 95 |
108 |
println!("Quitting window manager"); |
| 96 |
109 |
std::process::exit(0); |
| 97 |
110 |
} |
|
111 |
+ KeyAction::ViewTag => { |
|
112 |
+ if let Arg::Int(tag_index) = arg { |
|
113 |
+ self.view_tag(*tag_index as usize)?; |
|
114 |
+ } |
|
115 |
+ } |
|
116 |
+ KeyAction::MoveToTag => { |
|
117 |
+ if let Arg::Int(tag_index) = arg { |
|
118 |
+ self.move_to_tag(*tag_index as usize)?; |
|
119 |
+ } |
|
120 |
+ } |
| 98 |
121 |
KeyAction::None => { |
| 99 |
122 |
//no-op |
| 100 |
123 |
} |
|
|
@@ -102,28 +125,89 @@ impl WindowManager { |
| 102 |
125 |
Ok(()) |
| 103 |
126 |
} |
| 104 |
127 |
|
|
128 |
+ fn is_window_visible(&self, window: Window) -> bool { |
|
129 |
+ if let Some(&tags) = self.window_tags.get(&window) { |
|
130 |
+ (tags & self.selected_tags) != 0 |
|
131 |
+ } else { |
|
132 |
+ false |
|
133 |
+ } |
|
134 |
+ } |
|
135 |
+ |
|
136 |
+ fn visible_windows(&self) -> Vec<Window> { |
|
137 |
+ self.windows |
|
138 |
+ .iter() |
|
139 |
+ .filter(|&&w| self.is_window_visible(w)) |
|
140 |
+ .copied() |
|
141 |
+ .collect() |
|
142 |
+ } |
|
143 |
+ |
|
144 |
+ fn update_window_visibility(&self) -> Result<()> { |
|
145 |
+ for &window in &self.windows { |
|
146 |
+ if self.is_window_visible(window) { |
|
147 |
+ self.connection.map_window(window)?; |
|
148 |
+ } else { |
|
149 |
+ self.connection.unmap_window(window)?; |
|
150 |
+ } |
|
151 |
+ } |
|
152 |
+ self.connection.flush()?; |
|
153 |
+ Ok(()) |
|
154 |
+ } |
|
155 |
+ |
|
156 |
+ pub fn view_tag(&mut self, tag_index: usize) -> Result<()> { |
|
157 |
+ if tag_index >= TAG_COUNT { |
|
158 |
+ return Ok(()); |
|
159 |
+ } |
|
160 |
+ |
|
161 |
+ self.selected_tags = tag_mask(tag_index); |
|
162 |
+ println!("Viewing tag {}", tag_index + 1); |
|
163 |
+ |
|
164 |
+ self.update_window_visibility()?; |
|
165 |
+ self.apply_layout()?; |
|
166 |
+ |
|
167 |
+ let visible = self.visible_windows(); |
|
168 |
+ self.set_focus(visible.first().copied())?; |
|
169 |
+ |
|
170 |
+ Ok(()) |
|
171 |
+ } |
|
172 |
+ |
|
173 |
+ pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> { |
|
174 |
+ if tag_index >= TAG_COUNT { |
|
175 |
+ return Ok(()); |
|
176 |
+ } |
|
177 |
+ |
|
178 |
+ if let Some(focused) = self.focused_window { |
|
179 |
+ let mask = tag_mask(tag_index); |
|
180 |
+ self.window_tags.insert(focused, mask); |
|
181 |
+ |
|
182 |
+ println!("Moved window {} to tag {}", focused, tag_index + 1); |
|
183 |
+ |
|
184 |
+ self.update_window_visibility()?; |
|
185 |
+ self.apply_layout()?; |
|
186 |
+ } |
|
187 |
+ |
|
188 |
+ Ok(()) |
|
189 |
+ } |
|
190 |
+ |
| 105 |
191 |
pub fn cycle_focus(&mut self, direction: i32) -> Result<()> { |
| 106 |
|
- if self.windows.is_empty() { |
|
192 |
+ let visible = self.visible_windows(); |
|
193 |
+ |
|
194 |
+ if visible.is_empty() { |
| 107 |
195 |
return Ok(()); |
| 108 |
196 |
} |
| 109 |
197 |
|
| 110 |
198 |
let next_window = if let Some(current) = self.focused_window { |
| 111 |
|
- if let Some(current_index) = self.windows.iter().position(|&w| w == current) { |
|
199 |
+ if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 112 |
200 |
let next_index = if direction > 0 { |
| 113 |
|
- (current_index + 1) % self.windows.len() |
|
201 |
+ (current_index + 1) % visible.len() |
| 114 |
202 |
} else { |
| 115 |
|
- if current_index == 0 { |
| 116 |
|
- self.windows.len() - 1 |
| 117 |
|
- } else { |
| 118 |
|
- current_index - 1 |
| 119 |
|
- } |
|
203 |
+ (current_index + visible.len() - 1) % visible.len() |
| 120 |
204 |
}; |
| 121 |
|
- self.windows[next_index] |
|
205 |
+ visible[next_index] |
| 122 |
206 |
} else { |
| 123 |
|
- self.windows[0] |
|
207 |
+ visible[0] |
| 124 |
208 |
} |
| 125 |
209 |
} else { |
| 126 |
|
- self.windows[0] |
|
210 |
+ visible[0] |
| 127 |
211 |
}; |
| 128 |
212 |
|
| 129 |
213 |
self.set_focus(Some(next_window))?; |
|
|
@@ -174,15 +258,29 @@ impl WindowManager { |
| 174 |
258 |
Event::MapRequest(event) => { |
| 175 |
259 |
self.connection.map_window(event.window)?; |
| 176 |
260 |
self.windows.push(event.window); |
|
261 |
+ self.window_tags.insert(event.window, self.selected_tags); |
| 177 |
262 |
self.apply_layout()?; |
| 178 |
263 |
self.set_focus(Some(event.window))?; |
| 179 |
264 |
} |
| 180 |
265 |
Event::UnmapNotify(event) => { |
| 181 |
266 |
if self.windows.contains(&event.window) { |
| 182 |
|
- println!("Window {} unmapped, removing from layout", event.window); |
| 183 |
|
- self.remove_window(event.window)?; |
|
267 |
+ if self.is_window_visible(event.window) { |
|
268 |
+ println!("Visible window {} unmapped, removing", event.window); |
|
269 |
+ self.remove_window(event.window)?; |
|
270 |
+ } else { |
|
271 |
+ println!( |
|
272 |
+ "Hidden window {} unmapped (expected), ignoring", |
|
273 |
+ event.window |
|
274 |
+ ); |
|
275 |
+ } |
| 184 |
276 |
} |
| 185 |
277 |
} |
|
278 |
+ // Event::UnmapNotify(event) => { |
|
279 |
+ // if self.windows.contains(&event.window) { |
|
280 |
+ // println!("Window {} unmapped, removing from layout", event.window); |
|
281 |
+ // self.remove_window(event.window)?; |
|
282 |
+ // } |
|
283 |
+ // } |
| 186 |
284 |
Event::DestroyNotify(event) => { |
| 187 |
285 |
if self.windows.contains(&event.window) { |
| 188 |
286 |
println!("Window {} destroyed, removing from layout", event.window); |
|
|
@@ -203,12 +301,10 @@ impl WindowManager { |
| 203 |
301 |
let screen_height = self.screen.height_in_pixels as u32; |
| 204 |
302 |
let border_width = 2u32; |
| 205 |
303 |
|
| 206 |
|
- let geometries = self |
| 207 |
|
- .layout |
| 208 |
|
- .arrange(&self.windows, screen_width, screen_height); |
|
304 |
+ let visible = self.visible_windows(); |
|
305 |
+ let geometries = self.layout.arrange(&visible, screen_width, screen_height); |
| 209 |
306 |
|
| 210 |
|
- for (window, geometry) in self.windows.iter().zip(geometries.iter()) { |
| 211 |
|
- // Adjust for full borders |
|
307 |
+ for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 212 |
308 |
let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 213 |
309 |
let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 214 |
310 |
|
|
|
@@ -222,12 +318,13 @@ impl WindowManager { |
| 222 |
318 |
)?; |
| 223 |
319 |
} |
| 224 |
320 |
self.connection.flush()?; |
| 225 |
|
- return Ok(()); |
|
321 |
+ Ok(()) |
| 226 |
322 |
} |
| 227 |
323 |
|
| 228 |
324 |
fn remove_window(&mut self, window: Window) -> Result<()> { |
| 229 |
325 |
let initial_count = self.windows.len(); |
| 230 |
326 |
self.windows.retain(|&w| w != window); |
|
327 |
+ self.window_tags.remove(&window); |
| 231 |
328 |
|
| 232 |
329 |
if self.windows.len() < initial_count { |
| 233 |
330 |
println!("Removed window {} from management", window); |
|
|
@@ -243,7 +340,6 @@ impl WindowManager { |
| 243 |
340 |
|
| 244 |
341 |
self.apply_layout()?; |
| 245 |
342 |
} |
| 246 |
|
- |
| 247 |
343 |
Ok(()) |
| 248 |
344 |
} |
| 249 |
345 |
} |