Skip to content

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

feat: handle minimize ended and maximize/restore events

Lars Berger4b9f879dcfa2
diff --git a/packages/wm/src/common/events/handle_window_location_changed.rs b/packages/wm/src/common/events/handle_window_location_changed.rs
new file mode 100644
index 00000000..03a84b6d
--- /dev/null
+++ b/packages/wm/src/common/events/handle_window_location_changed.rs
@@ -0,0 +1,55 @@
+use tracing::info;
+
+use crate::{
+ common::platform::NativeWindow,
+ user_config::{FullscreenStateConfig, UserConfig},
+ windows::{
+ commands::{toggle_window_state, update_window_state},
+ traits::WindowGetters,
+ WindowState,
+ },
+ wm_state::WmState,
+};
+
+pub fn handle_window_location_changed(
+ native_window: NativeWindow,
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ let found_window = state.window_from_native(&native_window);
+
+ // Update the window's state to be fullscreen or toggled from fullscreen.
+ if let Some(window) = found_window {
+ match window.state() {
+ WindowState::Fullscreen(_) => {
+ // TODO: Add fullscreen check.
+ if !window.native().is_maximized() {
+ info!("Window restored");
+ toggle_window_state(
+ window.clone(),
+ window.state(),
+ state,
+ config,
+ )?;
+ }
+ }
+ _ => {
+ if window.native().is_maximized()
+ || window.native().is_fullscreen()
+ {
+ info!("Window maximized");
+ update_window_state(
+ window,
+ WindowState::Fullscreen(FullscreenStateConfig {
+ ..config.value.window_state_defaults.fullscreen
+ }),
+ state,
+ config,
+ )?;
+ }
+ }
+ }
+ }
+
+ Ok(())
+}
diff --git a/packages/wm/src/common/events/handle_window_minimize_ended.rs b/packages/wm/src/common/events/handle_window_minimize_ended.rs
new file mode 100644
index 00000000..7afabe6d
--- /dev/null
+++ b/packages/wm/src/common/events/handle_window_minimize_ended.rs
@@ -0,0 +1,29 @@
+use tracing::info;
+
+use crate::{
+ common::platform::NativeWindow,
+ user_config::UserConfig,
+ windows::{
+ commands::toggle_window_state, traits::WindowGetters, WindowState,
+ },
+ wm_state::WmState,
+};
+
+pub fn handle_window_minimize_ended(
+ native_window: NativeWindow,
+ state: &mut WmState,
+ config: &UserConfig,
+) -> anyhow::Result<()> {
+ let found_window = state.window_from_native(&native_window);
+
+ // Update the window's state to not be minimized.
+ if let Some(window) = found_window {
+ if window.state() == WindowState::Minimized {
+ // TODO: Log window details.
+ info!("Window minimize ended");
+ toggle_window_state(window, WindowState::Minimized, state, config)?;
+ }
+ }
+
+ Ok(())
+}
diff --git a/packages/wm/src/common/events/mod.rs b/packages/wm/src/common/events/mod.rs
index 2d3fc8a5..62c767d0 100644
--- a/packages/wm/src/common/events/mod.rs
+++ b/packages/wm/src/common/events/mod.rs
@@ -1,11 +1,15 @@
mod handle_window_destroyed;
mod handle_window_focused;
mod handle_window_hidden;
+mod handle_window_location_changed;
+mod handle_window_minimize_ended;
mod handle_window_minimized;
mod handle_window_shown;
pub use handle_window_destroyed::*;
pub use handle_window_focused::*;
pub use handle_window_hidden::*;
+pub use handle_window_location_changed::*;
+pub use handle_window_minimize_ended::*;
pub use handle_window_minimized::*;
pub use handle_window_shown::*;
diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rs
index af6ba154..c2059a89 100644
--- a/packages/wm/src/common/platform/native_window.rs
+++ b/packages/wm/src/common/platform/native_window.rs
@@ -17,15 +17,15 @@ use windows::{
WindowsAndMessaging::{
EnumWindows, GetClassNameW, GetWindow, GetWindowLongPtrW,
GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId,
- IsWindowVisible, SendNotifyMessageW, SetForegroundWindow,
- SetWindowPos, ShowWindowAsync, GWL_EXSTYLE, GWL_STYLE, GW_OWNER,
- HWND_NOTOPMOST, HWND_TOPMOST, SWP_ASYNCWINDOWPOS,
- SWP_FRAMECHANGED, SWP_HIDEWINDOW, SWP_NOACTIVATE, SWP_NOCOPYBITS,
- SWP_NOMOVE, SWP_NOSENDCHANGING, SWP_NOSIZE, SWP_SHOWWINDOW,
- SW_MAXIMIZE, SW_MINIMIZE, SW_RESTORE, WINDOWPLACEMENT,
- WINDOW_EX_STYLE, WINDOW_STYLE, WM_CLOSE, WS_CAPTION, WS_CHILD,
- WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_MAXIMIZE, WS_MINIMIZE,
- WS_THICKFRAME,
+ IsIconic, IsWindowVisible, SendNotifyMessageW,
+ SetForegroundWindow, SetWindowPos, ShowWindowAsync, GWL_EXSTYLE,
+ GWL_STYLE, GW_OWNER, HWND_NOTOPMOST, HWND_TOPMOST,
+ SWP_ASYNCWINDOWPOS, SWP_FRAMECHANGED, SWP_HIDEWINDOW,
+ SWP_NOACTIVATE, SWP_NOCOPYBITS, SWP_NOMOVE, SWP_NOSENDCHANGING,
+ SWP_NOSIZE, SWP_SHOWWINDOW, SW_MAXIMIZE, SW_MINIMIZE, SW_RESTORE,
+ WINDOWPLACEMENT, WINDOW_EX_STYLE, WINDOW_STYLE, WM_CLOSE,
+ WS_CAPTION, WS_CHILD, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
+ WS_MAXIMIZE, WS_MINIMIZE, WS_THICKFRAME,
},
},
},
@@ -185,11 +185,13 @@ impl NativeWindow {
}
pub fn is_minimized(&self) -> bool {
- self.has_window_style(WS_MINIMIZE)
+ unsafe { IsIconic(self.handle) }.as_bool()
}
pub fn is_maximized(&self) -> bool {
- self.has_window_style(WS_MAXIMIZE)
+ let mut placement = WINDOWPLACEMENT::default();
+ let _ = unsafe { GetWindowPlacement(self.handle, &mut placement) };
+ placement.showCmd == SW_MAXIMIZE.0 as u32
}
pub fn is_resizable(&self) -> bool {
diff --git a/packages/wm/src/containers/commands/detach_container.rs b/packages/wm/src/containers/commands/detach_container.rs
index 077ce8b1..befaf1c8 100644
--- a/packages/wm/src/containers/commands/detach_container.rs
+++ b/packages/wm/src/containers/commands/detach_container.rs
@@ -51,16 +51,14 @@ fn resize_sibling_containers(
let tiling_size_increment =
child_to_remove.tiling_size() / tiling_siblings.len() as f32;
- // Adjust size of the siblings of the removed container.
- for container_to_resize in &tiling_siblings {
- container_to_resize.set_tiling_size(
- container_to_resize.tiling_size() + tiling_size_increment,
- );
+ // Adjust size of the siblings based on the freed up space.
+ for sibling in &tiling_siblings {
+ sibling.set_tiling_size(sibling.tiling_size() + tiling_size_increment);
}
- // If there is exactly *one* sibling to the detached container, then flatten that
- // sibling if it's a split container. This is to handle layouts like H[1 V[2 H[3]]],
- // where container 2 gets detached.
+ // If there is exactly *one* sibling to the detached container, then
+ // flatten that sibling if it's a split container. This is to handle
+ // layouts like H[1 V[2 H[3]]], where container 2 gets detached.
if tiling_siblings.len() == 1 {
if let Some(split_sibling) = tiling_siblings[0].as_split().cloned() {
let split_sibling_parent =
diff --git a/packages/wm/src/containers/commands/replace_container.rs b/packages/wm/src/containers/commands/replace_container.rs
index 1f605b0c..b7c07d57 100644
--- a/packages/wm/src/containers/commands/replace_container.rs
+++ b/packages/wm/src/containers/commands/replace_container.rs
@@ -28,12 +28,28 @@ pub fn replace_container(
.cloned()
.with_context(|| format!("No container at index {}.", target_index))?;
- if let (Ok(container_to_replace), Ok(replacement_container)) = (
+ match (
container_to_replace.as_tiling_container(),
replacement_container.as_tiling_container(),
) {
- replacement_container
- .set_tiling_size(container_to_replace.tiling_size());
+ (Ok(container_to_replace), Ok(replacement_container)) => {
+ replacement_container
+ .set_tiling_size(container_to_replace.tiling_size());
+ }
+ (Ok(container_to_replace), Err(_)) => {
+ let tiling_siblings =
+ container_to_replace.tiling_siblings().collect::<Vec<_>>();
+
+ let tiling_size_increment =
+ container_to_replace.tiling_size() / tiling_siblings.len() as f32;
+
+ // Adjust size of the siblings of the removed container.
+ for sibling in &tiling_siblings {
+ sibling
+ .set_tiling_size(sibling.tiling_size() + tiling_size_increment);
+ }
+ }
+ _ => {}
}
// Replace the container at the given index.
diff --git a/packages/wm/src/windows/commands/update_window_state.rs b/packages/wm/src/windows/commands/update_window_state.rs
index dee6e2e1..836cde05 100644
--- a/packages/wm/src/windows/commands/update_window_state.rs
+++ b/packages/wm/src/windows/commands/update_window_state.rs
@@ -68,11 +68,11 @@ fn set_tiling(
move_container_within_tree(
tiling_window.clone().into(),
- target_parent,
+ target_parent.clone(),
target_index,
)?;
- state.add_container_to_redraw(tiling_window.into());
+ state.add_container_to_redraw(target_parent);
}
Ok(())
@@ -108,15 +108,15 @@ fn set_non_tiling(
window.index(),
)?;
+ // Focus should be reassigned after a window has been minimized.
if window_state == WindowState::Minimized {
- state.unmanaged_or_minimized_timestamp =
- Some(std::time::Instant::now());
- state.has_pending_focus_sync = true;
-
if let Some(focus_target) =
state.focus_target_after_removal(&non_tiling_window.into())
{
set_focused_descendant(focus_target, None);
+ state.has_pending_focus_sync = true;
+ state.unmanaged_or_minimized_timestamp =
+ Some(std::time::Instant::now());
}
}
diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rs
index 930744b0..6f318074 100644
--- a/packages/wm/src/wm.rs
+++ b/packages/wm/src/wm.rs
@@ -13,7 +13,9 @@ use crate::{
commands::sync_native_focus,
events::{
handle_window_destroyed, handle_window_focused,
- handle_window_hidden, handle_window_minimized, handle_window_shown,
+ handle_window_hidden, handle_window_location_changed,
+ handle_window_minimize_ended, handle_window_minimized,
+ handle_window_shown,
},
platform::PlatformEvent,
},
@@ -71,11 +73,15 @@ impl WindowManager {
PlatformEvent::WindowHidden(window) => {
handle_window_hidden(window, &mut state, config)
}
- PlatformEvent::WindowLocationChanged(_) => Ok(()),
+ PlatformEvent::WindowLocationChanged(window) => {
+ handle_window_location_changed(window, &mut state, config)
+ }
PlatformEvent::WindowMinimized(window) => {
handle_window_minimized(window, &mut state, config)
}
- PlatformEvent::WindowMinimizeEnded(_) => Ok(()),
+ PlatformEvent::WindowMinimizeEnded(window) => {
+ handle_window_minimize_ended(window, &mut state, config)
+ }
PlatformEvent::WindowMovedOrResized(_) => Ok(()),
PlatformEvent::WindowShown(window) => {
handle_window_shown(window, &mut state, config)