From 59885e9a363dd73b3a3e8dd125decf0e34130c59 Mon Sep 17 00:00:00 2001 From: Night_Hunter Date: Thu, 22 Feb 2024 17:13:12 +1300 Subject: Add `fetch_location` command to `window` module --- runtime/src/window.rs | 11 +++++++++++ runtime/src/window/action.rs | 8 ++++++++ winit/src/application.rs | 14 ++++++++++++++ winit/src/multi_window.rs | 18 ++++++++++++++++++ 4 files changed, 51 insertions(+) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 04bcfcd8..c5acfddc 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -106,6 +106,17 @@ pub fn move_to(id: Id, position: Point) -> Command { Command::single(command::Action::Window(Action::Move(id, position))) } +/// Fetches the window's location in logical coordinates. +pub fn fetch_location( + id: Id, + f: impl FnOnce(Option) -> Message + 'static, +) -> Command { + Command::single(command::Action::Window(Action::FetchLocation( + id, + Box::new(f), + ))) +} + /// Changes the [`Mode`] of the window. pub fn change_mode(id: Id, mode: Mode) -> Command { Command::single(command::Action::Window(Action::ChangeMode(id, mode))) diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index 9bfc2b62..39eb1008 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -42,6 +42,8 @@ pub enum Action { /// /// Unsupported on Wayland. Move(Id, Point), + /// Fetch the current logical coordinates of the window. + FetchLocation(Id, Box) -> T + 'static>), /// Change the [`Mode`] of the window. ChangeMode(Id, Mode), /// Fetch the current [`Mode`] of the window. @@ -135,6 +137,9 @@ impl Action { } Self::Minimize(id, minimized) => Action::Minimize(id, minimized), Self::Move(id, position) => Action::Move(id, position), + Self::FetchLocation(id, o) => { + Action::FetchLocation(id, Box::new(move |s| f(o(s)))) + } Self::ChangeMode(id, mode) => Action::ChangeMode(id, mode), Self::FetchMode(id, o) => { Action::FetchMode(id, Box::new(move |s| f(o(s)))) @@ -189,6 +194,9 @@ impl fmt::Debug for Action { Self::Move(id, position) => { write!(f, "Action::Move({id:?}, {position})") } + Self::FetchLocation(id, _) => { + write!(f, "Action::FetchLocation({id:?})") + } Self::ChangeMode(id, mode) => { write!(f, "Action::SetMode({id:?}, {mode:?})") } diff --git a/winit/src/application.rs b/winit/src/application.rs index 1fd51d82..c86d1619 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -773,6 +773,20 @@ pub fn run_command( y: position.y, }); } + window::Action::FetchLocation(_id, callback) => { + let position = window + .inner_position() + .map(|p| { + let pos = + p.to_logical::(window.scale_factor()); + crate::core::Point::new(pos.x, pos.y) + }) + .ok(); + + proxy + .send_event(callback(position)) + .expect("Send message to event loop"); + } window::Action::ChangeMode(_id, mode) => { window.set_visible(conversion::visible(mode)); window.set_fullscreen(conversion::fullscreen( diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index c63dd433..3b00200b 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -1003,6 +1003,24 @@ fn run_command( ); } } + window::Action::FetchLocation(id, callback) => { + if let Some(window) = window_manager.get_mut(id) { + let position = window + .raw + .inner_position() + .map(|p| { + let pos = p.to_logical::( + window.raw.scale_factor(), + ); + crate::core::Point::new(pos.x, pos.y) + }) + .ok(); + + proxy + .send_event(callback(position)) + .expect("Send message to event loop"); + } + } window::Action::ChangeMode(id, mode) => { if let Some(window) = window_manager.get_mut(id) { window.raw.set_visible(conversion::visible(mode)); -- cgit From d6454b5d0ca4c3812d1614805de1094638153df1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Feb 2024 09:19:51 +0100 Subject: Rename `fetch_location` to `fetch_position` --- runtime/src/window.rs | 16 ++++++++-------- runtime/src/window/action.rs | 16 ++++++++-------- winit/src/application.rs | 23 ++++++++++++----------- winit/src/multi_window.rs | 29 +++++++++++++++-------------- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/runtime/src/window.rs b/runtime/src/window.rs index c5acfddc..24171e3e 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -101,22 +101,22 @@ pub fn minimize(id: Id, minimized: bool) -> Command { Command::single(command::Action::Window(Action::Minimize(id, minimized))) } -/// Moves the window to the given logical coordinates. -pub fn move_to(id: Id, position: Point) -> Command { - Command::single(command::Action::Window(Action::Move(id, position))) -} - -/// Fetches the window's location in logical coordinates. -pub fn fetch_location( +/// Fetches the current window position in logical coordinates. +pub fn fetch_position( id: Id, f: impl FnOnce(Option) -> Message + 'static, ) -> Command { - Command::single(command::Action::Window(Action::FetchLocation( + Command::single(command::Action::Window(Action::FetchPosition( id, Box::new(f), ))) } +/// Moves the window to the given logical coordinates. +pub fn move_to(id: Id, position: Point) -> Command { + Command::single(command::Action::Window(Action::Move(id, position))) +} + /// Changes the [`Mode`] of the window. pub fn change_mode(id: Id, mode: Mode) -> Command { Command::single(command::Action::Window(Action::ChangeMode(id, mode))) diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index 39eb1008..e44ff5a6 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -38,12 +38,12 @@ pub enum Action { FetchMinimized(Id, Box) -> T + 'static>), /// Set the window to minimized or back Minimize(Id, bool), + /// Fetch the current logical coordinates of the window. + FetchPosition(Id, Box) -> T + 'static>), /// Move the window to the given logical coordinates. /// /// Unsupported on Wayland. Move(Id, Point), - /// Fetch the current logical coordinates of the window. - FetchLocation(Id, Box) -> T + 'static>), /// Change the [`Mode`] of the window. ChangeMode(Id, Mode), /// Fetch the current [`Mode`] of the window. @@ -136,10 +136,10 @@ impl Action { Action::FetchMinimized(id, Box::new(move |s| f(o(s)))) } Self::Minimize(id, minimized) => Action::Minimize(id, minimized), - Self::Move(id, position) => Action::Move(id, position), - Self::FetchLocation(id, o) => { - Action::FetchLocation(id, Box::new(move |s| f(o(s)))) + Self::FetchPosition(id, o) => { + Action::FetchPosition(id, Box::new(move |s| f(o(s)))) } + Self::Move(id, position) => Action::Move(id, position), Self::ChangeMode(id, mode) => Action::ChangeMode(id, mode), Self::FetchMode(id, o) => { Action::FetchMode(id, Box::new(move |s| f(o(s)))) @@ -191,12 +191,12 @@ impl fmt::Debug for Action { Self::Minimize(id, minimized) => { write!(f, "Action::Minimize({id:?}, {minimized}") } + Self::FetchPosition(id, _) => { + write!(f, "Action::FetchPosition({id:?})") + } Self::Move(id, position) => { write!(f, "Action::Move({id:?}, {position})") } - Self::FetchLocation(id, _) => { - write!(f, "Action::FetchLocation({id:?})") - } Self::ChangeMode(id, mode) => { write!(f, "Action::SetMode({id:?}, {mode:?})") } diff --git a/winit/src/application.rs b/winit/src/application.rs index c86d1619..8d0d7456 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -767,19 +767,14 @@ pub fn run_command( window::Action::Minimize(_id, minimized) => { window.set_minimized(minimized); } - window::Action::Move(_id, position) => { - window.set_outer_position(winit::dpi::LogicalPosition { - x: position.x, - y: position.y, - }); - } - window::Action::FetchLocation(_id, callback) => { + window::Action::FetchPosition(_id, callback) => { let position = window .inner_position() - .map(|p| { - let pos = - p.to_logical::(window.scale_factor()); - crate::core::Point::new(pos.x, pos.y) + .map(|position| { + let position = position + .to_logical::(window.scale_factor()); + + crate::core::Point::new(position.x, position.y) }) .ok(); @@ -787,6 +782,12 @@ pub fn run_command( .send_event(callback(position)) .expect("Send message to event loop"); } + window::Action::Move(_id, position) => { + window.set_outer_position(winit::dpi::LogicalPosition { + x: position.x, + y: position.y, + }); + } window::Action::ChangeMode(_id, mode) => { window.set_visible(conversion::visible(mode)); window.set_fullscreen(conversion::fullscreen( diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 3b00200b..c9056c9f 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -993,26 +993,17 @@ fn run_command( window.raw.set_minimized(minimized); } } - window::Action::Move(id, position) => { - if let Some(window) = window_manager.get_mut(id) { - window.raw.set_outer_position( - winit::dpi::LogicalPosition { - x: position.x, - y: position.y, - }, - ); - } - } - window::Action::FetchLocation(id, callback) => { + window::Action::FetchPosition(id, callback) => { if let Some(window) = window_manager.get_mut(id) { let position = window .raw .inner_position() - .map(|p| { - let pos = p.to_logical::( + .map(|position| { + let position = position.to_logical::( window.raw.scale_factor(), ); - crate::core::Point::new(pos.x, pos.y) + + crate::core::Point::new(position.x, position.y) }) .ok(); @@ -1021,6 +1012,16 @@ fn run_command( .expect("Send message to event loop"); } } + window::Action::Move(id, position) => { + if let Some(window) = window_manager.get_mut(id) { + window.raw.set_outer_position( + winit::dpi::LogicalPosition { + x: position.x, + y: position.y, + }, + ); + } + } window::Action::ChangeMode(id, mode) => { if let Some(window) = window_manager.get_mut(id) { window.raw.set_visible(conversion::visible(mode)); -- cgit From f693aa4bc4d9994d1dce81d4ec7ce0b15dfc1ee2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Feb 2024 09:21:19 +0100 Subject: Use `crate::core::Point` instead of prefixing --- winit/src/application.rs | 4 ++-- winit/src/multi_window.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/winit/src/application.rs b/winit/src/application.rs index 8d0d7456..05a4f070 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -10,7 +10,7 @@ use crate::core::renderer; use crate::core::time::Instant; use crate::core::widget::operation; use crate::core::window; -use crate::core::{Event, Size}; +use crate::core::{Event, Point, Size}; use crate::futures::futures; use crate::futures::{Executor, Runtime, Subscription}; use crate::graphics::compositor::{self, Compositor}; @@ -774,7 +774,7 @@ pub fn run_command( let position = position .to_logical::(window.scale_factor()); - crate::core::Point::new(position.x, position.y) + Point::new(position.x, position.y) }) .ok(); diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index c9056c9f..03066d6c 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -10,7 +10,7 @@ use crate::core::mouse; use crate::core::renderer; use crate::core::widget::operation; use crate::core::window; -use crate::core::Size; +use crate::core::{Point, Size}; use crate::futures::futures::channel::mpsc; use crate::futures::futures::{task, Future, StreamExt}; use crate::futures::{Executor, Runtime, Subscription}; @@ -1003,7 +1003,7 @@ fn run_command( window.raw.scale_factor(), ); - crate::core::Point::new(position.x, position.y) + Point::new(position.x, position.y) }) .ok(); -- cgit From 9339728b6863523152fc40dd86fb78d1461c2b40 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Feb 2024 09:22:19 +0100 Subject: Update `CHANGELOG` --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75ccf988..4a3b668c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Default `disabled` style for `checkbox` and `hovered` style for `Svg`. [#2273](https://github.com/iced-rs/iced/pull/2273) - `From` and `From` implementations for `border::Radius`. [#2274](https://github.com/iced-rs/iced/pull/2274) - `size_hint` method for `Component` trait. [#2275](https://github.com/iced-rs/iced/pull/2275) +- `fetch_position` command in `window` module. [#2280](https://github.com/iced-rs/iced/pull/2280) ### Fixed - Black images when using OpenGL backend in `iced_wgpu`. [#2259](https://github.com/iced-rs/iced/pull/2259) @@ -22,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Many thanks to... +- @n1ght-hunter - @PolyMeilex - @rizzen-yazston - @wash2 -- cgit