Skip to content

Commit

Permalink
Merge pull request #1927 from thunderstorm010/master
Browse files Browse the repository at this point in the history
Add command to retrieve window size
  • Loading branch information
hecrj authored Jul 6, 2023
2 parents a057f88 + f350a2f commit 7f805bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
12 changes: 10 additions & 2 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use screenshot::Screenshot;
use crate::command::{self, Command};
use crate::core::time::Instant;
use crate::core::window::{Event, Icon, Level, Mode, UserAttention};
use crate::core::Size;
use crate::futures::subscription::{self, Subscription};

/// Subscribes to the frames of the window of the running application.
Expand Down Expand Up @@ -37,8 +38,15 @@ pub fn drag<Message>() -> Command<Message> {
}

/// Resizes the window to the given logical dimensions.
pub fn resize<Message>(width: u32, height: u32) -> Command<Message> {
Command::single(command::Action::Window(Action::Resize { width, height }))
pub fn resize<Message>(new_size: Size<u32>) -> Command<Message> {
Command::single(command::Action::Window(Action::Resize(new_size)))
}

/// Fetches the current window size in logical dimensions.
pub fn fetch_size<Message>(
f: impl FnOnce(Size<u32>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Window(Action::FetchSize(Box::new(f))))
}

/// Maximizes the window.
Expand Down
19 changes: 8 additions & 11 deletions runtime/src/window/action.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::core::window::{Icon, Level, Mode, UserAttention};
use crate::core::Size;
use crate::futures::MaybeSend;
use crate::window::Screenshot;

Expand All @@ -15,12 +16,9 @@ pub enum Action<T> {
/// button was pressed immediately before this function is called.
Drag,
/// Resize the window.
Resize {
/// The new logical width of the window
width: u32,
/// The new logical height of the window
height: u32,
},
Resize(Size<u32>),
/// Fetch the current size of the window.
FetchSize(Box<dyn FnOnce(Size<u32>) -> T + 'static>),
/// Set the window to maximized or back
Maximize(bool),
/// Set the window to minimized or back
Expand Down Expand Up @@ -106,7 +104,8 @@ impl<T> Action<T> {
match self {
Self::Close => Action::Close,
Self::Drag => Action::Drag,
Self::Resize { width, height } => Action::Resize { width, height },
Self::Resize(size) => Action::Resize(size),
Self::FetchSize(o) => Action::FetchSize(Box::new(move |s| f(o(s)))),
Self::Maximize(maximized) => Action::Maximize(maximized),
Self::Minimize(minimized) => Action::Minimize(minimized),
Self::Move { x, y } => Action::Move { x, y },
Expand Down Expand Up @@ -135,10 +134,8 @@ impl<T> fmt::Debug for Action<T> {
match self {
Self::Close => write!(f, "Action::Close"),
Self::Drag => write!(f, "Action::Drag"),
Self::Resize { width, height } => write!(
f,
"Action::Resize {{ widget: {width}, height: {height} }}"
),
Self::Resize(size) => write!(f, "Action::Resize({size:?})"),
Self::FetchSize(_) => write!(f, "Action::FetchSize"),
Self::Maximize(maximized) => {
write!(f, "Action::Maximize({maximized})")
}
Expand Down
16 changes: 13 additions & 3 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,12 +748,22 @@ pub fn run_command<A, C, E>(
window::Action::Drag => {
let _res = window.drag_window();
}
window::Action::Resize { width, height } => {
window::Action::Resize(size) => {
window.set_inner_size(winit::dpi::LogicalSize {
width,
height,
width: size.width,
height: size.height,
});
}
window::Action::FetchSize(callback) => {
let size = window.inner_size();

proxy
.send_event(callback(Size::new(
size.width,
size.height,
)))
.expect("Send message to event loop")
}
window::Action::Maximize(maximized) => {
window.set_maximized(maximized);
}
Expand Down

0 comments on commit 7f805bc

Please sign in to comment.