Skip to content

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

feat: be able to change tiling direction when moving window via mouse (#673)

Luca396182c572a3
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 4d1d352c..c063040a 100644
--- a/packages/wm/src/common/events/handle_window_location_changed.rs
+++ b/packages/wm/src/common/events/handle_window_location_changed.rs
@@ -188,8 +188,8 @@ fn update_drag_state(
if is_move {
let parent = window.parent().context("No parent")?;
- update_window_state(
- window.into(),
+ let window = update_window_state(
+ window.clone().into(),
WindowState::Floating(FloatingStateConfig {
centered: false,
..config.value.window_behavior.state_defaults.floating
@@ -198,10 +198,25 @@ fn update_drag_state(
config,
)?;
+ // Windows are added for redraw on state changes, so here we need to
+ // remove the window from the pending redraw.
+ state
+ .pending_sync
+ .containers_to_redraw
+ .retain(|container| container.id() != window.id());
+
// Flatten the parent split container if it only contains the window.
if let Some(split_parent) = parent.as_split() {
if split_parent.child_count() == 1 {
flatten_split_container(split_parent.clone())?;
+
+ // Hacky fix to redraw siblings after flattening. The parent is
+ // queued for redraw from the state change, which gets detached
+ // on flatten.
+ state
+ .pending_sync
+ .containers_to_redraw
+ .extend(window.tiling_siblings().map(Into::into));
}
}
}
diff --git a/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs b/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs
index aad10f0a..4bb3953d 100644
--- a/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs
+++ b/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs
@@ -4,12 +4,12 @@ use tracing::info;
use crate::{
common::{
platform::{NativeWindow, Platform},
- LengthValue, TilingDirection,
+ LengthValue, Point, Rect, TilingDirection,
},
containers::{
- commands::move_container_within_tree,
+ commands::{move_container_within_tree, wrap_in_split_container},
traits::{CommonGetters, PositionGetters, TilingDirectionGetters},
- DirectionContainer, TilingContainer, WindowContainer,
+ DirectionContainer, SplitContainer, TilingContainer, WindowContainer,
},
try_warn,
user_config::UserConfig,
@@ -97,6 +97,7 @@ fn drop_as_tiling_window(
info!("Tiling window drag end event.");
let mouse_pos = Platform::mouse_position()?;
+ let workspace = moved_window.workspace().context("No workspace.")?;
// Get the workspace, split containers, and other windows under the
// dragged window.
@@ -105,8 +106,6 @@ fn drop_as_tiling_window(
.into_iter()
.filter(|container| container.id() != moved_window.id());
- let workspace = moved_window.workspace().context("No workspace.")?;
-
// Get the deepest direction container under the dragged window.
let target_parent: DirectionContainer = containers_at_pos
.filter_map(|container| container.as_direction_container().ok())
@@ -131,50 +130,24 @@ fn drop_as_tiling_window(
return Ok(());
}
- let tiling_direction = target_parent.tiling_direction();
-
let nearest_container = target_parent
.children()
.into_iter()
.filter_map(|container| container.as_tiling_container().ok())
- .try_fold(None, |acc: Option<TilingContainer>, container| {
- let distance = |container: &TilingContainer| -> anyhow::Result<i32> {
- let rect = container.to_rect()?;
-
- Ok(match tiling_direction {
- TilingDirection::Horizontal => (rect.x() - mouse_pos.x)
- .abs()
- .min((rect.x() + rect.width() - mouse_pos.x).abs()),
- TilingDirection::Vertical => (rect.y() - mouse_pos.y)
- .abs()
- .min((rect.y() + rect.height() - mouse_pos.y).abs()),
- })
- };
-
- match acc {
- Some(acc) => {
- let is_nearer = distance(&acc)? < distance(&container)?;
- anyhow::Ok(Some(if is_nearer { acc } else { container }))
- }
- None => Ok(Some(container)),
+ .try_fold(None, |acc: Option<TilingContainer>, container| match acc {
+ Some(acc) => {
+ let is_nearer = acc.to_rect()?.distance_to_point(&mouse_pos)
+ < container.to_rect()?.distance_to_point(&mouse_pos);
+
+ anyhow::Ok(Some(if is_nearer { acc } else { container }))
}
+ None => Ok(Some(container)),
})?
.context("No nearest container.")?;
- let target_index = match tiling_direction {
- TilingDirection::Horizontal => {
- match mouse_pos.x < nearest_container.to_rect()?.center_point().x {
- true => nearest_container.index(),
- false => nearest_container.index() + 1,
- }
- }
- TilingDirection::Vertical => {
- match mouse_pos.y < nearest_container.to_rect()?.center_point().y {
- true => nearest_container.index(),
- false => nearest_container.index() + 1,
- }
- }
- };
+ let tiling_direction = target_parent.tiling_direction();
+ let drop_position =
+ drop_position(&mouse_pos, &nearest_container.to_rect()?);
let moved_window = update_window_state(
moved_window.clone().into(),
@@ -183,12 +156,54 @@ fn drop_as_tiling_window(
config,
)?;
- move_container_within_tree(
- moved_window.into(),
- target_parent.clone().into(),
- target_index,
- state,
- )?;
+ let should_split = nearest_container.is_tiling_window()
+ && match tiling_direction {
+ TilingDirection::Horizontal => {
+ drop_position == DropPosition::Top
+ || drop_position == DropPosition::Bottom
+ }
+ TilingDirection::Vertical => {
+ drop_position == DropPosition::Left
+ || drop_position == DropPosition::Right
+ }
+ };
+
+ if should_split {
+ let split_container = SplitContainer::new(
+ tiling_direction.inverse(),
+ config.value.gaps.inner_gap.clone(),
+ );
+
+ wrap_in_split_container(
+ split_container.clone(),
+ target_parent.clone().into(),
+ vec![nearest_container],
+ )?;
+
+ let target_index = match drop_position {
+ DropPosition::Top | DropPosition::Left => 0,
+ _ => 1,
+ };
+
+ move_container_within_tree(
+ moved_window.clone().into(),
+ split_container.into(),
+ target_index,
+ state,
+ )?;
+ } else {
+ let target_index = match drop_position {
+ DropPosition::Top | DropPosition::Left => nearest_container.index(),
+ _ => nearest_container.index() + 1,
+ };
+
+ move_container_within_tree(
+ moved_window.clone().into(),
+ target_parent.clone().into(),
+ target_index,
+ state,
+ )?;
+ }
state
.pending_sync
@@ -197,3 +212,34 @@ fn drop_as_tiling_window(
Ok(())
}
+
+/// Represents where the window was dropped over another.
+#[derive(Debug, Clone, PartialEq)]
+enum DropPosition {
+ Top,
+ Bottom,
+ Left,
+ Right,
+}
+
+/// Gets the drop position for a window based on the mouse position.
+///
+/// This approach divides the window rect into an "X", creating four
+/// triangular quadrants, to determine which side the cursor is closest to.
+fn drop_position(mouse_pos: &Point, rect: &Rect) -> DropPosition {
+ let delta_x = mouse_pos.x - rect.center_point().x;
+ let delta_y = mouse_pos.y - rect.center_point().y;
+
+ match delta_x.abs() > delta_y.abs() {
+ // Window is in the left or right triangle.
+ true => match delta_x > 0 {
+ true => DropPosition::Right,
+ false => DropPosition::Left,
+ },
+ // Window is in the top or bottom triangle.
+ false => match delta_y > 0 {
+ true => DropPosition::Bottom,
+ false => DropPosition::Top,
+ },
+ }
+}
diff --git a/packages/wm/src/common/rect.rs b/packages/wm/src/common/rect.rs
index 0301b014..353fc578 100644
--- a/packages/wm/src/common/rect.rs
+++ b/packages/wm/src/common/rect.rs
@@ -152,6 +152,18 @@ impl Rect {
pub fn contains_point(&self, point: &Point) -> bool {
let is_in_x = point.x >= self.left && point.x <= self.right;
let is_in_y = point.y >= self.top && point.y <= self.bottom;
- return is_in_x && is_in_y;
+ is_in_x && is_in_y
+ }
+
+ pub fn distance_to_point(&self, point: &Point) -> f32 {
+ let dx = (self.x() - point.x)
+ .abs()
+ .max((self.x() + self.width() - point.x).abs());
+
+ let dy = (self.y() - point.y)
+ .abs()
+ .max((self.y() + self.height() - point.y).abs());
+
+ ((dx * dx + dy * dy) as f32).sqrt()
}
}