Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
8 changes: 8 additions & 0 deletions runtime/src/window/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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})")
}
Expand Down
17 changes: 16 additions & 1 deletion winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(
Expand Down