Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaCoduriV committed Jul 30, 2024
1 parent 3fb3d57 commit 3ef0155
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions packages/wm/src/common/events/handle_window_moved_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
};
use crate::containers::commands::detach_container;
use crate::user_config::FloatingStateConfig;
use crate::workspaces::Workspace;

/// Handles window move events
pub fn window_moved_end(
Expand All @@ -33,44 +34,27 @@ pub fn window_moved_end(
) {
return Ok(());
}
info!("Tiling window drag end");
info!("Tiling window drag end event");

let workspace = moved_window
.workspace()
.context("Couldn't find a workspace")?;

let mouse_position = Platform::mouse_position()?;

let children_at_mouse_position: Vec<_> = workspace
.descendants()
.filter_map(|container| match container {
Container::TilingWindow(tiling) => Some(tiling),
_ => None,
})
.filter(|c| {
let frame = c.to_rect();
frame.unwrap().contains_point(&mouse_position)
})
.filter(|window| window.id() != moved_window.id())
.collect();

if children_at_mouse_position.is_empty() {
return Ok(());
}

let window_under_cursor =
children_at_mouse_position.into_iter().next().unwrap();
let window_under_cursor = match get_window_at_mouse_pos(&moved_window, workspace, &mouse_position) {
Some(value) => value,
None => return Ok(()),
};

info!(
"Swapping {} with {}",
"Moved window: {:?} \n Target window: {:?}",
moved_window
.native()
.process_name()
.unwrap_or("".to_string()),
.process_name(),
window_under_cursor
.native()
.process_name()
.unwrap_or("".to_string())
.process_name(),
);

let tiling_direction = get_split_direction(&window_under_cursor)?;
Expand All @@ -86,7 +70,7 @@ pub fn window_moved_end(

match &parent {
// If the parent is a workspace we only need to add the window to it or
// to create a VerticalSplitDirection
// to create a Vertical split container
Container::Workspace(_) => {
let current_tiling_direction = TilingDirection::Horizontal;
move_window_to_target(
Expand All @@ -101,7 +85,7 @@ pub fn window_moved_end(
)?;
}
// If the parent is a split we need to check the current split
// direction add the window to it or create a VerticalSplitDirection
// direction add the window to it or create a [Vertical/Horizontal] split container
Container::Split(split) => {
let current_tiling_direction = split.tiling_direction();
move_window_to_target(
Expand All @@ -122,6 +106,28 @@ pub fn window_moved_end(
Ok(())
}

/// Return the window under the mouse position excluding the dragged window
fn get_window_at_mouse_pos(exclude_window: &NonTilingWindow, workspace: Workspace, mouse_position: &Point) -> Option<TilingWindow> {
let children_at_mouse_position: Vec<_> = workspace
.descendants()
.filter_map(|container| match container {
Container::TilingWindow(tiling) => Some(tiling),
_ => None,
})
.filter(|c| {
let frame = c.to_rect();
frame.unwrap().contains_point(&mouse_position)
})
.filter(|window| window.id() != exclude_window.id())
.collect();

if children_at_mouse_position.is_empty() {
return None;
}

children_at_mouse_position.into_iter().next()
}

fn move_window_to_target(
state: &mut WmState,
config: &UserConfig,
Expand Down Expand Up @@ -245,6 +251,9 @@ enum DropPosition {
End,
}

/// Returns the closest position of the mouse over the window
///
/// Example: Mouse x pos: 3, Window width: 5, Result: [DropPosition::End]
fn get_new_window_position(
mouse_position: &Point,
window: &TilingWindow,
Expand All @@ -271,6 +280,8 @@ fn get_new_window_position(
}
}
}

/// Returns the TilingDirection based on the window size
fn get_split_direction(
window: &TilingWindow,
) -> anyhow::Result<TilingDirection> {
Expand Down

0 comments on commit 3ef0155

Please sign in to comment.