Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

fix: improvements to focus_follows_cursor

holby85eb1ec161fc
diff --git a/packages/wm/src/common/events/handle_mouse_move.rs b/packages/wm/src/common/events/handle_mouse_move.rs
new file mode 100644
index 00000000..35f34c93
--- /dev/null
+++ b/packages/wm/src/common/events/handle_mouse_move.rs
@@ -0,0 +1,36 @@
+use crate::common::commands::sync_native_focus;
+use crate::common::platform::{MouseMoveEvent, Platform};
+use crate::containers::commands::{redraw, set_focused_descendant};
+use crate::containers::traits::CommonGetters;
+use crate::user_config::UserConfig;
+use crate::wm_state::WmState;
+
+pub fn handle_mouse_move(
+ mouse_move_event: MouseMoveEvent,
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ if !config.value.general.focus_follows_cursor {
+ return Ok(());
+ }
+ let mut window_under_cursor =
+ Platform::window_from_point(&mouse_move_event.point).unwrap();
+
+ // walk the window tree to find the top-level window
+ while let Ok(parent) = Platform::get_parent(&window_under_cursor) {
+ window_under_cursor = parent;
+ }
+
+ if let Some(window) = state.window_from_native(&window_under_cursor) {
+ let currently_focused_container = state.focused_container().unwrap();
+ let tiling_window =
+ currently_focused_container.as_tiling_window().unwrap();
+ if (tiling_window.id() != window.id()) {
+ set_focused_descendant(window.as_container(), None);
+ state.has_pending_focus_sync = true;
+ redraw(state)?;
+ sync_native_focus(state)?;
+ }
+ }
+ Ok(())
+}
diff --git a/packages/wm/src/common/events/mod.rs b/packages/wm/src/common/events/mod.rs
index 01599fe7..1a09daf1 100644
--- a/packages/wm/src/common/events/mod.rs
+++ b/packages/wm/src/common/events/mod.rs
@@ -1,4 +1,5 @@
mod handle_display_settings_changed;
+mod handle_mouse_move;
mod handle_window_destroyed;
mod handle_window_focused;
mod handle_window_hidden;
@@ -9,6 +10,7 @@ mod handle_window_moved_or_resized;
mod handle_window_shown;
pub use handle_display_settings_changed::*;
+pub use handle_mouse_move::*;
pub use handle_window_destroyed::*;
pub use handle_window_focused::*;
pub use handle_window_hidden::*;
diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rs
index 1261faad..c2656dd1 100644
--- a/packages/wm/src/common/platform/platform.rs
+++ b/packages/wm/src/common/platform/platform.rs
@@ -6,24 +6,25 @@ use anyhow::{bail, Context};
use tokio::sync::{oneshot, Mutex};
use tracing::warn;
use windows::core::{w, PCWSTR};
-use windows::Win32::Foundation::POINT;
+use windows::Win32::Foundation::{HWND, POINT};
use windows::Win32::System::Environment::ExpandEnvironmentStringsW;
use windows::Win32::UI::Shell::{
ShellExecuteExW, SEE_MASK_NOASYNC, SEE_MASK_NOCLOSEPROCESS,
SHELLEXECUTEINFOW,
};
-use windows::Win32::UI::WindowsAndMessaging::{
- CreateWindowExW, DestroyWindow, DispatchMessageW, GetMessageW,
- RegisterClassW, SetCursorPos, TranslateMessage, WindowFromPoint,
- CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, MSG, SW_NORMAL, WNDCLASSW,
- WNDPROC, WS_OVERLAPPEDWINDOW,
-};
+
use windows::Win32::UI::{
HiDpi::{
SetProcessDpiAwarenessContext,
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
},
- WindowsAndMessaging::{GetDesktopWindow, GetForegroundWindow},
+ WindowsAndMessaging::{
+ CreateWindowExW, DestroyWindow, DispatchMessageW, GetDesktopWindow,
+ GetForegroundWindow, GetMessageW, GetParent, RegisterClassW,
+ SetCursorPos, TranslateMessage, WindowFromPoint, CS_HREDRAW,
+ CS_VREDRAW, CW_USEDEFAULT, MSG, SW_NORMAL, WNDCLASSW, WNDPROC,
+ WS_OVERLAPPEDWINDOW,
+ },
};
use crate::user_config::UserConfig;
@@ -113,6 +114,27 @@ impl Platform {
pub fn new_single_instance() -> anyhow::Result<SingleInstance> {
SingleInstance::new()
}
+
+ // Gets the parent window of the specified window.
+ pub fn get_parent(
+ window: &NativeWindow,
+ ) -> anyhow::Result<NativeWindow> {
+ let handle = unsafe { GetParent(window.handle) };
+ // if zero pointer is returned, it means the window has no parent.
+ if handle == HWND(0) {
+ bail!("Window has no parent.");
+ }
+ Ok(NativeWindow::new(handle))
+ }
+
+ /// Sets the cursor position to the specified coordinates.
+ pub fn set_cursor_pos(x: i32, y: i32) -> anyhow::Result<()> {
+ unsafe {
+ SetCursorPos(x, y)?;
+ };
+
+ Ok(())
+ }
/// Sets the DPI awareness for the current process to per-monitor
/// awareness (v2).
@@ -126,6 +148,7 @@ impl Platform {
Ok(())
}
+ // Find the window at the specified point in screen space.
pub fn window_from_point(point: &Point) -> anyhow::Result<NativeWindow> {
let point = POINT {
x: point.x,
@@ -135,15 +158,6 @@ impl Platform {
Ok(NativeWindow::new(handle))
}
- /// Sets the cursor position to the specified coordinates.
- pub fn set_cursor_pos(x: i32, y: i32) -> anyhow::Result<()> {
- unsafe {
- SetCursorPos(x, y)?;
- };
-
- Ok(())
- }
-
/// Spawns a hidden message window and starts a message loop.
///
/// This function will block until the message loop is aborted via the
diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rs
index d2f895c7..465f194b 100644
--- a/packages/wm/src/wm.rs
+++ b/packages/wm/src/wm.rs
@@ -7,7 +7,7 @@ use tokio::sync::{
};
use uuid::Uuid;
-use crate::containers::commands::set_focused_descendant;
+use crate::common::events::handle_mouse_move;
use crate::{
app_command::InvokeCommand,
common::{
@@ -19,7 +19,7 @@ use crate::{
handle_window_minimized, handle_window_moved_or_resized,
handle_window_shown,
},
- platform::{Platform, PlatformEvent},
+ platform::{PlatformEvent},
},
containers::{commands::redraw, traits::CommonGetters},
user_config::UserConfig,
@@ -68,25 +68,9 @@ impl WindowManager {
PlatformEvent::DisplaySettingsChanged => {
handle_display_settings_changed(&mut state, config)
}
- PlatformEvent::MouseMove(event) => Ok({
- if config.value.general.focus_follows_cursor {
- let native_window =
- Platform::window_from_point(&event.point).unwrap();
-
- if let Some(window) = state.window_from_native(&native_window) {
- let currently_focused_container =
- state.focused_container().unwrap();
- let tiling_window =
- currently_focused_container.as_tiling_window().unwrap();
- if (tiling_window.id() != window.id()) {
- set_focused_descendant(window.as_container(), None);
- state.has_pending_focus_sync = true;
- redraw(&mut state)?;
- sync_native_focus(&mut state)?;
- }
- }
- }
- }),
+ PlatformEvent::MouseMove(event) => {
+ handle_mouse_move(event, &mut state, config)
+ }
PlatformEvent::WindowDestroyed(window) => {
handle_window_destroyed(window, &mut state)
}