tony / oxwm

Branch: 23924cf
Switch branches/tags
master
tonybanters removed unneeded functions, fixed recompile function (again?) 23924cf 2025-10-13
867 lines · 26.5 KB Raw Blame History
1 use crate::Config;
2 use crate::bar::Bar;
3 use crate::keyboard::{self, Arg, KeyAction, handlers};
4 use crate::layout::GapConfig;
5 use crate::layout::Layout;
6 use crate::layout::tiling::TilingLayout;
7 use anyhow::Result;
8 use std::collections::HashSet;
9 use std::path::PathBuf;
10 use x11rb::cursor::Handle as CursorHandle;
11
12 use x11rb::connection::Connection;
13 use x11rb::protocol::Event;
14 use x11rb::protocol::xproto::*;
15 use x11rb::rust_connection::RustConnection;
16
17 pub type TagMask = u32;
18 pub fn tag_mask(tag: usize) -> TagMask {
19 1 << tag
20 }
21
22 pub struct WindowManager {
23 config: Config,
24 connection: RustConnection,
25 screen_number: usize,
26 root: Window,
27 screen: Screen,
28 windows: Vec<Window>,
29 focused_window: Option<Window>,
30 layout: Box<dyn Layout>,
31 window_tags: std::collections::HashMap<Window, TagMask>,
32 selected_tags: TagMask,
33 gaps_enabled: bool,
34 fullscreen_window: Option<Window>,
35 floating_windows: HashSet<Window>,
36 bar: Bar,
37 }
38
39 impl WindowManager {
40 pub fn new(config: Config) -> Result<Self> {
41 let (connection, screen_number) = x11rb::connect(None)?;
42 let root = connection.setup().roots[screen_number].root;
43 let screen = connection.setup().roots[screen_number].clone();
44
45 let normal_cursor = CursorHandle::new(
46 &connection,
47 screen_number,
48 &x11rb::resource_manager::new_from_default(&connection)?,
49 )?
50 .reply()?
51 .load_cursor(&connection, "left_ptr")?;
52
53 connection
54 .change_window_attributes(
55 root,
56 &ChangeWindowAttributesAux::new()
57 .cursor(normal_cursor)
58 .event_mask(
59 EventMask::SUBSTRUCTURE_REDIRECT
60 | EventMask::SUBSTRUCTURE_NOTIFY
61 | EventMask::PROPERTY_CHANGE
62 | EventMask::KEY_PRESS
63 | EventMask::BUTTON_PRESS,
64 ),
65 )?
66 .check()?;
67
68 connection.grab_button(
69 false,
70 root,
71 EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
72 GrabMode::SYNC,
73 GrabMode::ASYNC,
74 x11rb::NONE,
75 x11rb::NONE,
76 ButtonIndex::M1,
77 u16::from(config.modkey).into(),
78 )?;
79
80 connection.grab_button(
81 false,
82 root,
83 EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
84 GrabMode::SYNC,
85 GrabMode::ASYNC,
86 x11rb::NONE,
87 x11rb::NONE,
88 ButtonIndex::M3,
89 u16::from(config.modkey).into(),
90 )?;
91
92 let bar = Bar::new(&connection, &screen, screen_number, &config)?;
93
94 let selected_tags = Self::get_saved_selected_tags(&connection, root, config.tags.len())?;
95 let gaps_enabled = config.gaps_enabled;
96
97 let mut window_manager = Self {
98 config,
99 connection,
100 screen_number,
101 root,
102 screen,
103 windows: Vec::new(),
104 focused_window: None,
105 layout: Box::new(TilingLayout),
106 window_tags: std::collections::HashMap::new(),
107 selected_tags,
108 gaps_enabled,
109 fullscreen_window: None,
110 floating_windows: HashSet::new(),
111 bar,
112 };
113
114 window_manager.scan_existing_windows()?;
115 window_manager.update_bar()?;
116
117 Ok(window_manager)
118 }
119
120 fn get_saved_selected_tags(
121 connection: &RustConnection,
122 root: Window,
123 tag_count: usize,
124 ) -> Result<TagMask> {
125 let net_current_desktop = connection
126 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
127 .reply()?
128 .atom;
129
130 match connection
131 .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)?
132 .reply()
133 {
134 Ok(prop) if prop.value.len() >= 4 => {
135 let desktop = u32::from_ne_bytes([
136 prop.value[0],
137 prop.value[1],
138 prop.value[2],
139 prop.value[3],
140 ]);
141 if desktop < tag_count as u32 {
142 let mask = tag_mask(desktop as usize);
143 return Ok(mask);
144 }
145 }
146 Ok(_) => {}
147 Err(e) => {
148 eprintln!("No _NET_CURRENT_DESKTOP property ({})", e);
149 }
150 }
151 Ok(tag_mask(0))
152 }
153
154 fn scan_existing_windows(&mut self) -> Result<()> {
155 let tree = self.connection.query_tree(self.root)?.reply()?;
156 let net_client_info = self
157 .connection
158 .intern_atom(false, b"_NET_CLIENT_INFO")?
159 .reply()?
160 .atom;
161
162 let wm_state_atom = self
163 .connection
164 .intern_atom(false, b"WM_STATE")?
165 .reply()?
166 .atom;
167
168 for &window in &tree.children {
169 if window == self.bar.window() {
170 continue;
171 }
172
173 let attrs = match self.connection.get_window_attributes(window)?.reply() {
174 Ok(attrs) => attrs,
175 Err(_) => continue,
176 };
177
178 if attrs.override_redirect {
179 continue;
180 }
181
182 if attrs.map_state == MapState::VIEWABLE {
183 let tag = self.get_saved_tag(window, net_client_info)?;
184 self.windows.push(window);
185 self.window_tags.insert(window, tag);
186 continue;
187 }
188
189 if attrs.map_state == MapState::UNMAPPED {
190 let has_wm_state = match self
191 .connection
192 .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)?
193 .reply()
194 {
195 Ok(prop) => !prop.value.is_empty(),
196 Err(_) => false,
197 };
198
199 if !has_wm_state {
200 continue;
201 }
202
203 let has_wm_class = match self
204 .connection
205 .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)?
206 .reply()
207 {
208 Ok(prop) => !prop.value.is_empty(),
209 Err(_) => false,
210 };
211
212 if has_wm_class {
213 let tag = self.get_saved_tag(window, net_client_info)?;
214 self.windows.push(window);
215 self.window_tags.insert(window, tag);
216 }
217 }
218 }
219
220 if let Some(&first) = self.windows.first() {
221 self.set_focus(Some(first))?;
222 }
223
224 self.apply_layout()?;
225 Ok(())
226 }
227
228 fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> Result<TagMask> {
229 match self
230 .connection
231 .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)?
232 .reply()
233 {
234 Ok(prop) if prop.value.len() >= 4 => {
235 let tags = u32::from_ne_bytes([
236 prop.value[0],
237 prop.value[1],
238 prop.value[2],
239 prop.value[3],
240 ]);
241
242 if tags != 0 && tags < (1 << self.config.tags.len()) {
243 return Ok(tags);
244 }
245 }
246 Ok(_) => {}
247 Err(e) => {
248 eprintln!("No _NET_CLIENT_INFO property ({})", e);
249 }
250 }
251
252 Ok(self.selected_tags)
253 }
254
255 fn save_client_tag(&self, window: Window, tag: TagMask) -> Result<()> {
256 let net_client_info = self
257 .connection
258 .intern_atom(false, b"_NET_CLIENT_INFO")?
259 .reply()?
260 .atom;
261
262 let bytes = tag.to_ne_bytes().to_vec();
263
264 self.connection.change_property(
265 PropMode::REPLACE,
266 window,
267 net_client_info,
268 AtomEnum::CARDINAL,
269 32,
270 1,
271 &bytes,
272 )?;
273
274 self.connection.flush()?;
275 Ok(())
276 }
277
278 fn set_wm_state(&self, window: Window, state: u32) -> Result<()> {
279 let wm_state_atom = self
280 .connection
281 .intern_atom(false, b"WM_STATE")?
282 .reply()?
283 .atom;
284
285 let data = [state, 0u32];
286 let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect();
287
288 self.connection.change_property(
289 PropMode::REPLACE,
290 window,
291 wm_state_atom,
292 wm_state_atom,
293 32,
294 2,
295 &bytes,
296 )?;
297
298 self.connection.flush()?;
299 Ok(())
300 }
301
302 fn handle_restart(&self) -> Result<bool> {
303 Ok(true)
304 }
305
306 pub fn run(&mut self) -> Result<bool> {
307 println!("oxwm started on display {}", self.screen_number);
308
309 keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?;
310 self.update_bar()?;
311
312 loop {
313 self.bar.update_blocks()?;
314
315 if let Ok(Some(event)) = self.connection.poll_for_event() {
316 if let Some(should_restart) = self.handle_event(event)? {
317 return Ok(should_restart);
318 }
319 }
320
321 if self.bar.needs_redraw() {
322 self.update_bar()?;
323 }
324
325 std::thread::sleep(std::time::Duration::from_millis(10));
326 }
327 }
328
329 fn toggle_fullscreen(&mut self) -> Result<()> {
330 if let Some(focused) = self.focused_window {
331 if self.fullscreen_window == Some(focused) {
332 self.fullscreen_window = None;
333
334 self.connection.map_window(self.bar.window())?;
335
336 self.apply_layout()?;
337 self.update_focus_visuals()?;
338 } else {
339 self.fullscreen_window = Some(focused);
340
341 self.connection.unmap_window(self.bar.window())?;
342
343 let screen_width = self.screen.width_in_pixels as u32;
344 let screen_height = self.screen.height_in_pixels as u32;
345
346 self.connection.configure_window(
347 focused,
348 &ConfigureWindowAux::new()
349 .x(0)
350 .y(0)
351 .width(screen_width)
352 .height(screen_height)
353 .border_width(0)
354 .stack_mode(StackMode::ABOVE),
355 )?;
356
357 self.connection.flush()?;
358 }
359 }
360 Ok(())
361 }
362
363 fn update_bar(&mut self) -> Result<()> {
364 let mut occupied_tags: TagMask = 0;
365 for &tags in self.window_tags.values() {
366 occupied_tags |= tags;
367 }
368
369 self.bar.invalidate();
370 self.bar
371 .draw(&self.connection, self.selected_tags, occupied_tags)?;
372 Ok(())
373 }
374
375 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
376 match action {
377 KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?,
378 KeyAction::KillClient => {
379 if let Some(focused) = self.focused_window {
380 match self.connection.kill_client(focused) {
381 Ok(_) => {
382 self.connection.flush()?;
383 }
384 Err(e) => {
385 eprintln!("Failed to kill window {}: {}", focused, e);
386 }
387 }
388 }
389 }
390 KeyAction::ToggleFullScreen => {
391 self.toggle_fullscreen()?;
392 }
393 KeyAction::FocusStack => {
394 if let Arg::Int(direction) = arg {
395 self.cycle_focus(*direction)?;
396 }
397 }
398 KeyAction::Quit | KeyAction::Restart => {
399 // Handled in handle_event
400 }
401 KeyAction::Recompile => {
402 match std::process::Command::new("oxwm")
403 .arg("--recompile")
404 .spawn()
405 {
406 Ok(_) => eprintln!("Recompiling in background"),
407 Err(e) => eprintln!("Failed to spawn recompile: {}", e),
408 }
409 }
410 KeyAction::ViewTag => {
411 if let Arg::Int(tag_index) = arg {
412 self.view_tag(*tag_index as usize)?;
413 }
414 }
415 KeyAction::MoveToTag => {
416 if let Arg::Int(tag_index) = arg {
417 self.move_to_tag(*tag_index as usize)?;
418 }
419 }
420 KeyAction::ToggleGaps => {
421 self.gaps_enabled = !self.gaps_enabled;
422 self.apply_layout()?;
423 }
424 KeyAction::None => {}
425 }
426 Ok(())
427 }
428
429 fn is_window_visible(&self, window: Window) -> bool {
430 if let Some(&tags) = self.window_tags.get(&window) {
431 (tags & self.selected_tags) != 0
432 } else {
433 false
434 }
435 }
436
437 fn visible_windows(&self) -> Vec<Window> {
438 self.windows
439 .iter()
440 .filter(|&&w| self.is_window_visible(w))
441 .copied()
442 .collect()
443 }
444
445 fn update_window_visibility(&self) -> Result<()> {
446 for &window in &self.windows {
447 if self.is_window_visible(window) {
448 self.connection.map_window(window)?;
449 } else {
450 self.connection.unmap_window(window)?;
451 }
452 }
453 self.connection.flush()?;
454 Ok(())
455 }
456
457 pub fn view_tag(&mut self, tag_index: usize) -> Result<()> {
458 if tag_index >= self.config.tags.len() {
459 return Ok(());
460 }
461
462 if self.fullscreen_window.is_some() {
463 self.fullscreen_window = None;
464 self.connection.map_window(self.bar.window())?;
465 }
466
467 self.selected_tags = tag_mask(tag_index);
468
469 self.save_selected_tags()?;
470
471 self.update_window_visibility()?;
472 self.apply_layout()?;
473 self.update_bar()?;
474
475 let visible = self.visible_windows();
476 self.set_focus(visible.first().copied())?;
477
478 Ok(())
479 }
480
481 fn save_selected_tags(&self) -> Result<()> {
482 let net_current_desktop = self
483 .connection
484 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
485 .reply()?
486 .atom;
487
488 let desktop = self.selected_tags.trailing_zeros();
489
490 let bytes = (desktop as u32).to_ne_bytes();
491 self.connection.change_property(
492 PropMode::REPLACE,
493 self.root,
494 net_current_desktop,
495 AtomEnum::CARDINAL,
496 32,
497 1,
498 &bytes,
499 )?;
500
501 self.connection.flush()?;
502 Ok(())
503 }
504
505 pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> {
506 if tag_index >= self.config.tags.len() {
507 return Ok(());
508 }
509
510 if let Some(focused) = self.focused_window {
511 let mask = tag_mask(tag_index);
512 self.window_tags.insert(focused, mask);
513
514 let _ = self.save_client_tag(focused, mask);
515
516 self.update_window_visibility()?;
517 self.apply_layout()?;
518 self.update_bar()?;
519 }
520
521 Ok(())
522 }
523
524 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
525 let visible = self.visible_windows();
526
527 if visible.is_empty() {
528 return Ok(());
529 }
530
531 let next_window = if let Some(current) = self.focused_window {
532 if let Some(current_index) = visible.iter().position(|&w| w == current) {
533 let next_index = if direction > 0 {
534 (current_index + 1) % visible.len()
535 } else {
536 (current_index + visible.len() - 1) % visible.len()
537 };
538 visible[next_index]
539 } else {
540 visible[0]
541 }
542 } else {
543 visible[0]
544 };
545
546 self.set_focus(Some(next_window))?;
547 Ok(())
548 }
549
550 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
551 self.focused_window = window;
552
553 if let Some(win) = window {
554 self.connection
555 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
556 self.connection.flush()?;
557 }
558
559 self.update_focus_visuals()?;
560 Ok(())
561 }
562
563 fn update_focus_visuals(&self) -> Result<()> {
564 for &window in &self.windows {
565 let (border_color, border_width) = if self.focused_window == Some(window) {
566 (self.config.border_focused, self.config.border_width)
567 } else {
568 (self.config.border_unfocused, self.config.border_width)
569 };
570
571 self.connection.configure_window(
572 window,
573 &ConfigureWindowAux::new().border_width(border_width),
574 )?;
575
576 self.connection.change_window_attributes(
577 window,
578 &ChangeWindowAttributesAux::new().border_pixel(border_color),
579 )?;
580 }
581 self.connection.flush()?;
582 Ok(())
583 }
584
585 fn move_mouse(&mut self, window: Window) -> Result<()> {
586 self.floating_windows.insert(window);
587
588 let geometry = self.connection.get_geometry(window)?.reply()?;
589
590 self.connection
591 .grab_pointer(
592 false,
593 self.root,
594 (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(),
595 GrabMode::ASYNC,
596 GrabMode::ASYNC,
597 x11rb::NONE,
598 x11rb::NONE,
599 x11rb::CURRENT_TIME,
600 )?
601 .reply()?;
602
603 let pointer = self.connection.query_pointer(self.root)?.reply()?;
604 let (start_x, start_y) = (pointer.root_x, pointer.root_y);
605
606 self.connection.configure_window(
607 window,
608 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
609 )?;
610
611 loop {
612 let event = self.connection.wait_for_event()?;
613 match event {
614 Event::MotionNotify(e) => {
615 let new_x = geometry.x + (e.root_x - start_x);
616 let new_y = geometry.y + (e.root_y - start_y);
617 self.connection.configure_window(
618 window,
619 &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32),
620 )?;
621 self.connection.flush()?;
622 }
623 Event::ButtonRelease(_) => break,
624 _ => {}
625 }
626 }
627
628 self.connection
629 .ungrab_pointer(x11rb::CURRENT_TIME)?
630 .check()?;
631 self.connection
632 .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)?
633 .check()?;
634
635 Ok(())
636 }
637
638 fn resize_mouse(&mut self, window: Window) -> Result<()> {
639 self.floating_windows.insert(window);
640
641 let geometry = self.connection.get_geometry(window)?.reply()?;
642
643 self.connection.warp_pointer(
644 x11rb::NONE,
645 window,
646 0,
647 0,
648 0,
649 0,
650 geometry.width as i16,
651 geometry.height as i16,
652 )?;
653
654 self.connection
655 .grab_pointer(
656 false,
657 self.root,
658 (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(),
659 GrabMode::ASYNC,
660 GrabMode::ASYNC,
661 x11rb::NONE,
662 x11rb::NONE,
663 x11rb::CURRENT_TIME,
664 )?
665 .reply()?;
666
667 self.connection.configure_window(
668 window,
669 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
670 )?;
671
672 loop {
673 let event = self.connection.wait_for_event()?;
674 match event {
675 Event::MotionNotify(e) => {
676 let new_width = (e.root_x - geometry.x).max(1) as u32;
677 let new_height = (e.root_y - geometry.y).max(1) as u32;
678
679 self.connection.configure_window(
680 window,
681 &ConfigureWindowAux::new()
682 .width(new_width)
683 .height(new_height),
684 )?;
685 self.connection.flush()?;
686 }
687 Event::ButtonRelease(_) => break,
688 _ => {}
689 }
690 }
691
692 self.connection
693 .ungrab_pointer(x11rb::CURRENT_TIME)?
694 .check()?;
695 self.connection
696 .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)?
697 .check()?;
698
699 Ok(())
700 }
701
702 fn handle_event(&mut self, event: Event) -> Result<Option<bool>> {
703 match event {
704 Event::MapRequest(event) => {
705 let attrs = match self.connection.get_window_attributes(event.window)?.reply() {
706 Ok(attrs) => attrs,
707 Err(_) => return Ok(None),
708 };
709
710 if attrs.override_redirect {
711 return Ok(None);
712 }
713
714 if self.windows.contains(&event.window) {
715 return Ok(None);
716 }
717
718 self.connection.map_window(event.window)?;
719 self.connection.change_window_attributes(
720 event.window,
721 &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW),
722 )?;
723
724 self.windows.push(event.window);
725 self.window_tags.insert(event.window, self.selected_tags);
726 self.set_wm_state(event.window, 1)?;
727 let _ = self.save_client_tag(event.window, self.selected_tags);
728
729 self.apply_layout()?;
730 self.update_bar()?;
731 self.set_focus(Some(event.window))?;
732 }
733 Event::UnmapNotify(event) => {
734 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
735 self.remove_window(event.window)?;
736 }
737 }
738 Event::DestroyNotify(event) => {
739 if self.windows.contains(&event.window) {
740 self.remove_window(event.window)?;
741 }
742 }
743 Event::EnterNotify(event) => {
744 if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL {
745 return Ok(None);
746 }
747 if self.windows.contains(&event.event) {
748 self.set_focus(Some(event.event))?;
749 }
750 }
751 Event::KeyPress(event) => {
752 let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings)?;
753 match action {
754 KeyAction::Quit => return Ok(Some(false)),
755 KeyAction::Restart => return Ok(Some(self.handle_restart()?)),
756 _ => self.handle_key_action(action, &arg)?,
757 }
758 }
759 Event::ButtonPress(event) => {
760 if event.event == self.bar.window() {
761 if let Some(tag_index) = self.bar.handle_click(event.event_x) {
762 self.view_tag(tag_index)?;
763 }
764 } else if event.child != x11rb::NONE {
765 self.set_focus(Some(event.child))?;
766
767 if event.detail == ButtonIndex::M1.into() {
768 self.move_mouse(event.child)?;
769 } else if event.detail == ButtonIndex::M3.into() {
770 self.resize_mouse(event.child)?;
771 }
772 }
773 }
774 Event::Expose(event) => {
775 if event.window == self.bar.window() {
776 self.bar.invalidate();
777 self.update_bar()?;
778 }
779 }
780 _ => {}
781 }
782 Ok(None)
783 }
784
785 fn apply_layout(&self) -> Result<()> {
786 if self.fullscreen_window.is_some() {
787 return Ok(());
788 }
789 let screen_width = self.screen.width_in_pixels as u32;
790 let screen_height = self.screen.height_in_pixels as u32;
791 let border_width = self.config.border_width;
792
793 let bar_height = self.bar.height() as u32;
794 let usable_height = screen_height.saturating_sub(bar_height);
795
796 let gaps = if self.gaps_enabled {
797 GapConfig {
798 inner_horizontal: self.config.gap_inner_horizontal,
799 inner_vertical: self.config.gap_inner_vertical,
800 outer_horizontal: self.config.gap_outer_horizontal,
801 outer_vertical: self.config.gap_outer_vertical,
802 }
803 } else {
804 GapConfig {
805 inner_horizontal: 0,
806 inner_vertical: 0,
807 outer_horizontal: 0,
808 outer_vertical: 0,
809 }
810 };
811
812 let visible: Vec<Window> = self
813 .visible_windows()
814 .into_iter()
815 .filter(|w| !self.floating_windows.contains(w))
816 .collect();
817
818 let geometries = self
819 .layout
820 .arrange(&visible, screen_width, usable_height, &gaps);
821
822 for (window, geometry) in visible.iter().zip(geometries.iter()) {
823 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
824 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
825
826 let adjusted_y = geometry.y_coordinate + bar_height as i32;
827
828 self.connection.configure_window(
829 *window,
830 &ConfigureWindowAux::new()
831 .x(geometry.x_coordinate)
832 .y(adjusted_y)
833 .width(adjusted_width)
834 .height(adjusted_height),
835 )?;
836 }
837 self.connection.flush()?;
838 Ok(())
839 }
840
841 fn remove_window(&mut self, window: Window) -> Result<()> {
842 let initial_count = self.windows.len();
843 self.windows.retain(|&w| w != window);
844 self.window_tags.remove(&window);
845 self.floating_windows.remove(&window);
846
847 if self.fullscreen_window == Some(window) {
848 self.fullscreen_window = None;
849 self.connection.map_window(self.bar.window())?;
850 }
851
852 if self.windows.len() < initial_count {
853 if self.focused_window == Some(window) {
854 let new_focus = if self.windows.is_empty() {
855 None
856 } else {
857 Some(self.windows[self.windows.len() - 1])
858 };
859 self.set_focus(new_focus)?;
860 }
861
862 self.apply_layout()?;
863 self.update_bar()?;
864 }
865 Ok(())
866 }
867 }