Skip to content

Commit

Permalink
add fetch_location action
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ght-hunter committed Feb 22, 2024
1 parent 6c00e61 commit 06e26df
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ pub fn move_to<Message>(id: Id, position: Point) -> Command<Message> {
Command::single(command::Action::Window(Action::Move(id, position)))
}

/// Fetches the window's location in logical coordinates.
pub fn fetch_location<Message>(
id: Id,

Check failure on line 111 in runtime/src/window.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/runtime/src/window.rs
f: impl FnOnce(Option<Point>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Window(Action::FetchLocation(id, Box::new(f))))
}

/// Changes the [`Mode`] of the window.
pub fn change_mode<Message>(id: Id, mode: Mode) -> Command<Message> {
Command::single(command::Action::Window(Action::ChangeMode(id, mode)))
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 @@ -42,6 +42,8 @@ pub enum Action<T> {
///
/// Unsupported on Wayland.
Move(Id, Point),
/// Fetch the current logical coordinates of the window.
FetchLocation(Id, Box<dyn FnOnce(Option<Point>) -> T + 'static>),
/// Change the [`Mode`] of the window.
ChangeMode(Id, Mode),
/// Fetch the current [`Mode`] of the window.
Expand Down Expand Up @@ -135,6 +137,9 @@ impl<T> Action<T> {
}
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))))
Expand Down Expand Up @@ -189,6 +194,9 @@ impl<T> fmt::Debug for Action<T> {
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:?})")
}
Expand Down
10 changes: 10 additions & 0 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,16 @@ pub fn run_command<A, C, E>(
y: position.y,
});

Check failure on line 774 in winit/src/application.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/winit/src/application.rs
}
window::Action::FetchLocation(_id, callback) => {
let position = window.inner_position().map(|p| {
let pos = p.to_logical::<f32>(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(
Expand Down
17 changes: 17 additions & 0 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,23 @@ fn run_command<A, C, E>(
);
}
}
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::<f32>(
window.raw.scale_factor(),

Check failure on line 1013 in winit/src/multi_window.rs

View workflow job for this annotation

GitHub Actions / all

Diff in /home/runner/work/iced/iced/winit/src/multi_window.rs
);
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));
Expand Down

0 comments on commit 06e26df

Please sign in to comment.