Skip to content

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

feat: implement cursor jumping

Lars Bergerb3ee515417e1
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 2cac0f2a..f55f64df 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -278,7 +278,7 @@ impl InvokeCommand {
}
InvokeCommand::Focus(args) => {
if let Some(direction) = &args.direction {
- focus_in_direction(subject_container, direction, state)?;
+ focus_in_direction(subject_container, direction, state, config)?;
}
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 a7894e52..c063ac2b 100644
--- a/packages/wm/src/common/commands/platform_sync.rs
+++ b/packages/wm/src/common/commands/platform_sync.rs
@@ -1,4 +1,5 @@
use anyhow::Context;
+use tracing::warn;
use crate::{
common::{platform::Platform, DisplayState},
@@ -30,6 +31,18 @@ pub fn platform_sync(
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);
}
@@ -61,22 +74,18 @@ pub fn sync_focus(
focused_container: Container,
state: &mut WmState,
) -> anyhow::Result<()> {
+ let native_window = match focused_container.as_window_container() {
+ Ok(window) => window.native().clone(),
+ _ => Platform::desktop_window(),
+ };
+
// Set focus to the given window handle. If the container is a normal
// window, then this will trigger a `PlatformEvent::WindowFocused` event.
- match focused_container.as_window_container() {
- Ok(window) => {
- if Platform::foreground_window() != *window.native() {
- let _ = window.native().set_foreground();
- }
- }
- _ => {
- let desktop_window = Platform::desktop_window();
-
- if Platform::foreground_window() != desktop_window {
- let _ = desktop_window.set_foreground();
- }
+ if Platform::foreground_window() != native_window {
+ if let Err(err) = native_window.set_foreground() {
+ warn!("Failed to set foreground window: {}", err);
}
- };
+ }
// TODO: Change z-index of workspace windows that match the focused
// container's state. Make sure not to decrease z-index for floating
@@ -113,12 +122,14 @@ fn redraw_containers(state: &mut WmState) -> anyhow::Result<()> {
let rect =
window.to_rect()?.apply_delta(&window.total_border_delta()?);
- let _ = window.native().set_position(
+ if let Err(err) = window.native().set_position(
&window.state(),
&window.display_state(),
&rect,
window.has_pending_dpi_adjustment(),
- );
+ ) {
+ warn!("Failed to set window position: {}", err);
+ }
}
Ok(())
diff --git a/packages/wm/src/containers/commands/center_cursor_on_container.rs b/packages/wm/src/containers/commands/center_cursor_on_container.rs
deleted file mode 100644
index dfff1496..00000000
--- a/packages/wm/src/containers/commands/center_cursor_on_container.rs
+++ /dev/null
@@ -1,17 +0,0 @@
-use crate::{
- common::platform::Platform, containers::traits::PositionGetters,
- containers::Container, user_config::UserConfig,
-};
-
-pub fn center_cursor_on_container(
- target: Container,
- config: &UserConfig,
-) -> anyhow::Result<()> {
- match config.value.general.cursor_jump.enabled {
- false => Ok(()),
- true => {
- let center = target.to_rect()?.center_point();
- Platform::set_cursor_pos(center.x, center.y)
- }
- }
-}
diff --git a/packages/wm/src/containers/commands/focus_in_direction.rs b/packages/wm/src/containers/commands/focus_in_direction.rs
index 27aea80f..ed9a0056 100644
--- a/packages/wm/src/containers/commands/focus_in_direction.rs
+++ b/packages/wm/src/containers/commands/focus_in_direction.rs
@@ -6,6 +6,7 @@ use crate::{
traits::{CommonGetters, TilingDirectionGetters},
Container, TilingContainer,
},
+ user_config::{CursorJumpTrigger, UserConfig},
windows::{traits::WindowGetters, WindowState},
wm_state::WmState,
};
@@ -16,6 +17,7 @@ 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(_) => {
@@ -46,8 +48,21 @@ pub fn focus_in_direction(
// Set focus to the target container.
if let Some(focus_target) = focus_target {
- set_focused_descendant(focus_target, None);
+ 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());
+ }
+ }
+ }
}
Ok(())
@@ -136,7 +151,7 @@ fn tiling_focus_target(
///
/// This will descend into the workspace in the given direction, and will
/// always return a tiling container. This makes it different from the
-/// `focus_worskspace` command with `FocusWorkspaceTarget::Direction`.
+/// `focus_workspace` command with `FocusWorkspaceTarget::Direction`.
fn workspace_focus_target(
origin_container: Container,
direction: &Direction,
diff --git a/packages/wm/src/containers/commands/mod.rs b/packages/wm/src/containers/commands/mod.rs
index 70569418..b42854eb 100644
--- a/packages/wm/src/containers/commands/mod.rs
+++ b/packages/wm/src/containers/commands/mod.rs
@@ -1,5 +1,4 @@
mod attach_container;
-mod center_cursor_on_container;
mod detach_container;
mod flatten_child_split_containers;
mod flatten_split_container;
@@ -12,7 +11,6 @@ mod toggle_tiling_direction;
mod wrap_in_split_container;
pub use attach_container::*;
-pub use center_cursor_on_container::*;
pub use detach_container::*;
pub use flatten_child_split_containers::*;
pub use flatten_split_container::*;
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 17b72f91..5e9c4f0d 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::UserConfig,
+ user_config::{CursorJumpTrigger, UserConfig},
windows::{
traits::WindowGetters, NonTilingWindow, TilingWindow, WindowState,
},
@@ -89,6 +89,7 @@ fn move_tiling_window(
window_to_move,
direction,
state,
+ config,
);
}
@@ -206,6 +207,7 @@ fn move_to_workspace_in_direction(
window_to_move: TilingWindow,
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.")?;
@@ -234,7 +236,7 @@ fn move_to_workspace_in_direction(
};
move_container_within_tree(
- window_to_move.into(),
+ window_to_move.clone().into(),
workspace.clone().into(),
target_index,
state,
@@ -245,6 +247,22 @@ fn move_to_workspace_in_direction(
.pending_sync
.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());
+ }
+ }
+ }
};
Ok(())
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index f3a8172a..2e8b79dd 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -72,6 +72,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>,
}
impl WmState {
@@ -85,6 +88,7 @@ impl WmState {
containers_to_redraw: Vec::new(),
focus_change: false,
reset_window_effects: false,
+ cursor_container: None,
},
recent_focused_container: None,
recent_workspace_name: None,
diff --git a/packages/wm/src/workspaces/commands/focus_workspace.rs b/packages/wm/src/workspaces/commands/focus_workspace.rs
index bdd43bd6..02149357 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::UserConfig,
+ user_config::{CursorJumpTrigger, UserConfig},
wm_state::WmState,
workspaces::{
commands::{activate_workspace, deactivate_workspace},
@@ -63,7 +63,7 @@ pub fn focus_workspace(
.next()
.unwrap_or_else(|| target_workspace.clone().into());
- set_focused_descendant(container_to_focus, None);
+ set_focused_descendant(container_to_focus.clone(), None);
state.pending_sync.focus_change = true;
// Display the workspace to switch focus to.
@@ -87,6 +87,21 @@ 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());
+ }
+ }
+ }
}
Ok(())