Skip to content

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

feat: change `set_floating` to `update_window_state` and handle all window state transitions

Lars Bergerc2d51ad47d30
diff --git a/packages/wm/src/app_command.rs b/packages/wm/src/app_command.rs
index b995e09e..cff0530b 100644
--- a/packages/wm/src/app_command.rs
+++ b/packages/wm/src/app_command.rs
@@ -9,7 +9,9 @@ use crate::{
common::{Direction, LengthValue},
containers::{traits::CommonGetters, Container},
user_config::UserConfig,
- windows::{commands::set_floating, traits::WindowGetters},
+ windows::{
+ commands::update_window_state, traits::WindowGetters, WindowState,
+ },
wm_state::WmState,
};
@@ -176,7 +178,12 @@ impl InvokeCommand {
InvokeCommand::Resize(_) => todo!(),
InvokeCommand::SetFloating { centered } => {
match subject_container.as_window_container() {
- Ok(window) => set_floating(window, state),
+ Ok(window) => update_window_state(
+ window,
+ WindowState::Floating,
+ state,
+ config,
+ ),
_ => Ok(()),
}
}
@@ -193,12 +200,24 @@ impl InvokeCommand {
_ => Ok(()),
}
}
- InvokeCommand::SetTiling => todo!(),
+ InvokeCommand::SetTiling => {
+ match subject_container.as_window_container() {
+ Ok(window) => {
+ update_window_state(window, WindowState::Tiling, state, config)
+ }
+ _ => Ok(()),
+ }
+ }
InvokeCommand::ShellExec { command } => todo!(),
InvokeCommand::ToggleFloating { centered } => {
match subject_container.as_window_container() {
// TODO: Toggle floating.
- Ok(window) => set_floating(window, state),
+ Ok(window) => update_window_state(
+ window,
+ WindowState::Floating,
+ state,
+ config,
+ ),
_ => Ok(()),
}
}
diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rs
index aa0dc0cc..559e8879 100644
--- a/packages/wm/src/common/platform/native_window.rs
+++ b/packages/wm/src/common/platform/native_window.rs
@@ -163,7 +163,7 @@ impl NativeWindow {
return false;
}
- // Ensure window is top-level (ie. not a child window). Ignore windows
+ // Ensure window is top-level (i.e. not a child window). Ignore windows
// that cannot be focused or if they're unavailable in task switcher
// (alt+tab menu).
let is_application_window = !self.has_window_style(WS_CHILD)
diff --git a/packages/wm/src/containers/commands/detach_container.rs b/packages/wm/src/containers/commands/detach_container.rs
index 25c79614..077ce8b1 100644
--- a/packages/wm/src/containers/commands/detach_container.rs
+++ b/packages/wm/src/containers/commands/detach_container.rs
@@ -14,7 +14,7 @@ pub fn detach_container(child_to_remove: Container) -> anyhow::Result<()> {
// Flatten the parent split container if it'll be empty after removing
// the child.
if let Some(split_parent) = parent.as_split().cloned() {
- if split_parent.children().len() == 1 {
+ if split_parent.child_count() == 1 {
parent = parent.parent().context("No parent.")?;
flatten_split_container(split_parent)?;
}
diff --git a/packages/wm/src/containers/commands/move_container_within_tree.rs b/packages/wm/src/containers/commands/move_container_within_tree.rs
index d4d538cc..f4c6447e 100644
--- a/packages/wm/src/containers/commands/move_container_within_tree.rs
+++ b/packages/wm/src/containers/commands/move_container_within_tree.rs
@@ -116,13 +116,12 @@ fn move_to_lowest_common_ancestor(
// Keep reference to container index and number of children that LCA has.
let original_index = container_to_move.index();
- let original_lca_child_count = lowest_common_ancestor.children().len();
+ let original_lca_child_count = lowest_common_ancestor.child_count();
detach_container(container_to_move.clone())?;
- let new_lca_child_count = lowest_common_ancestor.children().len();
let should_adjust_target_index = original_lca_child_count
- > new_lca_child_count
+ > lowest_common_ancestor.child_count()
&& original_index < target_index;
// Adjust for when target index changes on detach of container. For
diff --git a/packages/wm/src/containers/traits/common_getters.rs b/packages/wm/src/containers/traits/common_getters.rs
index 28b04882..097d43ee 100644
--- a/packages/wm/src/containers/traits/common_getters.rs
+++ b/packages/wm/src/containers/traits/common_getters.rs
@@ -21,7 +21,7 @@ pub trait CommonGetters {
/// A unique identifier for the container.
fn id(&self) -> Uuid;
- /// Derived container type (eg. `ContainerType::Monitor`).
+ /// Derived container type (e.g. `ContainerType::Monitor`).
fn r#type(&self) -> ContainerType;
fn as_container(&self) -> Container;
@@ -64,7 +64,7 @@ pub trait CommonGetters {
!self.borrow_children().is_empty()
}
- /// Whether this container is detached from the tree (ie. it does not
+ /// Whether this container is detached from the tree (i.e. it does not
/// have a parent).
fn is_detached(&self) -> bool {
self.borrow_parent().as_ref().is_none()
@@ -98,6 +98,40 @@ pub trait CommonGetters {
Descendants { stack }
}
+ /// Leaf nodes (i.e. windows and workspaces) in order of last focus.
+ fn descendant_focus_order(
+ &self,
+ ) -> Box<dyn Iterator<Item = Container> + '_> {
+ let mut stack = Vec::new();
+ stack.push(self.as_container());
+
+ Box::new(std::iter::from_fn(move || {
+ while let Some(current) = stack.pop() {
+ // Get containers that have no children. Descendant also cannot be
+ // the container itself.
+ if current.id() != self.id() && !current.has_children() {
+ return Some(current);
+ }
+
+ // Reverse the child focus order so that the first element is
+ // pushed last and popped first.
+ for focus_child_id in
+ current.borrow_child_focus_order().iter().rev()
+ {
+ if let Some(focus_child) = current
+ .borrow_children()
+ .iter()
+ .find(|child| child.id() == *focus_child_id)
+ .cloned()
+ {
+ stack.push(focus_child);
+ }
+ }
+ }
+ None
+ }))
+ }
+
fn siblings(&self) -> Box<dyn Iterator<Item = Container> + '_> {
Box::new(
self
diff --git a/packages/wm/src/windows/commands/mod.rs b/packages/wm/src/windows/commands/mod.rs
index 4f48e7c1..3a0f94a1 100644
--- a/packages/wm/src/windows/commands/mod.rs
+++ b/packages/wm/src/windows/commands/mod.rs
@@ -1,7 +1,7 @@
mod manage_window;
-mod set_floating;
mod unmanage_window;
+mod update_window_state;
pub use manage_window::*;
-pub use set_floating::*;
pub use unmanage_window::*;
+pub use update_window_state::*;
diff --git a/packages/wm/src/windows/commands/set_floating.rs b/packages/wm/src/windows/commands/set_floating.rs
deleted file mode 100644
index ed64db41..00000000
--- a/packages/wm/src/windows/commands/set_floating.rs
+++ /dev/null
@@ -1,50 +0,0 @@
-use anyhow::Context;
-
-use crate::{
- containers::{
- commands::{move_container_within_tree, replace_container},
- traits::CommonGetters,
- WindowContainer,
- },
- windows::{traits::WindowGetters, WindowState},
- wm_state::WmState,
-};
-
-pub fn set_floating(
- window: WindowContainer,
- state: &mut WmState,
-) -> anyhow::Result<()> {
- if window.state() == WindowState::Floating {
- return Ok(());
- }
-
- match window {
- WindowContainer::NonTilingWindow(window) => {
- window.set_state(WindowState::Floating);
- state.add_container_to_redraw(window.into());
- }
- WindowContainer::TilingWindow(window) => {
- let workspace = window
- .parent_workspace()
- .context("Window has no workspace.")?;
-
- move_container_within_tree(
- window.clone().into(),
- workspace.clone().into(),
- workspace.child_count() - 1,
- )?;
-
- let floating_window = window.to_non_tiling(WindowState::Floating);
-
- replace_container(
- floating_window.clone().into(),
- window.parent().context("No parent")?,
- window.index(),
- )?;
-
- state.add_container_to_redraw(floating_window.into());
- }
- }
-
- Ok(())
-}
diff --git a/packages/wm/src/windows/commands/update_window_state.rs b/packages/wm/src/windows/commands/update_window_state.rs
new file mode 100644
index 00000000..245fef09
--- /dev/null
+++ b/packages/wm/src/windows/commands/update_window_state.rs
@@ -0,0 +1,114 @@
+use anyhow::Context;
+
+use crate::{
+ containers::{
+ commands::{move_container_within_tree, replace_container},
+ traits::CommonGetters,
+ WindowContainer,
+ },
+ user_config::UserConfig,
+ windows::{traits::WindowGetters, WindowState},
+ wm_state::WmState,
+};
+
+pub fn update_window_state(
+ window: WindowContainer,
+ window_state: WindowState,
+ state: &mut WmState,
+ config: &mut UserConfig,
+) -> anyhow::Result<()> {
+ if window.state() == window_state {
+ return Ok(());
+ }
+
+ match window_state {
+ WindowState::Tiling => set_tiling(window, state, config),
+ _ => set_non_tiling(window, window_state, state),
+ }
+}
+
+fn set_tiling(
+ window: WindowContainer,
+ state: &mut WmState,
+ config: &mut UserConfig,
+) -> anyhow::Result<()> {
+ if let WindowContainer::NonTilingWindow(window) = window {
+ let workspace = window
+ .parent_workspace()
+ .context("Window has no workspace.")?;
+
+ let insertion_target = window
+ .descendant_focus_order()
+ .filter(|c| c.is_tiling_window())
+ .next();
+
+ let tiling_window =
+ window.to_tiling(config.value.gaps.inner_gap.clone());
+
+ // Replace the original window with the created tiling window.
+ replace_container(
+ tiling_window.clone().into(),
+ window.parent().context("No parent")?,
+ window.index(),
+ )?;
+
+ // Insert the created tiling window after the last focused tiling
+ // window within the workspace.
+ match insertion_target {
+ Some(insertion_target) => {
+ move_container_within_tree(
+ tiling_window.clone().into(),
+ insertion_target.parent().context("No parent.")?,
+ insertion_target.index() + 1,
+ )?;
+ }
+ None => {
+ move_container_within_tree(
+ tiling_window.clone().into(),
+ workspace.into(),
+ 0,
+ )?;
+ }
+ }
+
+ state.add_container_to_redraw(tiling_window.into());
+ }
+
+ Ok(())
+}
+
+fn set_non_tiling(
+ window: WindowContainer,
+ window_state: WindowState,
+ state: &mut WmState,
+) -> anyhow::Result<()> {
+ match window {
+ WindowContainer::NonTilingWindow(window) => {
+ window.set_state(window_state);
+ state.add_container_to_redraw(window.into());
+ }
+ WindowContainer::TilingWindow(window) => {
+ let workspace = window
+ .parent_workspace()
+ .context("Window has no workspace.")?;
+
+ move_container_within_tree(
+ window.clone().into(),
+ workspace.clone().into(),
+ workspace.child_count() - 1,
+ )?;
+
+ let non_tiling_window = window.to_non_tiling(window_state);
+
+ replace_container(
+ non_tiling_window.clone().into(),
+ window.parent().context("No parent")?,
+ window.index(),
+ )?;
+
+ state.add_container_to_redraw(non_tiling_window.into());
+ }
+ }
+
+ Ok(())
+}
diff --git a/packages/wm/src/wm.rs b/packages/wm/src/wm.rs
index 9f285b39..07af8615 100644
--- a/packages/wm/src/wm.rs
+++ b/packages/wm/src/wm.rs
@@ -97,11 +97,14 @@ impl WindowManager {
let mut state = self.state.lock().await;
// Get container to run WM commands with.
- let mut subject_container = subject_container_id
- .map(|id| state.container_by_id(id))
- .context("No container found with the given ID.")?
- .or_else(|| state.focused_container())
- .context("No subject container for command.")?;
+ let mut subject_container = match subject_container_id {
+ Some(id) => state.container_by_id(id).with_context(|| {
+ format!("No container found with the given ID '{}'.", id)
+ })?,
+ None => state
+ .focused_container()
+ .context("No subject container for command.")?,
+ };
for command in commands {
command.run(subject_container.clone(), &mut state, config)?;