-
Notifications
You must be signed in to change notification settings - Fork 509
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
Implemented resizing for non-decorated winit windows #5026
Changes from 2 commits
4e0504f
bcd64b2
839fcb8
e287d39
52f68a2
496ef1b
3a0fc18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use winit::window::{CursorIcon, ResizeDirection}; | ||
|
||
pub fn handle_cursor_move_for_resize( | ||
window: &winit::window::Window, | ||
position: winit::dpi::PhysicalPosition<f64>, | ||
current_direction: Option<ResizeDirection>, | ||
) -> Option<ResizeDirection> { | ||
if !window.is_decorated() { | ||
let location = get_resize_direction(window.inner_size(), position, 8_f64); | ||
|
||
if current_direction != location { | ||
window.set_cursor_icon(resize_direction_cursor_icon(location)); | ||
} | ||
|
||
return location; | ||
} | ||
|
||
None | ||
} | ||
|
||
pub fn handle_resize(window: &winit::window::Window, direction: Option<ResizeDirection>) { | ||
if let Some(dir) = direction { | ||
let _ = window.drag_resize_window(dir); | ||
} | ||
} | ||
|
||
/// Get the cursor icon that corresponds to the resize direction. | ||
fn resize_direction_cursor_icon(resize_direction: Option<ResizeDirection>) -> CursorIcon { | ||
match resize_direction { | ||
Some(resize_direction) => match resize_direction { | ||
ResizeDirection::East => CursorIcon::EResize, | ||
ResizeDirection::North => CursorIcon::NResize, | ||
ResizeDirection::NorthEast => CursorIcon::NeResize, | ||
ResizeDirection::NorthWest => CursorIcon::NwResize, | ||
ResizeDirection::South => CursorIcon::SResize, | ||
ResizeDirection::SouthEast => CursorIcon::SeResize, | ||
ResizeDirection::SouthWest => CursorIcon::SwResize, | ||
ResizeDirection::West => CursorIcon::WResize, | ||
}, | ||
None => CursorIcon::Default, | ||
} | ||
} | ||
|
||
fn get_resize_direction( | ||
win_size: winit::dpi::PhysicalSize<u32>, | ||
position: winit::dpi::PhysicalPosition<f64>, | ||
border_size: f64, | ||
) -> Option<ResizeDirection> { | ||
enum X { | ||
West, | ||
East, | ||
Default, | ||
} | ||
|
||
enum Y { | ||
North, | ||
South, | ||
Default, | ||
} | ||
|
||
let xdir = if position.x < border_size { | ||
X::West | ||
} else if position.x > (win_size.width as f64 - border_size) { | ||
X::East | ||
} else { | ||
X::Default | ||
}; | ||
|
||
let ydir = if position.y < border_size { | ||
Y::North | ||
} else if position.y > (win_size.height as f64 - border_size) { | ||
Y::South | ||
} else { | ||
Y::Default | ||
}; | ||
|
||
Some(match xdir { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for this pattern I'd have done a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Funny you mention this because I wasn't sure which pattern was better for readability after trying both. I'll commit this change and you can let me know which one you like better once you read it. |
||
X::West => match ydir { | ||
Y::North => ResizeDirection::NorthWest, | ||
Y::South => ResizeDirection::SouthWest, | ||
Y::Default => ResizeDirection::West, | ||
}, | ||
|
||
X::East => match ydir { | ||
Y::North => ResizeDirection::NorthEast, | ||
Y::South => ResizeDirection::SouthEast, | ||
Y::Default => ResizeDirection::East, | ||
}, | ||
|
||
X::Default => match ydir { | ||
Y::North => ResizeDirection::North, | ||
Y::South => ResizeDirection::South, | ||
Y::Default => return None, | ||
}, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8 seems like a huge border.
I think it should be 1 or 2 logical pixel maximum. Don't you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I tried a few numbers and 3_f64 is the smallest I could go before I felt it started to get hard to hit it properly. Is that alright with you?