From 21a71b753d6da2233bce913f4e623ee14859ec23 Mon Sep 17 00:00:00 2001 From: Yiğit Özdemir Date: Wed, 21 Jun 2023 19:43:20 +0300 Subject: Add command to retrieve window size --- core/src/window.rs | 2 ++ core/src/window/fetch_size.rs | 8 ++++++++ runtime/src/window/action.rs | 16 ++++++++++++++++ winit/src/application.rs | 16 ++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 core/src/window/fetch_size.rs diff --git a/core/src/window.rs b/core/src/window.rs index a6dbdfb4..cd343893 100644 --- a/core/src/window.rs +++ b/core/src/window.rs @@ -6,6 +6,7 @@ mod level; mod mode; mod redraw_request; mod user_attention; +mod fetch_size; pub use event::Event; pub use icon::Icon; @@ -13,3 +14,4 @@ pub use level::Level; pub use mode::Mode; pub use redraw_request::RedrawRequest; pub use user_attention::UserAttention; +pub use fetch_size::SizeType; \ No newline at end of file diff --git a/core/src/window/fetch_size.rs b/core/src/window/fetch_size.rs new file mode 100644 index 00000000..aa3431aa --- /dev/null +++ b/core/src/window/fetch_size.rs @@ -0,0 +1,8 @@ +/// A `struct` defining which size to fetch. +#[derive(Debug, Clone, Copy)] +pub enum SizeType { + /// Inner size. (not including title bars and so other OS decorations) + Inner, + /// Outer size. (including everything) + Outer, +} \ No newline at end of file diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index a9d2a3d0..4ea9d474 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -1,3 +1,7 @@ + + +use iced_core::window::SizeType; + use crate::core::window::{Icon, Level, Mode, UserAttention}; use crate::futures::MaybeSend; @@ -20,6 +24,13 @@ pub enum Action { /// The new logical height of the window height: u32, }, + /// Fetch the current size of the window. + FetchSize { + /// Which size to fetch + size_type: SizeType, + /// Callback function + callback: Box T + 'static>, + }, /// Set the window to maximized or back Maximize(bool), /// Set the window to minimized or back @@ -104,6 +115,10 @@ impl Action { Self::Close => Action::Close, Self::Drag => Action::Drag, Self::Resize { width, height } => Action::Resize { width, height }, + Self::FetchSize { size_type, callback } => Action::FetchSize { + size_type: size_type, + callback: Box::new(move |s| f(callback(s))), + }, Self::Maximize(maximized) => Action::Maximize(maximized), Self::Minimize(minimized) => Action::Minimize(minimized), Self::Move { x, y } => Action::Move { x, y }, @@ -131,6 +146,7 @@ impl fmt::Debug for Action { f, "Action::Resize {{ widget: {width}, height: {height} }}" ), + Self::FetchSize { size_type, .. } => write!(f, "Action::FetchSize {{ size_type: {size_type:?} }}"), Self::Maximize(maximized) => { write!(f, "Action::Maximize({maximized})") } diff --git a/winit/src/application.rs b/winit/src/application.rs index be416ac8..ff5afa69 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -3,6 +3,7 @@ mod profiler; mod state; +use iced_graphics::core::window::SizeType; pub use state::State; use crate::conversion; @@ -747,6 +748,21 @@ pub fn run_command( height, }); } + window::Action::FetchSize { + size_type, + callback, + } => { + let width_height = match size_type { + SizeType::Inner => window.inner_size(), + SizeType::Outer => window.outer_size(), + }; + let width_height = + (width_height.width, width_height.height); + + proxy + .send_event(callback(width_height)) + .expect("Send message to event loop") + } window::Action::Maximize(maximized) => { window.set_maximized(maximized); } -- cgit From b394c84b37eacb266d45663d5d6626f1b616af7e Mon Sep 17 00:00:00 2001 From: Yiğit Özdemir Date: Thu, 22 Jun 2023 18:28:32 +0300 Subject: Add FetchSize command - apply the changes discussed at #water-cooler --- core/src/window.rs | 4 +--- core/src/window/fetch_size.rs | 8 -------- runtime/src/window/action.rs | 18 +++--------------- winit/src/application.rs | 15 +++------------ 4 files changed, 7 insertions(+), 38 deletions(-) delete mode 100644 core/src/window/fetch_size.rs diff --git a/core/src/window.rs b/core/src/window.rs index cd343893..4beebf9f 100644 --- a/core/src/window.rs +++ b/core/src/window.rs @@ -6,12 +6,10 @@ mod level; mod mode; mod redraw_request; mod user_attention; -mod fetch_size; pub use event::Event; pub use icon::Icon; pub use level::Level; pub use mode::Mode; pub use redraw_request::RedrawRequest; -pub use user_attention::UserAttention; -pub use fetch_size::SizeType; \ No newline at end of file +pub use user_attention::UserAttention; \ No newline at end of file diff --git a/core/src/window/fetch_size.rs b/core/src/window/fetch_size.rs deleted file mode 100644 index aa3431aa..00000000 --- a/core/src/window/fetch_size.rs +++ /dev/null @@ -1,8 +0,0 @@ -/// A `struct` defining which size to fetch. -#[derive(Debug, Clone, Copy)] -pub enum SizeType { - /// Inner size. (not including title bars and so other OS decorations) - Inner, - /// Outer size. (including everything) - Outer, -} \ No newline at end of file diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index 4ea9d474..551d0a01 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -1,7 +1,3 @@ - - -use iced_core::window::SizeType; - use crate::core::window::{Icon, Level, Mode, UserAttention}; use crate::futures::MaybeSend; @@ -25,12 +21,7 @@ pub enum Action { height: u32, }, /// Fetch the current size of the window. - FetchSize { - /// Which size to fetch - size_type: SizeType, - /// Callback function - callback: Box T + 'static>, - }, + FetchSize(Box T + 'static>), /// Set the window to maximized or back Maximize(bool), /// Set the window to minimized or back @@ -115,10 +106,7 @@ impl Action { Self::Close => Action::Close, Self::Drag => Action::Drag, Self::Resize { width, height } => Action::Resize { width, height }, - Self::FetchSize { size_type, callback } => Action::FetchSize { - size_type: size_type, - callback: Box::new(move |s| f(callback(s))), - }, + 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 }, @@ -146,7 +134,7 @@ impl fmt::Debug for Action { f, "Action::Resize {{ widget: {width}, height: {height} }}" ), - Self::FetchSize { size_type, .. } => write!(f, "Action::FetchSize {{ size_type: {size_type:?} }}"), + Self::FetchSize(_) => write!(f, "Action::FetchSize"), Self::Maximize(maximized) => { write!(f, "Action::Maximize({maximized})") } diff --git a/winit/src/application.rs b/winit/src/application.rs index ff5afa69..b0824e0e 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -3,7 +3,6 @@ mod profiler; mod state; -use iced_graphics::core::window::SizeType; pub use state::State; use crate::conversion; @@ -748,19 +747,11 @@ pub fn run_command( height, }); } - window::Action::FetchSize { - size_type, - callback, - } => { - let width_height = match size_type { - SizeType::Inner => window.inner_size(), - SizeType::Outer => window.outer_size(), - }; - let width_height = - (width_height.width, width_height.height); + window::Action::FetchSize(callback) => { + let size = window.inner_size(); proxy - .send_event(callback(width_height)) + .send_event(callback((size.width, size.height))) .expect("Send message to event loop") } window::Action::Maximize(maximized) => { -- cgit From cc32bd4de09ee58c15d1b3f2cec4a79dc65dd035 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Jul 2023 06:41:28 +0200 Subject: Use `Size` in both `Resize` and `FetchSize` window actions --- runtime/src/window.rs | 5 +++-- runtime/src/window/action.rs | 17 +++++------------ winit/src/application.rs | 11 +++++++---- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index d4111293..094a713d 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -6,6 +6,7 @@ pub use action::Action; 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. @@ -34,8 +35,8 @@ pub fn drag() -> Command { } /// Resizes the window to the given logical dimensions. -pub fn resize(width: u32, height: u32) -> Command { - Command::single(command::Action::Window(Action::Resize { width, height })) +pub fn resize(new_size: Size) -> Command { + Command::single(command::Action::Window(Action::Resize(new_size))) } /// Maximizes the window. diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index 551d0a01..d0137895 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -1,4 +1,5 @@ use crate::core::window::{Icon, Level, Mode, UserAttention}; +use crate::core::Size; use crate::futures::MaybeSend; use std::fmt; @@ -14,14 +15,9 @@ pub enum Action { /// 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), /// Fetch the current size of the window. - FetchSize(Box T + 'static>), + FetchSize(Box) -> T + 'static>), /// Set the window to maximized or back Maximize(bool), /// Set the window to minimized or back @@ -105,7 +101,7 @@ impl Action { 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), @@ -130,10 +126,7 @@ impl fmt::Debug for Action { 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})") diff --git a/winit/src/application.rs b/winit/src/application.rs index b0824e0e..afc523b6 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -741,17 +741,20 @@ pub fn run_command( 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.width, size.height))) + .send_event(callback(Size::new( + size.width, + size.height, + ))) .expect("Send message to event loop") } window::Action::Maximize(maximized) => { -- cgit From f43a272d7d201b39553f954183362ef42b31d11c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Jul 2023 06:42:30 +0200 Subject: Add missing newline in `core::window` --- core/src/window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/window.rs b/core/src/window.rs index 4beebf9f..a6dbdfb4 100644 --- a/core/src/window.rs +++ b/core/src/window.rs @@ -12,4 +12,4 @@ pub use icon::Icon; pub use level::Level; pub use mode::Mode; pub use redraw_request::RedrawRequest; -pub use user_attention::UserAttention; \ No newline at end of file +pub use user_attention::UserAttention; -- cgit From f350a2f812487af9a43c36e28d9b904e76a66474 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 6 Jul 2023 06:44:09 +0200 Subject: Add `fetch_size` helper to `runtime::window` --- runtime/src/window.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 094a713d..9356581a 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -39,6 +39,13 @@ pub fn resize(new_size: Size) -> Command { Command::single(command::Action::Window(Action::Resize(new_size))) } +/// Fetches the current window size in logical dimensions. +pub fn fetch_size( + f: impl FnOnce(Size) -> Message + 'static, +) -> Command { + Command::single(command::Action::Window(Action::FetchSize(Box::new(f)))) +} + /// Maximizes the window. pub fn maximize(maximized: bool) -> Command { Command::single(command::Action::Window(Action::Maximize(maximized))) -- cgit