Skip to content

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

feat: handle position and workspace updates of floating windows

Lars Bergera3f2f5679bc0
diff --git a/packages/wm/src/common/events/handle_window_location_changed.rs b/packages/wm/src/common/events/handle_window_location_changed.rs
index fbc8f1a2..d4a0ab51 100644
--- a/packages/wm/src/common/events/handle_window_location_changed.rs
+++ b/packages/wm/src/common/events/handle_window_location_changed.rs
@@ -1,7 +1,11 @@
+use anyhow::Context;
use tracing::info;
use crate::{
common::platform::NativeWindow,
+ containers::{
+ commands::move_container_within_tree, traits::CommonGetters,
+ },
user_config::{FullscreenStateConfig, UserConfig},
windows::{
commands::{toggle_window_state, update_window_state},
@@ -50,6 +54,31 @@ pub fn handle_window_location_changed(
state,
config,
)?;
+ } else if matches!(window.state(), WindowState::Floating(_)) {
+ // Update state with the new location of the floating window.
+ let new_position = window.native().outer_position()?;
+ window.set_floating_placement(new_position);
+
+ let workspace = window.workspace().context("No workspace.")?;
+
+ // Get workspace that encompasses most of the window after moving.
+ let updated_workspace = state
+ .nearest_monitor(&window.native())
+ .and_then(|monitor| monitor.displayed_workspace())
+ .context("Failed to get workspace of nearest monitor.")?;
+
+ // Update the window's workspace if it goes out of bounds of its
+ // current workspace.
+ if workspace.id() != updated_workspace.id() {
+ info!("Window moved to new workspace.");
+
+ move_container_within_tree(
+ window.into(),
+ updated_workspace.clone().into(),
+ updated_workspace.child_count(),
+ state,
+ )?;
+ }
}
}
}
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.rs
index 5927270d..241b9569 100644
--- a/packages/wm/src/common/events/handle_window_moved_or_resized.rs
+++ b/packages/wm/src/common/events/handle_window_moved_or_resized.rs
@@ -2,110 +2,56 @@ use anyhow::Context;
use tracing::info;
use crate::{
- common::{platform::NativeWindow, LengthValue, Rect, ResizeDimension},
+ common::{platform::NativeWindow, LengthValue, ResizeDimension},
containers::{
- commands::move_container_within_tree,
traits::{CommonGetters, PositionGetters},
WindowContainer,
},
- windows::{
- commands::resize_window, traits::WindowGetters, NonTilingWindow,
- TilingWindow, WindowState,
- },
+ windows::{commands::resize_window, traits::WindowGetters},
wm_state::WmState,
};
+/// Handles the event for when a window is finished being moved or resized
+/// by the user (e.g. via the window's drag handles).
+///
+/// This resizes the window if it's a tiling window.
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 {
+ if let Some(WindowContainer::TilingWindow(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<()> {
- let parent = window.parent().context("No parent.")?;
-
- // Snap window to its original position if it's the only window in the
- // workspace.
- if parent.is_workspace() && window.tiling_siblings().count() == 0 {
- state.containers_to_redraw.push(window.into());
- return Ok(());
- }
+ info!("Tiling window moved/resized");
- let new_position = window.native().outer_position()?;
- let delta_width = new_position.width() - window.width()?;
- let delta_height = new_position.height() - window.height()?;
+ let parent = window.parent().context("No parent.")?;
- resize_window(
- window.clone().into(),
- ResizeDimension::Width,
- LengthValue::new_px(delta_width as f32),
- state,
- )?;
-
- resize_window(
- window.into(),
- ResizeDimension::Height,
- LengthValue::new_px(delta_height as f32),
- state,
- )
-}
-
-fn update_floating_window(
- window: NonTilingWindow,
- state: &mut WmState,
-) -> anyhow::Result<()> {
- // Update state with new location of the floating window.
- let new_position = window.native().outer_position()?;
- window.set_floating_placement(new_position);
-
- // Change floating window's parent workspace if moved out of its bounds.
- update_parent_workspace(window.clone().into(), state)
-}
-
-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())
- .and_then(|monitor| monitor.displayed_workspace())
- .context("Failed to get workspace of nearest monitor.")?;
+ // Snap window to its original position if it's the only window in the
+ // workspace.
+ if parent.is_workspace() && window.tiling_siblings().count() == 0 {
+ state.containers_to_redraw.push(window.into());
+ return Ok(());
+ }
- // Ignore if window is still within the bounds of its current workspace.
- if target_workspace.id()
- == window.workspace().context("No workspace.")?.id()
- {
- return Ok(());
+ let new_position = window.native().outer_position()?;
+ let delta_width = new_position.width() - window.width()?;
+ let delta_height = new_position.height() - window.height()?;
+
+ resize_window(
+ window.clone().into(),
+ ResizeDimension::Width,
+ LengthValue::new_px(delta_width as f32),
+ state,
+ )?;
+
+ resize_window(
+ window.into(),
+ ResizeDimension::Height,
+ LengthValue::new_px(delta_height as f32),
+ state,
+ )?;
}
- // Change the window's parent workspace.
- move_container_within_tree(
- window.clone().into(),
- target_workspace.clone().into(),
- target_workspace.child_count(),
- state,
- )
+ Ok(())
}