summaryrefslogtreecommitdiffstats
path: root/runtime/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-06-20 01:23:01 +0200
committerLibravatar GitHub <noreply@github.com>2024-06-20 01:23:01 +0200
commit714d4503154a6224c26f2eed6e399c73d57b4bf8 (patch)
tree9456eb2d40f2761c29dfa51f370efe11dc22f5e5 /runtime/src
parent19db068bbbebcda1756720525da247f35bd3a5e0 (diff)
parentc5f4bebeda8d6ef10efade7933a5ee58f06b62d1 (diff)
downloadiced-714d4503154a6224c26f2eed6e399c73d57b4bf8.tar.gz
iced-714d4503154a6224c26f2eed6e399c73d57b4bf8.tar.bz2
iced-714d4503154a6224c26f2eed6e399c73d57b4bf8.zip
Merge pull request #2469 from iced-rs/unify-shell-runtimes
`Daemon` API and Shell Runtime Unification
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/lib.rs16
-rw-r--r--runtime/src/task.rs33
-rw-r--r--runtime/src/window.rs76
3 files changed, 95 insertions, 30 deletions
diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs
index 5fde3039..b4a5e819 100644
--- a/runtime/src/lib.rs
+++ b/runtime/src/lib.rs
@@ -70,6 +70,12 @@ pub enum Action<T> {
/// Run a system action.
System(system::Action),
+
+ /// Exits the runtime.
+ ///
+ /// This will normally close any application windows and
+ /// terminate the runtime loop.
+ Exit,
}
impl<T> Action<T> {
@@ -88,6 +94,7 @@ impl<T> Action<T> {
Action::Clipboard(action) => Err(Action::Clipboard(action)),
Action::Window(action) => Err(Action::Window(action)),
Action::System(action) => Err(Action::System(action)),
+ Action::Exit => Err(Action::Exit),
}
}
}
@@ -110,6 +117,15 @@ where
}
Action::Window(_) => write!(f, "Action::Window"),
Action::System(action) => write!(f, "Action::System({action:?})"),
+ Action::Exit => write!(f, "Action::Exit"),
}
}
}
+
+/// Creates a [`Task`] that exits the iced runtime.
+///
+/// This will normally close any application windows and
+/// terminate the runtime loop.
+pub fn exit<T>() -> Task<T> {
+ Task::effect(Action::Exit)
+}
diff --git a/runtime/src/task.rs b/runtime/src/task.rs
index 740360ac..b8a83d6d 100644
--- a/runtime/src/task.rs
+++ b/runtime/src/task.rs
@@ -242,6 +242,39 @@ impl<T> Task<T> {
}
}
+impl<T> Task<Option<T>> {
+ /// Executes a new [`Task`] after this one, only when it produces `Some` value.
+ ///
+ /// The value is provided to the closure to create the subsequent [`Task`].
+ pub fn and_then<A>(
+ self,
+ f: impl Fn(T) -> Task<A> + MaybeSend + 'static,
+ ) -> Task<A>
+ where
+ T: MaybeSend + 'static,
+ A: MaybeSend + 'static,
+ {
+ self.then(move |option| option.map_or_else(Task::none, &f))
+ }
+}
+
+impl<T, E> Task<Result<T, E>> {
+ /// Executes a new [`Task`] after this one, only when it succeeds with an `Ok` value.
+ ///
+ /// The success value is provided to the closure to create the subsequent [`Task`].
+ pub fn and_then<A>(
+ self,
+ f: impl Fn(T) -> Task<A> + MaybeSend + 'static,
+ ) -> Task<A>
+ where
+ T: MaybeSend + 'static,
+ E: MaybeSend + 'static,
+ A: MaybeSend + 'static,
+ {
+ self.then(move |option| option.map_or_else(|_| Task::none(), &f))
+ }
+}
+
impl<T> From<()> for Task<T>
where
T: MaybeSend + 'static,
diff --git a/runtime/src/window.rs b/runtime/src/window.rs
index 956a20e1..b04e5d59 100644
--- a/runtime/src/window.rs
+++ b/runtime/src/window.rs
@@ -26,6 +26,12 @@ pub enum Action {
/// Close the window and exits the application.
Close(Id),
+ /// Gets the [`Id`] of the oldest window.
+ GetOldest(oneshot::Sender<Option<Id>>),
+
+ /// Gets the [`Id`] of the latest window.
+ GetLatest(oneshot::Sender<Option<Id>>),
+
/// Move the window with the left mouse button until the button is
/// released.
///
@@ -36,26 +42,26 @@ pub enum Action {
/// Resize the window to the given logical dimensions.
Resize(Id, Size),
- /// Fetch the current logical dimensions of the window.
- FetchSize(Id, oneshot::Sender<Size>),
+ /// Get the current logical dimensions of the window.
+ GetSize(Id, oneshot::Sender<Size>),
- /// Fetch if the current window is maximized or not.
- FetchMaximized(Id, oneshot::Sender<bool>),
+ /// Get if the current window is maximized or not.
+ GetMaximized(Id, oneshot::Sender<bool>),
/// Set the window to maximized or back
Maximize(Id, bool),
- /// Fetch if the current window is minimized or not.
+ /// Get if the current window is minimized or not.
///
/// ## Platform-specific
/// - **Wayland:** Always `None`.
- FetchMinimized(Id, oneshot::Sender<Option<bool>>),
+ GetMinimized(Id, oneshot::Sender<Option<bool>>),
/// Set the window to minimized or back
Minimize(Id, bool),
- /// Fetch the current logical coordinates of the window.
- FetchPosition(Id, oneshot::Sender<Option<Point>>),
+ /// Get the current logical coordinates of the window.
+ GetPosition(Id, oneshot::Sender<Option<Point>>),
/// Move the window to the given logical coordinates.
///
@@ -65,8 +71,8 @@ pub enum Action {
/// Change the [`Mode`] of the window.
ChangeMode(Id, Mode),
- /// Fetch the current [`Mode`] of the window.
- FetchMode(Id, oneshot::Sender<Mode>),
+ /// Get the current [`Mode`] of the window.
+ GetMode(Id, oneshot::Sender<Mode>),
/// Toggle the window to maximized or back
ToggleMaximize(Id),
@@ -114,8 +120,8 @@ pub enum Action {
/// Android / iOS / macOS / Orbital / Web / X11: Unsupported.
ShowSystemMenu(Id),
- /// Fetch the raw identifier unique to the window.
- FetchRawId(Id, oneshot::Sender<u64>),
+ /// Get the raw identifier unique to the window.
+ GetRawId(Id, oneshot::Sender<u64>),
/// Change the window [`Icon`].
///
@@ -214,6 +220,16 @@ pub fn close<T>(id: Id) -> Task<T> {
Task::effect(crate::Action::Window(Action::Close(id)))
}
+/// Gets the window [`Id`] of the oldest window.
+pub fn get_oldest() -> Task<Option<Id>> {
+ Task::oneshot(|channel| crate::Action::Window(Action::GetOldest(channel)))
+}
+
+/// Gets the window [`Id`] of the latest window.
+pub fn get_latest() -> Task<Option<Id>> {
+ Task::oneshot(|channel| crate::Action::Window(Action::GetLatest(channel)))
+}
+
/// Begins dragging the window while the left mouse button is held.
pub fn drag<T>(id: Id) -> Task<T> {
Task::effect(crate::Action::Window(Action::Drag(id)))
@@ -224,17 +240,17 @@ pub fn resize<T>(id: Id, new_size: Size) -> Task<T> {
Task::effect(crate::Action::Window(Action::Resize(id, new_size)))
}
-/// Fetches the window's size in logical dimensions.
-pub fn fetch_size(id: Id) -> Task<Size> {
+/// Get the window's size in logical dimensions.
+pub fn get_size(id: Id) -> Task<Size> {
Task::oneshot(move |channel| {
- crate::Action::Window(Action::FetchSize(id, channel))
+ crate::Action::Window(Action::GetSize(id, channel))
})
}
-/// Fetches if the window is maximized.
-pub fn fetch_maximized(id: Id) -> Task<bool> {
+/// Gets the maximized state of the window with the given [`Id`].
+pub fn get_maximized(id: Id) -> Task<bool> {
Task::oneshot(move |channel| {
- crate::Action::Window(Action::FetchMaximized(id, channel))
+ crate::Action::Window(Action::GetMaximized(id, channel))
})
}
@@ -243,10 +259,10 @@ pub fn maximize<T>(id: Id, maximized: bool) -> Task<T> {
Task::effect(crate::Action::Window(Action::Maximize(id, maximized)))
}
-/// Fetches if the window is minimized.
-pub fn fetch_minimized(id: Id) -> Task<Option<bool>> {
+/// Gets the minimized state of the window with the given [`Id`].
+pub fn get_minimized(id: Id) -> Task<Option<bool>> {
Task::oneshot(move |channel| {
- crate::Action::Window(Action::FetchMinimized(id, channel))
+ crate::Action::Window(Action::GetMinimized(id, channel))
})
}
@@ -255,10 +271,10 @@ pub fn minimize<T>(id: Id, minimized: bool) -> Task<T> {
Task::effect(crate::Action::Window(Action::Minimize(id, minimized)))
}
-/// Fetches the current window position in logical coordinates.
-pub fn fetch_position(id: Id) -> Task<Option<Point>> {
+/// Gets the position in logical coordinates of the window with the given [`Id`].
+pub fn get_position(id: Id) -> Task<Option<Point>> {
Task::oneshot(move |channel| {
- crate::Action::Window(Action::FetchPosition(id, channel))
+ crate::Action::Window(Action::GetPosition(id, channel))
})
}
@@ -272,10 +288,10 @@ pub fn change_mode<T>(id: Id, mode: Mode) -> Task<T> {
Task::effect(crate::Action::Window(Action::ChangeMode(id, mode)))
}
-/// Fetches the current [`Mode`] of the window.
-pub fn fetch_mode(id: Id) -> Task<Mode> {
+/// Gets the current [`Mode`] of the window.
+pub fn get_mode(id: Id) -> Task<Mode> {
Task::oneshot(move |channel| {
- crate::Action::Window(Action::FetchMode(id, channel))
+ crate::Action::Window(Action::GetMode(id, channel))
})
}
@@ -327,11 +343,11 @@ pub fn show_system_menu<T>(id: Id) -> Task<T> {
Task::effect(crate::Action::Window(Action::ShowSystemMenu(id)))
}
-/// Fetches an identifier unique to the window, provided by the underlying windowing system. This is
+/// Gets an identifier unique to the window, provided by the underlying windowing system. This is
/// not to be confused with [`Id`].
-pub fn fetch_raw_id<Message>(id: Id) -> Task<u64> {
+pub fn get_raw_id<Message>(id: Id) -> Task<u64> {
Task::oneshot(|channel| {
- crate::Action::Window(Action::FetchRawId(id, channel))
+ crate::Action::Window(Action::GetRawId(id, channel))
})
}