2022-08-18 14:39:15 +02:00
|
|
|
|
use crate::window::Mode;
|
|
|
|
|
|
|
|
|
|
|
|
use iced_futures::MaybeSend;
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
2021-09-01 19:21:49 +07:00
|
|
|
|
/// An operation to be performed on some window.
|
2022-08-18 14:39:15 +02:00
|
|
|
|
pub enum Action<T> {
|
2022-10-06 20:38:21 +02:00
|
|
|
|
/// Moves the window with the left mouse button until the button is
|
|
|
|
|
|
/// released.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// There’s no guarantee that this will work unless the left mouse
|
|
|
|
|
|
/// button was pressed immediately before this function is called.
|
|
|
|
|
|
Drag,
|
2021-09-01 19:21:49 +07:00
|
|
|
|
/// Resize the window.
|
|
|
|
|
|
Resize {
|
|
|
|
|
|
/// The new logical width of the window
|
|
|
|
|
|
width: u32,
|
|
|
|
|
|
/// The new logical height of the window
|
|
|
|
|
|
height: u32,
|
|
|
|
|
|
},
|
2022-10-09 16:35:28 +02:00
|
|
|
|
/// Sets the window to maximized or back
|
|
|
|
|
|
Maximize(bool),
|
2022-10-11 15:24:26 +02:00
|
|
|
|
/// Set the window to minimized or back
|
|
|
|
|
|
Minimize(bool),
|
2021-09-01 19:21:49 +07:00
|
|
|
|
/// Move the window.
|
2022-09-15 12:08:39 -07:00
|
|
|
|
///
|
|
|
|
|
|
/// Unsupported on Wayland.
|
2021-09-01 19:21:49 +07:00
|
|
|
|
Move {
|
|
|
|
|
|
/// The new logical x location of the window
|
|
|
|
|
|
x: i32,
|
|
|
|
|
|
/// The new logical y location of the window
|
|
|
|
|
|
y: i32,
|
|
|
|
|
|
},
|
2022-08-18 14:39:15 +02:00
|
|
|
|
/// Set the [`Mode`] of the window.
|
|
|
|
|
|
SetMode(Mode),
|
2022-10-09 16:35:28 +02:00
|
|
|
|
/// Sets the window to maximized or back
|
|
|
|
|
|
ToggleMaximize,
|
2022-12-10 01:40:53 +13:00
|
|
|
|
/// Toggles whether window has decorations
|
|
|
|
|
|
/// ## Platform-specific
|
|
|
|
|
|
/// - **X11:** Not implemented.
|
|
|
|
|
|
/// - **iOS / Android / Web:** Unsupported.
|
|
|
|
|
|
ToggleDecorations,
|
2022-08-18 14:39:15 +02:00
|
|
|
|
/// Fetch the current [`Mode`] of the window.
|
|
|
|
|
|
FetchMode(Box<dyn FnOnce(Mode) -> T + 'static>),
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> Action<T> {
|
|
|
|
|
|
/// Maps the output of a window [`Action`] using the provided closure.
|
|
|
|
|
|
pub fn map<A>(
|
|
|
|
|
|
self,
|
|
|
|
|
|
f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
|
|
|
|
|
|
) -> Action<A>
|
|
|
|
|
|
where
|
|
|
|
|
|
T: 'static,
|
|
|
|
|
|
{
|
|
|
|
|
|
match self {
|
2022-10-06 20:38:21 +02:00
|
|
|
|
Self::Drag => Action::Drag,
|
2022-08-18 14:39:15 +02:00
|
|
|
|
Self::Resize { width, height } => Action::Resize { width, height },
|
2022-10-09 16:35:28 +02:00
|
|
|
|
Self::Maximize(bool) => Action::Maximize(bool),
|
2022-10-11 15:24:26 +02:00
|
|
|
|
Self::Minimize(bool) => Action::Minimize(bool),
|
2022-08-18 14:39:15 +02:00
|
|
|
|
Self::Move { x, y } => Action::Move { x, y },
|
|
|
|
|
|
Self::SetMode(mode) => Action::SetMode(mode),
|
2022-10-09 16:35:28 +02:00
|
|
|
|
Self::ToggleMaximize => Action::ToggleMaximize,
|
2022-12-10 01:40:53 +13:00
|
|
|
|
Self::ToggleDecorations => Action::ToggleDecorations,
|
2022-08-18 14:39:15 +02:00
|
|
|
|
Self::FetchMode(o) => Action::FetchMode(Box::new(move |s| f(o(s)))),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for Action<T> {
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
|
match self {
|
2022-10-06 20:38:21 +02:00
|
|
|
|
Self::Drag => write!(f, "Action::Drag"),
|
2022-08-18 14:39:15 +02:00
|
|
|
|
Self::Resize { width, height } => write!(
|
|
|
|
|
|
f,
|
|
|
|
|
|
"Action::Resize {{ widget: {}, height: {} }}",
|
|
|
|
|
|
width, height
|
|
|
|
|
|
),
|
2022-10-09 16:35:28 +02:00
|
|
|
|
Self::Maximize(value) => write!(f, "Action::Maximize({})", value),
|
2022-10-11 15:24:26 +02:00
|
|
|
|
Self::Minimize(value) => write!(f, "Action::Minimize({}", value),
|
2022-08-18 14:39:15 +02:00
|
|
|
|
Self::Move { x, y } => {
|
|
|
|
|
|
write!(f, "Action::Move {{ x: {}, y: {} }}", x, y)
|
|
|
|
|
|
}
|
|
|
|
|
|
Self::SetMode(mode) => write!(f, "Action::SetMode({:?})", mode),
|
2022-10-09 16:35:28 +02:00
|
|
|
|
Self::ToggleMaximize => write!(f, "Action::ToggleMaximize"),
|
2022-12-10 01:40:53 +13:00
|
|
|
|
Self::ToggleDecorations => write!(f, "Action::ToggleDecorations"),
|
2022-08-18 14:39:15 +02:00
|
|
|
|
Self::FetchMode(_) => write!(f, "Action::FetchMode"),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-01 19:21:49 +07:00
|
|
|
|
}
|