Skip to content

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

feat: focus monitor with general.focus_follows_cursor (#959)

Jack151136657bff
diff --git a/packages/wm/src/events/handle_mouse_move.rs b/packages/wm/src/events/handle_mouse_move.rs
index 75e93f6d..695adc59 100644
--- a/packages/wm/src/events/handle_mouse_move.rs
+++ b/packages/wm/src/events/handle_mouse_move.rs
@@ -13,7 +13,12 @@ pub fn handle_mouse_move(
) -> anyhow::Result<()> {
// Ignore event if left/right-click is down. Otherwise, this causes focus
// to jitter when a window is being resized by its drag handles.
- if event.is_mouse_down || !config.value.general.focus_follows_cursor {
+ // Also ignore if the OS focused window isn't the same as the WM's
+ // focused window.
+ if event.is_mouse_down
+ || !state.is_focus_synced
+ || !config.value.general.focus_follows_cursor
+ {
return Ok(());
}
@@ -30,6 +35,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 cursor_monitor = state
+ .monitor_at_point(&event.point)
+ .context("No monitor under cursor.")?;
+
+ let focused_monitor = state
+ .focused_container()
+ .context("No focused container.")?
+ .monitor()
+ .context("Focused container has no monitor.")?;
+
+ // Avoid setting focus to the same monitor.
+ if cursor_monitor.id() != focused_monitor.id() {
+ set_focused_descendant(&cursor_monitor.as_container(), None);
+ state.pending_sync.queue_focus_change();
+ }
}
Ok(())
diff --git a/packages/wm/src/events/handle_window_focused.rs b/packages/wm/src/events/handle_window_focused.rs
index a7b309d4..1c900d50 100644
--- a/packages/wm/src/events/handle_window_focused.rs
+++ b/packages/wm/src/events/handle_window_focused.rs
@@ -8,7 +8,7 @@ use crate::{
container::set_focused_descendant, window::run_window_rules,
workspace::focus_workspace,
},
- models::{Container, WorkspaceTarget},
+ models::WorkspaceTarget,
traits::{CommonGetters, WindowGetters},
user_config::UserConfig,
wm_state::WmState,
@@ -23,13 +23,20 @@ pub fn handle_window_focused(
let focused_container =
state.focused_container().context("No focused container.")?;
+ // Update the focus sync state. If the OS focused window is not same as
+ // the WM's focused container, then the focus is not synced.
+ state.is_focus_synced = match focused_container.as_window_container() {
+ Ok(window) => *window.native() == *native_window,
+ _ => Platform::desktop_window() == *native_window,
+ };
+
// Handle overriding focus on close/minimize. After a window is closed
// or minimized, the OS or the closed application might automatically
// switch focus to a different window. To force focus to go to the WM's
// target focus container, we reassign any focus events 100ms after
// close/minimize. This will cause focus to briefly flicker to the OS
// focus target and then to the WM's focus target.
- if should_override_focus(&focused_container, native_window, state) {
+ if should_override_focus(state) {
state.pending_sync.queue_focus_change();
return Ok(());
}
@@ -54,6 +61,7 @@ pub fn handle_window_focused(
// Native focus has been synced to the WM's focused container.
if focused_container == window.clone().into() {
+ state.is_focus_synced = true;
state.pending_sync.queue_workspace_to_reorder(workspace);
return Ok(());
}
@@ -79,6 +87,7 @@ pub fn handle_window_focused(
// Run window rules for focus events.
run_window_rules(window, &WindowRuleEvent::Focus, state, config)?;
+ state.is_focus_synced = true;
state.pending_sync.queue_workspace_to_reorder(workspace);
}
@@ -86,19 +95,10 @@ pub fn handle_window_focused(
}
/// Returns true if focus should be reassigned to the WM's focus container.
-fn should_override_focus(
- focused_container: &Container,
- native_window: &NativeWindow,
- state: &WmState,
-) -> bool {
+fn should_override_focus(state: &WmState) -> bool {
let has_recent_unmanage = state
.unmanaged_or_minimized_timestamp
.is_some_and(|time| time.elapsed().as_millis() < 100);
- let has_correct_focus = match focused_container.as_window_container() {
- Ok(window) => *window.native() == *native_window,
- _ => Platform::desktop_window() == *native_window,
- };
-
- has_recent_unmanage && !has_correct_focus
+ has_recent_unmanage && !state.is_focus_synced
}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 66a80408..78816d25 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -58,6 +58,9 @@ pub struct WmState {
/// Whether the WM is paused.
pub is_paused: bool,
+ /// Whether the OS focused window is the same as the WM focused window.
+ pub is_focus_synced: bool,
+
/// Whether the initial state has been populated.
has_initialized: bool,
@@ -82,6 +85,7 @@ impl WmState {
binding_modes: Vec::new(),
ignored_windows: Vec::new(),
is_paused: false,
+ is_focus_synced: false,
has_initialized: false,
event_tx,
exit_tx,
@@ -129,6 +133,7 @@ impl WmState {
.context("Failed to get container to focus.")?;
set_focused_descendant(&container_to_focus, None);
+ self.is_focus_synced = true;
self
.pending_sync