GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: remove unnecessary float casting when using `LengthValue`
Lars Bergerbe1a64b2af23
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rsindex 20df2f8c..3fc674a2 100644--- a/packages/wm/src/app_command.rs+++ b/packages/wm/src/app_command.rs@@ -262,10 +262,10 @@ impl InvokeCommand {Ok(window) => {let args = args.clone();let border_delta = RectDelta::new(- args.left.unwrap_or(LengthValue::new_px(0.)),- args.top.unwrap_or(LengthValue::new_px(0.)),- args.right.unwrap_or(LengthValue::new_px(0.)),- args.bottom.unwrap_or(LengthValue::new_px(0.)),+ args.left.unwrap_or(LengthValue::from_px(0)),+ args.top.unwrap_or(LengthValue::from_px(0)),+ args.right.unwrap_or(LengthValue::from_px(0)),+ args.bottom.unwrap_or(LengthValue::from_px(0)),);window.set_border_delta(border_delta);diff --git a/packages/wm/src/common/events/handle_window_location_changed.rs b/packages/wm/src/common/events/handle_window_location_changed.rsindex 70b9733e..0b1812d6 100644--- a/packages/wm/src/common/events/handle_window_location_changed.rs+++ b/packages/wm/src/common/events/handle_window_location_changed.rs@@ -4,8 +4,7 @@ use tracing::info;use crate::{common::platform::NativeWindow,containers::{- commands::move_container_within_tree,- traits::{CommonGetters, PositionGetters},+ commands::move_container_within_tree, traits::CommonGetters,},user_config::{FullscreenStateConfig, UserConfig},windows::{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 f6622210..c6ac3b3f 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@@ -40,8 +40,8 @@ pub fn handle_window_moved_or_resized(resize_window(window.clone().into(),- Some(LengthValue::new_px(width_delta as f32)),- Some(LengthValue::new_px(height_delta as f32)),+ Some(LengthValue::from_px(width_delta)),+ Some(LengthValue::from_px(height_delta)),state,)?;}diff --git a/packages/wm/src/common/length_value.rs b/packages/wm/src/common/length_value.rsindex 5fb549f5..0c8b6337 100644--- a/packages/wm/src/common/length_value.rs+++ b/packages/wm/src/common/length_value.rs@@ -13,32 +13,29 @@ pub struct LengthValue {#[derive(Debug, Deserialize, Clone, Serialize)]#[serde(rename_all = "snake_case")]pub enum LengthUnit {- Pixel,Percentage,+ Pixel,}impl LengthValue {- // TODO: Rename `from_px`.- pub fn new_px(amount: f32) -> Self {+ pub fn from_px(px: i32) -> Self {Self {- amount,+ amount: px as f32,unit: LengthUnit::Pixel,}}- pub fn to_pixels(&self, total: i32) -> i32 {+ pub fn to_px(&self, total_px: i32) -> i32 {match self.unit {+ LengthUnit::Percentage => (self.amount * total_px as f32) as i32,LengthUnit::Pixel => self.amount as i32,- LengthUnit::Percentage => {- (self.amount / 100.0 * total as f32) as i32- }}}- pub fn to_percent(&self, total: i32) -> f32 {+ pub fn to_percentage(&self, total_px: i32) -> f32 {match self.unit {- LengthUnit::Pixel => self.amount / total as f32,- LengthUnit::Percentage => self.amount / 100.0,+ LengthUnit::Percentage => self.amount,+ LengthUnit::Pixel => self.amount / total_px as f32,}}}@@ -54,7 +51,7 @@ impl FromStr for LengthValue {/// 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+)(%|px)?")?;let err_msg = format!("Not a valid length value '{}'. Must be of format '10px' or '10%'.",@@ -65,9 +62,6 @@ impl FromStr for LengthValue {.captures(unparsed).context(err_msg.to_string())?;- let amount =- f32::from_str(&captures[1]).context(err_msg.to_string())?;-let unit_str = captures.get(2).map_or("", |m| m.as_str());let unit = match unit_str {"px" | "" => LengthUnit::Pixel,@@ -75,6 +69,16 @@ impl FromStr for LengthValue {_ => bail!(err_msg),};+ let amount = captures+ .get(1)+ .and_then(|amount_str| f32::from_str(amount_str.into()).ok())+ // Store percentage units as a fraction of 1.+ .map(|amount| match unit {+ LengthUnit::Pixel => amount,+ LengthUnit::Percentage => amount / 100.0,+ })+ .context(err_msg.to_string())?;+Ok(LengthValue { amount, unit })}}diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rsindex edfa2865..9676ac8d 100644--- a/packages/wm/src/common/platform/native_window.rs+++ b/packages/wm/src/common/platform/native_window.rs@@ -392,10 +392,10 @@ impl NativeWindow {let frame_pos = self.frame_position()?;Ok(RectDelta::new(- LengthValue::new_px((frame_pos.left - border_pos.left) as f32),- LengthValue::new_px((frame_pos.top - border_pos.top) as f32),- LengthValue::new_px((border_pos.right - frame_pos.right) as f32),- LengthValue::new_px((border_pos.bottom - frame_pos.bottom) as f32),+ LengthValue::from_px(frame_pos.left - border_pos.left),+ LengthValue::from_px(frame_pos.top - border_pos.top),+ LengthValue::from_px(border_pos.right - frame_pos.right),+ LengthValue::from_px(border_pos.bottom - frame_pos.bottom),))}diff --git a/packages/wm/src/common/rect.rs b/packages/wm/src/common/rect.rsindex c8748aba..9de7cef5 100644--- a/packages/wm/src/common/rect.rs+++ b/packages/wm/src/common/rect.rs@@ -118,20 +118,20 @@ impl Rect {// TODO: Pass in a `Rect` for unit conversion.pub fn apply_delta(&self, delta: &RectDelta) -> Self {Self::from_ltrb(- self.left - delta.left.to_pixels(self.width()),- self.top - delta.top.to_pixels(self.height()),- self.right + delta.right.to_pixels(self.width()),- self.bottom + delta.bottom.to_pixels(self.height()),+ self.left - delta.left.to_px(self.width()),+ self.top - delta.top.to_px(self.height()),+ self.right + delta.right.to_px(self.width()),+ self.bottom + delta.bottom.to_px(self.height()),)}// TODO: Pass in a `Rect` for unit conversion.pub fn apply_inverse_delta(&self, delta: &RectDelta) -> Self {Self::from_ltrb(- self.left + delta.left.to_pixels(self.width()),- self.top + delta.top.to_pixels(self.height()),- self.right - delta.right.to_pixels(self.width()),- self.bottom - delta.bottom.to_pixels(self.height()),+ self.left + delta.left.to_px(self.width()),+ self.top + delta.top.to_px(self.height()),+ self.right - delta.right.to_px(self.width()),+ self.bottom - delta.bottom.to_px(self.height()),)}diff --git a/packages/wm/src/containers/traits/common_getters.rs b/packages/wm/src/containers/traits/common_getters.rsindex 39fa5603..3bfe6861 100644--- a/packages/wm/src/containers/traits/common_getters.rs+++ b/packages/wm/src/containers/traits/common_getters.rs@@ -3,7 +3,6 @@ use std::{collections::VecDeque,};-use anyhow::Context;use enum_dispatch::enum_dispatch;use uuid::Uuid;diff --git a/packages/wm/src/containers/traits/position_getters.rs b/packages/wm/src/containers/traits/position_getters.rsindex da061957..71f67264 100644--- a/packages/wm/src/containers/traits/position_getters.rs+++ b/packages/wm/src/containers/traits/position_getters.rs@@ -45,7 +45,7 @@ macro_rules! impl_position_getters_as_resizable {match parent.tiling_direction() {TilingDirection::Vertical => parent.width(),TilingDirection::Horizontal => {- let inner_gap = self.inner_gap().to_pixels(+ let inner_gap = self.inner_gap().to_px(self.monitor().context("No parent monitor.")?.width()?,);@@ -66,7 +66,7 @@ macro_rules! impl_position_getters_as_resizable {match parent.tiling_direction() {TilingDirection::Horizontal => parent.height(),TilingDirection::Vertical => {- let inner_gap = self.inner_gap().to_pixels(+ let inner_gap = self.inner_gap().to_px(self.monitor().context("No parent monitor.")?.width()?,);@@ -95,7 +95,7 @@ macro_rules! impl_position_getters_as_resizable {return parent.x();}- let inner_gap = self.inner_gap().to_pixels(+ let inner_gap = self.inner_gap().to_px(self.monitor().context("No parent monitor.")?.width()?,);@@ -121,7 +121,7 @@ macro_rules! impl_position_getters_as_resizable {return parent.y();}- let inner_gap = self.inner_gap().to_pixels(+ let inner_gap = self.inner_gap().to_px(self.monitor().context("No parent monitor.")?.width()?,);diff --git a/packages/wm/src/windows/commands/manage_window.rs b/packages/wm/src/windows/commands/manage_window.rsindex e9eac040..f0f77746 100644--- a/packages/wm/src/windows/commands/manage_window.rs+++ b/packages/wm/src/windows/commands/manage_window.rs@@ -115,10 +115,10 @@ fn create_window(// Window has no border delta unless it's later changed via the// `adjust_borders` command.let border_delta = RectDelta::new(- LengthValue::new_px(0.),- LengthValue::new_px(0.),- LengthValue::new_px(0.),- LengthValue::new_px(0.),+ LengthValue::from_px(0),+ LengthValue::from_px(0),+ LengthValue::from_px(0),+ LengthValue::from_px(0),);let inner_gap = config.value.gaps.inner_gap.clone();diff --git a/packages/wm/src/windows/commands/resize_window.rs b/packages/wm/src/windows/commands/resize_window.rsindex d94aba4a..59ae172f 100644--- a/packages/wm/src/windows/commands/resize_window.rs+++ b/packages/wm/src/windows/commands/resize_window.rs@@ -30,14 +30,14 @@ pub fn resize_window(.and_then(|parent| parent.width().ok()).map(|parent_width| {parent_width- - tiling_window.inner_gap().to_pixels(monitor_rect.width())+ - tiling_window.inner_gap().to_px(monitor_rect.width())* tiling_window.tiling_siblings().count() as i32}),_ => window.parent().and_then(|parent| parent.width().ok()),};parent_width.map(|parent_width| {- let delta_px = delta.to_pixels(parent_width);+ let delta_px = delta.to_px(parent_width);window_rect.width() + delta_px})}@@ -53,14 +53,14 @@ pub fn resize_window(.and_then(|parent| parent.height().ok()).map(|parent_height| {parent_height- - tiling_window.inner_gap().to_pixels(monitor_rect.height())+ - tiling_window.inner_gap().to_px(monitor_rect.height())* tiling_window.tiling_siblings().count() as i32}),_ => window.parent().and_then(|parent| parent.height().ok()),};parent_height.map(|parent_height| {- let delta_px = delta.to_pixels(parent_height);+ let delta_px = delta.to_px(parent_height);window_rect.height() + delta_px})}@@ -69,10 +69,8 @@ pub fn resize_window(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)),+ target_width.map(|target_width| LengthValue::from_px(target_width)),+ target_height.map(|target_height| LengthValue::from_px(target_height)),state,)?;diff --git a/packages/wm/src/windows/commands/set_window_size.rs b/packages/wm/src/windows/commands/set_window_size.rsindex f65cc105..4cecdf4c 100644--- a/packages/wm/src/windows/commands/set_window_size.rs+++ b/packages/wm/src/windows/commands/set_window_size.rs@@ -78,18 +78,18 @@ fn set_tiling_window_length(let parent_length = match is_width_resize {true => {parent.width()?- - window.inner_gap().to_pixels(monitor_rect.width())+ - window.inner_gap().to_px(monitor_rect.width())* window.tiling_siblings().count() as i32}false => {parent.height()?- - window.inner_gap().to_pixels(monitor_rect.height())+ - window.inner_gap().to_px(monitor_rect.height())* window.tiling_siblings().count() as i32}};// Convert the target length to a tiling size.- let tiling_size = target_length.to_percent(parent_length);+ let tiling_size = target_length.to_percentage(parent_length);// Skip the resize if the window is already at the target size.if container_to_resize.tiling_size() - tiling_size != 0. {@@ -132,7 +132,7 @@ fn set_floating_window_size(};let target_width_px = target_width- .map(|target_width| target_width.to_pixels(monitor_rect.width()));+ .map(|target_width| target_width.to_px(monitor_rect.width()));let new_width = length_with_clamp(target_width_px,@@ -141,7 +141,7 @@ fn set_floating_window_size();let target_height_px = target_height- .map(|target_height| target_height.to_pixels(monitor_rect.height()));+ .map(|target_height| target_height.to_px(monitor_rect.height()));let new_height = length_with_clamp(target_height_px,diff --git a/packages/wm/src/windows/traits/window_getters.rs b/packages/wm/src/windows/traits/window_getters.rsindex e57ca37c..25603f43 100644--- a/packages/wm/src/windows/traits/window_getters.rs+++ b/packages/wm/src/windows/traits/window_getters.rs@@ -72,21 +72,17 @@ pub trait WindowGetters {// TODO: Allow percentage length values.Ok(RectDelta {- left: LengthValue::new_px(- (border_delta.left.to_pixels(0)- + shadow_border_delta.left.to_pixels(0)) as f32,+ left: LengthValue::from_px(+ border_delta.left.to_px(0) + shadow_border_delta.left.to_px(0),),- right: LengthValue::new_px(- (border_delta.right.to_pixels(0)- + shadow_border_delta.right.to_pixels(0)) as f32,+ right: LengthValue::from_px(+ border_delta.right.to_px(0) + shadow_border_delta.right.to_px(0),),- top: LengthValue::new_px(- (border_delta.top.to_pixels(0)- + shadow_border_delta.top.to_pixels(0)) as f32,+ top: LengthValue::from_px(+ border_delta.top.to_px(0) + shadow_border_delta.top.to_px(0),),- bottom: LengthValue::new_px(- (border_delta.bottom.to_pixels(0)- + shadow_border_delta.bottom.to_pixels(0)) as f32,+ bottom: LengthValue::from_px(+ border_delta.bottom.to_px(0) + shadow_border_delta.bottom.to_px(0),),})}