Skip to content

Commit

Permalink
feat: focus monitor with focus_follows_cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
jackssrt committed Feb 2, 2025
1 parent 110ed89 commit 10f08f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/wm-platform/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ impl Platform {
Ok(NativeWindow::new(handle.0))
}

pub fn monitor_from_point(
point: &Point,
) -> anyhow::Result<NativeMonitor> {
let monitors = native_monitor::available_monitors()?;
monitors
.iter()
.find_map(|monitor| {
let rect = monitor.rect().ok()?;
rect.contains_point(point).then(|| monitor.clone())
})
.context("Monitor not found")
}

/// Gets the mouse position in screen space.
pub fn mouse_position() -> anyhow::Result<Point> {
let mut point = POINT { x: 0, y: 0 };
Expand Down
17 changes: 17 additions & 0 deletions packages/wm/src/events/handle_mouse_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ pub fn handle_mouse_move(
set_focused_descendant(&window.as_container(), None);
state.pending_sync.queue_focus_change();
}
} else {
// Focus the monitor if no window is under the cursor.
let monitor_under_cursor = Platform::monitor_from_point(&event.point)
.map(|workspace| state.monitor_from_native(&workspace))?
.context("No monitor under cursor.")?;

let currently_focused_monitor = state
.focused_container()
.context("No focused container.")?
.monitor()
.context("Focused container has no monitor.")?;

// Avoid setting focus to the same monitor.
if monitor_under_cursor.id() != currently_focused_monitor.id() {
set_focused_descendant(&monitor_under_cursor.as_container(), None);
state.pending_sync.queue_focus_change();
}
}

Ok(())
Expand Down

0 comments on commit 10f08f0

Please sign in to comment.