diff options
author | 2024-02-22 09:33:13 +0100 | |
---|---|---|
committer | 2024-02-22 09:33:13 +0100 | |
commit | a27267b8fbd7cfc1033c1ca9aff9a57ae57f0030 (patch) | |
tree | 4ef2366666b5df94a5dde191088c7d7385a1c788 | |
parent | ce4eef64cdde5f3e5809c963a1d84c933d27e7ae (diff) | |
parent | 9339728b6863523152fc40dd86fb78d1461c2b40 (diff) | |
download | iced-a27267b8fbd7cfc1033c1ca9aff9a57ae57f0030.tar.gz iced-a27267b8fbd7cfc1033c1ca9aff9a57ae57f0030.tar.bz2 iced-a27267b8fbd7cfc1033c1ca9aff9a57ae57f0030.zip |
Merge pull request #2280 from n1ght-hunter/add-action-fetch-location
add fetch_location action
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | runtime/src/window.rs | 11 | ||||
-rw-r--r-- | runtime/src/window/action.rs | 8 | ||||
-rw-r--r-- | winit/src/application.rs | 17 | ||||
-rw-r--r-- | winit/src/multi_window.rs | 21 |
5 files changed, 57 insertions, 2 deletions
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<u16>` and `From<i32>` 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 diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 04bcfcd8..24171e3e 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -101,6 +101,17 @@ pub fn minimize<Message>(id: Id, minimized: bool) -> Command<Message> { Command::single(command::Action::Window(Action::Minimize(id, minimized))) } +/// Fetches the current window position in logical coordinates. +pub fn fetch_position<Message>( + id: Id, + f: impl FnOnce(Option<Point>) -> Message + 'static, +) -> Command<Message> { + Command::single(command::Action::Window(Action::FetchPosition( + id, + Box::new(f), + ))) +} + /// Moves the window to the given logical coordinates. pub fn move_to<Message>(id: Id, position: Point) -> Command<Message> { Command::single(command::Action::Window(Action::Move(id, position))) diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index 9bfc2b62..e44ff5a6 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -38,6 +38,8 @@ pub enum Action<T> { FetchMinimized(Id, Box<dyn FnOnce(Option<bool>) -> T + 'static>), /// Set the window to minimized or back Minimize(Id, bool), + /// Fetch the current logical coordinates of the window. + FetchPosition(Id, Box<dyn FnOnce(Option<Point>) -> T + 'static>), /// Move the window to the given logical coordinates. /// /// Unsupported on Wayland. @@ -134,6 +136,9 @@ impl<T> Action<T> { Action::FetchMinimized(id, Box::new(move |s| f(o(s)))) } Self::Minimize(id, minimized) => Action::Minimize(id, minimized), + 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) => { @@ -186,6 +191,9 @@ impl<T> fmt::Debug for Action<T> { 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})") } diff --git a/winit/src/application.rs b/winit/src/application.rs index 1fd51d82..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}; @@ -767,6 +767,21 @@ pub fn run_command<A, C, E>( window::Action::Minimize(_id, minimized) => { window.set_minimized(minimized); } + window::Action::FetchPosition(_id, callback) => { + let position = window + .inner_position() + .map(|position| { + let position = position + .to_logical::<f32>(window.scale_factor()); + + Point::new(position.x, position.y) + }) + .ok(); + + proxy + .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, diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index c63dd433..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}; @@ -993,6 +993,25 @@ fn run_command<A, C, E>( window.raw.set_minimized(minimized); } } + window::Action::FetchPosition(id, callback) => { + if let Some(window) = window_manager.get_mut(id) { + let position = window + .raw + .inner_position() + .map(|position| { + let position = position.to_logical::<f32>( + window.raw.scale_factor(), + ); + + Point::new(position.x, position.y) + }) + .ok(); + + proxy + .send_event(callback(position)) + .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( |