Skip to content

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

feat: improve `--centered` flag behavior for `set-floating` and `toggle-floating` (#842)

DreamMaoMao47ef45c9add2
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index 579e7ebb..d0d64a08 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -26,8 +26,8 @@ use crate::{
windows::{
commands::{
ignore_window, move_window_in_direction, move_window_to_workspace,
- resize_window, set_window_position, set_window_position_to_center,
- set_window_size, update_window_state,
+ resize_window, set_window_position, set_window_size,
+ update_window_state, WindowPositionTarget,
},
traits::WindowGetters,
WindowState,
@@ -451,11 +451,17 @@ impl InvokeCommand {
InvokeCommand::Position(args) => {
match subject_container.as_window_container() {
Ok(window) => match args.centered {
- true => set_window_position_to_center(window, state),
+ true => set_window_position(
+ window,
+ WindowPositionTarget::Centered,
+ state,
+ ),
false => set_window_position(
window,
- args.x_pos.clone(),
- args.y_pos.clone(),
+ WindowPositionTarget::Coordinates(
+ args.x_pos.clone(),
+ args.y_pos.clone(),
+ ),
state,
),
},
@@ -484,12 +490,12 @@ impl InvokeCommand {
Ok(window) => {
let floating_defaults =
&config.value.window_behavior.state_defaults.floating;
- let is_centered = centered.unwrap_or(floating_defaults.centered);
+ let centered = centered.unwrap_or(floating_defaults.centered);
let window = update_window_state(
window.clone(),
WindowState::Floating(FloatingStateConfig {
- centered: is_centered,
+ centered,
shown_on_top: shown_on_top
.unwrap_or(floating_defaults.shown_on_top),
}),
@@ -497,24 +503,34 @@ impl InvokeCommand {
config,
)?;
- if width.is_some() || height.is_some() {
- set_window_size(
- window.clone(),
- width.clone(),
- height.clone(),
- state,
- )?;
- }
+ // Allow size and position to be set if window has not previously
+ // been manually placed.
+ if !window.has_custom_floating_placement() {
+ 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,
- )?;
+ if centered {
+ set_window_position(
+ window,
+ WindowPositionTarget::Centered,
+ state,
+ )?;
+ } else if x_pos.is_some() || y_pos.is_some() {
+ set_window_position(
+ window,
+ WindowPositionTarget::Coordinates(
+ x_pos.clone(),
+ y_pos.clone(),
+ ),
+ state,
+ )?;
+ }
}
Ok(())
@@ -609,19 +625,28 @@ impl InvokeCommand {
let floating_defaults =
&config.value.window_behavior.state_defaults.floating;
+ let centered = centered.unwrap_or(floating_defaults.centered);
let target_state = WindowState::Floating(FloatingStateConfig {
- centered: centered.unwrap_or(floating_defaults.centered),
+ centered,
shown_on_top: shown_on_top
.unwrap_or(floating_defaults.shown_on_top),
});
- update_window_state(
+ let window = update_window_state(
window.clone(),
window.toggled_state(target_state, config),
state,
config,
)?;
+ if !window.has_custom_floating_placement() && centered {
+ set_window_position(
+ window,
+ WindowPositionTarget::Centered,
+ state,
+ )?;
+ }
+
Ok(())
}
_ => Ok(()),
diff --git a/packages/wm/src/common/events/handle_window_location_changed.rs b/packages/wm/src/common/events/handle_window_location_changed.rs
index 94d14a4c..84c0b5b5 100644
--- a/packages/wm/src/common/events/handle_window_location_changed.rs
+++ b/packages/wm/src/common/events/handle_window_location_changed.rs
@@ -124,6 +124,7 @@ pub fn handle_window_location_changed(
// Update state with the new location of the floating window.
info!("Updating floating window position.");
window.set_floating_placement(frame_position);
+ window.set_has_custom_floating_placement(true);
let monitor = window.monitor().context("No monitor.")?;
diff --git a/packages/wm/src/windows/commands/manage_window.rs b/packages/wm/src/windows/commands/manage_window.rs
index 73fb6640..1194055c 100644
--- a/packages/wm/src/windows/commands/manage_window.rs
+++ b/packages/wm/src/windows/commands/manage_window.rs
@@ -134,6 +134,7 @@ fn create_window(
None,
border_delta,
floating_placement,
+ false,
gaps_config,
Vec::new(),
None,
@@ -147,6 +148,7 @@ fn create_window(
border_delta,
None,
floating_placement,
+ false,
Vec::new(),
None,
)
diff --git a/packages/wm/src/windows/commands/set_window_position.rs b/packages/wm/src/windows/commands/set_window_position.rs
index 387ac500..7e95671a 100644
--- a/packages/wm/src/windows/commands/set_window_position.rs
+++ b/packages/wm/src/windows/commands/set_window_position.rs
@@ -10,47 +10,35 @@ use crate::{
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 enum WindowPositionTarget {
+ Centered,
+ Coordinates(Option<i32>, Option<i32>),
}
-pub fn set_window_position_to_center(
+pub fn set_window_position(
window: WindowContainer,
+ target: WindowPositionTarget,
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()?),
- );
+ let placement = window.floating_placement();
+
+ let new_placement = match target {
+ WindowPositionTarget::Centered => placement.translate_to_center(
+ &window.workspace().context("No workspace.")?.to_rect()?,
+ ),
+ WindowPositionTarget::Coordinates(target_x, target_y) => {
+ Rect::from_xy(
+ target_x.unwrap_or(placement.x()),
+ target_y.unwrap_or(placement.y()),
+ placement.width(),
+ placement.height(),
+ )
+ }
+ };
- state
- .pending_sync
- .containers_to_redraw
- .push(window.clone().into());
+ window.set_floating_placement(new_placement);
+ state.pending_sync.containers_to_redraw.push(window.into());
}
Ok(())
diff --git a/packages/wm/src/windows/non_tiling_window.rs b/packages/wm/src/windows/non_tiling_window.rs
index 53b0fea7..f195ef57 100644
--- a/packages/wm/src/windows/non_tiling_window.rs
+++ b/packages/wm/src/windows/non_tiling_window.rs
@@ -37,6 +37,7 @@ struct NonTilingWindowInner {
border_delta: RectDelta,
has_pending_dpi_adjustment: bool,
floating_placement: Rect,
+ has_custom_floating_placement: bool,
done_window_rules: Vec<WindowRuleConfig>,
active_drag: Option<ActiveDrag>,
}
@@ -50,6 +51,7 @@ impl NonTilingWindow {
border_delta: RectDelta,
insertion_target: Option<(Container, usize)>,
floating_placement: Rect,
+ has_custom_floating_placement: bool,
done_window_rules: Vec<WindowRuleConfig>,
active_drag: Option<ActiveDrag>,
) -> Self {
@@ -66,6 +68,7 @@ impl NonTilingWindow {
border_delta,
has_pending_dpi_adjustment: false,
floating_placement,
+ has_custom_floating_placement,
done_window_rules,
active_drag,
};
@@ -91,6 +94,7 @@ impl NonTilingWindow {
Some(self.state()),
self.border_delta(),
self.floating_placement(),
+ self.has_custom_floating_placement(),
gaps_config,
self.done_window_rules(),
self.active_drag(),
diff --git a/packages/wm/src/windows/tiling_window.rs b/packages/wm/src/windows/tiling_window.rs
index cba71833..b384d198 100644
--- a/packages/wm/src/windows/tiling_window.rs
+++ b/packages/wm/src/windows/tiling_window.rs
@@ -45,6 +45,7 @@ struct TilingWindowInner {
border_delta: RectDelta,
has_pending_dpi_adjustment: bool,
floating_placement: Rect,
+ has_custom_floating_placement: bool,
gaps_config: GapsConfig,
done_window_rules: Vec<WindowRuleConfig>,
active_drag: Option<ActiveDrag>,
@@ -57,6 +58,7 @@ impl TilingWindow {
prev_state: Option<WindowState>,
border_delta: RectDelta,
floating_placement: Rect,
+ has_custom_floating_placement: bool,
gaps_config: GapsConfig,
done_window_rules: Vec<WindowRuleConfig>,
active_drag: Option<ActiveDrag>,
@@ -74,6 +76,7 @@ impl TilingWindow {
border_delta,
has_pending_dpi_adjustment: false,
floating_placement,
+ has_custom_floating_placement,
gaps_config,
done_window_rules,
active_drag,
@@ -95,6 +98,7 @@ impl TilingWindow {
self.border_delta(),
insertion_target,
self.floating_placement(),
+ self.has_custom_floating_placement(),
self.done_window_rules(),
self.active_drag(),
)
diff --git a/packages/wm/src/windows/traits/window_getters.rs b/packages/wm/src/windows/traits/window_getters.rs
index 16b2387b..bdaabe78 100644
--- a/packages/wm/src/windows/traits/window_getters.rs
+++ b/packages/wm/src/windows/traits/window_getters.rs
@@ -105,6 +105,13 @@ pub trait WindowGetters {
fn set_floating_placement(&self, floating_placement: Rect);
+ fn has_custom_floating_placement(&self) -> bool;
+
+ fn set_has_custom_floating_placement(
+ &self,
+ has_custom_floating_placement: bool,
+ );
+
fn done_window_rules(&self) -> Vec<WindowRuleConfig>;
fn set_done_window_rules(
@@ -182,6 +189,18 @@ macro_rules! impl_window_getters {
self.0.borrow_mut().floating_placement = floating_placement;
}
+ fn has_custom_floating_placement(&self) -> bool {
+ self.0.borrow().has_custom_floating_placement.clone()
+ }
+
+ fn set_has_custom_floating_placement(
+ &self,
+ has_custom_floating_placement: bool,
+ ) {
+ self.0.borrow_mut().has_custom_floating_placement =
+ has_custom_floating_placement;
+ }
+
fn done_window_rules(&self) -> Vec<WindowRuleConfig> {
self.0.borrow().done_window_rules.clone()
}