Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: be able to change tiling direction when moving window via mouse #673

Merged
merged 12 commits into from
Aug 15, 2024
Merged
Prev Previous commit
Next Next commit
fix: get drop position function
  • Loading branch information
LucaCoduriV committed Aug 12, 2024
commit 2f44026136ea47ba417b86a19d1b6a6427683822
91 changes: 30 additions & 61 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 @@ -4,7 +4,7 @@ use tracing::{debug, info};
use crate::{
common::{
platform::{NativeWindow, Platform},
LengthValue, Point, TilingDirection,
LengthValue, Point, Rect, TilingDirection,
},
containers::{
commands::{
Expand Down Expand Up @@ -116,7 +116,9 @@ fn drop_as_tiling_window(

let window_under_cursor = window_under_cursor.unwrap();
let new_window_position =
get_drop_position(&mouse_pos, &window_under_cursor)?;
determine_drop_position(&mouse_pos, &window_under_cursor.to_rect()?);

info!("{:?}", new_window_position);

let parent = window_under_cursor
.direction_container()
Expand Down Expand Up @@ -297,66 +299,33 @@ enum DropPosition {
///
/// This function calculates whether the mouse is in the top, bottom,
/// left, or right triangular region of the window.
fn get_drop_position(
fn determine_drop_position(
mouse_position: &Point,
window: &TilingWindow,
) -> anyhow::Result<DropPosition> {
let rect = window.to_rect()?;

// Calculate the middle points of the window
let middle_y = rect.top + (rect.height() / 2);
let middle_x = rect.left + (rect.width() / 2);

// Determine which triangle the mouse is in
if mouse_position.y < middle_y {
// Mouse is in the top half
if mouse_position.x < middle_x {
// Top-left triangle
if mouse_position.y
< rect.top
+ ((mouse_position.x - rect.left)
* (rect.height() / rect.width()))
{
Ok(DropPosition::Top)
} else {
Ok(DropPosition::Left)
}
} else {
// Top-right triangle
if mouse_position.y
< rect.top
+ ((rect.right - mouse_position.x)
* (rect.height() / rect.width()))
{
Ok(DropPosition::Top)
} else {
Ok(DropPosition::Right)
}
}
frame: &Rect,
) -> DropPosition {
let x = mouse_position.x;
let y = mouse_position.y;

// Calculate the center of the frame
let center_x = (frame.left + frame.right) / 2;
let center_y = (frame.top + frame.bottom) / 2;

// Calculate the slopes of the diagonals
let diag1_slope = (frame.bottom - frame.top) as f32 / (frame.right - frame.left) as f32; // positive slope
let diag2_slope = -diag1_slope; // negative slope

// Calculate the y values on the diagonals for the given x position
let diag1_y_at_x = center_y as f32 + diag1_slope * (x - center_x) as f32;
let diag2_y_at_x = center_y as f32 + diag2_slope * (x - center_x) as f32;

// Determine the position based on the region
if (y as f32) < diag1_y_at_x && (y as f32) < diag2_y_at_x {
DropPosition::Top
} else if (y as f32) > diag1_y_at_x && (y as f32) > diag2_y_at_x {
DropPosition::Bottom
} else if (y as f32) > diag1_y_at_x && (y as f32) < diag2_y_at_x {
DropPosition::Left
} else {
// Mouse is in the bottom half
if mouse_position.x < middle_x {
// Bottom-left triangle
if mouse_position.y
> rect.bottom
- ((mouse_position.x - rect.left)
* (rect.height() / rect.width()))
{
Ok(DropPosition::Bottom)
} else {
Ok(DropPosition::Left)
}
} else {
// Bottom-right triangle
if mouse_position.y
> rect.bottom
- ((rect.right - mouse_position.x)
* (rect.height() / rect.width()))
{
Ok(DropPosition::Bottom)
} else {
Ok(DropPosition::Right)
}
}
DropPosition::Right
}
}