Skip to content

Commit

Permalink
fix(wm): cross-border focus direction awareness
Browse files Browse the repository at this point in the history
This commit ensures that when focusing across a monitor boundary to the
left, the container at the back of the Ring<Container> in the target
workspace will be focused, and when focusing across a monitor boundary
to the right, the one at the front will be focused.
  • Loading branch information
LGUG2Z committed Sep 23, 2024
1 parent 3720ce4 commit c3f1357
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ impl WindowManager {

let mut cross_monitor_monocle = false;

// this is for when we are scrolling across workspaces like PaperWM
if new_idx.is_none()
&& matches!(
self.cross_boundary_behaviour,
Expand Down Expand Up @@ -1289,6 +1290,22 @@ impl WindowManager {

self.focus_workspace(next_idx)?;

if let Ok(focused_workspace) = self.focused_workspace_mut() {
if focused_workspace.monocle_container().is_none() {
match direction {
OperationDirection::Left => {
focused_workspace.focus_container(
focused_workspace.containers().len().saturating_sub(1),
);
}
OperationDirection::Right => {
focused_workspace.focus_container(0);
}
_ => {}
};
}
}

return Ok(());
}

Expand All @@ -1300,17 +1317,30 @@ impl WindowManager {
.ok_or_else(|| anyhow!("there is no container or monitor in this direction"))?;

self.focus_monitor(monitor_idx)?;
let mouse_follows_focus = self.mouse_follows_focus;

if let Ok(focused_workspace) = self.focused_workspace() {
if let Ok(focused_workspace) = self.focused_workspace_mut() {
if let Some(monocle) = focused_workspace.monocle_container() {
if let Some(window) = monocle.focused_window() {
window.focus(self.mouse_follows_focus)?;
window.focus(mouse_follows_focus)?;
WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect(
window.hwnd(),
)?)?;

cross_monitor_monocle = true;
}
} else {
match direction {
OperationDirection::Left => {
focused_workspace.focus_container(
focused_workspace.containers().len().saturating_sub(1),
);
}
OperationDirection::Right => {
focused_workspace.focus_container(0);
}
_ => {}
};
}
}
}
Expand Down Expand Up @@ -1348,6 +1378,7 @@ impl WindowManager {
let origin_monitor_idx = self.focused_monitor_idx();
let target_container_idx = workspace.new_idx_for_direction(direction);

// this is for when we are scrolling across workspaces like PaperWM
if target_container_idx.is_none()
&& matches!(
self.cross_boundary_behaviour,
Expand Down Expand Up @@ -1376,6 +1407,8 @@ impl WindowManager {
_ => workspace_idx,
};

// passing the direction here is how we handle whether to insert at the front
// or the back of the container vecdeque in the target workspace
self.move_container_to_workspace(next_idx, true, Some(direction))?;
self.update_focused_workspace(self.mouse_follows_focus, true)?;

Expand Down

0 comments on commit c3f1357

Please sign in to comment.