From 8d65e83f40136f9f4fb5baaded62512660b5743d Mon Sep 17 00:00:00 2001 From: Luca Date: Mon, 29 Jul 2024 00:18:30 +0200 Subject: [PATCH] fix: bug when moving window to a target on the right side --- .../common/events/handle_window_moved_end.rs | 42 ++++++++++--------- .../handle_window_moved_or_resized_end.rs | 42 +++++++------------ 2 files changed, 36 insertions(+), 48 deletions(-) diff --git a/packages/wm/src/common/events/handle_window_moved_end.rs b/packages/wm/src/common/events/handle_window_moved_end.rs index 645ea590b..57be51542 100644 --- a/packages/wm/src/common/events/handle_window_moved_end.rs +++ b/packages/wm/src/common/events/handle_window_moved_end.rs @@ -1,6 +1,5 @@ use anyhow::Context; use tracing::info; - use crate::{ common::{platform::Platform, Point, TilingDirection}, containers::{ @@ -15,6 +14,7 @@ use crate::{ }, wm_state::WmState, }; +use crate::containers::commands::detach_container; /// Handles window move events pub fn window_moved_end( @@ -123,17 +123,17 @@ fn move_window_to_target( new_tiling_direction: TilingDirection, new_window_position: DropPosition, ) -> anyhow::Result<()> { - let target_window_index = target_window_parent - .children() - .iter() - .position(|c| { - if let Some(tiling_window) = c.as_tiling_window() { - tiling_window.id() == target_window.id() - } else { - false - } - }) - .context("Window index not found")?; + + // TODO: We can optimize that for sure by not detaching and attaching the window + // Little trick to get the right index + detach_container(Container::NonTilingWindow(moved_window.clone()))?; + let target_window_index = target_window.index(); + attach_container(&Container::NonTilingWindow(moved_window.clone()), target_window_parent, None)?; + + let target_index = match new_window_position { + DropPosition::Start => Some(target_window_index), + DropPosition::End => Some(target_window_index + 1), + }; update_window_state( moved_window.as_window_container().unwrap(), @@ -142,6 +142,7 @@ fn move_window_to_target( config, )?; + let moved_window = state .windows() .iter() @@ -151,13 +152,11 @@ fn move_window_to_target( .context("window is not a tiled window")? .clone(); + match (new_tiling_direction, current_tiling_direction) { (TilingDirection::Horizontal, TilingDirection::Horizontal) | (TilingDirection::Vertical, TilingDirection::Vertical) => { - let target_index = match new_window_position { - DropPosition::Start => Some(target_window_index), - DropPosition::End => None, - }; + info!("Target window index {}", target_window_index); move_container_within_tree( Container::TilingWindow(moved_window.clone()), @@ -175,6 +174,7 @@ fn move_window_to_target( target_window, new_window_position, &target_window_parent, + target_index, )?; } (TilingDirection::Vertical, TilingDirection::Horizontal) => { @@ -186,6 +186,7 @@ fn move_window_to_target( target_window, new_window_position, &target_window_parent, + target_index, )?; } } @@ -201,17 +202,18 @@ fn create_split_container( target_window: TilingWindow, dropped_position: DropPosition, parent: &Container, + split_container_index: Option, ) -> anyhow::Result<()> { - let target_index = match dropped_position { + let target_index_inside_split_container = match dropped_position { DropPosition::Start => Some(0), - DropPosition::End => None, + DropPosition::End => Some(1), }; let split_container = Container::Split(SplitContainer::new( tiling_direction, config.value.gaps.inner_gap.clone(), )); - attach_container(&split_container, &parent, None)?; + attach_container(&split_container, &parent, split_container_index)?; move_container_within_tree( Container::TilingWindow(target_window), @@ -222,7 +224,7 @@ fn create_split_container( move_container_within_tree( Container::TilingWindow(moved_window), split_container, - target_index, + target_index_inside_split_container, state, )?; Ok(()) diff --git a/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs b/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs index e34348084..4f62557d1 100644 --- a/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs +++ b/packages/wm/src/common/events/handle_window_moved_or_resized_end.rs @@ -37,9 +37,7 @@ pub fn handle_window_moved_or_resized_end( config: &UserConfig, ) -> anyhow::Result<()> { let found_window = state.window_from_native(&native_window); - info!("{:#?}", found_window); - - if let Some(WindowContainer::NonTilingWindow(window)) = found_window { + if let Some(window) = found_window{ // TODO: Log window details. let parent = window.parent().context("No parent.")?; @@ -57,34 +55,22 @@ pub fn handle_window_moved_or_resized_end( let width_delta = new_rect.width() - old_rect.width(); let height_delta = new_rect.height() - old_rect.height(); - let has_window_moved = match (width_delta, height_delta) { - (0, 0) => true, - _ => false, - }; - - if has_window_moved { - window_moved_end(window, state, config)?; - } - } else if let Some(WindowContainer::TilingWindow(window)) = found_window - { - let parent = window.parent().context("No parent.")?; - - // Snap window to its original position if it's the only window in the - // workspace. - if parent.is_workspace() && window.tiling_siblings().count() == 0 { - state.pending_sync.containers_to_redraw.push(window.into()); - return Ok(()); + if let WindowContainer::NonTilingWindow(window) = window { + let has_window_moved = match (width_delta, height_delta) { + (0, 0) => true, + _ => false, + }; + + if has_window_moved { + window_moved_end(window, state, config)?; + } + } else if let WindowContainer::TilingWindow(window) = window + { + window_resized_end(window, state, width_delta, height_delta)?; } - - let new_rect = window.native().refresh_frame_position()?; - let old_rect = window.to_rect()?; - - let width_delta = new_rect.width() - old_rect.width(); - let height_delta = new_rect.height() - old_rect.height(); - - window_resized_end(window, state, width_delta, height_delta)?; } + Ok(()) }