From 341c9a3c12aa9d327ef1d8f168ea0adb9b5ad10b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 19 Jun 2024 01:53:40 +0200 Subject: Introduce `daemon` API and unify shell runtimes --- runtime/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'runtime/src') 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 { /// Run a system action. System(system::Action), + + /// Exits the runtime. + /// + /// This will normally close any application windows and + /// terminate the runtime loop. + Exit, } impl Action { @@ -88,6 +94,7 @@ impl Action { 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() -> Task { + Task::effect(Action::Exit) +} -- cgit From bdd30f7ab8c2280c368b86cb12baec0578669d44 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 20 Jun 2024 01:11:21 +0200 Subject: Introduce `and_then` methods for fallible `Task`s --- runtime/src/task.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'runtime/src') 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 Task { } } +impl Task> { + /// 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( + self, + f: impl Fn(T) -> Task + MaybeSend + 'static, + ) -> Task + where + T: MaybeSend + 'static, + A: MaybeSend + 'static, + { + self.then(move |option| option.map_or_else(Task::none, &f)) + } +} + +impl Task> { + /// 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( + self, + f: impl Fn(T) -> Task + MaybeSend + 'static, + ) -> Task + where + T: MaybeSend + 'static, + E: MaybeSend + 'static, + A: MaybeSend + 'static, + { + self.then(move |option| option.map_or_else(|_| Task::none(), &f)) + } +} + impl From<()> for Task where T: MaybeSend + 'static, -- cgit From 92e08c8f07511cc212cbce545fb7739ef1a4bf1f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 20 Jun 2024 01:13:09 +0200 Subject: Add `get_latest` and `get_oldest` tasks in `window` --- runtime/src/window.rs | 76 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 30 deletions(-) (limited to 'runtime/src') 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>), + + /// Gets the [`Id`] of the latest window. + GetLatest(oneshot::Sender>), + /// 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), + /// Get the current logical dimensions of the window. + GetSize(Id, oneshot::Sender), - /// Fetch if the current window is maximized or not. - FetchMaximized(Id, oneshot::Sender), + /// Get if the current window is maximized or not. + GetMaximized(Id, oneshot::Sender), /// 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>), + GetMinimized(Id, oneshot::Sender>), /// Set the window to minimized or back Minimize(Id, bool), - /// Fetch the current logical coordinates of the window. - FetchPosition(Id, oneshot::Sender>), + /// Get the current logical coordinates of the window. + GetPosition(Id, oneshot::Sender>), /// 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), + /// Get the current [`Mode`] of the window. + GetMode(Id, oneshot::Sender), /// 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), + /// Get the raw identifier unique to the window. + GetRawId(Id, oneshot::Sender), /// Change the window [`Icon`]. /// @@ -214,6 +220,16 @@ pub fn close(id: Id) -> Task { Task::effect(crate::Action::Window(Action::Close(id))) } +/// Gets the window [`Id`] of the oldest window. +pub fn get_oldest() -> Task> { + Task::oneshot(|channel| crate::Action::Window(Action::GetOldest(channel))) +} + +/// Gets the window [`Id`] of the latest window. +pub fn get_latest() -> Task> { + Task::oneshot(|channel| crate::Action::Window(Action::GetLatest(channel))) +} + /// Begins dragging the window while the left mouse button is held. pub fn drag(id: Id) -> Task { Task::effect(crate::Action::Window(Action::Drag(id))) @@ -224,17 +240,17 @@ pub fn resize(id: Id, new_size: Size) -> Task { 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 { +/// Get the window's size in logical dimensions. +pub fn get_size(id: Id) -> Task { 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 { +/// Gets the maximized state of the window with the given [`Id`]. +pub fn get_maximized(id: Id) -> Task { 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(id: Id, maximized: bool) -> Task { Task::effect(crate::Action::Window(Action::Maximize(id, maximized))) } -/// Fetches if the window is minimized. -pub fn fetch_minimized(id: Id) -> Task> { +/// Gets the minimized state of the window with the given [`Id`]. +pub fn get_minimized(id: Id) -> Task> { 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(id: Id, minimized: bool) -> Task { Task::effect(crate::Action::Window(Action::Minimize(id, minimized))) } -/// Fetches the current window position in logical coordinates. -pub fn fetch_position(id: Id) -> Task> { +/// Gets the position in logical coordinates of the window with the given [`Id`]. +pub fn get_position(id: Id) -> Task> { 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(id: Id, mode: Mode) -> Task { Task::effect(crate::Action::Window(Action::ChangeMode(id, mode))) } -/// Fetches the current [`Mode`] of the window. -pub fn fetch_mode(id: Id) -> Task { +/// Gets the current [`Mode`] of the window. +pub fn get_mode(id: Id) -> Task { 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(id: Id) -> Task { 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(id: Id) -> Task { +pub fn get_raw_id(id: Id) -> Task { Task::oneshot(|channel| { - crate::Action::Window(Action::FetchRawId(id, channel)) + crate::Action::Window(Action::GetRawId(id, channel)) }) } -- cgit