Add fetch_location command to window module
This commit is contained in:
parent
6c00e615b9
commit
59885e9a36
4 changed files with 51 additions and 0 deletions
|
|
@ -106,6 +106,17 @@ pub fn move_to<Message>(id: Id, position: Point) -> Command<Message> {
|
||||||
Command::single(command::Action::Window(Action::Move(id, position)))
|
Command::single(command::Action::Window(Action::Move(id, position)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetches the window's location in logical coordinates.
|
||||||
|
pub fn fetch_location<Message>(
|
||||||
|
id: Id,
|
||||||
|
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.
|
/// Changes the [`Mode`] of the window.
|
||||||
pub fn change_mode<Message>(id: Id, mode: Mode) -> Command<Message> {
|
pub fn change_mode<Message>(id: Id, mode: Mode) -> Command<Message> {
|
||||||
Command::single(command::Action::Window(Action::ChangeMode(id, mode)))
|
Command::single(command::Action::Window(Action::ChangeMode(id, mode)))
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,8 @@ pub enum Action<T> {
|
||||||
///
|
///
|
||||||
/// Unsupported on Wayland.
|
/// Unsupported on Wayland.
|
||||||
Move(Id, Point),
|
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.
|
/// Change the [`Mode`] of the window.
|
||||||
ChangeMode(Id, Mode),
|
ChangeMode(Id, Mode),
|
||||||
/// Fetch the current [`Mode`] of the window.
|
/// Fetch the current [`Mode`] of the window.
|
||||||
|
|
@ -135,6 +137,9 @@ impl<T> Action<T> {
|
||||||
}
|
}
|
||||||
Self::Minimize(id, minimized) => Action::Minimize(id, minimized),
|
Self::Minimize(id, minimized) => Action::Minimize(id, minimized),
|
||||||
Self::Move(id, position) => Action::Move(id, position),
|
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::ChangeMode(id, mode) => Action::ChangeMode(id, mode),
|
||||||
Self::FetchMode(id, o) => {
|
Self::FetchMode(id, o) => {
|
||||||
Action::FetchMode(id, Box::new(move |s| f(o(s))))
|
Action::FetchMode(id, Box::new(move |s| f(o(s))))
|
||||||
|
|
@ -189,6 +194,9 @@ impl<T> fmt::Debug for Action<T> {
|
||||||
Self::Move(id, position) => {
|
Self::Move(id, position) => {
|
||||||
write!(f, "Action::Move({id:?}, {position})")
|
write!(f, "Action::Move({id:?}, {position})")
|
||||||
}
|
}
|
||||||
|
Self::FetchLocation(id, _) => {
|
||||||
|
write!(f, "Action::FetchLocation({id:?})")
|
||||||
|
}
|
||||||
Self::ChangeMode(id, mode) => {
|
Self::ChangeMode(id, mode) => {
|
||||||
write!(f, "Action::SetMode({id:?}, {mode:?})")
|
write!(f, "Action::SetMode({id:?}, {mode:?})")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -773,6 +773,20 @@ pub fn run_command<A, C, E>(
|
||||||
y: position.y,
|
y: position.y,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
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::Action::ChangeMode(_id, mode) => {
|
||||||
window.set_visible(conversion::visible(mode));
|
window.set_visible(conversion::visible(mode));
|
||||||
window.set_fullscreen(conversion::fullscreen(
|
window.set_fullscreen(conversion::fullscreen(
|
||||||
|
|
|
||||||
|
|
@ -1003,6 +1003,24 @@ 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(),
|
||||||
|
);
|
||||||
|
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::Action::ChangeMode(id, mode) => {
|
||||||
if let Some(window) = window_manager.get_mut(id) {
|
if let Some(window) = window_manager.get_mut(id) {
|
||||||
window.raw.set_visible(conversion::visible(mode));
|
window.raw.set_visible(conversion::visible(mode));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue