Replace Command with a new Task API with chain support

This commit is contained in:
Héctor Ramón Jiménez 2024-06-14 01:47:39 +02:00
parent e6d0b3bda5
commit a25b1af456
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
74 changed files with 1351 additions and 1767 deletions

View file

@ -2,7 +2,7 @@
use crate::core::text;
use crate::graphics::compositor;
use crate::shell::application;
use crate::{Command, Element, Executor, Settings, Subscription};
use crate::{Element, Executor, Settings, Subscription, Task};
pub use application::{Appearance, DefaultStyle};
@ -16,7 +16,7 @@ pub use application::{Appearance, DefaultStyle};
/// document.
///
/// An [`Application`] can execute asynchronous actions by returning a
/// [`Command`] in some of its methods.
/// [`Task`] in some of its methods.
///
/// When using an [`Application`] with the `debug` feature enabled, a debug view
/// can be toggled by pressing `F12`.
@ -62,7 +62,7 @@ pub use application::{Appearance, DefaultStyle};
/// ```no_run
/// use iced::advanced::Application;
/// use iced::executor;
/// use iced::{Command, Element, Settings, Theme, Renderer};
/// use iced::{Task, Element, Settings, Theme, Renderer};
///
/// pub fn main() -> iced::Result {
/// Hello::run(Settings::default())
@ -77,16 +77,16 @@ pub use application::{Appearance, DefaultStyle};
/// type Theme = Theme;
/// type Renderer = Renderer;
///
/// fn new(_flags: ()) -> (Hello, Command<Self::Message>) {
/// (Hello, Command::none())
/// fn new(_flags: ()) -> (Hello, Task<Self::Message>) {
/// (Hello, Task::none())
/// }
///
/// fn title(&self) -> String {
/// String::from("A cool application")
/// }
///
/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
/// Command::none()
/// fn update(&mut self, _message: Self::Message) -> Task<Self::Message> {
/// Task::none()
/// }
///
/// fn view(&self) -> Element<Self::Message> {
@ -123,12 +123,12 @@ where
///
/// Here is where you should return the initial state of your app.
///
/// Additionally, you can return a [`Command`] if you need to perform some
/// Additionally, you can return a [`Task`] if you need to perform some
/// async action in the background on startup. This is useful if you want to
/// load state from a file, perform an initial HTTP request, etc.
///
/// [`run`]: Self::run
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
fn new(flags: Self::Flags) -> (Self, Task<Self::Message>);
/// Returns the current title of the [`Application`].
///
@ -142,8 +142,8 @@ where
/// produced by either user interactions or commands, will be handled by
/// this method.
///
/// Any [`Command`] returned will be executed immediately in the background.
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
/// Any [`Task`] returned will be executed immediately in the background.
fn update(&mut self, message: Self::Message) -> Task<Self::Message>;
/// Returns the widgets to display in the [`Application`].
///
@ -234,7 +234,7 @@ where
type Theme = A::Theme;
type Renderer = A::Renderer;
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
self.0.update(message)
}
@ -250,7 +250,7 @@ where
{
type Flags = A::Flags;
fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
fn new(flags: Self::Flags) -> (Self, Task<A::Message>) {
let (app, command) = A::new(flags);
(Instance(app), command)

View file

@ -203,6 +203,7 @@ pub use crate::core::{
Length, Padding, Pixels, Point, Radians, Rectangle, Rotation, Shadow, Size,
Theme, Transformation, Vector,
};
pub use crate::runtime::Task;
pub mod clipboard {
//! Access the clipboard.
@ -256,11 +257,6 @@ pub mod mouse {
};
}
pub mod command {
//! Run asynchronous actions.
pub use crate::runtime::command::{channel, Command};
}
pub mod subscription {
//! Listen to external events in your application.
pub use iced_futures::subscription::{
@ -312,7 +308,6 @@ pub mod widget {
mod runtime {}
}
pub use command::Command;
pub use error::Error;
pub use event::Event;
pub use executor::Executor;

View file

@ -1,6 +1,6 @@
//! Leverage multi-window support in your application.
use crate::window;
use crate::{Command, Element, Executor, Settings, Subscription};
use crate::{Element, Executor, Settings, Subscription, Task};
pub use crate::application::{Appearance, DefaultStyle};
@ -14,7 +14,7 @@ pub use crate::application::{Appearance, DefaultStyle};
/// document and display only the contents of the `window::Id::MAIN` window.
///
/// An [`Application`] can execute asynchronous actions by returning a
/// [`Command`] in some of its methods.
/// [`Task`] in some of its methods.
///
/// When using an [`Application`] with the `debug` feature enabled, a debug view
/// can be toggled by pressing `F12`.
@ -29,7 +29,7 @@ pub use crate::application::{Appearance, DefaultStyle};
///
/// ```no_run
/// use iced::{executor, window};
/// use iced::{Command, Element, Settings, Theme};
/// use iced::{Task, Element, Settings, Theme};
/// use iced::multi_window::{self, Application};
///
/// pub fn main() -> iced::Result {
@ -44,16 +44,16 @@ pub use crate::application::{Appearance, DefaultStyle};
/// type Message = ();
/// type Theme = Theme;
///
/// fn new(_flags: ()) -> (Hello, Command<Self::Message>) {
/// (Hello, Command::none())
/// fn new(_flags: ()) -> (Hello, Task<Self::Message>) {
/// (Hello, Task::none())
/// }
///
/// fn title(&self, _window: window::Id) -> String {
/// String::from("A cool application")
/// }
///
/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
/// Command::none()
/// fn update(&mut self, _message: Self::Message) -> Task<Self::Message> {
/// Task::none()
/// }
///
/// fn view(&self, _window: window::Id) -> Element<Self::Message> {
@ -89,12 +89,12 @@ where
///
/// Here is where you should return the initial state of your app.
///
/// Additionally, you can return a [`Command`] if you need to perform some
/// Additionally, you can return a [`Task`] if you need to perform some
/// async action in the background on startup. This is useful if you want to
/// load state from a file, perform an initial HTTP request, etc.
///
/// [`run`]: Self::run
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
fn new(flags: Self::Flags) -> (Self, Task<Self::Message>);
/// Returns the current title of the `window` of the [`Application`].
///
@ -108,8 +108,8 @@ where
/// produced by either user interactions or commands, will be handled by
/// this method.
///
/// Any [`Command`] returned will be executed immediately in the background.
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
/// Any [`Task`] returned will be executed immediately in the background.
fn update(&mut self, message: Self::Message) -> Task<Self::Message>;
/// Returns the widgets to display in the `window` of the [`Application`].
///
@ -207,7 +207,7 @@ where
type Theme = A::Theme;
type Renderer = crate::Renderer;
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
self.0.update(message)
}
@ -226,7 +226,7 @@ where
{
type Flags = A::Flags;
fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
fn new(flags: Self::Flags) -> (Self, Task<A::Message>) {
let (app, command) = A::new(flags);
(Instance(app), command)

View file

@ -35,7 +35,7 @@ use crate::core::text;
use crate::executor::{self, Executor};
use crate::graphics::compositor;
use crate::window;
use crate::{Command, Element, Font, Result, Settings, Size, Subscription};
use crate::{Element, Font, Result, Settings, Size, Subscription, Task};
pub use crate::application::{Appearance, DefaultStyle};
@ -76,7 +76,7 @@ pub fn program<State, Message, Theme, Renderer>(
) -> Program<impl Definition<State = State, Message = Message, Theme = Theme>>
where
State: 'static,
Message: Send + std::fmt::Debug,
Message: Send + std::fmt::Debug + 'static,
Theme: Default + DefaultStyle,
Renderer: self::Renderer,
{
@ -94,7 +94,7 @@ where
impl<State, Message, Theme, Renderer, Update, View> Definition
for Application<State, Message, Theme, Renderer, Update, View>
where
Message: Send + std::fmt::Debug,
Message: Send + std::fmt::Debug + 'static,
Theme: Default + DefaultStyle,
Renderer: self::Renderer,
Update: self::Update<State, Message>,
@ -106,15 +106,15 @@ where
type Renderer = Renderer;
type Executor = executor::Default;
fn load(&self) -> Command<Self::Message> {
Command::none()
fn load(&self) -> Task<Self::Message> {
Task::none()
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
) -> Task<Self::Message> {
self.update.update(state, message).into()
}
@ -197,7 +197,7 @@ impl<P: Definition> Program<P> {
fn new(
(program, initialize): Self::Flags,
) -> (Self, Command<Self::Message>) {
) -> (Self, Task<Self::Message>) {
let state = initialize();
let command = program.load();
@ -218,7 +218,7 @@ impl<P: Definition> Program<P> {
fn update(
&mut self,
message: Self::Message,
) -> Command<Self::Message> {
) -> Task<Self::Message> {
self.program.update(&mut self.state, message)
}
@ -357,10 +357,10 @@ impl<P: Definition> Program<P> {
}
}
/// Runs the [`Command`] produced by the closure at startup.
/// Runs the [`Task`] produced by the closure at startup.
pub fn load(
self,
f: impl Fn() -> Command<P::Message>,
f: impl Fn() -> Task<P::Message>,
) -> Program<
impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
@ -420,7 +420,7 @@ pub trait Definition: Sized {
type State;
/// The message of the program.
type Message: Send + std::fmt::Debug;
type Message: Send + std::fmt::Debug + 'static;
/// The theme of the program.
type Theme: Default + DefaultStyle;
@ -431,13 +431,13 @@ pub trait Definition: Sized {
/// The executor of the program.
type Executor: Executor;
fn load(&self) -> Command<Self::Message>;
fn load(&self) -> Task<Self::Message>;
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message>;
) -> Task<Self::Message>;
fn view<'a>(
&self,
@ -484,7 +484,7 @@ fn with_title<P: Definition>(
type Renderer = P::Renderer;
type Executor = P::Executor;
fn load(&self) -> Command<Self::Message> {
fn load(&self) -> Task<Self::Message> {
self.program.load()
}
@ -496,7 +496,7 @@ fn with_title<P: Definition>(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
) -> Task<Self::Message> {
self.program.update(state, message)
}
@ -532,7 +532,7 @@ fn with_title<P: Definition>(
fn with_load<P: Definition>(
program: P,
f: impl Fn() -> Command<P::Message>,
f: impl Fn() -> Task<P::Message>,
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithLoad<P, F> {
program: P,
@ -541,7 +541,7 @@ fn with_load<P: Definition>(
impl<P: Definition, F> Definition for WithLoad<P, F>
where
F: Fn() -> Command<P::Message>,
F: Fn() -> Task<P::Message>,
{
type State = P::State;
type Message = P::Message;
@ -549,15 +549,15 @@ fn with_load<P: Definition>(
type Renderer = P::Renderer;
type Executor = executor::Default;
fn load(&self) -> Command<Self::Message> {
Command::batch([self.program.load(), (self.load)()])
fn load(&self) -> Task<Self::Message> {
Task::batch([self.program.load(), (self.load)()])
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
) -> Task<Self::Message> {
self.program.update(state, message)
}
@ -621,7 +621,7 @@ fn with_subscription<P: Definition>(
(self.subscription)(state)
}
fn load(&self) -> Command<Self::Message> {
fn load(&self) -> Task<Self::Message> {
self.program.load()
}
@ -629,7 +629,7 @@ fn with_subscription<P: Definition>(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
) -> Task<Self::Message> {
self.program.update(state, message)
}
@ -686,7 +686,7 @@ fn with_theme<P: Definition>(
(self.theme)(state)
}
fn load(&self) -> Command<Self::Message> {
fn load(&self) -> Task<Self::Message> {
self.program.load()
}
@ -698,7 +698,7 @@ fn with_theme<P: Definition>(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
) -> Task<Self::Message> {
self.program.update(state, message)
}
@ -755,7 +755,7 @@ fn with_style<P: Definition>(
(self.style)(state, theme)
}
fn load(&self) -> Command<Self::Message> {
fn load(&self) -> Task<Self::Message> {
self.program.load()
}
@ -767,7 +767,7 @@ fn with_style<P: Definition>(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
) -> Task<Self::Message> {
self.program.update(state, message)
}
@ -822,26 +822,26 @@ where
/// The update logic of some [`Program`].
///
/// This trait allows the [`program`] builder to take any closure that
/// returns any `Into<Command<Message>>`.
/// returns any `Into<Task<Message>>`.
pub trait Update<State, Message> {
/// Processes the message and updates the state of the [`Program`].
fn update(
&self,
state: &mut State,
message: Message,
) -> impl Into<Command<Message>>;
) -> impl Into<Task<Message>>;
}
impl<T, State, Message, C> Update<State, Message> for T
where
T: Fn(&mut State, Message) -> C,
C: Into<Command<Message>>,
C: Into<Task<Message>>,
{
fn update(
&self,
state: &mut State,
message: Message,
) -> impl Into<Command<Message>> {
) -> impl Into<Task<Message>> {
self(state, message)
}
}