Skip to content

Commit

Permalink
refactor: get containers mouse position
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaCoduriV committed Jul 31, 2024
1 parent 3e988e8 commit 323ed80
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 21 deletions.
35 changes: 14 additions & 21 deletions packages/wm/src/common/events/handle_window_moved_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,6 +20,7 @@ use crate::{
},
wm_state::WmState,
};
use crate::containers::WindowContainer;

/// Handles window move events
pub fn window_moved_end(
Expand All @@ -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 => {
Expand Down Expand Up @@ -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<TilingWindow> {
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<TilingWindow> =
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()
}
Expand Down
32 changes: 32 additions & 0 deletions packages/wm/src/containers/commands/get_containers_at_pos.rs
Original file line number Diff line number Diff line change
@@ -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<WindowContainer> {
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()
}
2 changes: 2 additions & 0 deletions packages/wm/src/containers/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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::*;

0 comments on commit 323ed80

Please sign in to comment.