2022-10-19 23:33:20 -03:00
|
|
|
|
use crate::window::{self, Mode, UserAttention};
|
2022-08-18 14:39:15 +02:00
|
|
|
|
|
|
|
|
|
|
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-12-15 03:06:04 +01:00
|
|
|
|
/// Closes the current window and exits the application.
|
|
|
|
|
|
Close,
|
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,
|
2022-10-19 23:33:20 -03:00
|
|
|
|
/// TODO(derezzedex)
|
|
|
|
|
|
Spawn {
|
|
|
|
|
|
/// TODO(derezzedex)
|
|
|
|
|
|
settings: window::Settings,
|
|
|
|
|
|
},
|
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.
|
2022-10-19 23:33:20 -03:00
|
|
|
|
SetMode(window::Mode),
|
2022-12-10 01:53:00 +13:00
|
|
|
|
/// Fetch the current [`Mode`] of the window.
|
2022-10-19 23:33:20 -03:00
|
|
|
|
FetchMode(Box<dyn FnOnce(window::Mode) -> T + 'static>),
|
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.
|
2022-12-13 00:13:01 +01:00
|
|
|
|
/// - **Web:** Unsupported.
|
2022-12-10 01:40:53 +13:00
|
|
|
|
ToggleDecorations,
|
2022-12-10 01:53:00 +13:00
|
|
|
|
/// Requests user attention to the window, this has no effect if the application
|
|
|
|
|
|
/// is already focused. How requesting for user attention manifests is platform dependent,
|
|
|
|
|
|
/// see [`UserAttentionType`] for details.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Providing `None` will unset the request for user attention. Unsetting the request for
|
|
|
|
|
|
/// user attention might not be done automatically by the WM when the window receives input.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// ## Platform-specific
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - **iOS / Android / Web:** Unsupported.
|
|
|
|
|
|
/// - **macOS:** `None` has no effect.
|
|
|
|
|
|
/// - **X11:** Requests for user attention must be manually cleared.
|
|
|
|
|
|
/// - **Wayland:** Requires `xdg_activation_v1` protocol, `None` has no effect.
|
|
|
|
|
|
RequestUserAttention(Option<UserAttention>),
|
2022-12-10 01:49:29 +13:00
|
|
|
|
/// Brings the window to the front and sets input focus. Has no effect if the window is
|
|
|
|
|
|
/// already in focus, minimized, or not visible.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// This method steals input focus from other applications. Do not use this method unless
|
|
|
|
|
|
/// you are certain that's what the user wants. Focus stealing can cause an extremely disruptive
|
|
|
|
|
|
/// user experience.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// ## Platform-specific
|
|
|
|
|
|
///
|
|
|
|
|
|
/// - **Web / Wayland:** Unsupported.
|
2023-01-02 21:14:41 +01:00
|
|
|
|
GainFocus,
|
2022-08-18 14:39:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-19 23:33:20 -03:00
|
|
|
|
Self::Spawn { settings } => Action::Spawn { settings },
|
2022-12-15 03:06:04 +01:00
|
|
|
|
Self::Close => Action::Close,
|
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-12-10 01:53:00 +13:00
|
|
|
|
Self::FetchMode(o) => Action::FetchMode(Box::new(move |s| f(o(s)))),
|
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-12-10 01:53:00 +13:00
|
|
|
|
Self::RequestUserAttention(attention_type) => {
|
|
|
|
|
|
Action::RequestUserAttention(attention_type)
|
|
|
|
|
|
}
|
2023-01-02 21:14:41 +01:00
|
|
|
|
Self::GainFocus => Action::GainFocus,
|
2022-08-18 14:39:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for Action<T> {
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
|
match self {
|
2022-12-15 03:06:04 +01:00
|
|
|
|
Self::Close => write!(f, "Action::Close"),
|
2022-10-06 20:38:21 +02:00
|
|
|
|
Self::Drag => write!(f, "Action::Drag"),
|
2022-10-19 23:33:20 -03:00
|
|
|
|
Self::Spawn { settings } => {
|
|
|
|
|
|
write!(f, "Action::Spawn {{ settings: {:?} }}", settings)
|
|
|
|
|
|
}
|
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-12-10 01:53:00 +13:00
|
|
|
|
Self::FetchMode(_) => write!(f, "Action::FetchMode"),
|
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-12-10 01:53:00 +13:00
|
|
|
|
Self::RequestUserAttention(_) => {
|
|
|
|
|
|
write!(f, "Action::RequestUserAttention")
|
|
|
|
|
|
}
|
2023-01-02 21:14:41 +01:00
|
|
|
|
Self::GainFocus => write!(f, "Action::GainFocus"),
|
2022-08-18 14:39:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2021-09-01 19:21:49 +07:00
|
|
|
|
}
|