tony / oxwm

Added smartmovewin() and exchangeclient() functions Browse files
tonybtw committed 2025-10-25 parent d7af0b4 commit c92066c248ea74a0ffa96f62c1f16c91908f470f
Showing 4 changed files with 374 additions and 0 deletions. UnifiedSplit
+2 −0 src/keyboard/handlers.rs
@@ -23,6 +23,8 @@ pub enum KeyAction {
23 23 CycleLayout,
24 24 MoveToTag,
25 25 FocusMonitor,
26 + SmartMoveWin,
27 + ExchangeClient,
26 28 None,
27 29 }
28 30
+24 −0 src/lib.rs
@@ -132,6 +132,30 @@ impl Default for Config {
132 132 KeyAction::FocusStack,
133 133 Arg::Int(1),
134 134 ),
135 + Key::new(
136 + vec![MODKEY, SHIFT],
137 + keycodes::K,
138 + KeyAction::ExchangeClient,
139 + Arg::Int(0), // UP
140 + ),
141 + Key::new(
142 + vec![MODKEY, SHIFT],
143 + keycodes::J,
144 + KeyAction::ExchangeClient,
145 + Arg::Int(1), // DOWN
146 + ),
147 + Key::new(
148 + vec![MODKEY, SHIFT],
149 + keycodes::H,
150 + KeyAction::ExchangeClient,
151 + Arg::Int(2), // LEFT
152 + ),
153 + Key::new(
154 + vec![MODKEY, SHIFT],
155 + keycodes::L,
156 + KeyAction::ExchangeClient,
157 + Arg::Int(3), // RIGHT
158 + ),
135 159 Key::new(
136 160 vec![MODKEY],
137 161 keycodes::KEY_1,
+343 −0 src/window_manager.rs
@@ -411,6 +411,337 @@ impl WindowManager {
411 411 Ok(())
412 412 }
413 413
414 + fn smart_move_window(&mut self, direction: i32) -> WmResult<()> {
415 + let focused = match self
416 + .monitors
417 + .get(self.selected_monitor)
418 + .and_then(|m| m.focused_window)
419 + {
420 + Some(win) => win,
421 + None => return Ok(()),
422 + };
423 +
424 + if self.fullscreen_window == Some(focused) {
425 + return Ok(());
426 + }
427 +
428 + if !self.floating_windows.contains(&focused) {
429 + let float_width = (self.screen.width_in_pixels / 2) as u32;
430 + let float_height = (self.screen.height_in_pixels / 2) as u32;
431 + let border_width = self.config.border_width;
432 + let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32;
433 + let center_height = ((self.screen.height_in_pixels - float_height as u16) / 2) as i32;
434 +
435 + self.connection.configure_window(
436 + focused,
437 + &ConfigureWindowAux::new()
438 + .x(center_width)
439 + .y(center_height)
440 + .width(float_width)
441 + .height(float_height)
442 + .border_width(border_width)
443 + .stack_mode(StackMode::ABOVE),
444 + )?;
445 + self.floating_windows.insert(focused);
446 + }
447 +
448 + let current_geom = match self.connection.get_geometry(focused)?.reply() {
449 + Ok(geom) => geom,
450 + Err(_) => return Ok(()),
451 + };
452 +
453 + let c_x = current_geom.x as i32;
454 + let c_y = current_geom.y as i32;
455 + let c_width = current_geom.width as i32;
456 + let c_height = current_geom.height as i32;
457 +
458 + let monitor = match self.monitors.get(self.selected_monitor) {
459 + Some(m) => m,
460 + None => return Ok(()),
461 + };
462 +
463 + let (gap_ih, gap_iv, gap_oh, gap_ov) = if self.gaps_enabled {
464 + (
465 + self.config.gap_inner_horizontal as i32,
466 + self.config.gap_inner_vertical as i32,
467 + self.config.gap_outer_horizontal as i32,
468 + self.config.gap_outer_vertical as i32,
469 + )
470 + } else {
471 + (0, 0, 0, 0)
472 + };
473 +
474 + let (new_x, new_y) = match direction {
475 + 0 => {
476 + // UP
477 + let mut target = i32::MIN;
478 + let top = c_y;
479 + let mut ny = c_y - (monitor.height as i32 / 4);
480 +
481 + for &other_window in &self.windows {
482 + if other_window == focused {
483 + continue;
484 + }
485 + if !self.floating_windows.contains(&other_window) {
486 + continue;
487 + }
488 + if !self.is_window_visible(other_window) {
489 + continue;
490 + }
491 + let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
492 + if other_mon != self.selected_monitor {
493 + continue;
494 + }
495 +
496 + let other_geom = match self.connection.get_geometry(other_window)?.reply() {
497 + Ok(geom) => geom,
498 + Err(_) => continue,
499 + };
500 +
501 + let o_x = other_geom.x as i32;
502 + let o_y = other_geom.y as i32;
503 + let o_width = other_geom.width as i32;
504 + let o_height = other_geom.height as i32;
505 +
506 + if c_x + c_width < o_x || c_x > o_x + o_width {
507 + continue;
508 + }
509 +
510 + let bottom = o_y + o_height + gap_iv;
511 + if top > bottom && ny < bottom {
512 + target = target.max(bottom);
513 + }
514 + }
515 +
516 + if target != i32::MIN {
517 + ny = target;
518 + }
519 + ny = ny.max(monitor.y as i32 + gap_ov);
520 + (c_x, ny)
521 + }
522 + 1 => {
523 + // DOWN
524 + let mut target = i32::MAX;
525 + let bottom = c_y + c_height;
526 + let mut ny = c_y + (monitor.height as i32 / 4);
527 +
528 + for &other_window in &self.windows {
529 + if other_window == focused {
530 + continue;
531 + }
532 + if !self.floating_windows.contains(&other_window) {
533 + continue;
534 + }
535 + if !self.is_window_visible(other_window) {
536 + continue;
537 + }
538 + let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
539 + if other_mon != self.selected_monitor {
540 + continue;
541 + }
542 +
543 + let other_geom = match self.connection.get_geometry(other_window)?.reply() {
544 + Ok(geom) => geom,
545 + Err(_) => continue,
546 + };
547 +
548 + let o_x = other_geom.x as i32;
549 + let o_y = other_geom.y as i32;
550 + let o_width = other_geom.width as i32;
551 +
552 + if c_x + c_width < o_x || c_x > o_x + o_width {
553 + continue;
554 + }
555 +
556 + let top = o_y - gap_iv;
557 + if bottom < top && (ny + c_height) > top {
558 + target = target.min(top - c_height);
559 + }
560 + }
561 +
562 + if target != i32::MAX {
563 + ny = target;
564 + }
565 + ny = ny.min(monitor.y as i32 + monitor.height as i32 - c_height - gap_ov);
566 + (c_x, ny)
567 + }
568 + 2 => {
569 + // LEFT
570 + let mut target = i32::MIN;
571 + let left = c_x;
572 + let mut nx = c_x - (monitor.width as i32 / 6);
573 +
574 + for &other_window in &self.windows {
575 + if other_window == focused {
576 + continue;
577 + }
578 + if !self.floating_windows.contains(&other_window) {
579 + continue;
580 + }
581 + if !self.is_window_visible(other_window) {
582 + continue;
583 + }
584 + let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
585 + if other_mon != self.selected_monitor {
586 + continue;
587 + }
588 +
589 + let other_geom = match self.connection.get_geometry(other_window)?.reply() {
590 + Ok(geom) => geom,
591 + Err(_) => continue,
592 + };
593 +
594 + let o_x = other_geom.x as i32;
595 + let o_y = other_geom.y as i32;
596 + let o_width = other_geom.width as i32;
597 + let o_height = other_geom.height as i32;
598 +
599 + if c_y + c_height < o_y || c_y > o_y + o_height {
600 + continue;
601 + }
602 +
603 + let right = o_x + o_width + gap_ih;
604 + if left > right && nx < right {
605 + target = target.max(right);
606 + }
607 + }
608 +
609 + if target != i32::MIN {
610 + nx = target;
611 + }
612 + nx = nx.max(monitor.x as i32 + gap_oh);
613 + (nx, c_y)
614 + }
615 + 3 => {
616 + // RIGHT
617 + let mut target = i32::MAX;
618 + let right = c_x + c_width;
619 + let mut nx = c_x + (monitor.width as i32 / 6);
620 +
621 + for &other_window in &self.windows {
622 + if other_window == focused {
623 + continue;
624 + }
625 + if !self.floating_windows.contains(&other_window) {
626 + continue;
627 + }
628 + if !self.is_window_visible(other_window) {
629 + continue;
630 + }
631 + let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0);
632 + if other_mon != self.selected_monitor {
633 + continue;
634 + }
635 +
636 + let other_geom = match self.connection.get_geometry(other_window)?.reply() {
637 + Ok(geom) => geom,
638 + Err(_) => continue,
639 + };
640 +
641 + let o_x = other_geom.x as i32;
642 + let o_y = other_geom.y as i32;
643 + let o_height = other_geom.height as i32;
644 +
645 + if c_y + c_height < o_y || c_y > o_y + o_height {
646 + continue;
647 + }
648 +
649 + let left = o_x - gap_ih;
650 + if right < left && (nx + c_width) > left {
651 + target = target.min(left - c_width);
652 + }
653 + }
654 +
655 + if target != i32::MAX {
656 + nx = target;
657 + }
658 + nx = nx.min(monitor.x as i32 + monitor.width as i32 - c_width - gap_oh);
659 + (nx, c_y)
660 + }
661 + _ => return Ok(()),
662 + };
663 +
664 + self.connection.configure_window(
665 + focused,
666 + &ConfigureWindowAux::new()
667 + .x(new_x)
668 + .y(new_y)
669 + .stack_mode(StackMode::ABOVE),
670 + )?;
671 +
672 + self.connection.flush()?;
673 + Ok(())
674 + }
675 +
676 + fn exchange_client(&mut self, direction: i32) -> WmResult<()> {
677 + let focused = match self
678 + .monitors
679 + .get(self.selected_monitor)
680 + .and_then(|m| m.focused_window)
681 + {
682 + Some(win) => win,
683 + None => return Ok(()),
684 + };
685 +
686 + if self.fullscreen_window == Some(focused) || self.floating_windows.contains(&focused) {
687 + return Ok(());
688 + }
689 +
690 + let visible = self.visible_windows();
691 + if visible.len() < 2 {
692 + return Ok(());
693 + }
694 +
695 + let current_idx = match visible.iter().position(|&w| w == focused) {
696 + Some(idx) => idx,
697 + None => return Ok(()),
698 + };
699 +
700 + let target_idx = match direction {
701 + 0 | 2 => {
702 + // UP or LEFT - previous in stack
703 + if current_idx == 0 {
704 + visible.len() - 1
705 + } else {
706 + current_idx - 1
707 + }
708 + }
709 + 1 | 3 => {
710 + // DOWN or RIGHT - next in stack
711 + (current_idx + 1) % visible.len()
712 + }
713 + _ => return Ok(()),
714 + };
715 +
716 + let target = visible[target_idx];
717 +
718 + let focused_pos = self.windows.iter().position(|&w| w == focused);
719 + let target_pos = self.windows.iter().position(|&w| w == target);
720 +
721 + if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) {
722 + self.windows.swap(f_pos, t_pos);
723 +
724 + self.apply_layout()?;
725 +
726 + self.set_focus(focused)?;
727 +
728 + if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() {
729 + self.connection.warp_pointer(
730 + x11rb::NONE,
731 + focused,
732 + 0,
733 + 0,
734 + 0,
735 + 0,
736 + geometry.width as i16 / 2,
737 + geometry.height as i16 / 2,
738 + )?;
739 + }
740 + }
741 +
742 + Ok(())
743 + }
744 +
414 745 fn toggle_fullscreen(&mut self) -> WmResult<()> {
415 746 if let Some(focused) = self
416 747 .monitors
@@ -526,6 +857,18 @@ impl WindowManager {
526 857 self.toggle_floating()?;
527 858 }
528 859
860 + KeyAction::SmartMoveWin => {
861 + if let Arg::Int(direction) = arg {
862 + self.smart_move_window(*direction)?;
863 + }
864 + }
865 +
866 + KeyAction::ExchangeClient => {
867 + if let Arg::Int(direction) = arg {
868 + self.exchange_client(*direction)?;
869 + }
870 + }
871 +
529 872 KeyAction::FocusStack => {
530 873 if let Arg::Int(direction) = arg {
531 874 self.cycle_focus(*direction)?;
+5 −0 test-config.ron
@@ -40,6 +40,11 @@
40 40 (modifiers: [Mod1, Shift], key: R, action: Restart),
41 41 (modifiers: [Mod1], key: J, action: FocusStack, arg: -1),
42 42 (modifiers: [Mod1], key: K, action: FocusStack, arg: 1),
43 + // Exchange client (vim-style with Mod+Shift)
44 + (modifiers: [Mod1, Shift], key: K, action: ExchangeClient, arg: 0), // UP
45 + (modifiers: [Mod1, Shift], key: J, action: ExchangeClient, arg: 1), // DOWN
46 + (modifiers: [Mod1, Shift], key: H, action: ExchangeClient, arg: 2), // LEFT
47 + (modifiers: [Mod1, Shift], key: L, action: ExchangeClient, arg: 3), // RIGHT
43 48 (modifiers: [Mod1], key: Key1, action: ViewTag, arg: 0),
44 49 (modifiers: [Mod1], key: Key2, action: ViewTag, arg: 1),
45 50 (modifiers: [Mod1], key: Key3, action: ViewTag, arg: 2),