Skip to content

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

fix: cursor jump unnecessarily setting cursor position

Lars Bergerb379aba95106
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 95a59a72..8763b2c7 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -284,7 +284,7 @@ impl InvokeCommand {
}
InvokeCommand::Focus(args) => {
if let Some(direction) = &args.direction {
- focus_in_direction(subject_container, direction, state, config)?;
+ focus_in_direction(subject_container, direction, state)?;
}
if let Some(name) = &args.workspace {
diff --git a/packages/wm/src/common/commands/platform_sync.rs b/packages/wm/src/common/commands/platform_sync.rs
index 7107ae4b..dceb2f70 100644
--- a/packages/wm/src/common/commands/platform_sync.rs
+++ b/packages/wm/src/common/commands/platform_sync.rs
@@ -7,7 +7,7 @@ use crate::{
traits::{CommonGetters, PositionGetters},
Container, WindowContainer,
},
- user_config::UserConfig,
+ user_config::{CursorJumpTrigger, UserConfig},
windows::traits::WindowGetters,
wm_event::WmEvent,
wm_state::WmState,
@@ -26,23 +26,16 @@ pub fn platform_sync(
let focused_container =
state.focused_container().context("No focused container.")?;
+ if state.pending_sync.cursor_jump {
+ jump_cursor(focused_container.clone(), state, config)?;
+ state.pending_sync.cursor_jump = false;
+ }
+
if state.pending_sync.focus_change {
sync_focus(focused_container.clone(), state)?;
state.pending_sync.focus_change = false;
}
- if let Some(target) = &state.pending_sync.cursor_container {
- if !target.is_detached() {
- let center = target.to_rect()?.center_point();
-
- if let Err(err) = Platform::set_cursor_pos(center.x, center.y) {
- warn!("Failed to set cursor position: {}", err);
- }
- }
-
- state.pending_sync.cursor_container = None;
- }
-
if let Ok(window) = focused_container.as_window_container() {
apply_window_effects(window, true, config);
}
@@ -70,7 +63,7 @@ pub fn platform_sync(
Ok(())
}
-pub fn sync_focus(
+fn sync_focus(
focused_container: Container,
state: &mut WmState,
) -> anyhow::Result<()> {
@@ -140,6 +133,39 @@ fn redraw_containers(state: &mut WmState) -> anyhow::Result<()> {
Ok(())
}
+fn jump_cursor(
+ focused_container: Container,
+ state: &WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ let cursor_jump = &config.value.general.cursor_jump;
+
+ let jump_target = match cursor_jump.trigger {
+ CursorJumpTrigger::WindowFocus => Some(focused_container),
+ CursorJumpTrigger::MonitorFocus => {
+ let target_monitor =
+ focused_container.monitor().context("No monitor.")?;
+
+ let cursor_monitor =
+ state.monitor_at_position(&Platform::mouse_position()?);
+
+ cursor_monitor
+ .filter(|monitor| monitor.id() != target_monitor.id())
+ .map(Into::into)
+ }
+ };
+
+ if let Some(jump_target) = jump_target {
+ let center = jump_target.to_rect()?.center_point();
+
+ if let Err(err) = Platform::set_cursor_pos(center.x, center.y) {
+ warn!("Failed to set cursor position: {}", err);
+ }
+ }
+
+ Ok(())
+}
+
fn apply_window_effects(
window: WindowContainer,
is_focused: bool,
diff --git a/packages/wm/src/common/platform/platform.rs b/packages/wm/src/common/platform/platform.rs
index e23563de..32f5e316 100644
--- a/packages/wm/src/common/platform/platform.rs
+++ b/packages/wm/src/common/platform/platform.rs
@@ -136,7 +136,7 @@ impl Platform {
Ok(())
}
- /// Find the window at the specified point in screen space.
+ /// Finds 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,
@@ -147,12 +147,10 @@ impl Platform {
Ok(NativeWindow::new(handle.0))
}
- /// gets the mouse position in screen space.
+ /// Gets the mouse position in screen space.
pub fn mouse_position() -> anyhow::Result<Point> {
let mut point = POINT { x: 0, y: 0 };
- unsafe {
- GetCursorPos(&mut point)?;
- };
+ unsafe { GetCursorPos(&mut point) }?;
Ok(Point {
x: point.x,
diff --git a/packages/wm/src/containers/commands/focus_in_direction.rs b/packages/wm/src/containers/commands/focus_in_direction.rs
index 9b6d7bdc..87d74d2a 100644
--- a/packages/wm/src/containers/commands/focus_in_direction.rs
+++ b/packages/wm/src/containers/commands/focus_in_direction.rs
@@ -7,7 +7,6 @@ use crate::{
traits::{CommonGetters, TilingDirectionGetters},
Container, TilingContainer,
},
- user_config::{CursorJumpTrigger, UserConfig},
windows::{traits::WindowGetters, WindowState},
wm_state::WmState,
};
@@ -16,7 +15,6 @@ pub fn focus_in_direction(
origin_container: Container,
direction: &Direction,
state: &mut WmState,
- config: &UserConfig,
) -> anyhow::Result<()> {
let focus_target = match origin_container {
Container::TilingWindow(_) => {
@@ -49,19 +47,7 @@ pub fn focus_in_direction(
if let Some(focus_target) = focus_target {
set_focused_descendant(focus_target.clone(), None);
state.pending_sync.focus_change = true;
-
- // Jump cursor to the focus target if enabled.
- if config.value.general.cursor_jump.enabled {
- match config.value.general.cursor_jump.trigger {
- CursorJumpTrigger::WindowFocus => {
- state.pending_sync.cursor_container = Some(focus_target);
- }
- CursorJumpTrigger::MonitorFocus => {
- let monitor = focus_target.monitor().context("No monitor.")?;
- state.pending_sync.cursor_container = Some(monitor.into());
- }
- }
- }
+ state.pending_sync.cursor_jump = true;
}
Ok(())
diff --git a/packages/wm/src/containers/root_container.rs b/packages/wm/src/containers/root_container.rs
index 1cb9526a..5848f988 100644
--- a/packages/wm/src/containers/root_container.rs
+++ b/packages/wm/src/containers/root_container.rs
@@ -13,7 +13,10 @@ use super::{
Container, ContainerDto, DirectionContainer, TilingContainer,
WindowContainer,
};
-use crate::{common::Rect, impl_common_getters, impl_container_debug};
+use crate::{
+ common::Rect, impl_common_getters, impl_container_debug,
+ monitors::Monitor,
+};
/// Root node of the container tree.
#[derive(Clone)]
@@ -50,6 +53,14 @@ impl RootContainer {
Self(Rc::new(RefCell::new(root)))
}
+ pub fn monitors(&self) -> Vec<Monitor> {
+ self
+ .children()
+ .into_iter()
+ .filter_map(|container| container.as_monitor().cloned())
+ .collect()
+ }
+
pub fn to_dto(&self) -> anyhow::Result<ContainerDto> {
let children = self
.children()
diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rs
index 89dfbad6..8d8eb2ec 100644
--- a/packages/wm/src/monitors/monitor.rs
+++ b/packages/wm/src/monitors/monitor.rs
@@ -86,7 +86,7 @@ impl Monitor {
.children()
.into_iter()
.filter_map(|container| container.as_workspace().cloned())
- .collect::<Vec<_>>()
+ .collect()
}
/// Whether there is a difference in DPI between this monitor and the
diff --git a/packages/wm/src/windows/commands/move_window_in_direction.rs b/packages/wm/src/windows/commands/move_window_in_direction.rs
index fc6cd14f..f052ae79 100644
--- a/packages/wm/src/windows/commands/move_window_in_direction.rs
+++ b/packages/wm/src/windows/commands/move_window_in_direction.rs
@@ -12,7 +12,7 @@ use crate::{
DirectionContainer, SplitContainer, TilingContainer, WindowContainer,
},
monitors::Monitor,
- user_config::{CursorJumpTrigger, UserConfig},
+ user_config::UserConfig,
windows::{
traits::WindowGetters, NonTilingWindow, TilingWindow, WindowState,
},
@@ -38,7 +38,6 @@ pub fn move_window_in_direction(
non_tiling_window.into(),
direction,
state,
- config,
),
_ => Ok(()),
}
@@ -92,7 +91,6 @@ fn move_tiling_window(
window_to_move.into(),
direction,
state,
- config,
);
}
@@ -210,7 +208,6 @@ fn move_to_workspace_in_direction(
window_to_move: WindowContainer,
direction: &Direction,
state: &mut WmState,
- config: &UserConfig,
) -> anyhow::Result<()> {
let parent = window_to_move.parent().context("No parent.")?;
let monitor = parent.monitor().context("No monitor.")?;
@@ -257,21 +254,7 @@ fn move_to_workspace_in_direction(
.containers_to_redraw
.extend([workspace.into(), parent.into()]);
- // Jump cursor to the focus target if enabled.
- if config.value.general.cursor_jump.enabled
- && window_to_move.has_focus(None)
- {
- match config.value.general.cursor_jump.trigger {
- CursorJumpTrigger::WindowFocus => {
- state.pending_sync.cursor_container =
- Some(window_to_move.into());
- }
- CursorJumpTrigger::MonitorFocus => {
- let monitor = window_to_move.monitor().context("No monitor.")?;
- state.pending_sync.cursor_container = Some(monitor.into());
- }
- }
- }
+ state.pending_sync.cursor_jump = true;
};
Ok(())
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 21d7baf6..bc65a962 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -75,8 +75,9 @@ pub struct PendingSync {
/// Whether window effects should be reset.
pub reset_window_effects: bool,
- /// Container to center the cursor on.
- pub cursor_container: Option<Container>,
+ /// Whether to jump the cursor to the focused container (if enabled in
+ /// user config).
+ pub cursor_jump: bool,
}
impl WmState {
@@ -90,7 +91,7 @@ impl WmState {
containers_to_redraw: Vec::new(),
focus_change: false,
reset_window_effects: false,
- cursor_container: None,
+ cursor_jump: false,
},
recent_focused_container: None,
recent_workspace_name: None,
@@ -151,12 +152,7 @@ impl WmState {
}
pub fn monitors(&self) -> Vec<Monitor> {
- self
- .root_container
- .children()
- .iter()
- .filter_map(|container| container.as_monitor().cloned())
- .collect()
+ self.root_container.monitors()
}
pub fn workspaces(&self) -> Vec<Workspace> {
diff --git a/packages/wm/src/workspaces/commands/focus_workspace.rs b/packages/wm/src/workspaces/commands/focus_workspace.rs
index 02149357..5691043b 100644
--- a/packages/wm/src/workspaces/commands/focus_workspace.rs
+++ b/packages/wm/src/workspaces/commands/focus_workspace.rs
@@ -3,7 +3,7 @@ use tracing::info;
use crate::{
containers::{commands::set_focused_descendant, traits::CommonGetters},
- user_config::{CursorJumpTrigger, UserConfig},
+ user_config::UserConfig,
wm_state::WmState,
workspaces::{
commands::{activate_workspace, deactivate_workspace},
@@ -87,21 +87,7 @@ pub fn focus_workspace(
// Save the currently focused workspace as recent.
state.recent_workspace_name = Some(focused_workspace.config().name);
-
- // Jump cursor to the focus target if enabled.
- if config.value.general.cursor_jump.enabled {
- match config.value.general.cursor_jump.trigger {
- CursorJumpTrigger::WindowFocus => {
- state.pending_sync.cursor_container = Some(container_to_focus);
- }
- CursorJumpTrigger::MonitorFocus => {
- let monitor =
- container_to_focus.monitor().context("No monitor.")?;
-
- state.pending_sync.cursor_container = Some(monitor.into());
- }
- }
- }
+ state.pending_sync.cursor_jump = true;
}
Ok(())