Skip to content

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

refactor: simplify how target index is adjusted in `move_container_within_tree`

Lars Berger77380321048b
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 e8938273..c23b795b 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
@@ -125,7 +125,7 @@ fn update_parent_workspace(
move_container_within_tree(
window.clone().into(),
target_workspace.clone().into(),
- target_workspace.children().len() - 1,
+ target_workspace.child_count(),
)?;
if window.is_focused() {
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 f4c6447e..0440f35f 100644
--- a/packages/wm/src/containers/commands/move_container_within_tree.rs
+++ b/packages/wm/src/containers/commands/move_container_within_tree.rs
@@ -114,24 +114,13 @@ fn move_to_lowest_common_ancestor(
.context("TODO.")?
.focus_index();
- // 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.child_count();
-
detach_container(container_to_move.clone())?;
- let should_adjust_target_index = original_lca_child_count
- > lowest_common_ancestor.child_count()
- && original_index < target_index;
-
// Adjust for when target index changes on detach of container. For
// example, when shifting a top-level container to the right in a
// workspace.
- let adjusted_target_index = if should_adjust_target_index {
- target_index - 1
- } else {
- target_index
- };
+ let adjusted_target_index =
+ target_index.clamp(0, lowest_common_ancestor.child_count());
attach_container(
&container_to_move.clone(),
diff --git a/packages/wm/src/windows/commands/update_window_state.rs b/packages/wm/src/windows/commands/update_window_state.rs
index 32bd3dc5..953f3f10 100644
--- a/packages/wm/src/windows/commands/update_window_state.rs
+++ b/packages/wm/src/windows/commands/update_window_state.rs
@@ -36,13 +36,13 @@ fn set_tiling(
config: &UserConfig,
) -> anyhow::Result<()> {
if let WindowContainer::NonTilingWindow(window) = window {
- let workspace = window
- .workspace()
- .context("Window has no workspace.")?;
+ let workspace =
+ window.workspace().context("Window has no workspace.")?;
// Get the position in the tree to insert the new tiling window. This
// will be the window's previous tiling position if it has one, or
// instead beside the last focused tiling window in the workspace.
+ // TODO: Something is going wrong here.
let (target_parent, target_index) = window
.insertion_target()
.or_else(|| {
@@ -89,22 +89,24 @@ fn set_non_tiling(
state.add_container_to_redraw(window.into());
}
WindowContainer::TilingWindow(window) => {
- let workspace = window
- .workspace()
- .context("Window has no workspace.")?;
-
- move_container_within_tree(
- window.clone().into(),
- workspace.clone().into(),
- workspace.child_count() - 1,
- )?;
+ let parent = window.parent().context("No parent")?;
+ let workspace = window.workspace().context("No workspace.")?;
+
+ // Non-tiling windows should always be direct children of the
+ // workspace.
+ if parent != workspace.clone().into() {
+ move_container_within_tree(
+ window.clone().into(),
+ workspace.clone().into(),
+ workspace.child_count(),
+ )?;
+ }
let non_tiling_window = window.to_non_tiling(window_state.clone());
- let parent = window.parent().context("No parent")?;
replace_container(
non_tiling_window.clone().into(),
- parent.clone(),
+ workspace.clone().into(),
window.index(),
)?;
@@ -120,7 +122,8 @@ fn set_non_tiling(
}
}
- state.add_container_to_redraw(parent);
+ // TODO: Only redraw tiling descendants of the workspace.
+ state.add_container_to_redraw(workspace.into());
}
}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index f03580ad..1f0c05df 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -143,6 +143,7 @@ impl WmState {
.or(self.monitors().first().cloned())
}
+ /// Gets monitor that corresponds to the given `NativeMonitor`.
pub fn monitor_from_native(
&self,
native_monitor: &NativeMonitor,
@@ -154,6 +155,7 @@ impl WmState {
.cloned()
}
+ /// Gets window that corresponds to the given `NativeWindow`.
pub fn window_from_native(
&self,
native_window: &NativeWindow,