GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: handle window moved/resized events
Lars Berger3771b888e1fd
diff --git a/packages/wm/src/common/events/handle_window_moved_or_resized.rs b/packages/wm/src/common/events/handle_window_moved_or_resized.rsnew file mode 100644index 00000000..e8938273--- /dev/null+++ b/packages/wm/src/common/events/handle_window_moved_or_resized.rs@@ -0,0 +1,138 @@+use anyhow::Context;+use tracing::info;++use crate::{+ common::{platform::NativeWindow, LengthValue, Rect, ResizeDimension},+ containers::{+ commands::move_container_within_tree,+ traits::{CommonGetters, PositionGetters},+ WindowContainer,+ },+ windows::{+ commands::resize_window, traits::WindowGetters, NonTilingWindow,+ TilingWindow, WindowState,+ },+ wm_event::WmEvent,+ wm_state::WmState,+};++pub fn handle_window_moved_or_resized(+ native_window: NativeWindow,+ state: &mut WmState,+) -> anyhow::Result<()> {+ let found_window = state.window_from_native(&native_window);++ if let Some(window) = found_window {+ // TODO: Log window details.+ info!("Window moved/resized");++ match window {+ WindowContainer::TilingWindow(tiling_window) => {+ update_tiling_window(tiling_window, state)?;+ }+ WindowContainer::NonTilingWindow(non_tiling_window) => {+ if matches!(non_tiling_window.state(), WindowState::Floating(_)) {+ update_floating_window(non_tiling_window, state)?;+ }+ }+ _ => (),+ }+ }++ Ok(())+}++fn update_tiling_window(+ window: TilingWindow,+ state: &mut WmState,+) -> anyhow::Result<()> {+ // Snap window to its original position if it's not being resized.+ if window.parent().context("No parent.")?.is_workspace()+ && window.tiling_siblings().count() == 0+ {+ state.containers_to_redraw.push(window.clone().into());+ return Ok(());+ }++ let monitor = window.monitor().context("Window has no monitor.")?;++ // Remove invisible borders from current placement to be able to compare+ // window width/height.+ let new_placement = window.native().placement();+ let adjusted_placement = Rect::from_ltrb(+ new_placement.left+ + window.border_delta().left.to_pixels(monitor.width()?),+ new_placement.top+ + window.border_delta().top.to_pixels(monitor.width()?),+ new_placement.right+ - window.border_delta().right.to_pixels(monitor.width()?),+ new_placement.bottom+ - window.border_delta().bottom.to_pixels(monitor.width()?),+ );++ let delta_width = adjusted_placement.width() - window.width()?;+ let delta_height = adjusted_placement.height() - window.height()?;++ resize_window(+ window.clone().into(),+ ResizeDimension::Width,+ LengthValue::new_px(delta_width as f32),+ state,+ )?;++ resize_window(+ window.clone().into(),+ ResizeDimension::Height,+ LengthValue::new_px(delta_height as f32),+ state,+ )?;++ Ok(())+}++fn update_floating_window(+ window: NonTilingWindow,+ state: &mut WmState,+) -> anyhow::Result<()> {+ // Update state with new location of the floating window.+ let new_placement = window.native().placement();+ window.set_floating_placement(new_placement);++ // Change floating window's parent workspace if moved out of its bounds.+ update_parent_workspace(window.clone().into(), state)?;+ Ok(())+}++fn update_parent_workspace(+ window: NonTilingWindow,+ state: &mut WmState,+) -> anyhow::Result<()> {+ // Get workspace that encompasses most of the window.+ let target_workspace = state+ .nearest_monitor(&window.native())+ .context("Couldn't find nearest monitor.")?+ .displayed_workspace()+ .context("Monitor has no displayed workspace.")?;++ // Ignore if window is still within the bounds of its current workspace.+ if target_workspace.id()+ == window.workspace().context("No workspace.")?.id()+ {+ return Ok(());+ }++ // Change the window's parent workspace.+ move_container_within_tree(+ window.clone().into(),+ target_workspace.clone().into(),+ target_workspace.children().len() - 1,+ )?;++ if window.is_focused() {+ state.emit_event(WmEvent::FocusedContainerMoved {+ focused_container: window.into(),+ });+ }++ Ok(())+}diff --git a/packages/wm/src/common/events/mod.rs b/packages/wm/src/common/events/mod.rsindex 62c767d0..11320ab7 100644--- a/packages/wm/src/common/events/mod.rs+++ b/packages/wm/src/common/events/mod.rs@@ -4,6 +4,7 @@ mod handle_window_hidden;mod handle_window_location_changed;mod handle_window_minimize_ended;mod handle_window_minimized;+mod handle_window_moved_or_resized;mod handle_window_shown;pub use handle_window_destroyed::*;@@ -12,4 +13,5 @@ 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_moved_or_resized::*;pub use handle_window_shown::*;diff --git a/packages/wm/src/common/rect.rs b/packages/wm/src/common/rect.rsindex d4b4b7eb..c34ea028 100644--- a/packages/wm/src/common/rect.rs+++ b/packages/wm/src/common/rect.rs@@ -3,16 +3,16 @@ use super::RectDelta;#[derive(Debug, Clone)]pub struct Rect {/// X-coordinate of the left edge of the rectangle.- left: i32,+ pub left: i32,/// Y-coordinate of the top edge of the rectangle.- top: i32,+ pub top: i32,/// X-coordinate of the right edge of the rectangle.- right: i32,+ pub right: i32,/// Y-coordinate of the bottom edge of the rectangle.- bottom: i32,+ pub bottom: i32,}impl Rect {diff --git a/packages/wm/src/common/units.rs b/packages/wm/src/common/units.rsindex d8c1c29d..95a608be 100644--- a/packages/wm/src/common/units.rs+++ b/packages/wm/src/common/units.rs@@ -24,6 +24,13 @@ impl LengthValue {}}+ pub fn new_percent(amount: f32) -> Self {+ Self {+ amount,+ unit: LengthUnit::Percentage,+ }+ }+pub fn to_pixels(&self, total: i32) -> i32 {match self.unit {LengthUnit::Pixel => self.amount as i32,diff --git a/packages/wm/src/containers/commands/toggle_tiling_direction.rs b/packages/wm/src/containers/commands/toggle_tiling_direction.rsindex f2d3ec4f..5ba07b63 100644--- a/packages/wm/src/containers/commands/toggle_tiling_direction.rs+++ b/packages/wm/src/containers/commands/toggle_tiling_direction.rs@@ -4,7 +4,7 @@ use uuid::Uuid;use crate::{common::TilingDirection,containers::{- traits::{CommonGetters, TilingDirectionGetters, TilingSizeGetters},+ traits::{CommonGetters, TilingDirectionGetters},Container, DirectionContainer, SplitContainer,},user_config::UserConfig,@@ -15,8 +15,7 @@ use crate::{};use super::{- attach_container, detach_container, flatten_split_container,- replace_container,+ attach_container, flatten_split_container, replace_container,};pub fn toggle_tiling_direction(diff --git a/packages/wm/src/containers/traits/common_getters.rs b/packages/wm/src/containers/traits/common_getters.rsindex ca3b8cd1..6d39003d 100644--- a/packages/wm/src/containers/traits/common_getters.rs+++ b/packages/wm/src/containers/traits/common_getters.rs@@ -264,6 +264,11 @@ pub trait CommonGetters {Some(descendant)}++ /// Whether this container is currently focused.+ fn is_focused(&self) -> bool {+ self.self_and_ancestors().all(|c| c.focus_index() == 0)+ }}/// An iterator over ancestors of a given container.diff --git a/packages/wm/src/windows/commands/resize_window.rs b/packages/wm/src/windows/commands/resize_window.rsindex 4515fd43..b627058f 100644--- a/packages/wm/src/windows/commands/resize_window.rs+++ b/packages/wm/src/windows/commands/resize_window.rs@@ -141,7 +141,7 @@ fn resize_floating_window(// Prevent resize from making the window smaller than minimum dimensions.// Always allow the size to be increased, even if the window would still// be within minimum dimension values.- if resize_pixels < 0+ if resize_pixels <= 0&& (new_width < MIN_FLOATING_WIDTH || new_height < MIN_FLOATING_HEIGHT){return Ok(());diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rsindex 6215de2a..dd6e1a34 100644--- a/packages/wm/src/wm.rs+++ b/packages/wm/src/wm.rs@@ -15,7 +15,7 @@ use crate::{handle_window_destroyed, handle_window_focused,handle_window_hidden, handle_window_location_changed,handle_window_minimize_ended, handle_window_minimized,- handle_window_shown,+ handle_window_moved_or_resized, handle_window_shown,},platform::PlatformEvent,},@@ -82,7 +82,9 @@ impl WindowManager {PlatformEvent::WindowMinimizeEnded(window) => {handle_window_minimize_ended(window, &mut state, config)}- PlatformEvent::WindowMovedOrResized(_) => Ok(()),+ PlatformEvent::WindowMovedOrResized(window) => {+ handle_window_moved_or_resized(window, &mut state)+ }PlatformEvent::WindowShown(window) => {handle_window_shown(window, &mut state, config)}