Skip to content

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

feat: add new `position` command; allow size and position to be set with `set-floating` command (#817)

DreamMaoMao04e6c2fd8b93
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index f7b21531..3664e167 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -26,7 +26,8 @@ use crate::{
windows::{
commands::{
ignore_window, move_window_in_direction, move_window_to_workspace,
- resize_window, set_window_size, update_window_state,
+ resize_window, set_window_position, set_window_position_to_center,
+ set_window_size, update_window_state,
},
traits::WindowGetters,
WindowState,
@@ -188,6 +189,7 @@ pub enum InvokeCommand {
#[clap(long)]
direction: Direction,
},
+ Position(InvokePositionCommand),
Resize(InvokeResizeCommand),
SetFloating {
#[clap(long, default_missing_value = "true", require_equals = true, num_args = 0..=1)]
@@ -195,6 +197,18 @@ pub enum InvokeCommand {
#[clap(long, default_missing_value = "true", require_equals = true, num_args = 0..=1)]
centered: Option<bool>,
+
+ #[clap(long, allow_hyphen_values = true)]
+ x_pos: Option<i32>,
+
+ #[clap(long, allow_hyphen_values = true)]
+ y_pos: Option<i32>,
+
+ #[clap(long, allow_hyphen_values = true)]
+ width: Option<LengthValue>,
+
+ #[clap(long, allow_hyphen_values = true)]
+ height: Option<LengthValue>,
},
SetFullscreen {
#[clap(long, default_missing_value = "true", require_equals = true, num_args = 0..=1)]
@@ -430,6 +444,20 @@ impl InvokeCommand {
config,
)
}
+ InvokeCommand::Position(args) => {
+ match subject_container.as_window_container() {
+ Ok(window) => match args.centered {
+ true => set_window_position_to_center(window, state),
+ false => set_window_position(
+ window,
+ args.x_pos.clone(),
+ args.y_pos.clone(),
+ state,
+ ),
+ },
+ _ => Ok(()),
+ }
+ }
InvokeCommand::Resize(args) => {
match subject_container.as_window_container() {
Ok(window) => resize_window(
@@ -444,15 +472,20 @@ impl InvokeCommand {
InvokeCommand::SetFloating {
centered,
shown_on_top,
+ x_pos,
+ y_pos,
+ width,
+ height,
} => match subject_container.as_window_container() {
Ok(window) => {
let floating_defaults =
&config.value.window_behavior.state_defaults.floating;
+ let is_centered = centered.unwrap_or(floating_defaults.centered);
- update_window_state(
+ let window = update_window_state(
window.clone(),
WindowState::Floating(FloatingStateConfig {
- centered: centered.unwrap_or(floating_defaults.centered),
+ centered: is_centered,
shown_on_top: shown_on_top
.unwrap_or(floating_defaults.shown_on_top),
}),
@@ -460,6 +493,26 @@ impl InvokeCommand {
config,
)?;
+ if width.is_some() || height.is_some() {
+ set_window_size(
+ window.clone(),
+ width.clone(),
+ height.clone(),
+ state,
+ )?;
+ }
+
+ if is_centered {
+ set_window_position_to_center(window, state)?;
+ } else if x_pos.is_some() || y_pos.is_some() {
+ set_window_position(
+ window.clone(),
+ x_pos.clone(),
+ y_pos.clone(),
+ state,
+ )?;
+ }
+
Ok(())
}
_ => Ok(()),
@@ -801,3 +854,16 @@ pub struct InvokeResizeCommand {
#[clap(long, allow_hyphen_values = true)]
height: Option<LengthValue>,
}
+
+#[derive(Args, Clone, Debug, PartialEq, Serialize)]
+#[group(required = true, multiple = true)]
+pub struct InvokePositionCommand {
+ #[clap(long, action)]
+ centered: bool,
+
+ #[clap(long, allow_hyphen_values = true)]
+ x_pos: Option<i32>,
+
+ #[clap(long, allow_hyphen_values = true)]
+ y_pos: Option<i32>,
+}
diff --git a/packages/wm/src/user_config.rs b/packages/wm/src/user_config.rs
index 279c20c5..d25406c8 100644
--- a/packages/wm/src/user_config.rs
+++ b/packages/wm/src/user_config.rs
@@ -118,6 +118,10 @@ impl UserConfig {
commands: vec![InvokeCommand::SetFloating {
centered: Some(floating_defaults.centered),
shown_on_top: Some(floating_defaults.shown_on_top),
+ x_pos: None,
+ y_pos: None,
+ width: None,
+ height: None,
}],
match_window: vec![
WindowMatchConfig {
diff --git a/packages/wm/src/windows/commands/mod.rs b/packages/wm/src/windows/commands/mod.rs
index 63abc576..1386ee9c 100644
--- a/packages/wm/src/windows/commands/mod.rs
+++ b/packages/wm/src/windows/commands/mod.rs
@@ -4,6 +4,7 @@ mod move_window_in_direction;
mod move_window_to_workspace;
mod resize_window;
mod run_window_rules;
+mod set_window_position;
mod set_window_size;
mod unmanage_window;
mod update_window_state;
@@ -14,6 +15,7 @@ pub use move_window_in_direction::*;
pub use move_window_to_workspace::*;
pub use resize_window::*;
pub use run_window_rules::*;
+pub use set_window_position::*;
pub use set_window_size::*;
pub use unmanage_window::*;
pub use update_window_state::*;
diff --git a/packages/wm/src/windows/commands/set_window_position.rs b/packages/wm/src/windows/commands/set_window_position.rs
new file mode 100644
index 00000000..387ac500
--- /dev/null
+++ b/packages/wm/src/windows/commands/set_window_position.rs
@@ -0,0 +1,57 @@
+use anyhow::Context;
+
+use crate::{
+ common::Rect,
+ containers::{
+ traits::{CommonGetters, PositionGetters},
+ WindowContainer,
+ },
+ windows::{traits::WindowGetters, WindowState},
+ wm_state::WmState,
+};
+
+pub fn set_window_position(
+ window: WindowContainer,
+ target_x_pos: Option<i32>,
+ target_y_pos: Option<i32>,
+ state: &mut WmState,
+) -> anyhow::Result<()> {
+ if matches!(window.state(), WindowState::Floating(_)) {
+ let window_rect = window.floating_placement();
+ let new_x_pos = target_x_pos.unwrap_or(window_rect.x());
+ let new_y_pos = target_y_pos.unwrap_or(window_rect.y());
+ window.set_floating_placement(Rect::from_xy(
+ new_x_pos,
+ new_y_pos,
+ window.floating_placement().width(),
+ window.floating_placement().height(),
+ ));
+
+ state
+ .pending_sync
+ .containers_to_redraw
+ .push(window.clone().into());
+ }
+
+ Ok(())
+}
+
+pub fn set_window_position_to_center(
+ window: WindowContainer,
+ state: &mut WmState,
+) -> anyhow::Result<()> {
+ if matches!(window.state(), WindowState::Floating(_)) {
+ let window_rect = window.floating_placement();
+ let workspace = window.workspace().context("no workspace find.")?;
+ window.set_floating_placement(
+ window_rect.translate_to_center(&workspace.to_rect()?),
+ );
+
+ state
+ .pending_sync
+ .containers_to_redraw
+ .push(window.clone().into());
+ }
+
+ Ok(())
+}