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 d02519ec..7c95cdd3 100644 --- a/packages/wm/src/common/events/handle_window_moved_end.rs +++ b/packages/wm/src/common/events/handle_window_moved_end.rs @@ -5,7 +5,8 @@ use crate::{ common::{platform::Platform, Point, TilingDirection}, containers::{ commands::{ - attach_container, detach_container, move_container_within_tree, + attach_container, detach_container, get_containers_at_position, + move_container_within_tree, }, traits::{CommonGetters, PositionGetters, TilingDirectionGetters}, Container, RootContainer, SplitContainer, @@ -19,6 +20,7 @@ use crate::{ }, wm_state::WmState, }; +use crate::containers::WindowContainer; /// Handles window move events pub fn window_moved_end( @@ -41,14 +43,12 @@ pub fn window_moved_end( } info!("Tiling window drag end event"); - let root_container = state.root_container.clone(); - let mouse_position = Platform::mouse_position()?; let window_under_cursor = match get_tiling_window_at_mouse_pos( &moved_window, - root_container, &mouse_position, + state, ) { Some(value) => value, None => { @@ -125,25 +125,18 @@ pub fn window_moved_end( /// Return the window under the mouse position excluding the dragged window fn get_tiling_window_at_mouse_pos( exclude_window: &NonTilingWindow, - root_container: RootContainer, mouse_position: &Point, + state: &WmState, ) -> Option { - let children_at_mouse_position: Vec<_> = root_container - .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; - } + let children_at_mouse_position: Vec = + get_containers_at_position(state, mouse_position) + .into_iter() + .filter_map(|container| match container { + WindowContainer::TilingWindow(tiling) => Some(tiling), + _ => None, + }) + .filter(|window: &TilingWindow| window.id() != exclude_window.id()) + .collect(); children_at_mouse_position.into_iter().next() } diff --git a/packages/wm/src/containers/commands/get_containers_at_pos.rs b/packages/wm/src/containers/commands/get_containers_at_pos.rs new file mode 100644 index 00000000..d84e690f --- /dev/null +++ b/packages/wm/src/containers/commands/get_containers_at_pos.rs @@ -0,0 +1,32 @@ +use crate::{ + common::Point, + containers::{ + traits::{CommonGetters, PositionGetters}, + Container, WindowContainer, + }, + wm_state::WmState, +}; + +/// Return the window under the mouse position excluding the dragged window +pub fn get_containers_at_position( + state: &WmState, + position: &Point, +) -> Vec { + state + .root_container + .descendants() + .filter_map(|container| match container { + Container::TilingWindow(tiling) => { + Some(WindowContainer::TilingWindow(tiling)) + } + Container::NonTilingWindow(non_tiling) => { + Some(WindowContainer::NonTilingWindow(non_tiling)) + } + _ => None, + }) + .filter(|c| { + let frame = c.to_rect(); + frame.unwrap().contains_point(&position) + }) + .collect() +} diff --git a/packages/wm/src/containers/commands/mod.rs b/packages/wm/src/containers/commands/mod.rs index b42854eb..4f062737 100644 --- a/packages/wm/src/containers/commands/mod.rs +++ b/packages/wm/src/containers/commands/mod.rs @@ -9,6 +9,7 @@ mod resize_tiling_container; mod set_focused_descendant; mod toggle_tiling_direction; mod wrap_in_split_container; +mod get_containers_at_pos; pub use attach_container::*; pub use detach_container::*; @@ -21,3 +22,4 @@ pub use resize_tiling_container::*; pub use set_focused_descendant::*; pub use toggle_tiling_direction::*; pub use wrap_in_split_container::*; +pub use get_containers_at_pos::*;