tony / oxwm

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