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: add a window drag resize task #2642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod level;
mod mode;
mod position;
mod redraw_request;
mod resize_direction;
mod user_attention;

pub use event::Event;
Expand All @@ -17,5 +18,6 @@ pub use level::Level;
pub use mode::Mode;
pub use position::Position;
pub use redraw_request::RedrawRequest;
pub use resize_direction::ResizeDirection;
pub use settings::Settings;
pub use user_attention::UserAttention;
27 changes: 27 additions & 0 deletions core/src/window/resize_direction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Defines the orientation that a window resize will be performed.
#[derive(Debug, Clone, Copy)]
pub enum ResizeDirection {
/// The window will be resized along the eastern ( right ) edge.
East,

/// The window will be resized along the eastern ( top ) edge.
North,

/// The window will be resized at the northeastern ( top-right ) corner.
NorthEast,

/// The window will be resized at the northwestern ( top-left ) corner.
NorthWest,

/// The window will be resized along the southern ( bottom ) edge.
South,

/// The window will be resized at the southeastern ( bottom-right ) corner.
SouthEast,

/// The window will be resized at the southwestern ( bottom-left ) corner.
SouthWest,

/// The window will be resized along the western ( left ) edge.
West,
}
14 changes: 13 additions & 1 deletion runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use screenshot::Screenshot;

use crate::core::time::Instant;
use crate::core::window::{
Event, Icon, Id, Level, Mode, Settings, UserAttention,
Event, Icon, Id, Level, Mode, ResizeDirection, Settings, UserAttention,
};
use crate::core::{Point, Size};
use crate::futures::event;
Expand Down Expand Up @@ -39,6 +39,13 @@ pub enum Action {
/// button was pressed immediately before this function is called.
Drag(Id),

/// Resize the window with the left mouse button until the button is
/// released.
///
/// There’s no guarantee that this will work unless the left mouse
/// button was pressed immediately before this function is called.
DragResize(Id, ResizeDirection),

/// Resize the window to the given logical dimensions.
Resize(Id, Size),

Expand Down Expand Up @@ -264,6 +271,11 @@ pub fn drag<T>(id: Id) -> Task<T> {
task::effect(crate::Action::Window(Action::Drag(id)))
}

/// Begins resizing the window while the left mouse button is held.
pub fn drag_resize<T>(id: Id, direction: ResizeDirection) -> Task<T> {
task::effect(crate::Action::Window(Action::DragResize(id, direction)))
}

/// Resizes the window to the given logical dimensions.
pub fn resize<T>(id: Id, new_size: Size) -> Task<T> {
task::effect(crate::Action::Window(Action::Resize(id, new_size)))
Expand Down
28 changes: 27 additions & 1 deletion winit/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ pub fn native_key_code(
}
}

/// Converts some [`UserAttention`] into it's `winit` counterpart.
/// Converts some [`UserAttention`] into its `winit` counterpart.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically out of scope, but I noticed this typo so I figured I'd fix it while I'm at it.

///
/// [`UserAttention`]: window::UserAttention
pub fn user_attention(
Expand All @@ -1128,6 +1128,32 @@ pub fn user_attention(
}
}

/// Converts some [`ResizeDirection`] into its `winit` counterpart.
///
/// [`ResizeDirection`]: window::ResizeDirection
pub fn resize_direction(
resize_direction: window::ResizeDirection,
) -> winit::window::ResizeDirection {
match resize_direction {
window::ResizeDirection::East => winit::window::ResizeDirection::East,
window::ResizeDirection::North => winit::window::ResizeDirection::North,
window::ResizeDirection::NorthEast => {
winit::window::ResizeDirection::NorthEast
}
window::ResizeDirection::NorthWest => {
winit::window::ResizeDirection::NorthWest
}
window::ResizeDirection::South => winit::window::ResizeDirection::South,
window::ResizeDirection::SouthEast => {
winit::window::ResizeDirection::SouthEast
}
window::ResizeDirection::SouthWest => {
winit::window::ResizeDirection::SouthWest
}
window::ResizeDirection::West => winit::window::ResizeDirection::West,
}
}

/// Converts some [`window::Icon`] into it's `winit` counterpart.
///
/// Returns `None` if there is an error during the conversion.
Expand Down
7 changes: 7 additions & 0 deletions winit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,13 @@ fn run_action<P, C>(
let _ = window.raw.drag_window();
}
}
window::Action::DragResize(id, direction) => {
if let Some(window) = window_manager.get_mut(id) {
let _ = window.raw.drag_resize_window(
conversion::resize_direction(direction),
);
}
}
window::Action::Resize(id, size) => {
if let Some(window) = window_manager.get_mut(id) {
let _ = window.raw.request_inner_size(
Expand Down