GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: implement `set_window_size` command
Lars Berger445f11a6d382
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rsindex 0e17c992..20df2f8c 100644--- a/packages/wm/src/app_command.rs+++ b/packages/wm/src/app_command.rs@@ -12,7 +12,7 @@ use crate::{cycle_focus, disable_binding_mode, enable_binding_mode,reload_config, shell_exec,},- Direction, LengthValue, RectDelta, ResizeDimension,+ Direction, LengthValue, RectDelta,},containers::{commands::{focus_in_direction, toggle_tiling_direction},@@ -23,7 +23,7 @@ use crate::{windows::{commands::{ignore_window, move_window_in_direction, move_window_to_workspace,- resize_window, update_window_state,+ resize_window, set_window_size, update_window_state,},traits::WindowGetters,WindowState,@@ -203,6 +203,8 @@ pub enum InvokeCommand {#[clap(long, num_args = 1.., allow_hyphen_values = true)]command: Vec<String>,},+ // Reuse `InvokeResizeCommand` struct.+ Size(InvokeResizeCommand),ToggleFloating {#[clap(long, default_missing_value = "true", require_equals = true, num_args = 0..=1)]shown_on_top: Option<bool>,@@ -337,27 +339,12 @@ impl InvokeCommand {InvokeCommand::MoveWorkspace { direction } => todo!(),InvokeCommand::Resize(args) => {match subject_container.as_window_container() {- Ok(window) => {- if let Some(width) = &args.width {- resize_window(- window.clone(),- ResizeDimension::Width,- width.clone(),- state,- )?- }-- if let Some(height) = &args.height {- resize_window(- window,- ResizeDimension::Height,- height.clone(),- state,- )?- }-- Ok(())- }+ Ok(window) => resize_window(+ window,+ args.width.clone(),+ args.height.clone(),+ state,+ ),_ => Ok(()),}}@@ -429,6 +416,17 @@ impl InvokeCommand {InvokeCommand::ShellExec { command } => {shell_exec(&command.join(" "))}+ InvokeCommand::Size(args) => {+ match subject_container.as_window_container() {+ Ok(window) => set_window_size(+ window,+ args.width.clone(),+ args.height.clone(),+ state,+ ),+ _ => Ok(()),+ }+ }InvokeCommand::ToggleFloating {centered,shown_on_top,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.rsindex f64fc720..f6622210 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,7 +2,7 @@ use anyhow::Context;use tracing::info;use crate::{- common::{platform::NativeWindow, LengthValue, ResizeDimension},+ common::{platform::NativeWindow, LengthValue},containers::{traits::{CommonGetters, PositionGetters},WindowContainer,@@ -35,20 +35,13 @@ pub fn handle_window_moved_or_resized(}let frame_position = window.native().refresh_frame_position()?;- let delta_width = frame_position.width() - window.width()?;- let delta_height = frame_position.height() - window.height()?;+ let width_delta = frame_position.width() - window.width()?;+ let height_delta = frame_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),+ Some(LengthValue::new_px(width_delta as f32)),+ Some(LengthValue::new_px(height_delta as f32)),state,)?;}diff --git a/packages/wm/src/common/length_value.rs b/packages/wm/src/common/length_value.rsindex d4b7f212..5fb549f5 100644--- a/packages/wm/src/common/length_value.rs+++ b/packages/wm/src/common/length_value.rs@@ -18,6 +18,7 @@ pub enum LengthUnit {}impl LengthValue {+ // TODO: Rename `from_px`.pub fn new_px(amount: f32) -> Self {Self {amount,diff --git a/packages/wm/src/common/mod.rs b/packages/wm/src/common/mod.rsindex a3fbffcd..7e2e012b 100644--- a/packages/wm/src/common/mod.rs+++ b/packages/wm/src/common/mod.rs@@ -9,7 +9,6 @@ pub mod platform;mod point;mod rect;mod rect_delta;-mod resize_dimension;mod tiling_direction;mod vec_deque_ext;@@ -21,6 +20,5 @@ pub use memo::*;pub use point::*;pub use rect::*;pub use rect_delta::*;-pub use resize_dimension::*;pub use tiling_direction::*;pub use vec_deque_ext::*;diff --git a/packages/wm/src/common/resize_dimension.rs b/packages/wm/src/common/resize_dimension.rsdeleted file mode 100644index fc059bb3..00000000--- a/packages/wm/src/common/resize_dimension.rs+++ /dev/null@@ -1,5 +0,0 @@-#[derive(Clone, Debug, PartialEq)]-pub enum ResizeDimension {- Width,- Height,-}diff --git a/packages/wm/src/containers/commands/resize_tiling_container.rs b/packages/wm/src/containers/commands/resize_tiling_container.rsindex 18183db1..727a5446 100644--- a/packages/wm/src/containers/commands/resize_tiling_container.rs+++ b/packages/wm/src/containers/commands/resize_tiling_container.rs@@ -5,6 +5,7 @@ use crate::containers::{const MIN_TILING_SIZE: f32 = 0.01;+// TODO: Rename to `set_tiling_container_size`.pub fn resize_tiling_container(container_to_resize: &TilingContainer,target_size: f32,diff --git a/packages/wm/src/containers/traits/common_getters.rs b/packages/wm/src/containers/traits/common_getters.rsindex 670c88f5..39fa5603 100644--- a/packages/wm/src/containers/traits/common_getters.rs+++ b/packages/wm/src/containers/traits/common_getters.rs@@ -288,45 +288,6 @@ pub trait CommonGetters {.chain(end_ancestor.clone()).all(|ancestor| ancestor.focus_index() == 0)}-- fn containers_to_flatten_on_detach(- &self,- ) -> anyhow::Result<Vec<SplitContainer>> {- let parent = self.parent().context("No parent.")?;- let mut containers_to_flatten = Vec::new();-- // Flatten the parent split container if it'll be empty after removing- // the child.- if let Some(split_parent) = parent.as_split() {- if split_parent.child_count() == 1 {- containers_to_flatten.push(split_parent.clone());- }- }-- let tiling_siblings = self.tiling_siblings().collect::<Vec<_>>();-- // If there is exactly *one* sibling to the detached container, then- // flatten that sibling if it's a split container. This is for layouts- // like V[1 H[2]] where container 1 gets detached, resulting in V[2].- if tiling_siblings.len() == 1 {- if let Some(split_sibling) = tiling_siblings[0].as_split() {- let split_sibling_parent =- split_sibling.parent().context("No parent.")?;-- containers_to_flatten.push(split_sibling.clone());-- // Additionally flatten parent in deeply nested layouts. This for- // layouts like H[1 V[2 H[3]]] where container 2 gets detached,- // resulting in H[1 3].- if let Some(split_sibling_parent) = split_sibling_parent.as_split()- {- containers_to_flatten.push(split_sibling_parent.clone());- }- }- }-- Ok(containers_to_flatten)- }}/// An iterator over ancestors of a given container.diff --git a/packages/wm/src/containers/traits/tiling_size_getters.rs b/packages/wm/src/containers/traits/tiling_size_getters.rsindex 162f059f..50d09bc0 100644--- a/packages/wm/src/containers/traits/tiling_size_getters.rs+++ b/packages/wm/src/containers/traits/tiling_size_getters.rs@@ -1,8 +1,12 @@+use anyhow::Context;use enum_dispatch::enum_dispatch;-use crate::{common::LengthValue, containers::TilingContainer};+use crate::{+ common::{LengthValue, TilingDirection},+ containers::{Container, DirectionContainer, TilingContainer},+};-use super::CommonGetters;+use super::{CommonGetters, TilingDirectionGetters};#[enum_dispatch]pub trait TilingSizeGetters: CommonGetters {@@ -13,6 +17,46 @@ pub trait TilingSizeGetters: CommonGetters {fn inner_gap(&self) -> LengthValue;fn set_inner_gap(&self, inner_gap: LengthValue);++ /// Gets the container to resize when resizing a tiling window.+ fn container_to_resize(+ &self,+ is_width_resize: bool,+ ) -> anyhow::Result<Option<TilingContainer>> {+ let parent = self.direction_container().context("No parent.")?;++ let tiling_direction = parent.tiling_direction();++ // Whether the resize is in the inverse of its tiling direction.+ let is_inverse_resize = match tiling_direction {+ TilingDirection::Horizontal => !is_width_resize,+ TilingDirection::Vertical => is_width_resize,+ };++ let container_to_resize = match is_inverse_resize {+ true => match parent {+ // Prevent workspaces from being resized.+ DirectionContainer::Split(parent) => Some(parent.into()),+ _ => None,+ },+ false => {+ let grandparent = parent.parent().context("No grandparent.")?;++ match self.tiling_siblings().count() > 0 {+ // Window can only be resized if it has siblings.+ true => Some(self.as_tiling_container()?),+ // Resize grandparent in layouts like H[1 V[2 H[3]]], where+ // container 3 is resized horizontally.+ false => match grandparent {+ Container::Split(grandparent) => Some(grandparent.into()),+ _ => None,+ },+ }+ }+ };++ Ok(container_to_resize)+ }}/// Implements the `TilingSizeGetters` trait for a given struct.diff --git a/packages/wm/src/windows/commands/mod.rs b/packages/wm/src/windows/commands/mod.rsindex c789fe2a..0561c91f 100644--- a/packages/wm/src/windows/commands/mod.rs+++ b/packages/wm/src/windows/commands/mod.rs@@ -3,6 +3,7 @@ mod manage_window;mod move_window_in_direction;mod move_window_to_workspace;mod resize_window;+mod set_window_size;mod unmanage_window;mod update_window_state;@@ -11,5 +12,6 @@ pub use manage_window::*;pub use move_window_in_direction::*;pub use move_window_to_workspace::*;pub use resize_window::*;+pub use set_window_size::*;pub use unmanage_window::*;pub use update_window_state::*;diff --git a/packages/wm/src/windows/commands/resize_window.rs b/packages/wm/src/windows/commands/resize_window.rsindex e5bea652..afe3cb91 100644--- a/packages/wm/src/windows/commands/resize_window.rs+++ b/packages/wm/src/windows/commands/resize_window.rs@@ -1,164 +1,66 @@-use anyhow::Context;-use crate::{- common::{LengthValue, Rect, ResizeDimension, TilingDirection},+ common::LengthValue,containers::{- commands::resize_tiling_container,- traits::{- CommonGetters, PositionGetters, TilingDirectionGetters,- TilingSizeGetters,- },- Container, DirectionContainer, TilingContainer, WindowContainer,- },- windows::{- traits::WindowGetters, NonTilingWindow, TilingWindow, WindowState,+ traits::{CommonGetters, PositionGetters, TilingSizeGetters},+ WindowContainer,},wm_state::WmState,};-const MIN_FLOATING_WIDTH: i32 = 250;-const MIN_FLOATING_HEIGHT: i32 = 140;+use super::set_window_size;pub fn resize_window(window: WindowContainer,- resize_dimension: ResizeDimension,- resize_amount: LengthValue,+ width_delta: Option<LengthValue>,+ height_delta: Option<LengthValue>,state: &mut WmState,) -> anyhow::Result<()> {- match window {- WindowContainer::TilingWindow(window) => {- resize_tiling_window(- window,- resize_dimension,- resize_amount,- state,- )?;- }- WindowContainer::NonTilingWindow(window) => {- if matches!(window.state(), WindowState::Floating(_)) {- resize_floating_window(- window,- resize_dimension,- resize_amount,- state,- )?;+ let window_rect = window.to_rect()?;++ let target_width = match width_delta {+ Some(delta) => {+ let parent_width = match window.as_tiling_container() {+ Ok(tiling_window) => tiling_window+ .container_to_resize(true)?+ .map(|container| container.as_container()),+ _ => Some(window.clone().into()),}- }- }+ .and_then(|container| container.parent())+ .and_then(|parent| parent.width().ok());- Ok(())-}--fn resize_tiling_window(- window: TilingWindow,- resize_dimension: ResizeDimension,- resize_amount: LengthValue,- state: &mut WmState,-) -> anyhow::Result<()> {- // When resizing a tiling window, the container to resize can actually be- // an ancestor split container.- if let Some(container_to_resize) =- container_to_resize(window.clone(), resize_dimension)?- {- // Convert to a percentage to increase/decrease the tiling size by.- let parent = container_to_resize.parent().context("No parent.")?;- let resize_delta = resize_amount.to_percent(parent.width()?);-- if resize_delta != 0. {- resize_tiling_container(- &container_to_resize,- container_to_resize.tiling_size() + resize_delta,- );-- state- .pending_sync- .containers_to_redraw- .extend(parent.tiling_children().map(Into::into));- }- }-- Ok(())-}--fn container_to_resize(- window: TilingWindow,- resize_dimension: ResizeDimension,-) -> anyhow::Result<Option<TilingContainer>> {- let parent = window.direction_container().context("No parent.")?;- let tiling_direction = parent.tiling_direction();-- // Whether the resize is in the inverse of its tiling direction.- let is_inverse_resize = match tiling_direction {- TilingDirection::Horizontal => {- resize_dimension == ResizeDimension::Height- }- TilingDirection::Vertical => {- resize_dimension == ResizeDimension::Width+ parent_width.map(|parent_width| {+ window_rect.width() + delta.to_pixels(parent_width)+ })}+ _ => None,};- let container_to_resize = match is_inverse_resize {- true => match parent {- // Prevent workspaces from being resized.- DirectionContainer::Split(parent) => Some(parent.into()),- _ => None,- },- false => {- let grandparent = parent.parent().context("No grandparent.")?;-- match window.tiling_siblings().count() > 0 {- // Window can only be resized if it has siblings.- true => Some(window.into()),- // Resize grandparent in layouts like H[1 V[2 H[3]]], where- // container 3 is resized horizontally.- false => match grandparent {- Container::Split(grandparent) => Some(grandparent.into()),- _ => None,- },+ let target_height = match height_delta {+ Some(delta) => {+ let parent_height = match window.as_tiling_container() {+ Ok(tiling_window) => tiling_window+ .container_to_resize(false)?+ .map(|container| container.as_container()),+ _ => Some(window.clone().into()),}+ .and_then(|container| container.parent())+ .and_then(|parent| parent.height().ok());++ parent_height.map(|parent_height| {+ window_rect.height() + delta.to_pixels(parent_height)+ })}+ _ => None,};- Ok(container_to_resize)-}--fn resize_floating_window(- window: NonTilingWindow,- resize_dimension: ResizeDimension,- resize_amount: LengthValue,- state: &mut WmState,-) -> anyhow::Result<()> {- let monitor = window.monitor().context("No monitor")?;- let resize_pixels = resize_amount.to_pixels(monitor.width()?);-- let mut new_width = window.floating_placement().width();- let mut new_height = window.floating_placement().height();-- match resize_dimension {- ResizeDimension::Width => new_width += resize_pixels,- ResizeDimension::Height => new_height += resize_pixels,- }-- // 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- && (new_width < MIN_FLOATING_WIDTH || new_height < MIN_FLOATING_HEIGHT)- {- return Ok(());- }-- window.set_floating_placement(Rect::from_xy(- window.floating_placement().x(),- window.floating_placement().y(),- new_width,- new_height,- ));-- state- .pending_sync- .containers_to_redraw- .push(window.clone().into());+ set_window_size(+ window.clone(),+ target_width+ .map(|target_width| LengthValue::new_px(target_width as f32)),+ target_height+ .map(|target_height| LengthValue::new_px(target_height as f32)),+ state,+ )?;Ok(())}diff --git a/packages/wm/src/windows/commands/set_window_size.rs b/packages/wm/src/windows/commands/set_window_size.rsnew file mode 100644index 00000000..a9fb31fa--- /dev/null+++ b/packages/wm/src/windows/commands/set_window_size.rs@@ -0,0 +1,154 @@+use anyhow::Context;++use crate::{+ common::{LengthValue, Rect},+ containers::{+ commands::resize_tiling_container,+ traits::{CommonGetters, PositionGetters, TilingSizeGetters},+ WindowContainer,+ },+ windows::{+ traits::WindowGetters, NonTilingWindow, TilingWindow, WindowState,+ },+ wm_state::WmState,+};++/// Arbitrary defaults for minimum floating window dimensions.+const MIN_FLOATING_WIDTH: i32 = 250;+const MIN_FLOATING_HEIGHT: i32 = 140;++pub fn set_window_size(+ window: WindowContainer,+ target_width: Option<LengthValue>,+ target_height: Option<LengthValue>,+ state: &mut WmState,+) -> anyhow::Result<()> {+ match window {+ WindowContainer::TilingWindow(window) => {+ set_tiling_window_size(window, target_width, target_height, state)?;+ }+ WindowContainer::NonTilingWindow(window) => {+ if matches!(window.state(), WindowState::Floating(_)) {+ set_floating_window_size(+ window,+ target_width,+ target_height,+ state,+ )?;+ }+ }+ }++ Ok(())+}++fn set_tiling_window_size(+ window: TilingWindow,+ target_width: Option<LengthValue>,+ target_height: Option<LengthValue>,+ state: &mut WmState,+) -> anyhow::Result<()> {+ if let Some(target_width) = target_width {+ set_tiling_window_length(window.clone(), target_width, true, state)?;+ }++ if let Some(target_height) = target_height {+ set_tiling_window_length(window.clone(), target_height, false, state)?;+ }++ Ok(())+}++/// Updates either the width or height of a tiling window.+fn set_tiling_window_length(+ window: TilingWindow,+ target_length: LengthValue,+ is_width_resize: bool,+ state: &mut WmState,+) -> anyhow::Result<()> {+ // When resizing a tiling window, the container to resize can actually be+ // an ancestor split container.+ let container_to_resize = window.container_to_resize(is_width_resize)?;++ if let Some(container_to_resize) = container_to_resize {+ let parent = container_to_resize.parent().context("No parent.")?;+ let parent_length = match is_width_resize {+ true => parent.width(),+ false => parent.height(),+ }?;++ // Convert the target length to a tiling size.+ let tiling_size = target_length.to_percent(parent_length);++ // Skip the resize if the window is already at the target size.+ if container_to_resize.tiling_size() - tiling_size != 0. {+ resize_tiling_container(&container_to_resize, tiling_size);++ state+ .pending_sync+ .containers_to_redraw+ .extend(parent.tiling_children().map(Into::into));+ }+ }++ Ok(())+}++fn set_floating_window_size(+ window: NonTilingWindow,+ target_width: Option<LengthValue>,+ target_height: Option<LengthValue>,+ state: &mut WmState,+) -> anyhow::Result<()> {+ let monitor = window.monitor().context("No monitor")?;+ let monitor_rect = monitor.to_rect()?;+ let window_rect = window.to_rect()?;++ // 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.+ let length_with_clamp =+ |target_length: Option<i32>, current_length, min_length| {+ target_length+ .map(|target_length| {+ if target_length >= current_length {+ target_length+ } else {+ target_length.max(min_length)+ }+ })+ .unwrap_or(current_length)+ };++ let target_width_px = target_width+ .map(|target_width| target_width.to_pixels(monitor_rect.width()));++ let new_width = length_with_clamp(+ target_width_px,+ window_rect.width(),+ MIN_FLOATING_WIDTH,+ );++ let target_height_px = target_height+ .map(|target_height| target_height.to_pixels(monitor_rect.height()));++ let new_height = length_with_clamp(+ target_height_px,+ window_rect.height(),+ MIN_FLOATING_HEIGHT,+ );++ window.set_floating_placement(Rect::from_xy(+ window.floating_placement().x(),+ window.floating_placement().y(),+ new_width,+ new_height,+ ));++ state+ .pending_sync+ .containers_to_redraw+ .push(window.clone().into());++ Ok(())+}