Merge pull request #2280 from n1ght-hunter/add-action-fetch-location

add fetch_location action
This commit is contained in:
Héctor Ramón 2024-02-22 09:33:13 +01:00 committed by GitHub
commit a27267b8fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 57 additions and 2 deletions

View file

@ -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

View file

@ -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)))

View file

@ -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})")
}

View file

@ -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,

View file

@ -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(