Skip to content

Commit

Permalink
fix: bug when moving window to a target on the right side
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaCoduriV committed Jul 28, 2024
1 parent 48b17a2 commit 8d65e83
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 48 deletions.
42 changes: 22 additions & 20 deletions packages/wm/src/common/events/handle_window_moved_end.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use anyhow::Context;
use tracing::info;

use crate::{
common::{platform::Platform, Point, TilingDirection},
containers::{
Expand All @@ -15,6 +14,7 @@ use crate::{
},
wm_state::WmState,
};
use crate::containers::commands::detach_container;

/// Handles window move events
pub fn window_moved_end(
Expand Down Expand Up @@ -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(),
Expand All @@ -142,6 +142,7 @@ fn move_window_to_target(
config,
)?;


let moved_window = state
.windows()
.iter()
Expand All @@ -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()),
Expand All @@ -175,6 +174,7 @@ fn move_window_to_target(
target_window,
new_window_position,
&target_window_parent,
target_index,
)?;
}
(TilingDirection::Vertical, TilingDirection::Horizontal) => {
Expand All @@ -186,6 +186,7 @@ fn move_window_to_target(
target_window,
new_window_position,
&target_window_parent,
target_index,
)?;
}
}
Expand All @@ -201,17 +202,18 @@ fn create_split_container(
target_window: TilingWindow,
dropped_position: DropPosition,
parent: &Container,
split_container_index: Option<usize>,
) -> 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),
Expand All @@ -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(())
Expand Down
42 changes: 14 additions & 28 deletions packages/wm/src/common/events/handle_window_moved_or_resized_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")?;
Expand All @@ -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(())
}

Expand Down

0 comments on commit 8d65e83

Please sign in to comment.