tony / oxwm

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