Skip to content

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

perf: remove individual position getters; use `to_rect` for all container position values

Lars Berger47f93c0686e8
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 c6ac3b3f..e155205c 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
@@ -34,9 +34,11 @@ pub fn handle_window_moved_or_resized(
return Ok(());
}
- let frame_position = window.native().refresh_frame_position()?;
- let width_delta = frame_position.width() - window.width()?;
- let height_delta = frame_position.height() - window.height()?;
+ let new_position = window.native().refresh_frame_position()?;
+ let old_position = window.to_rect()?;
+
+ let width_delta = new_position.width() - old_position.width();
+ let height_delta = new_position.height() - old_position.height();
resize_window(
window.clone().into(),
diff --git a/packages/wm/src/common/rect.rs b/packages/wm/src/common/rect.rs
index 9de7cef5..8b1da6f9 100644
--- a/packages/wm/src/common/rect.rs
+++ b/packages/wm/src/common/rect.rs
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
-use super::{Direction, RectDelta};
+use super::{Direction, Point, RectDelta};
#[derive(Debug, Deserialize, Clone, Serialize)]
pub struct Rect {
@@ -108,11 +108,11 @@ impl Rect {
)
}
- pub fn center_point(&self) -> (i32, i32) {
- (
- self.left + (self.width() / 2),
- self.top + (self.height() / 2),
- )
+ pub fn center_point(&self) -> Point {
+ Point {
+ x: self.left + (self.width() / 2),
+ y: self.top + (self.height() / 2),
+ }
}
// TODO: Pass in a `Rect` for unit conversion.
diff --git a/packages/wm/src/containers/commands/center_cursor_on_container.rs b/packages/wm/src/containers/commands/center_cursor_on_container.rs
index 4b8e65da..dfff1496 100644
--- a/packages/wm/src/containers/commands/center_cursor_on_container.rs
+++ b/packages/wm/src/containers/commands/center_cursor_on_container.rs
@@ -1,21 +1,17 @@
-use crate::common::platform::Platform;
-use crate::containers::traits::PositionGetters;
-use crate::containers::Container;
-use crate::user_config::UserConfig;
-use anyhow::{Error, Result};
+use crate::{
+ common::platform::Platform, containers::traits::PositionGetters,
+ containers::Container, user_config::UserConfig,
+};
pub fn center_cursor_on_container(
- target: &Container,
+ target: Container,
config: &UserConfig,
-) -> Result<(), Error> {
- if !config.value.general.cursor_jump.enabled {
- return Ok(());
+) -> anyhow::Result<()> {
+ match config.value.general.cursor_jump.enabled {
+ false => Ok(()),
+ true => {
+ let center = target.to_rect()?.center_point();
+ Platform::set_cursor_pos(center.x, center.y)
+ }
}
-
- let center_x = target.x()? + (target.width()? / 2);
- let center_y = target.y()? + (target.height()? / 2);
-
- Platform::set_cursor_pos(center_x, center_y)?;
-
- Ok(())
}
diff --git a/packages/wm/src/containers/root_container.rs b/packages/wm/src/containers/root_container.rs
index 6ea489a5..7affe2f5 100644
--- a/packages/wm/src/containers/root_container.rs
+++ b/packages/wm/src/containers/root_container.rs
@@ -4,10 +4,11 @@ use std::{
rc::Rc,
};
+use anyhow::bail;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
-use crate::{impl_common_getters, impl_container_debug};
+use crate::{common::Rect, impl_common_getters, impl_container_debug};
use super::{
traits::{CommonGetters, PositionGetters},
@@ -70,19 +71,7 @@ impl_container_debug!(RootContainer);
impl_common_getters!(RootContainer);
impl PositionGetters for RootContainer {
- fn width(&self) -> anyhow::Result<i32> {
- Ok(0)
- }
-
- fn height(&self) -> anyhow::Result<i32> {
- Ok(0)
- }
-
- fn x(&self) -> anyhow::Result<i32> {
- Ok(0)
- }
-
- fn y(&self) -> anyhow::Result<i32> {
- Ok(0)
+ fn to_rect(&self) -> anyhow::Result<Rect> {
+ bail!("Root container does not have a position.")
}
}
diff --git a/packages/wm/src/containers/split_container.rs b/packages/wm/src/containers/split_container.rs
index 68d356ed..60345792 100644
--- a/packages/wm/src/containers/split_container.rs
+++ b/packages/wm/src/containers/split_container.rs
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::{
- common::{LengthValue, TilingDirection},
+ common::{LengthValue, Rect, TilingDirection},
impl_common_getters, impl_container_debug,
impl_position_getters_as_resizable, impl_tiling_direction_getters,
impl_tiling_size_getters,
@@ -74,6 +74,7 @@ impl SplitContainer {
}
fn to_dto(&self) -> anyhow::Result<ContainerDto> {
+ let rect = self.to_rect()?;
let children = self
.children()
.iter()
@@ -87,10 +88,10 @@ impl SplitContainer {
child_focus_order: self.0.borrow().child_focus_order.clone().into(),
tiling_size: self.tiling_size(),
tiling_direction: self.tiling_direction(),
- width: self.width()?,
- height: self.height()?,
- x: self.x()?,
- y: self.y()?,
+ width: rect.width(),
+ height: rect.height(),
+ x: rect.x(),
+ y: rect.y(),
}))
}
}
diff --git a/packages/wm/src/containers/traits/position_getters.rs b/packages/wm/src/containers/traits/position_getters.rs
index 71f67264..787a50c2 100644
--- a/packages/wm/src/containers/traits/position_getters.rs
+++ b/packages/wm/src/containers/traits/position_getters.rs
@@ -9,22 +9,7 @@ use crate::{
#[enum_dispatch]
pub trait PositionGetters {
- fn width(&self) -> anyhow::Result<i32>;
-
- fn height(&self) -> anyhow::Result<i32>;
-
- fn x(&self) -> anyhow::Result<i32>;
-
- fn y(&self) -> anyhow::Result<i32>;
-
- fn to_rect(&self) -> anyhow::Result<Rect> {
- Ok(Rect::from_xy(
- self.x()?,
- self.y()?,
- self.width()?,
- self.height()?,
- ))
- }
+ fn to_rect(&self) -> anyhow::Result<Rect>;
}
/// Implements the `PositionGetters` trait for tiling containers that can
@@ -36,98 +21,70 @@ pub trait PositionGetters {
macro_rules! impl_position_getters_as_resizable {
($struct_name:ident) => {
impl PositionGetters for $struct_name {
- fn width(&self) -> anyhow::Result<i32> {
+ fn to_rect(&self) -> anyhow::Result<Rect> {
let parent = self
.parent()
- .and_then(|p| p.as_direction_container().ok())
+ .and_then(|parent| parent.as_direction_container().ok())
.context("Parent does not have a tiling direction.")?;
- match parent.tiling_direction() {
- TilingDirection::Vertical => parent.width(),
- TilingDirection::Horizontal => {
- let inner_gap = self.inner_gap().to_px(
- self.monitor().context("No parent monitor.")?.width()?,
- );
-
- let available_width = parent.width()?
- - inner_gap * self.tiling_siblings().count() as i32;
+ let parent_rect = parent.to_rect()?;
+ let monitor = self.monitor().context("No monitor.")?;
- Ok((self.tiling_size() * available_width as f32) as i32)
+ let inner_gap = match parent.tiling_direction() {
+ TilingDirection::Vertical => {
+ self.inner_gap().to_px(monitor.to_rect()?.height())
}
- }
- }
-
- fn height(&self) -> anyhow::Result<i32> {
- let parent = self
- .parent()
- .and_then(|p| p.as_direction_container().ok())
- .context("Parent does not have a tiling direction.")?;
+ TilingDirection::Horizontal => {
+ self.inner_gap().to_px(monitor.to_rect()?.width())
+ }
+ };
- match parent.tiling_direction() {
- TilingDirection::Horizontal => parent.height(),
+ let (width, height) = match parent.tiling_direction() {
TilingDirection::Vertical => {
- let inner_gap = self.inner_gap().to_px(
- self.monitor().context("No parent monitor.")?.width()?,
- );
-
- let available_height = parent.height()?
+ let available_height = parent_rect.height()
- inner_gap * self.tiling_siblings().count() as i32;
- Ok((self.tiling_size() * available_height as f32) as i32)
- }
- }
- }
+ let height =
+ (self.tiling_size() * available_height as f32) as i32;
- fn x(&self) -> anyhow::Result<i32> {
- let parent = self
- .parent()
- .and_then(|p| p.as_direction_container().ok())
- .context("Parent does not have a tiling direction.")?;
-
- let mut prev_siblings = self
- .prev_siblings()
- .filter_map(|c| c.as_tiling_container().ok());
-
- match prev_siblings.next() {
- None => parent.x(),
- Some(prev_sibling) => {
- if parent.tiling_direction() == TilingDirection::Vertical {
- return parent.x();
- }
-
- let inner_gap = self.inner_gap().to_px(
- self.monitor().context("No parent monitor.")?.width()?,
- );
-
- Ok(prev_sibling.x()? + prev_sibling.width()? + inner_gap)
+ (parent_rect.width(), height)
}
- }
- }
-
- fn y(&self) -> anyhow::Result<i32> {
- let parent = self
- .parent()
- .and_then(|p| p.as_direction_container().ok())
- .context("Parent does not have a tiling direction.")?;
+ TilingDirection::Horizontal => {
+ let available_width = parent_rect.width()
+ - inner_gap * self.tiling_siblings().count() as i32;
- let mut prev_siblings = self
- .prev_siblings()
- .filter_map(|c| c.as_tiling_container().ok());
+ let width =
+ (self.tiling_size() * available_width as f32) as i32;
- match prev_siblings.next() {
- None => parent.y(),
- Some(prev_sibling) => {
- if parent.tiling_direction() == TilingDirection::Horizontal {
- return parent.y();
+ (width, parent_rect.height())
+ }
+ };
+
+ let (x, y) = {
+ let mut prev_siblings = self
+ .prev_siblings()
+ .filter_map(|sibling| sibling.as_tiling_container().ok());
+
+ match prev_siblings.next() {
+ None => (parent_rect.x(), parent_rect.y()),
+ Some(sibling) => {
+ let sibling_rect = sibling.to_rect()?;
+
+ match parent.tiling_direction() {
+ TilingDirection::Vertical => (
+ parent_rect.x(),
+ sibling_rect.y() + sibling_rect.height() + inner_gap,
+ ),
+ TilingDirection::Horizontal => (
+ sibling_rect.x() + sibling_rect.width() + inner_gap,
+ parent_rect.y(),
+ ),
+ }
}
-
- let inner_gap = self.inner_gap().to_px(
- self.monitor().context("No parent monitor.")?.width()?,
- );
-
- Ok(prev_sibling.y()? + prev_sibling.height()? + inner_gap)
}
- }
+ };
+
+ Ok(Rect::from_xy(x, y, width, height))
}
}
};
diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rs
index 4916a537..658cac63 100644
--- a/packages/wm/src/monitors/monitor.rs
+++ b/packages/wm/src/monitors/monitor.rs
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::{
- common::platform::NativeMonitor,
+ common::{platform::NativeMonitor, Rect},
containers::{
traits::{CommonGetters, PositionGetters},
Container, ContainerDto, DirectionContainer, TilingContainer,
@@ -95,6 +95,7 @@ impl Monitor {
}
fn to_dto(&self) -> anyhow::Result<ContainerDto> {
+ let rect = self.to_rect()?;
let children = self
.children()
.iter()
@@ -106,10 +107,10 @@ impl Monitor {
parent: self.parent().map(|parent| parent.id()),
children,
child_focus_order: self.0.borrow().child_focus_order.clone().into(),
- width: self.width()?,
- height: self.height()?,
- x: self.x()?,
- y: self.y()?,
+ width: rect.width(),
+ height: rect.height(),
+ x: rect.x(),
+ y: rect.y(),
dpi: self.native().dpi()?,
}))
}
@@ -119,19 +120,7 @@ impl_container_debug!(Monitor);
impl_common_getters!(Monitor);
impl PositionGetters for Monitor {
- fn width(&self) -> anyhow::Result<i32> {
- Ok(self.0.borrow().native.rect()?.width())
- }
-
- fn height(&self) -> anyhow::Result<i32> {
- Ok(self.0.borrow().native.rect()?.height())
- }
-
- fn x(&self) -> anyhow::Result<i32> {
- Ok(self.0.borrow().native.rect()?.x())
- }
-
- fn y(&self) -> anyhow::Result<i32> {
- Ok(self.0.borrow().native.rect()?.y())
+ fn to_rect(&self) -> anyhow::Result<Rect> {
+ self.0.borrow().native.rect().cloned()
}
}
diff --git a/packages/wm/src/windows/commands/move_window_in_direction.rs b/packages/wm/src/windows/commands/move_window_in_direction.rs
index 7b1fff69..db82f0f9 100644
--- a/packages/wm/src/windows/commands/move_window_in_direction.rs
+++ b/packages/wm/src/windows/commands/move_window_in_direction.rs
@@ -496,9 +496,9 @@ fn new_floating_position(
let (monitor_length, window_length) = match direction {
Direction::Up | Direction::Down => {
- (monitor.height()?, window_pos.height())
+ (monitor_rect.height(), window_pos.height())
}
- _ => (monitor.width()?, window_pos.width()),
+ _ => (monitor_rect.width(), window_pos.width()),
};
let length_delta = monitor_length - window_length;
diff --git a/packages/wm/src/windows/commands/resize_window.rs b/packages/wm/src/windows/commands/resize_window.rs
index 59ae172f..860f1713 100644
--- a/packages/wm/src/windows/commands/resize_window.rs
+++ b/packages/wm/src/windows/commands/resize_window.rs
@@ -27,18 +27,21 @@ pub fn resize_window(
Ok(tiling_window) => tiling_window
.container_to_resize(true)?
.and_then(|container| container.parent())
- .and_then(|parent| parent.width().ok())
+ .and_then(|parent| {
+ parent.to_rect().ok().map(|rect| rect.width())
+ })
.map(|parent_width| {
parent_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()),
+ _ => window.parent().and_then(|parent| {
+ parent.to_rect().ok().map(|rect| rect.width())
+ }),
};
parent_width.map(|parent_width| {
- let delta_px = delta.to_px(parent_width);
- window_rect.width() + delta_px
+ window_rect.width() + delta.to_px(parent_width)
})
}
_ => None,
@@ -50,18 +53,21 @@ pub fn resize_window(
Ok(tiling_window) => tiling_window
.container_to_resize(false)?
.and_then(|container| container.parent())
- .and_then(|parent| parent.height().ok())
+ .and_then(|parent| {
+ parent.to_rect().ok().map(|rect| rect.width())
+ })
.map(|parent_height| {
parent_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()),
+ _ => window.parent().and_then(|parent| {
+ parent.to_rect().ok().map(|rect| rect.width())
+ }),
};
parent_height.map(|parent_height| {
- let delta_px = delta.to_px(parent_height);
- window_rect.height() + delta_px
+ window_rect.height() + delta.to_px(parent_height)
})
}
_ => None,
diff --git a/packages/wm/src/windows/commands/set_window_size.rs b/packages/wm/src/windows/commands/set_window_size.rs
index 4cecdf4c..b61f9323 100644
--- a/packages/wm/src/windows/commands/set_window_size.rs
+++ b/packages/wm/src/windows/commands/set_window_size.rs
@@ -77,12 +77,12 @@ fn set_tiling_window_length(
let parent = container_to_resize.parent().context("No parent.")?;
let parent_length = match is_width_resize {
true => {
- parent.width()?
+ parent.to_rect()?.width()
- window.inner_gap().to_px(monitor_rect.width())
* window.tiling_siblings().count() as i32
}
false => {
- parent.height()?
+ parent.to_rect()?.height()
- window.inner_gap().to_px(monitor_rect.height())
* window.tiling_siblings().count() as i32
}
diff --git a/packages/wm/src/windows/non_tiling_window.rs b/packages/wm/src/windows/non_tiling_window.rs
index d32d77a0..037d47de 100644
--- a/packages/wm/src/windows/non_tiling_window.rs
+++ b/packages/wm/src/windows/non_tiling_window.rs
@@ -83,14 +83,16 @@ impl NonTilingWindow {
}
fn to_dto(&self) -> anyhow::Result<ContainerDto> {
+ let rect = self.to_rect()?;
+
Ok(ContainerDto::Window(WindowDto {
id: self.id(),
parent: self.parent().map(|parent| parent.id()),
tiling_size: None,
- width: self.width()?,
- height: self.height()?,
- x: self.x()?,
- y: self.y()?,
+ width: rect.width(),
+ height: rect.height(),
+ x: rect.x(),
+ y: rect.y(),
state: self.state(),
prev_state: self.prev_state(),
display_state: self.display_state(),
@@ -105,57 +107,12 @@ impl_common_getters!(NonTilingWindow);
impl_window_getters!(NonTilingWindow);
impl PositionGetters for NonTilingWindow {
- fn width(&self) -> anyhow::Result<i32> {
- // TODO: Simplify with a `borrow_monitor_rect` fn.
- let width = match self.state() {
- WindowState::Fullscreen(_) => self
- .monitor()
- .context("No monitor.")?
- .native()
- .rect()?
- .width(),
- _ => self.floating_placement().width(),
- };
-
- Ok(width)
- }
-
- fn height(&self) -> anyhow::Result<i32> {
- // TODO: Simplify with a `borrow_monitor_rect` fn.
- let height = match self.state() {
- WindowState::Fullscreen(_) => self
- .monitor()
- .context("No monitor.")?
- .native()
- .rect()?
- .height(),
- _ => self.floating_placement().height(),
- };
-
- Ok(height)
- }
-
- fn x(&self) -> anyhow::Result<i32> {
- // TODO: Simplify with a `borrow_monitor_rect` fn.
- let x = match self.state() {
+ fn to_rect(&self) -> anyhow::Result<Rect> {
+ match self.state() {
WindowState::Fullscreen(_) => {
- self.monitor().context("No monitor.")?.native().rect()?.x()
+ self.monitor().context("No monitor.")?.to_rect()
}
- _ => self.floating_placement().x(),
- };
-
- Ok(x)
- }
-
- fn y(&self) -> anyhow::Result<i32> {
- // TODO: Simplify with a `borrow_monitor_rect` fn.
- let y = match self.state() {
- WindowState::Fullscreen(_) => {
- self.monitor().context("No monitor.")?.native().rect()?.y()
- }
- _ => self.floating_placement().y(),
- };
-
- Ok(y)
+ _ => Ok(self.floating_placement()),
+ }
}
}
diff --git a/packages/wm/src/windows/tiling_window.rs b/packages/wm/src/windows/tiling_window.rs
index 2cbcf8d6..448e2e6d 100644
--- a/packages/wm/src/windows/tiling_window.rs
+++ b/packages/wm/src/windows/tiling_window.rs
@@ -93,14 +93,16 @@ impl TilingWindow {
}
fn to_dto(&self) -> anyhow::Result<ContainerDto> {
+ let rect = self.to_rect()?;
+
Ok(ContainerDto::Window(WindowDto {
id: self.id(),
parent: self.parent().map(|parent| parent.id()),
tiling_size: Some(self.tiling_size()),
- width: self.width()?,
- height: self.height()?,
- x: self.x()?,
- y: self.y()?,
+ width: rect.width(),
+ height: rect.height(),
+ x: rect.x(),
+ y: rect.y(),
state: self.state(),
prev_state: self.prev_state(),
display_state: self.display_state(),
diff --git a/packages/wm/src/workspaces/commands/activate_workspace.rs b/packages/wm/src/workspaces/commands/activate_workspace.rs
index 074602cf..7b7e1631 100644
--- a/packages/wm/src/workspaces/commands/activate_workspace.rs
+++ b/packages/wm/src/workspaces/commands/activate_workspace.rs
@@ -26,11 +26,12 @@ pub fn activate_workspace(
let workspace_config =
workspace_config(workspace_name, target_monitor, state, config)?;
- let tiling_direction =
- match target_monitor.height()? > target_monitor.width()? {
- true => TilingDirection::Vertical,
- false => TilingDirection::Horizontal,
- };
+ let monitor_rect = target_monitor.to_rect()?;
+ let tiling_direction = match monitor_rect.height() > monitor_rect.width()
+ {
+ true => TilingDirection::Vertical,
+ false => TilingDirection::Horizontal,
+ };
let workspace = Workspace::new(
workspace_config.clone(),
diff --git a/packages/wm/src/workspaces/workspace.rs b/packages/wm/src/workspaces/workspace.rs
index bcf933db..0988fce3 100644
--- a/packages/wm/src/workspaces/workspace.rs
+++ b/packages/wm/src/workspaces/workspace.rs
@@ -94,6 +94,7 @@ impl Workspace {
}
fn to_dto(&self) -> anyhow::Result<ContainerDto> {
+ let rect = self.to_rect()?;
let children = self
.children()
.iter()
@@ -105,10 +106,10 @@ impl Workspace {
parent: self.parent().map(|parent| parent.id()),
children,
child_focus_order: self.0.borrow().child_focus_order.clone().into(),
- width: self.width()?,
- height: self.height()?,
- x: self.x()?,
- y: self.y()?,
+ width: rect.width(),
+ height: rect.height(),
+ x: rect.x(),
+ y: rect.y(),
tiling_direction: self.tiling_direction(),
}))
}
@@ -131,20 +132,4 @@ impl PositionGetters for Workspace {
let outer_gap = &self.0.borrow().outer_gap;
Ok(working_rect.apply_inverse_delta(outer_gap))
}
-
- fn width(&self) -> anyhow::Result<i32> {
- Ok(self.to_rect()?.width())
- }
-
- fn height(&self) -> anyhow::Result<i32> {
- Ok(self.to_rect()?.height())
- }
-
- fn x(&self) -> anyhow::Result<i32> {
- Ok(self.to_rect()?.x())
- }
-
- fn y(&self) -> anyhow::Result<i32> {
- Ok(self.to_rect()?.y())
- }
}