GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: finalize `resize_window` command
Lars Berger2cb287902783
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rsindex 6204ebda..16d64753 100644--- a/packages/wm/src/app_command.rs+++ b/packages/wm/src/app_command.rs@@ -6,13 +6,13 @@ use serde::{Deserialize, Deserializer};use tracing::Level;use crate::{- common::{Direction, LengthValue},+ common::{Direction, LengthValue, ResizeDimension},containers::{commands::toggle_tiling_direction, traits::CommonGetters, Container,},user_config::{FloatingStateConfig, FullscreenStateConfig, UserConfig},windows::{- commands::{toggle_window_state, update_window_state},+ commands::{resize_window, toggle_window_state, update_window_state},traits::WindowGetters,WindowState,},@@ -223,7 +223,32 @@ impl InvokeCommand {InvokeCommand::Ignore => todo!(),InvokeCommand::Move(_) => todo!(),InvokeCommand::MoveWorkspace { direction } => todo!(),- InvokeCommand::Resize(_) => 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(()),+ }+ }InvokeCommand::SetFloating {centered,show_on_top,@@ -359,8 +384,8 @@ impl InvokeCommand {toggle_tiling_direction(subject_container, state, config)}InvokeCommand::WmDisableBindingMode { name } => todo!(),- InvokeCommand::WmExit => todo!(),InvokeCommand::WmEnableBindingMode { name } => todo!(),+ InvokeCommand::WmExit => todo!(),InvokeCommand::WmRedraw => {let root_container = state.root_container.clone();state.add_container_to_redraw(root_container.into());diff --git a/packages/wm/src/common/units.rs b/packages/wm/src/common/units.rsindex 09ec53bc..d8c1c29d 100644--- a/packages/wm/src/common/units.rs+++ b/packages/wm/src/common/units.rs@@ -32,19 +32,27 @@ impl LengthValue {}}}++ pub fn to_percent(&self, total: i32) -> f32 {+ match self.unit {+ LengthUnit::Pixel => self.amount / total as f32,+ LengthUnit::Percentage => self.amount / 100.0,+ }+ }}impl FromStr for LengthValue {type Err = anyhow::Error;/// Parses a string containing a number followed by a unit (`px`, `%`).+ /// Allows for negative numbers.////// Example:/// ```/// LengthValue::from_str("100px") // { amount: 100.0, unit: LengthUnit::Pixel }/// ```fn from_str(unparsed: &str) -> anyhow::Result<Self> {- let units_regex = Regex::new(r"(\d+)(%|ppt|px)?")?;+ let units_regex = Regex::new(r"(-?\d+)(%|ppt|px)?")?;let err_msg = format!("Not a valid length value '{}'. Must be of format '10px' or '10%'.",diff --git a/packages/wm/src/containers/commands/exec_process.rs b/packages/wm/src/containers/commands/exec_process.rsindex 3c3f5b9b..d414307a 100644--- a/packages/wm/src/containers/commands/exec_process.rs+++ b/packages/wm/src/containers/commands/exec_process.rs@@ -2,6 +2,7 @@ use anyhow::{Context, Result};use regex::Regex;use std::env;use std::process::Command;+use tracing::error;pub fn exec_process(process_name: &str, args: &[String]) -> Result<()> {// Expand user profile environment variablediff --git a/packages/wm/src/containers/commands/resize_tiling_container.rs b/packages/wm/src/containers/commands/resize_tiling_container.rsindex 8c0ee366..18183db1 100644--- a/packages/wm/src/containers/commands/resize_tiling_container.rs+++ b/packages/wm/src/containers/commands/resize_tiling_container.rs@@ -18,16 +18,18 @@ pub fn resize_tiling_container(return;}- let available_size = available_size(&tiling_siblings);-// Prevent the container from being smaller than the minimum size, and// larger than the space available from sibling containers.- let clamped_target_size =- target_size.clamp(MIN_TILING_SIZE, available_size);+ let clamped_target_size = target_size.clamp(+ MIN_TILING_SIZE,+ 1. - (tiling_siblings.len() as f32 * MIN_TILING_SIZE),+ );let size_delta = clamped_target_size - container_to_resize.tiling_size();container_to_resize.set_tiling_size(clamped_target_size);+ let available_size = available_size(&tiling_siblings);+// Distribute the available tiling size amongst its siblings.for sibling in &tiling_siblings {let sibling_size_delta = sibling_size_delta(@@ -58,9 +60,13 @@ fn sibling_size_delta(let con_available_size =sibling_to_resize.tiling_size() - MIN_TILING_SIZE;- // Get percentage of resize that affects this container. The available- // tiling size here can be 0 when the main container to resize is shrunk- // from the max tiling size.+ // Get percentage of resize that affects this container. When shrinking+ // sibling containers, they are decreased in proportion to their size+ // (i.e. larger containers are shrunk more). When sizing up sibling+ // containers, they are scaled up equally.+ //+ // The available tiling size can be 0 here when the main container to+ // resize is shrunk from the max tiling size.let resize_factor = if available_size == 0.0 || size_delta < 0.0 {1.0 / sibling_count as f32} else {diff --git a/packages/wm/src/containers/traits/common_getters.rs b/packages/wm/src/containers/traits/common_getters.rsindex 413d8db6..ca3b8cd1 100644--- a/packages/wm/src/containers/traits/common_getters.rs+++ b/packages/wm/src/containers/traits/common_getters.rs@@ -91,6 +91,17 @@ pub trait CommonGetters {.unwrap_or(0)}+ fn tiling_children(+ &self,+ ) -> Box<dyn Iterator<Item = TilingContainer> + '_> {+ Box::new(+ self+ .children()+ .into_iter()+ .filter_map(|container| container.try_into().ok()),+ )+ }+fn descendants(&self) -> Descendants {Descendants {stack: self.children(),diff --git a/packages/wm/src/windows/commands/resize_window.rs b/packages/wm/src/windows/commands/resize_window.rsindex 8ba7dd71..4515fd43 100644--- a/packages/wm/src/windows/commands/resize_window.rs+++ b/packages/wm/src/windows/commands/resize_window.rs@@ -4,10 +4,12 @@ use crate::{common::{LengthValue, Rect, ResizeDimension, TilingDirection},containers::{commands::resize_tiling_container,- traits::{CommonGetters, PositionGetters, TilingDirectionGetters},- Container, WindowContainer,+ traits::{+ CommonGetters, PositionGetters, TilingDirectionGetters,+ TilingSizeGetters,+ },+ Container, DirectionContainer, TilingContainer, WindowContainer,},- user_config::UserConfig,windows::{traits::WindowGetters, NonTilingWindow, TilingWindow, WindowState,},@@ -22,16 +24,16 @@ pub fn resize_window(resize_dimension: ResizeDimension,resize_amount: LengthValue,state: &mut WmState,- config: &UserConfig,) -> anyhow::Result<()> {match window {- WindowContainer::TilingWindow(window) => resize_tiling_window(- window,- resize_dimension,- resize_amount,- state,- config,- ),+ WindowContainer::TilingWindow(window) => {+ resize_tiling_window(+ window,+ resize_dimension,+ resize_amount,+ state,+ )?;+ }WindowContainer::NonTilingWindow(window) => {if matches!(window.state(), WindowState::Floating(_)) {resize_floating_window(@@ -39,13 +41,12 @@ pub fn resize_window(resize_dimension,resize_amount,state,- config,)?;}-- Ok(())}}++ Ok(())}fn resize_tiling_window(@@ -53,24 +54,26 @@ fn resize_tiling_window(resize_dimension: ResizeDimension,resize_amount: LengthValue,state: &mut WmState,- config: &UserConfig,) -> 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 window size by.- // let tiling_size_delta =- // resize_amount.to_percentage(container_to_resize.tiling_size());-- // resize_tiling_container(container_to_resize, tiling_size_delta);-- // // TODO: Return early if `clamped_resize_percentage` is 0 to avoid- // // unnecessary redraws.- // state.add_container_to_redraw(- // container_to_resize.parent().context("No parent.")?,- // );+ // 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+ .containers_to_redraw+ .extend(parent.tiling_children().map(|c| c.into()));+ }}Ok(())@@ -79,7 +82,7 @@ fn resize_tiling_window(fn container_to_resize(window: TilingWindow,resize_dimension: ResizeDimension,-) -> anyhow::Result<Option<Container>> {+) -> anyhow::Result<Option<TilingContainer>> {let parent = window.direction_container().context("No parent.")?;let tiling_direction = parent.tiling_direction();@@ -94,29 +97,28 @@ fn container_to_resize(};let container_to_resize = match is_inverse_resize {- true => parent.into(),+ true => match parent {+ // Prevent workspaces from being resized.+ DirectionContainer::Split(parent) => Some(parent.into()),+ _ => None,+ },false => {let grandparent = parent.parent().context("No grandparent.")?;- // Resize grandparent in layouts like H[1 V[2 H[3]]], where container- // 3 is resized horizontally.- if window.tiling_siblings().count() == 0 && grandparent.is_split() {- grandparent- } else {- window.into()+ 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,+ },}}};- // Ignore cases where the container to resize is a workspace or the only- // child.- if container_to_resize.tiling_siblings().count() > 0- || container_to_resize.is_workspace()- {- return Ok(None);- }-- Ok(Some(container_to_resize))+ Ok(container_to_resize)}fn resize_floating_window(@@ -124,24 +126,23 @@ fn resize_floating_window(resize_dimension: ResizeDimension,resize_amount: LengthValue,state: &mut WmState,- config: &UserConfig,) -> anyhow::Result<()> {let monitor = window.monitor().context("No monitor")?;- let amount = resize_amount.to_pixels(monitor.width()?);+ let resize_pixels = resize_amount.to_pixels(monitor.width()?);- let mut width = window.floating_placement().width();- let mut height = window.floating_placement().height();+ let mut new_width = window.floating_placement().width();+ let mut new_height = window.floating_placement().height();match resize_dimension {- ResizeDimension::Width => width += amount,- ResizeDimension::Height => height += amount,+ 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 amount < 0- && (width < MIN_FLOATING_WIDTH || height < MIN_FLOATING_HEIGHT)+ if resize_pixels < 0+ && (new_width < MIN_FLOATING_WIDTH || new_height < MIN_FLOATING_HEIGHT){return Ok(());}@@ -149,8 +150,8 @@ fn resize_floating_window(window.set_floating_placement(Rect::from_xy(window.floating_placement().x(),window.floating_placement().y(),- width,- height,+ new_width,+ new_height,));state.add_container_to_redraw(window.clone().into());diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex 66add6b6..ccf7755a 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -29,7 +29,7 @@ pub struct WmState {pub root_container: RootContainer,/// Containers (and their descendants) that have a pending redraw.- containers_to_redraw: Vec<Container>,+ pub containers_to_redraw: Vec<Container>,/// Whether native focus needs to be reassigned to the WM's focused/// container.