tony / oxwm

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