Skip to content

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

fix: window effects not applying; additional z-order fix (#936)

Lars Bergerf564ed578ffb
diff --git a/packages/wm-platform/src/native_window.rs b/packages/wm-platform/src/native_window.rs
index 23ac5888..62bb9319 100644
--- a/packages/wm-platform/src/native_window.rs
+++ b/packages/wm-platform/src/native_window.rs
@@ -839,6 +839,24 @@ impl NativeWindow {
)
}?;
+ // Z-order can sometimes still be incorrect after the above call.
+ unsafe {
+ SetWindowPos(
+ HWND(self.handle),
+ z_order,
+ 0,
+ 0,
+ 0,
+ 0,
+ SWP_NOACTIVATE
+ | SWP_NOCOPYBITS
+ | SWP_ASYNCWINDOWPOS
+ | SWP_SHOWWINDOW
+ | SWP_NOMOVE
+ | SWP_NOSIZE,
+ )
+ }?;
+
Ok(())
}
diff --git a/packages/wm/src/commands/general/platform_sync.rs b/packages/wm/src/commands/general/platform_sync.rs
index 2f45b3e3..4271e371 100644
--- a/packages/wm/src/commands/general/platform_sync.rs
+++ b/packages/wm/src/commands/general/platform_sync.rs
@@ -23,16 +23,12 @@ pub fn platform_sync(
let focused_container =
state.focused_container().context("No focused container.")?;
- // Keep reference to the original focused container.
- let recent_focused_window = state.recent_focused_window.clone();
-
if state.pending_sync.needs_focus_update() {
sync_focus(&focused_container, state)?;
}
if !state.pending_sync.containers_to_redraw().is_empty()
|| !state.pending_sync.workspaces_to_reorder().is_empty()
- || state.pending_sync.needs_focus_update()
{
redraw_containers(&focused_container, state, config)?;
}
@@ -46,8 +42,15 @@ pub fn platform_sync(
if state.pending_sync.needs_focused_effect_update()
|| state.pending_sync.needs_all_effects_update()
{
+ // Keep reference to the previous window that had focus effects
+ // applied.
+ let prev_effects_window = state.prev_effects_window.clone();
+
if let Ok(window) = focused_container.as_window_container() {
apply_window_effects(&window, true, config);
+ state.prev_effects_window = Some(window.clone());
+ } else {
+ state.prev_effects_window = None;
}
// Get windows that should have the unfocused border applied to them.
@@ -58,10 +61,7 @@ pub fn platform_sync(
if state.pending_sync.needs_all_effects_update() {
state.windows()
} else {
- recent_focused_window
- .map(|(window, _)| window)
- .into_iter()
- .collect()
+ prev_effects_window.into_iter().collect()
}
.into_iter()
.filter(|window| window.id() != focused_container.id());
@@ -103,12 +103,6 @@ fn sync_focus(
focused_container: focused_container.to_dto()?,
});
- if let Ok(window) = focused_container.as_window_container() {
- state.recent_focused_window = Some((window.clone(), window.state()));
- } else {
- state.recent_focused_window = None;
- }
-
Ok(())
}
@@ -203,7 +197,7 @@ fn redraw_containers(
windows
};
- for window in &windows_to_update {
+ for window in windows_to_update.iter().rev() {
let should_bring_to_front = windows_to_bring_to_front.contains(window);
let workspace =
diff --git a/packages/wm/src/commands/window/move_window_in_direction.rs b/packages/wm/src/commands/window/move_window_in_direction.rs
index 2839d2f8..2876f1d7 100644
--- a/packages/wm/src/commands/window/move_window_in_direction.rs
+++ b/packages/wm/src/commands/window/move_window_in_direction.rs
@@ -265,6 +265,7 @@ fn move_to_workspace_in_direction(
state
.pending_sync
+ .queue_container_to_redraw(window_to_move.clone())
.queue_containers_to_redraw(target_workspace.tiling_children())
.queue_containers_to_redraw(parent.tiling_children())
.queue_cursor_jump()
diff --git a/packages/wm/src/events/handle_window_focused.rs b/packages/wm/src/events/handle_window_focused.rs
index f89241ce..a7b309d4 100644
--- a/packages/wm/src/events/handle_window_focused.rs
+++ b/packages/wm/src/events/handle_window_focused.rs
@@ -1,14 +1,14 @@
use anyhow::Context;
use tracing::info;
use wm_common::{DisplayState, WindowRuleEvent};
-use wm_platform::NativeWindow;
+use wm_platform::{NativeWindow, Platform};
use crate::{
commands::{
container::set_focused_descendant, window::run_window_rules,
workspace::focus_workspace,
},
- models::WorkspaceTarget,
+ models::{Container, WorkspaceTarget},
traits::{CommonGetters, WindowGetters},
user_config::UserConfig,
wm_state::WmState,
@@ -20,35 +20,45 @@ pub fn handle_window_focused(
config: &mut UserConfig,
) -> anyhow::Result<()> {
let found_window = state.window_from_native(native_window);
+ let focused_container =
+ state.focused_container().context("No focused container.")?;
- if let Some(window) = found_window {
- // Ignore the focus event if:
- // 1. Window is being hidden by the WM.
- // 2. Focus is already set to the WM's focused container.
- if window.display_state() == DisplayState::Hiding
- || state.focused_container() == Some(window.clone().into())
- {
+ // 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) {
+ state.pending_sync.queue_focus_change();
+ return Ok(());
+ }
+
+ // Ignore the focus event if window is being hidden by the WM.
+ if let Some(window) = &found_window {
+ if window.display_state() == DisplayState::Hiding {
return Ok(());
}
+ }
+
+ // Focus effect should be updated for any change in focus that shouldn't
+ // be overwritten. Focus here is either:
+ // 1. WM's focus container (window).
+ // 2. WM's focus container (workspace - i.e. the desktop window).
+ // 3. An ignored window.
+ // 4. A window that received manual focus.
+ state.pending_sync.queue_focused_effect_update();
+
+ if let Some(window) = found_window {
+ let workspace = window.workspace().context("No workspace")?;
- info!("Window focused: {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 state
- .unmanaged_or_minimized_timestamp
- .is_some_and(|time| time.elapsed().as_millis() < 100)
- {
- info!("Overriding native focus.");
- state.pending_sync.queue_focus_change();
+ // Native focus has been synced to the WM's focused container.
+ if focused_container == window.clone().into() {
+ state.pending_sync.queue_workspace_to_reorder(workspace);
return Ok(());
}
- let workspace = window.workspace().context("No workspace")?;
+ info!("Window manually focused: {window}");
// Handle focus events from windows on hidden workspaces. For example,
// if Discord is forcefully shown by the OS when it's on a hidden
@@ -67,18 +77,28 @@ pub fn handle_window_focused(
set_focused_descendant(&window.clone().into(), None);
// Run window rules for focus events.
- run_window_rules(
- window.clone(),
- &WindowRuleEvent::Focus,
- state,
- config,
- )?;
-
- state
- .pending_sync
- .queue_focused_effect_update()
- .queue_workspace_to_reorder(workspace);
+ run_window_rules(window, &WindowRuleEvent::Focus, state, config)?;
+
+ state.pending_sync.queue_workspace_to_reorder(workspace);
}
Ok(())
}
+
+/// 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 {
+ 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
+}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 8e1db1fb..956da9e8 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -36,13 +36,12 @@ pub struct WmState {
/// workspace focus.
pub recent_workspace_name: Option<String>,
- /// Window that most recently had focus synced and a snapshot of its
- /// window state.
+ /// The previously focused window that had focus effects applied.
///
- /// Used for updating window effects on focus change. Note that this
- /// container may be detached if it's a window that's recently been
- /// killed.
- pub recent_focused_window: Option<(WindowContainer, WindowState)>,
+ /// Used to efficiently update window effects by only removing focus
+ /// effects from the previous window rather than all windows when focus
+ /// changes.
+ pub prev_effects_window: Option<WindowContainer>,
/// Time since a previously focused window was unmanaged or minimized.
///
@@ -77,7 +76,7 @@ impl WmState {
Self {
root_container: RootContainer::new(),
pending_sync: PendingSync::default(),
- recent_focused_window: None,
+ prev_effects_window: None,
recent_workspace_name: None,
unmanaged_or_minimized_timestamp: None,
binding_modes: Vec::new(),