tony / oxwm

Branch: 53a4fb6
Switch branches/tags
master
emzywastaken inline `window_manager::handle_restart` 53a4fb6 2025-10-13
863 lines · 26.4 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 pub fn run(&mut self) -> Result<bool> {
303 println!("oxwm started on display {}", self.screen_number);
304
305 keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?;
306 self.update_bar()?;
307
308 loop {
309 self.bar.update_blocks()?;
310
311 if let Ok(Some(event)) = self.connection.poll_for_event() {
312 if let Some(should_restart) = self.handle_event(event)? {
313 return Ok(should_restart);
314 }
315 }
316
317 if self.bar.needs_redraw() {
318 self.update_bar()?;
319 }
320
321 std::thread::sleep(std::time::Duration::from_millis(10));
322 }
323 }
324
325 fn toggle_fullscreen(&mut self) -> Result<()> {
326 if let Some(focused) = self.focused_window {
327 if self.fullscreen_window == Some(focused) {
328 self.fullscreen_window = None;
329
330 self.connection.map_window(self.bar.window())?;
331
332 self.apply_layout()?;
333 self.update_focus_visuals()?;
334 } else {
335 self.fullscreen_window = Some(focused);
336
337 self.connection.unmap_window(self.bar.window())?;
338
339 let screen_width = self.screen.width_in_pixels as u32;
340 let screen_height = self.screen.height_in_pixels as u32;
341
342 self.connection.configure_window(
343 focused,
344 &ConfigureWindowAux::new()
345 .x(0)
346 .y(0)
347 .width(screen_width)
348 .height(screen_height)
349 .border_width(0)
350 .stack_mode(StackMode::ABOVE),
351 )?;
352
353 self.connection.flush()?;
354 }
355 }
356 Ok(())
357 }
358
359 fn update_bar(&mut self) -> Result<()> {
360 let mut occupied_tags: TagMask = 0;
361 for &tags in self.window_tags.values() {
362 occupied_tags |= tags;
363 }
364
365 self.bar.invalidate();
366 self.bar
367 .draw(&self.connection, self.selected_tags, occupied_tags)?;
368 Ok(())
369 }
370
371 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
372 match action {
373 KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?,
374 KeyAction::KillClient => {
375 if let Some(focused) = self.focused_window {
376 match self.connection.kill_client(focused) {
377 Ok(_) => {
378 self.connection.flush()?;
379 }
380 Err(e) => {
381 eprintln!("Failed to kill window {}: {}", focused, e);
382 }
383 }
384 }
385 }
386 KeyAction::ToggleFullScreen => {
387 self.toggle_fullscreen()?;
388 }
389 KeyAction::FocusStack => {
390 if let Arg::Int(direction) = arg {
391 self.cycle_focus(*direction)?;
392 }
393 }
394 KeyAction::Quit | KeyAction::Restart => {
395 // Handled in handle_event
396 }
397 KeyAction::Recompile => {
398 match std::process::Command::new("oxwm")
399 .arg("--recompile")
400 .spawn()
401 {
402 Ok(_) => eprintln!("Recompiling in background"),
403 Err(e) => eprintln!("Failed to spawn recompile: {}", e),
404 }
405 }
406 KeyAction::ViewTag => {
407 if let Arg::Int(tag_index) = arg {
408 self.view_tag(*tag_index as usize)?;
409 }
410 }
411 KeyAction::MoveToTag => {
412 if let Arg::Int(tag_index) = arg {
413 self.move_to_tag(*tag_index as usize)?;
414 }
415 }
416 KeyAction::ToggleGaps => {
417 self.gaps_enabled = !self.gaps_enabled;
418 self.apply_layout()?;
419 }
420 KeyAction::None => {}
421 }
422 Ok(())
423 }
424
425 fn is_window_visible(&self, window: Window) -> bool {
426 if let Some(&tags) = self.window_tags.get(&window) {
427 (tags & self.selected_tags) != 0
428 } else {
429 false
430 }
431 }
432
433 fn visible_windows(&self) -> Vec<Window> {
434 self.windows
435 .iter()
436 .filter(|&&w| self.is_window_visible(w))
437 .copied()
438 .collect()
439 }
440
441 fn update_window_visibility(&self) -> Result<()> {
442 for &window in &self.windows {
443 if self.is_window_visible(window) {
444 self.connection.map_window(window)?;
445 } else {
446 self.connection.unmap_window(window)?;
447 }
448 }
449 self.connection.flush()?;
450 Ok(())
451 }
452
453 pub fn view_tag(&mut self, tag_index: usize) -> Result<()> {
454 if tag_index >= self.config.tags.len() {
455 return Ok(());
456 }
457
458 if self.fullscreen_window.is_some() {
459 self.fullscreen_window = None;
460 self.connection.map_window(self.bar.window())?;
461 }
462
463 self.selected_tags = tag_mask(tag_index);
464
465 self.save_selected_tags()?;
466
467 self.update_window_visibility()?;
468 self.apply_layout()?;
469 self.update_bar()?;
470
471 let visible = self.visible_windows();
472 self.set_focus(visible.first().copied())?;
473
474 Ok(())
475 }
476
477 fn save_selected_tags(&self) -> Result<()> {
478 let net_current_desktop = self
479 .connection
480 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
481 .reply()?
482 .atom;
483
484 let desktop = self.selected_tags.trailing_zeros();
485
486 let bytes = (desktop as u32).to_ne_bytes();
487 self.connection.change_property(
488 PropMode::REPLACE,
489 self.root,
490 net_current_desktop,
491 AtomEnum::CARDINAL,
492 32,
493 1,
494 &bytes,
495 )?;
496
497 self.connection.flush()?;
498 Ok(())
499 }
500
501 pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> {
502 if tag_index >= self.config.tags.len() {
503 return Ok(());
504 }
505
506 if let Some(focused) = self.focused_window {
507 let mask = tag_mask(tag_index);
508 self.window_tags.insert(focused, mask);
509
510 let _ = self.save_client_tag(focused, mask);
511
512 self.update_window_visibility()?;
513 self.apply_layout()?;
514 self.update_bar()?;
515 }
516
517 Ok(())
518 }
519
520 pub fn cycle_focus(&mut self, direction: i32) -> Result<()> {
521 let visible = self.visible_windows();
522
523 if visible.is_empty() {
524 return Ok(());
525 }
526
527 let next_window = if let Some(current) = self.focused_window {
528 if let Some(current_index) = visible.iter().position(|&w| w == current) {
529 let next_index = if direction > 0 {
530 (current_index + 1) % visible.len()
531 } else {
532 (current_index + visible.len() - 1) % visible.len()
533 };
534 visible[next_index]
535 } else {
536 visible[0]
537 }
538 } else {
539 visible[0]
540 };
541
542 self.set_focus(Some(next_window))?;
543 Ok(())
544 }
545
546 pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> {
547 self.focused_window = window;
548
549 if let Some(win) = window {
550 self.connection
551 .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
552 self.connection.flush()?;
553 }
554
555 self.update_focus_visuals()?;
556 Ok(())
557 }
558
559 fn update_focus_visuals(&self) -> Result<()> {
560 for &window in &self.windows {
561 let (border_color, border_width) = if self.focused_window == Some(window) {
562 (self.config.border_focused, self.config.border_width)
563 } else {
564 (self.config.border_unfocused, self.config.border_width)
565 };
566
567 self.connection.configure_window(
568 window,
569 &ConfigureWindowAux::new().border_width(border_width),
570 )?;
571
572 self.connection.change_window_attributes(
573 window,
574 &ChangeWindowAttributesAux::new().border_pixel(border_color),
575 )?;
576 }
577 self.connection.flush()?;
578 Ok(())
579 }
580
581 fn move_mouse(&mut self, window: Window) -> Result<()> {
582 self.floating_windows.insert(window);
583
584 let geometry = self.connection.get_geometry(window)?.reply()?;
585
586 self.connection
587 .grab_pointer(
588 false,
589 self.root,
590 (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(),
591 GrabMode::ASYNC,
592 GrabMode::ASYNC,
593 x11rb::NONE,
594 x11rb::NONE,
595 x11rb::CURRENT_TIME,
596 )?
597 .reply()?;
598
599 let pointer = self.connection.query_pointer(self.root)?.reply()?;
600 let (start_x, start_y) = (pointer.root_x, pointer.root_y);
601
602 self.connection.configure_window(
603 window,
604 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
605 )?;
606
607 loop {
608 let event = self.connection.wait_for_event()?;
609 match event {
610 Event::MotionNotify(e) => {
611 let new_x = geometry.x + (e.root_x - start_x);
612 let new_y = geometry.y + (e.root_y - start_y);
613 self.connection.configure_window(
614 window,
615 &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32),
616 )?;
617 self.connection.flush()?;
618 }
619 Event::ButtonRelease(_) => break,
620 _ => {}
621 }
622 }
623
624 self.connection
625 .ungrab_pointer(x11rb::CURRENT_TIME)?
626 .check()?;
627 self.connection
628 .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)?
629 .check()?;
630
631 Ok(())
632 }
633
634 fn resize_mouse(&mut self, window: Window) -> Result<()> {
635 self.floating_windows.insert(window);
636
637 let geometry = self.connection.get_geometry(window)?.reply()?;
638
639 self.connection.warp_pointer(
640 x11rb::NONE,
641 window,
642 0,
643 0,
644 0,
645 0,
646 geometry.width as i16,
647 geometry.height as i16,
648 )?;
649
650 self.connection
651 .grab_pointer(
652 false,
653 self.root,
654 (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(),
655 GrabMode::ASYNC,
656 GrabMode::ASYNC,
657 x11rb::NONE,
658 x11rb::NONE,
659 x11rb::CURRENT_TIME,
660 )?
661 .reply()?;
662
663 self.connection.configure_window(
664 window,
665 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
666 )?;
667
668 loop {
669 let event = self.connection.wait_for_event()?;
670 match event {
671 Event::MotionNotify(e) => {
672 let new_width = (e.root_x - geometry.x).max(1) as u32;
673 let new_height = (e.root_y - geometry.y).max(1) as u32;
674
675 self.connection.configure_window(
676 window,
677 &ConfigureWindowAux::new()
678 .width(new_width)
679 .height(new_height),
680 )?;
681 self.connection.flush()?;
682 }
683 Event::ButtonRelease(_) => break,
684 _ => {}
685 }
686 }
687
688 self.connection
689 .ungrab_pointer(x11rb::CURRENT_TIME)?
690 .check()?;
691 self.connection
692 .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)?
693 .check()?;
694
695 Ok(())
696 }
697
698 fn handle_event(&mut self, event: Event) -> Result<Option<bool>> {
699 match event {
700 Event::MapRequest(event) => {
701 let attrs = match self.connection.get_window_attributes(event.window)?.reply() {
702 Ok(attrs) => attrs,
703 Err(_) => return Ok(None),
704 };
705
706 if attrs.override_redirect {
707 return Ok(None);
708 }
709
710 if self.windows.contains(&event.window) {
711 return Ok(None);
712 }
713
714 self.connection.map_window(event.window)?;
715 self.connection.change_window_attributes(
716 event.window,
717 &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW),
718 )?;
719
720 self.windows.push(event.window);
721 self.window_tags.insert(event.window, self.selected_tags);
722 self.set_wm_state(event.window, 1)?;
723 let _ = self.save_client_tag(event.window, self.selected_tags);
724
725 self.apply_layout()?;
726 self.update_bar()?;
727 self.set_focus(Some(event.window))?;
728 }
729 Event::UnmapNotify(event) => {
730 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
731 self.remove_window(event.window)?;
732 }
733 }
734 Event::DestroyNotify(event) => {
735 if self.windows.contains(&event.window) {
736 self.remove_window(event.window)?;
737 }
738 }
739 Event::EnterNotify(event) => {
740 if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL {
741 return Ok(None);
742 }
743 if self.windows.contains(&event.event) {
744 self.set_focus(Some(event.event))?;
745 }
746 }
747 Event::KeyPress(event) => {
748 let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings)?;
749 match action {
750 KeyAction::Quit => return Ok(Some(false)),
751 KeyAction::Restart => return Ok(Some(true)),
752 _ => self.handle_key_action(action, &arg)?,
753 }
754 }
755 Event::ButtonPress(event) => {
756 if event.event == self.bar.window() {
757 if let Some(tag_index) = self.bar.handle_click(event.event_x) {
758 self.view_tag(tag_index)?;
759 }
760 } else if event.child != x11rb::NONE {
761 self.set_focus(Some(event.child))?;
762
763 if event.detail == ButtonIndex::M1.into() {
764 self.move_mouse(event.child)?;
765 } else if event.detail == ButtonIndex::M3.into() {
766 self.resize_mouse(event.child)?;
767 }
768 }
769 }
770 Event::Expose(event) => {
771 if event.window == self.bar.window() {
772 self.bar.invalidate();
773 self.update_bar()?;
774 }
775 }
776 _ => {}
777 }
778 Ok(None)
779 }
780
781 fn apply_layout(&self) -> Result<()> {
782 if self.fullscreen_window.is_some() {
783 return Ok(());
784 }
785 let screen_width = self.screen.width_in_pixels as u32;
786 let screen_height = self.screen.height_in_pixels as u32;
787 let border_width = self.config.border_width;
788
789 let bar_height = self.bar.height() as u32;
790 let usable_height = screen_height.saturating_sub(bar_height);
791
792 let gaps = if self.gaps_enabled {
793 GapConfig {
794 inner_horizontal: self.config.gap_inner_horizontal,
795 inner_vertical: self.config.gap_inner_vertical,
796 outer_horizontal: self.config.gap_outer_horizontal,
797 outer_vertical: self.config.gap_outer_vertical,
798 }
799 } else {
800 GapConfig {
801 inner_horizontal: 0,
802 inner_vertical: 0,
803 outer_horizontal: 0,
804 outer_vertical: 0,
805 }
806 };
807
808 let visible: Vec<Window> = self
809 .visible_windows()
810 .into_iter()
811 .filter(|w| !self.floating_windows.contains(w))
812 .collect();
813
814 let geometries = self
815 .layout
816 .arrange(&visible, screen_width, usable_height, &gaps);
817
818 for (window, geometry) in visible.iter().zip(geometries.iter()) {
819 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
820 let adjusted_height = geometry.height.saturating_sub(2 * border_width);
821
822 let adjusted_y = geometry.y_coordinate + bar_height as i32;
823
824 self.connection.configure_window(
825 *window,
826 &ConfigureWindowAux::new()
827 .x(geometry.x_coordinate)
828 .y(adjusted_y)
829 .width(adjusted_width)
830 .height(adjusted_height),
831 )?;
832 }
833 self.connection.flush()?;
834 Ok(())
835 }
836
837 fn remove_window(&mut self, window: Window) -> Result<()> {
838 let initial_count = self.windows.len();
839 self.windows.retain(|&w| w != window);
840 self.window_tags.remove(&window);
841 self.floating_windows.remove(&window);
842
843 if self.fullscreen_window == Some(window) {
844 self.fullscreen_window = None;
845 self.connection.map_window(self.bar.window())?;
846 }
847
848 if self.windows.len() < initial_count {
849 if self.focused_window == Some(window) {
850 let new_focus = if self.windows.is_empty() {
851 None
852 } else {
853 Some(self.windows[self.windows.len() - 1])
854 };
855 self.set_focus(new_focus)?;
856 }
857
858 self.apply_layout()?;
859 self.update_bar()?;
860 }
861 Ok(())
862 }
863 }