From c22269bff3085012d326a0df77bf27ad5bcb41b7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 05:33:47 +0100 Subject: Introduce `Program` API --- src/lib.rs | 48 ++++ src/program.rs | 669 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/settings.rs | 2 +- 3 files changed, 718 insertions(+), 1 deletion(-) create mode 100644 src/program.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index c596f2a6..f42a845e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -175,6 +175,7 @@ mod error; mod sandbox; pub mod application; +pub mod program; pub mod settings; pub mod time; pub mod window; @@ -308,6 +309,7 @@ pub use error::Error; pub use event::Event; pub use executor::Executor; pub use font::Font; +pub use program::Program; pub use renderer::Renderer; pub use sandbox::Sandbox; pub use settings::Settings; @@ -327,3 +329,49 @@ pub type Element< /// /// [`Application`]: crate::Application pub type Result = std::result::Result<(), Error>; + +/// Runs a basic iced application with default [`Settings`] given +/// - its window title, +/// - its update logic, +/// - and its view logic. +/// +/// # Example +/// ```no_run +/// use iced::widget::{button, column, text, Column}; +/// +/// pub fn main() -> iced::Result { +/// iced::run("A counter", update, view) +/// } +/// +/// #[derive(Debug, Clone)] +/// enum Message { +/// Increment, +/// } +/// +/// fn update(value: &mut u64, message: Message) { +/// match message { +/// Message::Increment => *value += 1, +/// } +/// } +/// +/// fn view(value: &u64) -> Column { +/// column![ +/// text(value), +/// button("+").on_press(Message::Increment), +/// ] +/// } +/// ``` +pub fn run( + title: &'static str, + update: impl Fn(&mut State, Message) + 'static, + view: impl for<'a> program::View<'a, State, Message> + 'static, +) -> Result +where + State: Default + 'static, + Message: std::fmt::Debug + Send + 'static, +{ + sandbox(update, view).title(title).run() +} + +#[doc(inline)] +pub use program::{application, sandbox}; diff --git a/src/program.rs b/src/program.rs new file mode 100644 index 00000000..d78fa505 --- /dev/null +++ b/src/program.rs @@ -0,0 +1,669 @@ +//! Create iced applications out of simple functions. +//! +//! You can use this API to create and run iced applications +//! step by step—without coupling your logic to a trait +//! or a specific type. +//! +//! This API is meant to be a more convenient—although less +//! powerful—alternative to the [`Sandbox`] and [`Application`] traits. +//! +//! [`Sandbox`]: crate::Sandbox +//! +//! # Example +//! ```no_run +//! use iced::widget::{button, column, text, Column}; +//! use iced::Theme; +//! +//! pub fn main() -> iced::Result { +//! iced::sandbox(update, view) +//! .title("A counter") +//! .theme(|_| Theme::Dark) +//! .centered() +//! .run() +//! } +//! +//! #[derive(Debug, Clone)] +//! enum Message { +//! Increment, +//! } +//! +//! fn update(value: &mut u64, message: Message) { +//! match message { +//! Message::Increment => *value += 1, +//! } +//! } +//! +//! fn view(value: &u64) -> Column { +//! column![ +//! text(value), +//! button("+").on_press(Message::Increment), +//! ] +//! } +//! ``` +use crate::application::{self, Application}; +use crate::executor::{self, Executor}; +use crate::window; +use crate::{Command, Element, Font, Result, Settings, Subscription}; + +use std::borrow::Cow; + +/// Creates the most basic kind of [`Program`] from some update and view logic. +/// +/// # Example +/// ```no_run +/// use iced::widget::{button, column, text, Column}; +/// +/// pub fn main() -> iced::Result { +/// iced::sandbox(update, view).title("A counter").run() +/// } +/// +/// #[derive(Debug, Clone)] +/// enum Message { +/// Increment, +/// } +/// +/// fn update(value: &mut u64, message: Message) { +/// match message { +/// Message::Increment => *value += 1, +/// } +/// } +/// +/// fn view(value: &u64) -> Column { +/// column![ +/// text(value), +/// button("+").on_press(Message::Increment), +/// ] +/// } +/// ``` +pub fn sandbox( + update: impl Fn(&mut State, Message), + view: impl for<'a> self::View<'a, State, Message>, +) -> Program< + impl Definition, +> +where + State: Default + 'static, + Message: Send + std::fmt::Debug, +{ + use std::marker::PhantomData; + + struct Sandbox { + update: Update, + view: View, + _state: PhantomData, + _message: PhantomData, + } + + impl Definition + for Sandbox + where + State: Default + 'static, + Message: Send + std::fmt::Debug, + Update: Fn(&mut State, Message), + View: for<'a> self::View<'a, State, Message>, + { + type State = State; + type Message = Message; + type Theme = crate::Theme; + type Executor = iced_futures::backend::null::Executor; + + fn new(&self) -> (Self::State, Command) { + (State::default(), Command::none()) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + (self.update)(state, message); + + Command::none() + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.view.view(state).into() + } + } + + Program { + raw: Sandbox { + update, + view, + _state: PhantomData, + _message: PhantomData, + }, + settings: Settings::default(), + } +} + +/// Creates a [`Program`] that can leverage the [`Command`] API for +/// concurrent operations. +pub fn application( + new: impl Fn() -> (State, Command), + update: impl Fn(&mut State, Message) -> Command, + view: impl for<'a> self::View<'a, State, Message>, +) -> Program< + impl Definition, +> +where + State: 'static, + Message: Send + std::fmt::Debug, +{ + use std::marker::PhantomData; + + struct Application { + new: New, + update: Update, + view: View, + _state: PhantomData, + _message: PhantomData, + } + + impl Definition + for Application + where + Message: Send + std::fmt::Debug, + New: Fn() -> (State, Command), + Update: Fn(&mut State, Message) -> Command, + View: for<'a> self::View<'a, State, Message>, + { + type State = State; + type Message = Message; + type Theme = crate::Theme; + type Executor = executor::Default; + + fn new(&self) -> (Self::State, Command) { + (self.new)() + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + (self.update)(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.view.view(state).into() + } + } + + Program { + raw: Application { + new, + update, + view, + _state: PhantomData, + _message: PhantomData, + }, + settings: Settings::default(), + } +} + +/// A fully functioning and configured iced application. +/// +/// It can be [`run`]! +/// +/// Create one with either the [`sandbox`] or [`application`] helpers. +/// +/// [`run`]: Program::run +/// [`application`]: self::application() +#[derive(Debug)] +pub struct Program { + raw: P, + settings: Settings, +} + +impl Program

{ + /// Runs the [`Program`]. + pub fn run(self) -> Result + where + Self: 'static, + { + struct Instance { + program: P, + state: P::State, + } + + impl Application for Instance

{ + type Message = P::Message; + type Theme = P::Theme; + type Flags = P; + type Executor = P::Executor; + + fn new(program: Self::Flags) -> (Self, Command) { + let (state, command) = P::new(&program); + + (Self { program, state }, command) + } + + fn title(&self) -> String { + self.program.title(&self.state) + } + + fn update( + &mut self, + message: Self::Message, + ) -> Command { + self.program.update(&mut self.state, message) + } + + fn view( + &self, + ) -> crate::Element<'_, Self::Message, Self::Theme, crate::Renderer> + { + self.program.view(&self.state) + } + + fn theme(&self) -> Self::Theme { + self.program.theme(&self.state) + } + + fn subscription(&self) -> Subscription { + self.program.subscription(&self.state) + } + } + + let Self { raw, settings } = self; + + Instance::run(Settings { + flags: raw, + id: settings.id, + window: settings.window, + fonts: settings.fonts, + default_font: settings.default_font, + default_text_size: settings.default_text_size, + antialiasing: settings.antialiasing, + }) + } + + /// Sets the [`Settings`] that will be used to run the [`Program`]. + pub fn settings(self, settings: Settings) -> Self { + Self { settings, ..self } + } + + /// Toggles the [`Settings::antialiasing`] to `true` for the [`Program`]. + pub fn antialiased(self) -> Self { + Self { + settings: Settings { + antialiasing: true, + ..self.settings + }, + ..self + } + } + + /// Sets the default [`Font`] of the [`Program`]. + pub fn default_font(self, default_font: Font) -> Self { + Self { + settings: Settings { + default_font, + ..self.settings + }, + ..self + } + } + + /// Sets the fonts that will be loaded at the start of the [`Program`]. + pub fn fonts( + self, + fonts: impl IntoIterator>, + ) -> Self { + Self { + settings: Settings { + fonts: fonts.into_iter().collect(), + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Program`]. + pub fn centered(self) -> Self { + Self { + settings: Settings { + window: window::Settings { + position: window::Position::Centered, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::exit_on_close_request`] to `false` in the [`Program`]. + pub fn ignore_close_request(self) -> Self { + Self { + settings: Settings { + window: window::Settings { + exit_on_close_request: false, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`Title`] of the [`Program`]. + pub fn title( + self, + title: impl Title, + ) -> Program< + impl Definition, + > { + Program { + raw: with_title(self.raw, title), + settings: self.settings, + } + } + + /// Sets the subscription logic of the [`Program`]. + pub fn subscription( + self, + f: impl Fn(&P::State) -> Subscription, + ) -> Program< + impl Definition, + > { + Program { + raw: with_subscription(self.raw, f), + settings: self.settings, + } + } + + /// Sets the theme logic of the [`Program`]. + pub fn theme( + self, + f: impl Fn(&P::State) -> P::Theme, + ) -> Program< + impl Definition, + > { + Program { + raw: with_theme(self.raw, f), + settings: self.settings, + } + } +} + +/// The internal definition of a [`Program`]. +/// +/// You should not need to implement this trait directly. Instead, use the +/// helper functions available in the [`program`] module and the [`Program`] struct. +/// +/// [`program`]: crate::program +#[allow(missing_docs)] +pub trait Definition: Sized { + /// The state of the program. + type State; + + /// The message of the program. + type Message: Send + std::fmt::Debug; + + /// The theme of the program. + type Theme: Default + application::DefaultStyle; + + /// The executor of the program. + type Executor: Executor; + + fn new(&self) -> (Self::State, Command); + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command; + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme>; + + fn title(&self, _state: &Self::State) -> String { + String::from("A cool iced application!") + } + + fn subscription( + &self, + _state: &Self::State, + ) -> Subscription { + Subscription::none() + } + + fn theme(&self, _state: &Self::State) -> Self::Theme { + Self::Theme::default() + } +} + +fn with_title( + program: P, + title: impl Title, +) -> impl Definition { + struct WithTitle { + program: P, + title: Title, + } + + impl Definition for WithTitle + where + P: Definition, + Title: self::Title, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn new(&self) -> (Self::State, Command) { + self.program.new() + } + + fn title(&self, state: &Self::State) -> String { + self.title.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + } + + WithTitle { program, title } +} + +fn with_subscription( + program: P, + f: impl Fn(&P::State) -> Subscription, +) -> impl Definition { + struct WithSubscription { + program: P, + subscription: F, + } + + impl Definition for WithSubscription + where + F: Fn(&P::State) -> Subscription, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = executor::Default; + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + (self.subscription)(state) + } + + fn new(&self) -> (Self::State, Command) { + self.program.new() + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + } + + WithSubscription { + program, + subscription: f, + } +} + +fn with_theme( + program: P, + f: impl Fn(&P::State) -> P::Theme, +) -> impl Definition { + struct WithTheme { + program: P, + theme: F, + } + + impl Definition for WithTheme + where + F: Fn(&P::State) -> P::Theme, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn theme(&self, state: &Self::State) -> Self::Theme { + (self.theme)(state) + } + + fn new(&self) -> (Self::State, Command) { + self.program.new() + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + } + + WithTheme { program, theme: f } +} + +/// The title logic of some [`Program`]. +/// +/// This trait is implemented both for `&static str` and +/// any closure `Fn(&State) -> String`. +/// +/// You can use any of these in [`Program::title`]. +pub trait Title { + /// Produces the title of the [`Program`]. + fn title(&self, state: &State) -> String; +} + +impl Title for &'static str { + fn title(&self, _state: &State) -> String { + self.to_string() + } +} + +impl Title for T +where + T: Fn(&State) -> String, +{ + fn title(&self, state: &State) -> String { + self(state) + } +} + +/// The view logic of some [`Program`]. +/// +/// This trait allows [`sandbox`] and [`application`] to +/// take any closure that returns any `Into>`. +/// +/// [`application`]: self::application() +pub trait View<'a, State, Message> { + /// The widget returned by the view logic. + type Widget: Into>; + + /// Produces the widget of the [`Program`]. + fn view(&self, state: &'a State) -> Self::Widget; +} + +impl<'a, T, State, Message, Widget> View<'a, State, Message> for T +where + T: Fn(&'a State) -> Widget, + State: 'static, + Widget: Into>, +{ + type Widget = Widget; + + fn view(&self, state: &'a State) -> Self::Widget { + self(state) + } +} diff --git a/src/settings.rs b/src/settings.rs index d9476b61..92204847 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -6,7 +6,7 @@ use std::borrow::Cow; /// The settings of an application. #[derive(Debug, Clone)] -pub struct Settings { +pub struct Settings { /// The identifier of the application. /// /// If provided, this identifier may be used to identify the application or -- cgit From 5a986897d22f6d79a7a1fbaa4f3d1aaa1f9ca3bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 05:47:01 +0100 Subject: Rename `Program::new` to `build` --- src/program.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/program.rs b/src/program.rs index d78fa505..3a16ea29 100644 --- a/src/program.rs +++ b/src/program.rs @@ -107,7 +107,7 @@ where type Theme = crate::Theme; type Executor = iced_futures::backend::null::Executor; - fn new(&self) -> (Self::State, Command) { + fn build(&self) -> (Self::State, Command) { (State::default(), Command::none()) } @@ -176,7 +176,7 @@ where type Theme = crate::Theme; type Executor = executor::Default; - fn new(&self) -> (Self::State, Command) { + fn build(&self) -> (Self::State, Command) { (self.new)() } @@ -240,7 +240,7 @@ impl Program

{ type Executor = P::Executor; fn new(program: Self::Flags) -> (Self, Command) { - let (state, command) = P::new(&program); + let (state, command) = P::build(&program); (Self { program, state }, command) } @@ -414,7 +414,7 @@ pub trait Definition: Sized { /// The executor of the program. type Executor: Executor; - fn new(&self) -> (Self::State, Command); + fn build(&self) -> (Self::State, Command); fn update( &self, @@ -462,8 +462,8 @@ fn with_title( type Theme = P::Theme; type Executor = P::Executor; - fn new(&self) -> (Self::State, Command) { - self.program.new() + fn build(&self) -> (Self::State, Command) { + self.program.build() } fn title(&self, state: &Self::State) -> String { @@ -525,8 +525,8 @@ fn with_subscription( (self.subscription)(state) } - fn new(&self) -> (Self::State, Command) { - self.program.new() + fn build(&self) -> (Self::State, Command) { + self.program.build() } fn update( @@ -581,8 +581,8 @@ fn with_theme( (self.theme)(state) } - fn new(&self) -> (Self::State, Command) { - self.program.new() + fn build(&self) -> (Self::State, Command) { + self.program.build() } fn title(&self, state: &Self::State) -> String { -- cgit From 93ae790da14544667176ecdbdd6a4eaaa98a248a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 15:53:03 +0100 Subject: Implement `Program::load` to specify startup `Command` --- src/lib.rs | 2 +- src/program.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 80 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index f42a845e..19815f0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -362,7 +362,7 @@ pub type Result = std::result::Result<(), Error>; /// } /// ``` pub fn run( - title: &'static str, + title: impl program::Title + 'static, update: impl Fn(&mut State, Message) + 'static, view: impl for<'a> program::View<'a, State, Message> + 'static, ) -> Result diff --git a/src/program.rs b/src/program.rs index 3a16ea29..746f8f29 100644 --- a/src/program.rs +++ b/src/program.rs @@ -143,31 +143,30 @@ where /// Creates a [`Program`] that can leverage the [`Command`] API for /// concurrent operations. pub fn application( - new: impl Fn() -> (State, Command), + title: impl Title, update: impl Fn(&mut State, Message) -> Command, view: impl for<'a> self::View<'a, State, Message>, ) -> Program< impl Definition, > where - State: 'static, + State: Default + 'static, Message: Send + std::fmt::Debug, { use std::marker::PhantomData; - struct Application { - new: New, + struct Application { update: Update, view: View, _state: PhantomData, _message: PhantomData, } - impl Definition - for Application + impl Definition + for Application where + State: Default, Message: Send + std::fmt::Debug, - New: Fn() -> (State, Command), Update: Fn(&mut State, Message) -> Command, View: for<'a> self::View<'a, State, Message>, { @@ -177,7 +176,7 @@ where type Executor = executor::Default; fn build(&self) -> (Self::State, Command) { - (self.new)() + (Self::State::default(), Command::none()) } fn update( @@ -198,7 +197,6 @@ where Program { raw: Application { - new, update, view, _state: PhantomData, @@ -206,6 +204,7 @@ where }, settings: Settings::default(), } + .title(title) } /// A fully functioning and configured iced application. @@ -367,6 +366,19 @@ impl Program

{ } } + /// Runs the [`Command`] produced by the closure at startup. + pub fn load( + self, + f: impl Fn() -> Command, + ) -> Program< + impl Definition, + > { + Program { + raw: with_load(self.raw, f), + settings: self.settings, + } + } + /// Sets the subscription logic of the [`Program`]. pub fn subscription( self, @@ -500,6 +512,64 @@ fn with_title( WithTitle { program, title } } +fn with_load( + program: P, + f: impl Fn() -> Command, +) -> impl Definition { + struct WithLoad { + program: P, + load: F, + } + + impl Definition for WithLoad + where + F: Fn() -> Command, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = executor::Default; + + fn build(&self) -> (Self::State, Command) { + let (state, command) = self.program.build(); + + (state, Command::batch([command, (self.load)()])) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + } + + WithLoad { program, load: f } +} + fn with_subscription( program: P, f: impl Fn(&P::State) -> Subscription, -- cgit From bb71e8481ed59f991b9bd9dc55ea7e011ba0aac6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 16:12:07 +0100 Subject: Make `sandbox` helper take a `title` as well --- src/lib.rs | 2 +- src/program.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 19815f0f..cda5341c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -370,7 +370,7 @@ where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, { - sandbox(update, view).title(title).run() + sandbox(title, update, view).run() } #[doc(inline)] diff --git a/src/program.rs b/src/program.rs index 746f8f29..ab194a0d 100644 --- a/src/program.rs +++ b/src/program.rs @@ -15,8 +15,7 @@ //! use iced::Theme; //! //! pub fn main() -> iced::Result { -//! iced::sandbox(update, view) -//! .title("A counter") +//! iced::sandbox("A counter", update, view) //! .theme(|_| Theme::Dark) //! .centered() //! .run() @@ -54,7 +53,7 @@ use std::borrow::Cow; /// use iced::widget::{button, column, text, Column}; /// /// pub fn main() -> iced::Result { -/// iced::sandbox(update, view).title("A counter").run() +/// iced::sandbox("A counter", update, view).run() /// } /// /// #[derive(Debug, Clone)] @@ -76,6 +75,7 @@ use std::borrow::Cow; /// } /// ``` pub fn sandbox( + title: impl Title, update: impl Fn(&mut State, Message), view: impl for<'a> self::View<'a, State, Message>, ) -> Program< @@ -138,6 +138,7 @@ where }, settings: Settings::default(), } + .title(title) } /// Creates a [`Program`] that can leverage the [`Command`] API for -- cgit From 348e00e11cd976a16493f4ce693db614886c1ecd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 16:20:05 +0100 Subject: Make `Program::title` private --- src/program.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/program.rs b/src/program.rs index ab194a0d..c6c702f1 100644 --- a/src/program.rs +++ b/src/program.rs @@ -355,7 +355,7 @@ impl Program

{ } /// Sets the [`Title`] of the [`Program`]. - pub fn title( + pub(crate) fn title( self, title: impl Title, ) -> Program< -- cgit From cfc0383bbfff083786840e3f1fd499e5991fa629 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 16:58:48 +0100 Subject: Replace `Program::fonts` with simpler `font` method --- src/program.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/program.rs b/src/program.rs index c6c702f1..37ff64b1 100644 --- a/src/program.rs +++ b/src/program.rs @@ -312,18 +312,10 @@ impl Program

{ } } - /// Sets the fonts that will be loaded at the start of the [`Program`]. - pub fn fonts( - self, - fonts: impl IntoIterator>, - ) -> Self { - Self { - settings: Settings { - fonts: fonts.into_iter().collect(), - ..self.settings - }, - ..self - } + /// Adds a font to the list of fonts that will be loaded at the start of the [`Program`]. + pub fn font(mut self, font: impl Into>) -> Self { + self.settings.fonts.push(font.into()); + self } /// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Program`]. -- cgit From 28a27f08edccd53e06ad693e63b0a62dae921da5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 19:14:13 +0100 Subject: Remove `sandbox` by making `application` more generic :tada: --- src/lib.rs | 6 +-- src/program.rs | 123 ++++++++++++++++++--------------------------------------- 2 files changed, 42 insertions(+), 87 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index cda5341c..ae6bb344 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -363,15 +363,15 @@ pub type Result = std::result::Result<(), Error>; /// ``` pub fn run( title: impl program::Title + 'static, - update: impl Fn(&mut State, Message) + 'static, + update: impl program::Update + 'static, view: impl for<'a> program::View<'a, State, Message> + 'static, ) -> Result where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, { - sandbox(title, update, view).run() + application(title, update, view).run() } #[doc(inline)] -pub use program::{application, sandbox}; +pub use program::application; diff --git a/src/program.rs b/src/program.rs index 37ff64b1..aacbf876 100644 --- a/src/program.rs +++ b/src/program.rs @@ -15,7 +15,7 @@ //! use iced::Theme; //! //! pub fn main() -> iced::Result { -//! iced::sandbox("A counter", update, view) +//! iced::application("A counter", update, view) //! .theme(|_| Theme::Dark) //! .centered() //! .run() @@ -46,14 +46,14 @@ use crate::{Command, Element, Font, Result, Settings, Subscription}; use std::borrow::Cow; -/// Creates the most basic kind of [`Program`] from some update and view logic. +/// Creates an iced [`Program`] given its title, update, and view logic. /// /// # Example /// ```no_run /// use iced::widget::{button, column, text, Column}; /// /// pub fn main() -> iced::Result { -/// iced::sandbox("A counter", update, view).run() +/// iced::application("A counter", update, view).run() /// } /// /// #[derive(Debug, Clone)] @@ -74,78 +74,9 @@ use std::borrow::Cow; /// ] /// } /// ``` -pub fn sandbox( - title: impl Title, - update: impl Fn(&mut State, Message), - view: impl for<'a> self::View<'a, State, Message>, -) -> Program< - impl Definition, -> -where - State: Default + 'static, - Message: Send + std::fmt::Debug, -{ - use std::marker::PhantomData; - - struct Sandbox { - update: Update, - view: View, - _state: PhantomData, - _message: PhantomData, - } - - impl Definition - for Sandbox - where - State: Default + 'static, - Message: Send + std::fmt::Debug, - Update: Fn(&mut State, Message), - View: for<'a> self::View<'a, State, Message>, - { - type State = State; - type Message = Message; - type Theme = crate::Theme; - type Executor = iced_futures::backend::null::Executor; - - fn build(&self) -> (Self::State, Command) { - (State::default(), Command::none()) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - (self.update)(state, message); - - Command::none() - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.view.view(state).into() - } - } - - Program { - raw: Sandbox { - update, - view, - _state: PhantomData, - _message: PhantomData, - }, - settings: Settings::default(), - } - .title(title) -} - -/// Creates a [`Program`] that can leverage the [`Command`] API for -/// concurrent operations. pub fn application( title: impl Title, - update: impl Fn(&mut State, Message) -> Command, + update: impl Update, view: impl for<'a> self::View<'a, State, Message>, ) -> Program< impl Definition, @@ -168,7 +99,7 @@ where where State: Default, Message: Send + std::fmt::Debug, - Update: Fn(&mut State, Message) -> Command, + Update: self::Update, View: for<'a> self::View<'a, State, Message>, { type State = State; @@ -185,7 +116,7 @@ where state: &mut Self::State, message: Self::Message, ) -> Command { - (self.update)(state, message) + self.update.update(state, message).into() } fn view<'a>( @@ -704,18 +635,44 @@ where } } +/// The update logic of some [`Program`]. +/// +/// This trait allows [`application`] to take any closure that +/// returns any `Into>`. +/// +/// [`application`]: self::application() +pub trait Update { + /// Processes the message and updates the state of the [`Program`]. + fn update( + &self, + state: &mut State, + message: Message, + ) -> impl Into>; +} + +impl Update for T +where + T: Fn(&mut State, Message) -> C, + C: Into>, +{ + fn update( + &self, + state: &mut State, + message: Message, + ) -> impl Into> { + self(state, message) + } +} + /// The view logic of some [`Program`]. /// -/// This trait allows [`sandbox`] and [`application`] to -/// take any closure that returns any `Into>`. +/// This trait allows [`application`] to take any closure that +/// returns any `Into>`. /// /// [`application`]: self::application() pub trait View<'a, State, Message> { - /// The widget returned by the view logic. - type Widget: Into>; - /// Produces the widget of the [`Program`]. - fn view(&self, state: &'a State) -> Self::Widget; + fn view(&self, state: &'a State) -> impl Into>; } impl<'a, T, State, Message, Widget> View<'a, State, Message> for T @@ -724,9 +681,7 @@ where State: 'static, Widget: Into>, { - type Widget = Widget; - - fn view(&self, state: &'a State) -> Self::Widget { + fn view(&self, state: &'a State) -> impl Into> { self(state) } } -- cgit From 4f6a9ba88951a53ba3ce66f2b00ba75c2553baa1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 19:22:18 +0100 Subject: Fix broken intra-doc link --- src/program.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/program.rs b/src/program.rs index aacbf876..c2841763 100644 --- a/src/program.rs +++ b/src/program.rs @@ -143,7 +143,7 @@ where /// /// It can be [`run`]! /// -/// Create one with either the [`sandbox`] or [`application`] helpers. +/// Create one with the [`application`] helper. /// /// [`run`]: Program::run /// [`application`]: self::application() -- cgit From 846d76cd3f3f2faae5efbb3fda2a2bcb3b064481 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 13:46:52 +0100 Subject: Remove `Sandbox` trait :tada: --- src/lib.rs | 2 - src/program.rs | 157 +++++++++++++++++++++++++++++++++++++++++---- src/sandbox.rs | 199 --------------------------------------------------------- 3 files changed, 146 insertions(+), 212 deletions(-) delete mode 100644 src/sandbox.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index ae6bb344..060cc6c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,7 +172,6 @@ pub use iced_futures::futures; pub use iced_highlighter as highlighter; mod error; -mod sandbox; pub mod application; pub mod program; @@ -311,7 +310,6 @@ pub use executor::Executor; pub use font::Font; pub use program::Program; pub use renderer::Renderer; -pub use sandbox::Sandbox; pub use settings::Settings; pub use subscription::Subscription; diff --git a/src/program.rs b/src/program.rs index c2841763..8aa1a4e7 100644 --- a/src/program.rs +++ b/src/program.rs @@ -194,12 +194,16 @@ impl Program

{ self.program.view(&self.state) } + fn subscription(&self) -> Subscription { + self.program.subscription(&self.state) + } + fn theme(&self) -> Self::Theme { self.program.theme(&self.state) } - fn subscription(&self) -> Subscription { - self.program.subscription(&self.state) + fn style(&self, theme: &Self::Theme) -> application::Appearance { + self.program.style(&self.state, theme) } } @@ -221,11 +225,11 @@ impl Program

{ Self { settings, ..self } } - /// Toggles the [`Settings::antialiasing`] to `true` for the [`Program`]. - pub fn antialiased(self) -> Self { + /// Sets the [`Settings::antialiasing`] of the [`Program`]. + pub fn antialiasing(self, antialiasing: bool) -> Self { Self { settings: Settings { - antialiasing: true, + antialiasing, ..self.settings }, ..self @@ -263,12 +267,26 @@ impl Program

{ } } - /// Sets the [`window::Settings::exit_on_close_request`] to `false` in the [`Program`]. - pub fn ignore_close_request(self) -> Self { + /// Sets the [`window::Settings::exit_on_close_request`] of the [`Program`]. + pub fn exit_on_close_request(self, exit_on_close_request: bool) -> Self { Self { settings: Settings { window: window::Settings { - exit_on_close_request: false, + exit_on_close_request, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::transparent`] of the [`Program`]. + pub fn transparent(self, transparent: bool) -> Self { + Self { + settings: Settings { + window: window::Settings { + transparent, ..self.settings.window }, ..self.settings @@ -328,6 +346,19 @@ impl Program

{ settings: self.settings, } } + + /// Sets the style logic of the [`Program`]. + pub fn style( + self, + f: impl Fn(&P::State, &P::Theme) -> application::Appearance, + ) -> Program< + impl Definition, + > { + Program { + raw: with_style(self.raw, f), + settings: self.settings, + } + } } /// The internal definition of a [`Program`]. @@ -377,6 +408,14 @@ pub trait Definition: Sized { fn theme(&self, _state: &Self::State) -> Self::Theme { Self::Theme::default() } + + fn style( + &self, + _state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + application::DefaultStyle::default_style(theme) + } } fn with_title( @@ -431,6 +470,14 @@ fn with_title( ) -> Subscription { self.program.subscription(state) } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) + } } WithTitle { program, title } @@ -479,15 +526,23 @@ fn with_load( self.program.title(state) } + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + fn theme(&self, state: &Self::State) -> Self::Theme { self.program.theme(state) } - fn subscription( + fn style( &self, state: &Self::State, - ) -> Subscription { - self.program.subscription(state) + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) } } @@ -545,6 +600,14 @@ fn with_subscription( fn theme(&self, state: &Self::State) -> Self::Theme { self.program.theme(state) } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) + } } WithSubscription { @@ -604,11 +667,83 @@ fn with_theme( ) -> Subscription { self.program.subscription(state) } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) + } } WithTheme { program, theme: f } } +fn with_style( + program: P, + f: impl Fn(&P::State, &P::Theme) -> application::Appearance, +) -> impl Definition { + struct WithStyle { + program: P, + style: F, + } + + impl Definition for WithStyle + where + F: Fn(&P::State, &P::Theme) -> application::Appearance, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + (self.style)(state, theme) + } + + fn build(&self) -> (Self::State, Command) { + self.program.build() + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + } + + WithStyle { program, style: f } +} + /// The title logic of some [`Program`]. /// /// This trait is implemented both for `&static str` and diff --git a/src/sandbox.rs b/src/sandbox.rs deleted file mode 100644 index 568b673e..00000000 --- a/src/sandbox.rs +++ /dev/null @@ -1,199 +0,0 @@ -use crate::application::{self, Application}; -use crate::{Command, Element, Error, Settings, Subscription, Theme}; - -/// A sandboxed [`Application`]. -/// -/// If you are a just getting started with the library, this trait offers a -/// simpler interface than [`Application`]. -/// -/// Unlike an [`Application`], a [`Sandbox`] cannot run any asynchronous -/// actions or be initialized with some external flags. However, both traits -/// are very similar and upgrading from a [`Sandbox`] is very straightforward. -/// -/// Therefore, it is recommended to always start by implementing this trait and -/// upgrade only once necessary. -/// -/// # Examples -/// [The repository has a bunch of examples] that use the [`Sandbox`] trait: -/// -/// - [`bezier_tool`], a Paint-like tool for drawing Bézier curves using the -/// [`Canvas widget`]. -/// - [`counter`], the classic counter example explained in [the overview]. -/// - [`custom_widget`], a demonstration of how to build a custom widget that -/// draws a circle. -/// - [`geometry`], a custom widget showcasing how to draw geometry with the -/// `Mesh2D` primitive in [`iced_wgpu`]. -/// - [`pane_grid`], a grid of panes that can be split, resized, and -/// reorganized. -/// - [`progress_bar`], a simple progress bar that can be filled by using a -/// slider. -/// - [`styling`], an example showcasing custom styling with a light and dark -/// theme. -/// - [`svg`], an application that renders the [Ghostscript Tiger] by leveraging -/// the [`Svg` widget]. -/// - [`tour`], a simple UI tour that can run both on native platforms and the -/// web! -/// -/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.12/examples -/// [`bezier_tool`]: https://github.com/iced-rs/iced/tree/0.12/examples/bezier_tool -/// [`counter`]: https://github.com/iced-rs/iced/tree/0.12/examples/counter -/// [`custom_widget`]: https://github.com/iced-rs/iced/tree/0.12/examples/custom_widget -/// [`geometry`]: https://github.com/iced-rs/iced/tree/0.12/examples/geometry -/// [`pane_grid`]: https://github.com/iced-rs/iced/tree/0.12/examples/pane_grid -/// [`progress_bar`]: https://github.com/iced-rs/iced/tree/0.12/examples/progress_bar -/// [`styling`]: https://github.com/iced-rs/iced/tree/0.12/examples/styling -/// [`svg`]: https://github.com/iced-rs/iced/tree/0.12/examples/svg -/// [`tour`]: https://github.com/iced-rs/iced/tree/0.12/examples/tour -/// [`Canvas widget`]: crate::widget::Canvas -/// [the overview]: index.html#overview -/// [`iced_wgpu`]: https://github.com/iced-rs/iced/tree/0.12/wgpu -/// [`Svg` widget]: crate::widget::Svg -/// [Ghostscript Tiger]: https://commons.wikimedia.org/wiki/File:Ghostscript_Tiger.svg -/// -/// ## A simple "Hello, world!" -/// -/// If you just want to get started, here is a simple [`Sandbox`] that -/// says "Hello, world!": -/// -/// ```no_run -/// use iced::{Element, Sandbox, Settings}; -/// -/// pub fn main() -> iced::Result { -/// Hello::run(Settings::default()) -/// } -/// -/// struct Hello; -/// -/// impl Sandbox for Hello { -/// type Message = (); -/// -/// fn new() -> Hello { -/// Hello -/// } -/// -/// fn title(&self) -> String { -/// String::from("A cool application") -/// } -/// -/// fn update(&mut self, _message: Self::Message) { -/// // This application has no interactions -/// } -/// -/// fn view(&self) -> Element { -/// "Hello, world!".into() -/// } -/// } -/// ``` -pub trait Sandbox { - /// The type of __messages__ your [`Sandbox`] will produce. - type Message: std::fmt::Debug + Send; - - /// Initializes the [`Sandbox`]. - /// - /// Here is where you should return the initial state of your app. - fn new() -> Self; - - /// Returns the current title of the [`Sandbox`]. - /// - /// This title can be dynamic! The runtime will automatically update the - /// title of your application when necessary. - fn title(&self) -> String; - - /// Handles a __message__ and updates the state of the [`Sandbox`]. - /// - /// This is where you define your __update logic__. All the __messages__, - /// produced by user interactions, will be handled by this method. - fn update(&mut self, message: Self::Message); - - /// Returns the widgets to display in the [`Sandbox`]. - /// - /// These widgets can produce __messages__ based on user interaction. - fn view(&self) -> Element<'_, Self::Message>; - - /// Returns the current [`Theme`] of the [`Sandbox`]. - /// - /// If you want to use your own custom theme type, you will have to use an - /// [`Application`]. - /// - /// By default, it returns [`Theme::default`]. - fn theme(&self) -> Theme { - Theme::default() - } - - /// Returns the current [`application::Appearance`]. - fn style(&self, theme: &Theme) -> application::Appearance { - use application::DefaultStyle; - - theme.default_style() - } - - /// Returns the scale factor of the [`Sandbox`]. - /// - /// It can be used to dynamically control the size of the UI at runtime - /// (i.e. zooming). - /// - /// For instance, a scale factor of `2.0` will make widgets twice as big, - /// while a scale factor of `0.5` will shrink them to half their size. - /// - /// By default, it returns `1.0`. - fn scale_factor(&self) -> f64 { - 1.0 - } - - /// Runs the [`Sandbox`]. - /// - /// On native platforms, this method will take control of the current thread - /// and __will NOT return__. - /// - /// It should probably be that last thing you call in your `main` function. - fn run(settings: Settings<()>) -> Result<(), Error> - where - Self: 'static + Sized, - { - ::run(settings) - } -} - -impl Application for T -where - T: Sandbox, -{ - type Executor = iced_futures::backend::null::Executor; - type Flags = (); - type Message = T::Message; - type Theme = Theme; - - fn new(_flags: ()) -> (Self, Command) { - (T::new(), Command::none()) - } - - fn title(&self) -> String { - T::title(self) - } - - fn update(&mut self, message: T::Message) -> Command { - T::update(self, message); - - Command::none() - } - - fn view(&self) -> Element<'_, T::Message> { - T::view(self) - } - - fn theme(&self) -> Self::Theme { - T::theme(self) - } - - fn style(&self, theme: &Theme) -> application::Appearance { - T::style(self, theme) - } - - fn subscription(&self) -> Subscription { - Subscription::none() - } - - fn scale_factor(&self) -> f64 { - T::scale_factor(self) - } -} -- cgit From 179e8863b3c7a1f056eef5e06fbf4f3796a641ba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 13:55:15 +0100 Subject: Fix broken intra-doc links to `Sandbox` --- src/application.rs | 4 +--- src/lib.rs | 16 ++++++++++++---- src/multi_window.rs | 4 +--- src/program.rs | 4 +--- 4 files changed, 15 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/application.rs b/src/application.rs index be0fa0de..48fc672a 100644 --- a/src/application.rs +++ b/src/application.rs @@ -15,9 +15,7 @@ pub use application::{default, Appearance, DefaultStyle}; /// document. /// /// An [`Application`] can execute asynchronous actions by returning a -/// [`Command`] in some of its methods. If you do not intend to perform any -/// background work in your program, the [`Sandbox`] trait offers a simplified -/// interface. +/// [`Command`] in some of its methods. /// /// When using an [`Application`] with the `debug` feature enabled, a debug view /// can be toggled by pressing `F12`. diff --git a/src/lib.rs b/src/lib.rs index 060cc6c5..1f2b8c93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,8 +134,15 @@ //! } //! ``` //! -//! And that's everything! We just wrote a whole user interface. Iced is now -//! able to: +//! And that's everything! We just wrote a whole user interface. Let's run it: +//! +//! ```no_run +//! fn main() -> iced::Result { +//! iced::run("A cool counter", Counter::update, Counter::view) +//! } +//! ``` +//! +//! Iced will automatically: //! //! 1. Take the result of our __view logic__ and layout its widgets. //! 1. Process events from our system and produce __messages__ for our @@ -143,11 +150,12 @@ //! 1. Draw the resulting user interface. //! //! # Usage -//! The [`Application`] and [`Sandbox`] traits should get you started quickly, -//! streamlining all the process described above! +//! You can either use the [`application`] builder or implement the [`Application`] +//! trait directly. //! //! [Elm]: https://elm-lang.org/ //! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ +//! [`application`]: application() #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] diff --git a/src/multi_window.rs b/src/multi_window.rs index c4063563..fca0be46 100644 --- a/src/multi_window.rs +++ b/src/multi_window.rs @@ -14,9 +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. If you do not intend to perform any -/// background work in your program, the [`Sandbox`] trait offers a simplified -/// interface. +/// [`Command`] in some of its methods. /// /// When using an [`Application`] with the `debug` feature enabled, a debug view /// can be toggled by pressing `F12`. diff --git a/src/program.rs b/src/program.rs index 8aa1a4e7..b379dccf 100644 --- a/src/program.rs +++ b/src/program.rs @@ -5,7 +5,7 @@ //! or a specific type. //! //! This API is meant to be a more convenient—although less -//! powerful—alternative to the [`Sandbox`] and [`Application`] traits. +//! powerful—alternative to the [`Application`] traits. //! //! [`Sandbox`]: crate::Sandbox //! @@ -748,8 +748,6 @@ fn with_style( /// /// This trait is implemented both for `&static str` and /// any closure `Fn(&State) -> String`. -/// -/// You can use any of these in [`Program::title`]. pub trait Title { /// Produces the title of the [`Program`]. fn title(&self, state: &State) -> String; -- cgit From 7e1ef7d150aa3d4d05942eea2706348f20d61d64 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 13:57:53 +0100 Subject: Fix new doc test in root module --- src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 1f2b8c93..c2177484 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,6 +137,13 @@ //! And that's everything! We just wrote a whole user interface. Let's run it: //! //! ```no_run +//! # #[derive(Default)] +//! # struct Counter; +//! # impl Counter { +//! # fn update(&mut self, _message: ()) {} +//! # fn view(&self) -> iced::Element<()> { unimplemented!() } +//! # } +//! # //! fn main() -> iced::Result { //! iced::run("A cool counter", Counter::update, Counter::view) //! } -- cgit From 54f44754eb216d4b2c08cd2a7c3582f1dc295205 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:16:38 +0100 Subject: Move `Program` to `application` module --- src/application.rs | 5 +- src/application/program.rs | 810 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 15 +- src/program.rs | 820 --------------------------------------------- 4 files changed, 820 insertions(+), 830 deletions(-) create mode 100644 src/application/program.rs delete mode 100644 src/program.rs (limited to 'src') diff --git a/src/application.rs b/src/application.rs index 48fc672a..ba60db67 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,7 +1,10 @@ //! Build interactive cross-platform applications. -use crate::{Command, Element, Executor, Settings, Subscription}; +mod program; + +pub use program::{program, Definition, Program, Title, Update, View}; use crate::shell::application; +use crate::{Command, Element, Executor, Settings, Subscription}; pub use application::{default, Appearance, DefaultStyle}; diff --git a/src/application/program.rs b/src/application/program.rs new file mode 100644 index 00000000..24e00efe --- /dev/null +++ b/src/application/program.rs @@ -0,0 +1,810 @@ +//! +//! # Example +//! ```no_run +//! use iced::widget::{button, column, text, Column}; +//! use iced::Theme; +//! +//! pub fn main() -> iced::Result { +//! iced::program("A counter", update, view) +//! .theme(|_| Theme::Dark) +//! .centered() +//! .run() +//! } +//! +//! #[derive(Debug, Clone)] +//! enum Message { +//! Increment, +//! } +//! +//! fn update(value: &mut u64, message: Message) { +//! match message { +//! Message::Increment => *value += 1, +//! } +//! } +//! +//! fn view(value: &u64) -> Column { +//! column![ +//! text(value), +//! button("+").on_press(Message::Increment), +//! ] +//! } +//! ``` +use crate::application::{self, Application}; +use crate::executor::{self, Executor}; +use crate::window; +use crate::{Command, Element, Font, Result, Settings, Subscription}; + +use std::borrow::Cow; + +/// Creates an iced [`Program`] given its title, update, and view logic. +/// +/// # Example +/// ```no_run +/// use iced::widget::{button, column, text, Column}; +/// +/// pub fn main() -> iced::Result { +/// iced::program("A counter", update, view).run() +/// } +/// +/// #[derive(Debug, Clone)] +/// enum Message { +/// Increment, +/// } +/// +/// fn update(value: &mut u64, message: Message) { +/// match message { +/// Message::Increment => *value += 1, +/// } +/// } +/// +/// fn view(value: &u64) -> Column { +/// column![ +/// text(value), +/// button("+").on_press(Message::Increment), +/// ] +/// } +/// ``` +pub fn program( + title: impl Title, + update: impl Update, + view: impl for<'a> self::View<'a, State, Message>, +) -> Program< + impl Definition, +> +where + State: Default + 'static, + Message: Send + std::fmt::Debug, +{ + use std::marker::PhantomData; + + struct Application { + update: Update, + view: View, + _state: PhantomData, + _message: PhantomData, + } + + impl Definition + for Application + where + State: Default, + Message: Send + std::fmt::Debug, + Update: self::Update, + View: for<'a> self::View<'a, State, Message>, + { + type State = State; + type Message = Message; + type Theme = crate::Theme; + type Executor = executor::Default; + + fn build(&self) -> (Self::State, Command) { + (Self::State::default(), Command::none()) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.update.update(state, message).into() + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.view.view(state).into() + } + } + + Program { + raw: Application { + update, + view, + _state: PhantomData, + _message: PhantomData, + }, + settings: Settings::default(), + } + .title(title) +} + +/// The underlying definition and configuration of an iced [`Application`]. +/// +/// You can use this API to create and run iced applications +/// step by step—without coupling your logic to a trait +/// or a specific type. +/// +/// This API is meant to be a more convenient—although less +/// powerful—alternative to the [`Application`] trait. +/// +/// You can create a [`Program`] with the [`program`] helper. +/// +/// [`run`]: Program::run +#[derive(Debug)] +pub struct Program { + raw: P, + settings: Settings, +} + +impl Program

{ + /// Runs the underlying [`Application`] of the [`Program`]. + pub fn run(self) -> Result + where + Self: 'static, + { + struct Instance { + program: P, + state: P::State, + } + + impl Application for Instance

{ + type Message = P::Message; + type Theme = P::Theme; + type Flags = P; + type Executor = P::Executor; + + fn new(program: Self::Flags) -> (Self, Command) { + let (state, command) = P::build(&program); + + (Self { program, state }, command) + } + + fn title(&self) -> String { + self.program.title(&self.state) + } + + fn update( + &mut self, + message: Self::Message, + ) -> Command { + self.program.update(&mut self.state, message) + } + + fn view( + &self, + ) -> crate::Element<'_, Self::Message, Self::Theme, crate::Renderer> + { + self.program.view(&self.state) + } + + fn subscription(&self) -> Subscription { + self.program.subscription(&self.state) + } + + fn theme(&self) -> Self::Theme { + self.program.theme(&self.state) + } + + fn style(&self, theme: &Self::Theme) -> application::Appearance { + self.program.style(&self.state, theme) + } + } + + let Self { raw, settings } = self; + + Instance::run(Settings { + flags: raw, + id: settings.id, + window: settings.window, + fonts: settings.fonts, + default_font: settings.default_font, + default_text_size: settings.default_text_size, + antialiasing: settings.antialiasing, + }) + } + + /// Sets the [`Settings`] that will be used to run the [`Program`]. + pub fn settings(self, settings: Settings) -> Self { + Self { settings, ..self } + } + + /// Sets the [`Settings::antialiasing`] of the [`Program`]. + pub fn antialiasing(self, antialiasing: bool) -> Self { + Self { + settings: Settings { + antialiasing, + ..self.settings + }, + ..self + } + } + + /// Sets the default [`Font`] of the [`Program`]. + pub fn default_font(self, default_font: Font) -> Self { + Self { + settings: Settings { + default_font, + ..self.settings + }, + ..self + } + } + + /// Adds a font to the list of fonts that will be loaded at the start of the [`Program`]. + pub fn font(mut self, font: impl Into>) -> Self { + self.settings.fonts.push(font.into()); + self + } + + /// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Program`]. + pub fn centered(self) -> Self { + Self { + settings: Settings { + window: window::Settings { + position: window::Position::Centered, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::exit_on_close_request`] of the [`Program`]. + pub fn exit_on_close_request(self, exit_on_close_request: bool) -> Self { + Self { + settings: Settings { + window: window::Settings { + exit_on_close_request, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::transparent`] of the [`Program`]. + pub fn transparent(self, transparent: bool) -> Self { + Self { + settings: Settings { + window: window::Settings { + transparent, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`Title`] of the [`Program`]. + pub(crate) fn title( + self, + title: impl Title, + ) -> Program< + impl Definition, + > { + Program { + raw: with_title(self.raw, title), + settings: self.settings, + } + } + + /// Runs the [`Command`] produced by the closure at startup. + pub fn load( + self, + f: impl Fn() -> Command, + ) -> Program< + impl Definition, + > { + Program { + raw: with_load(self.raw, f), + settings: self.settings, + } + } + + /// Sets the subscription logic of the [`Program`]. + pub fn subscription( + self, + f: impl Fn(&P::State) -> Subscription, + ) -> Program< + impl Definition, + > { + Program { + raw: with_subscription(self.raw, f), + settings: self.settings, + } + } + + /// Sets the theme logic of the [`Program`]. + pub fn theme( + self, + f: impl Fn(&P::State) -> P::Theme, + ) -> Program< + impl Definition, + > { + Program { + raw: with_theme(self.raw, f), + settings: self.settings, + } + } + + /// Sets the style logic of the [`Program`]. + pub fn style( + self, + f: impl Fn(&P::State, &P::Theme) -> application::Appearance, + ) -> Program< + impl Definition, + > { + Program { + raw: with_style(self.raw, f), + settings: self.settings, + } + } +} + +/// The internal definition of a [`Program`]. +/// +/// You should not need to implement this trait directly. Instead, use the +/// methods available in the [`Program`] struct. +#[allow(missing_docs)] +pub trait Definition: Sized { + /// The state of the program. + type State; + + /// The message of the program. + type Message: Send + std::fmt::Debug; + + /// The theme of the program. + type Theme: Default + application::DefaultStyle; + + /// The executor of the program. + type Executor: Executor; + + fn build(&self) -> (Self::State, Command); + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command; + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme>; + + fn title(&self, _state: &Self::State) -> String { + String::from("A cool iced application!") + } + + fn subscription( + &self, + _state: &Self::State, + ) -> Subscription { + Subscription::none() + } + + fn theme(&self, _state: &Self::State) -> Self::Theme { + Self::Theme::default() + } + + fn style( + &self, + _state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + application::DefaultStyle::default_style(theme) + } +} + +fn with_title( + program: P, + title: impl Title, +) -> impl Definition { + struct WithTitle { + program: P, + title: Title, + } + + impl Definition for WithTitle + where + P: Definition, + Title: self::Title, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn build(&self) -> (Self::State, Command) { + self.program.build() + } + + fn title(&self, state: &Self::State) -> String { + self.title.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) + } + } + + WithTitle { program, title } +} + +fn with_load( + program: P, + f: impl Fn() -> Command, +) -> impl Definition { + struct WithLoad { + program: P, + load: F, + } + + impl Definition for WithLoad + where + F: Fn() -> Command, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = executor::Default; + + fn build(&self) -> (Self::State, Command) { + let (state, command) = self.program.build(); + + (state, Command::batch([command, (self.load)()])) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) + } + } + + WithLoad { program, load: f } +} + +fn with_subscription( + program: P, + f: impl Fn(&P::State) -> Subscription, +) -> impl Definition { + struct WithSubscription { + program: P, + subscription: F, + } + + impl Definition for WithSubscription + where + F: Fn(&P::State) -> Subscription, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = executor::Default; + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + (self.subscription)(state) + } + + fn build(&self) -> (Self::State, Command) { + self.program.build() + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) + } + } + + WithSubscription { + program, + subscription: f, + } +} + +fn with_theme( + program: P, + f: impl Fn(&P::State) -> P::Theme, +) -> impl Definition { + struct WithTheme { + program: P, + theme: F, + } + + impl Definition for WithTheme + where + F: Fn(&P::State) -> P::Theme, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn theme(&self, state: &Self::State) -> Self::Theme { + (self.theme)(state) + } + + fn build(&self) -> (Self::State, Command) { + self.program.build() + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + self.program.style(state, theme) + } + } + + WithTheme { program, theme: f } +} + +fn with_style( + program: P, + f: impl Fn(&P::State, &P::Theme) -> application::Appearance, +) -> impl Definition { + struct WithStyle { + program: P, + style: F, + } + + impl Definition for WithStyle + where + F: Fn(&P::State, &P::Theme) -> application::Appearance, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> application::Appearance { + (self.style)(state, theme) + } + + fn build(&self) -> (Self::State, Command) { + self.program.build() + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + } + + WithStyle { program, style: f } +} + +/// The title logic of some [`Program`]. +/// +/// This trait is implemented both for `&static str` and +/// any closure `Fn(&State) -> String`. +/// +/// This trait allows the [`program`] builder to take any of them. +pub trait Title { + /// Produces the title of the [`Program`]. + fn title(&self, state: &State) -> String; +} + +impl Title for &'static str { + fn title(&self, _state: &State) -> String { + self.to_string() + } +} + +impl Title for T +where + T: Fn(&State) -> String, +{ + fn title(&self, state: &State) -> String { + self(state) + } +} + +/// The update logic of some [`Program`]. +/// +/// This trait allows the [`program`] builder to take any closure that +/// returns any `Into>`. +pub trait Update { + /// Processes the message and updates the state of the [`Program`]. + fn update( + &self, + state: &mut State, + message: Message, + ) -> impl Into>; +} + +impl Update for T +where + T: Fn(&mut State, Message) -> C, + C: Into>, +{ + fn update( + &self, + state: &mut State, + message: Message, + ) -> impl Into> { + self(state, message) + } +} + +/// The view logic of some [`Program`]. +/// +/// This trait allows the [`program`] builder to take any closure that +/// returns any `Into>`. +pub trait View<'a, State, Message> { + /// Produces the widget of the [`Program`]. + fn view(&self, state: &'a State) -> impl Into>; +} + +impl<'a, T, State, Message, Widget> View<'a, State, Message> for T +where + T: Fn(&'a State) -> Widget, + State: 'static, + Widget: Into>, +{ + fn view(&self, state: &'a State) -> impl Into> { + self(state) + } +} diff --git a/src/lib.rs b/src/lib.rs index c2177484..def020e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,12 +157,11 @@ //! 1. Draw the resulting user interface. //! //! # Usage -//! You can either use the [`application`] builder or implement the [`Application`] +//! You can either use the [`program`] builder or implement the [`Application`] //! trait directly. //! //! [Elm]: https://elm-lang.org/ //! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ -//! [`application`]: application() #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] @@ -189,7 +188,6 @@ pub use iced_highlighter as highlighter; mod error; pub mod application; -pub mod program; pub mod settings; pub mod time; pub mod window; @@ -323,7 +321,6 @@ pub use error::Error; pub use event::Event; pub use executor::Executor; pub use font::Font; -pub use program::Program; pub use renderer::Renderer; pub use settings::Settings; pub use subscription::Subscription; @@ -375,16 +372,16 @@ pub type Result = std::result::Result<(), Error>; /// } /// ``` pub fn run( - title: impl program::Title + 'static, - update: impl program::Update + 'static, - view: impl for<'a> program::View<'a, State, Message> + 'static, + title: impl application::Title + 'static, + update: impl application::Update + 'static, + view: impl for<'a> application::View<'a, State, Message> + 'static, ) -> Result where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, { - application(title, update, view).run() + program(title, update, view).run() } #[doc(inline)] -pub use program::application; +pub use application::program; diff --git a/src/program.rs b/src/program.rs deleted file mode 100644 index b379dccf..00000000 --- a/src/program.rs +++ /dev/null @@ -1,820 +0,0 @@ -//! Create iced applications out of simple functions. -//! -//! You can use this API to create and run iced applications -//! step by step—without coupling your logic to a trait -//! or a specific type. -//! -//! This API is meant to be a more convenient—although less -//! powerful—alternative to the [`Application`] traits. -//! -//! [`Sandbox`]: crate::Sandbox -//! -//! # Example -//! ```no_run -//! use iced::widget::{button, column, text, Column}; -//! use iced::Theme; -//! -//! pub fn main() -> iced::Result { -//! iced::application("A counter", update, view) -//! .theme(|_| Theme::Dark) -//! .centered() -//! .run() -//! } -//! -//! #[derive(Debug, Clone)] -//! enum Message { -//! Increment, -//! } -//! -//! fn update(value: &mut u64, message: Message) { -//! match message { -//! Message::Increment => *value += 1, -//! } -//! } -//! -//! fn view(value: &u64) -> Column { -//! column![ -//! text(value), -//! button("+").on_press(Message::Increment), -//! ] -//! } -//! ``` -use crate::application::{self, Application}; -use crate::executor::{self, Executor}; -use crate::window; -use crate::{Command, Element, Font, Result, Settings, Subscription}; - -use std::borrow::Cow; - -/// Creates an iced [`Program`] given its title, update, and view logic. -/// -/// # Example -/// ```no_run -/// use iced::widget::{button, column, text, Column}; -/// -/// pub fn main() -> iced::Result { -/// iced::application("A counter", update, view).run() -/// } -/// -/// #[derive(Debug, Clone)] -/// enum Message { -/// Increment, -/// } -/// -/// fn update(value: &mut u64, message: Message) { -/// match message { -/// Message::Increment => *value += 1, -/// } -/// } -/// -/// fn view(value: &u64) -> Column { -/// column![ -/// text(value), -/// button("+").on_press(Message::Increment), -/// ] -/// } -/// ``` -pub fn application( - title: impl Title, - update: impl Update, - view: impl for<'a> self::View<'a, State, Message>, -) -> Program< - impl Definition, -> -where - State: Default + 'static, - Message: Send + std::fmt::Debug, -{ - use std::marker::PhantomData; - - struct Application { - update: Update, - view: View, - _state: PhantomData, - _message: PhantomData, - } - - impl Definition - for Application - where - State: Default, - Message: Send + std::fmt::Debug, - Update: self::Update, - View: for<'a> self::View<'a, State, Message>, - { - type State = State; - type Message = Message; - type Theme = crate::Theme; - type Executor = executor::Default; - - fn build(&self) -> (Self::State, Command) { - (Self::State::default(), Command::none()) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.update.update(state, message).into() - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.view.view(state).into() - } - } - - Program { - raw: Application { - update, - view, - _state: PhantomData, - _message: PhantomData, - }, - settings: Settings::default(), - } - .title(title) -} - -/// A fully functioning and configured iced application. -/// -/// It can be [`run`]! -/// -/// Create one with the [`application`] helper. -/// -/// [`run`]: Program::run -/// [`application`]: self::application() -#[derive(Debug)] -pub struct Program { - raw: P, - settings: Settings, -} - -impl Program

{ - /// Runs the [`Program`]. - pub fn run(self) -> Result - where - Self: 'static, - { - struct Instance { - program: P, - state: P::State, - } - - impl Application for Instance

{ - type Message = P::Message; - type Theme = P::Theme; - type Flags = P; - type Executor = P::Executor; - - fn new(program: Self::Flags) -> (Self, Command) { - let (state, command) = P::build(&program); - - (Self { program, state }, command) - } - - fn title(&self) -> String { - self.program.title(&self.state) - } - - fn update( - &mut self, - message: Self::Message, - ) -> Command { - self.program.update(&mut self.state, message) - } - - fn view( - &self, - ) -> crate::Element<'_, Self::Message, Self::Theme, crate::Renderer> - { - self.program.view(&self.state) - } - - fn subscription(&self) -> Subscription { - self.program.subscription(&self.state) - } - - fn theme(&self) -> Self::Theme { - self.program.theme(&self.state) - } - - fn style(&self, theme: &Self::Theme) -> application::Appearance { - self.program.style(&self.state, theme) - } - } - - let Self { raw, settings } = self; - - Instance::run(Settings { - flags: raw, - id: settings.id, - window: settings.window, - fonts: settings.fonts, - default_font: settings.default_font, - default_text_size: settings.default_text_size, - antialiasing: settings.antialiasing, - }) - } - - /// Sets the [`Settings`] that will be used to run the [`Program`]. - pub fn settings(self, settings: Settings) -> Self { - Self { settings, ..self } - } - - /// Sets the [`Settings::antialiasing`] of the [`Program`]. - pub fn antialiasing(self, antialiasing: bool) -> Self { - Self { - settings: Settings { - antialiasing, - ..self.settings - }, - ..self - } - } - - /// Sets the default [`Font`] of the [`Program`]. - pub fn default_font(self, default_font: Font) -> Self { - Self { - settings: Settings { - default_font, - ..self.settings - }, - ..self - } - } - - /// Adds a font to the list of fonts that will be loaded at the start of the [`Program`]. - pub fn font(mut self, font: impl Into>) -> Self { - self.settings.fonts.push(font.into()); - self - } - - /// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Program`]. - pub fn centered(self) -> Self { - Self { - settings: Settings { - window: window::Settings { - position: window::Position::Centered, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`window::Settings::exit_on_close_request`] of the [`Program`]. - pub fn exit_on_close_request(self, exit_on_close_request: bool) -> Self { - Self { - settings: Settings { - window: window::Settings { - exit_on_close_request, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`window::Settings::transparent`] of the [`Program`]. - pub fn transparent(self, transparent: bool) -> Self { - Self { - settings: Settings { - window: window::Settings { - transparent, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`Title`] of the [`Program`]. - pub(crate) fn title( - self, - title: impl Title, - ) -> Program< - impl Definition, - > { - Program { - raw: with_title(self.raw, title), - settings: self.settings, - } - } - - /// Runs the [`Command`] produced by the closure at startup. - pub fn load( - self, - f: impl Fn() -> Command, - ) -> Program< - impl Definition, - > { - Program { - raw: with_load(self.raw, f), - settings: self.settings, - } - } - - /// Sets the subscription logic of the [`Program`]. - pub fn subscription( - self, - f: impl Fn(&P::State) -> Subscription, - ) -> Program< - impl Definition, - > { - Program { - raw: with_subscription(self.raw, f), - settings: self.settings, - } - } - - /// Sets the theme logic of the [`Program`]. - pub fn theme( - self, - f: impl Fn(&P::State) -> P::Theme, - ) -> Program< - impl Definition, - > { - Program { - raw: with_theme(self.raw, f), - settings: self.settings, - } - } - - /// Sets the style logic of the [`Program`]. - pub fn style( - self, - f: impl Fn(&P::State, &P::Theme) -> application::Appearance, - ) -> Program< - impl Definition, - > { - Program { - raw: with_style(self.raw, f), - settings: self.settings, - } - } -} - -/// The internal definition of a [`Program`]. -/// -/// You should not need to implement this trait directly. Instead, use the -/// helper functions available in the [`program`] module and the [`Program`] struct. -/// -/// [`program`]: crate::program -#[allow(missing_docs)] -pub trait Definition: Sized { - /// The state of the program. - type State; - - /// The message of the program. - type Message: Send + std::fmt::Debug; - - /// The theme of the program. - type Theme: Default + application::DefaultStyle; - - /// The executor of the program. - type Executor: Executor; - - fn build(&self) -> (Self::State, Command); - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command; - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme>; - - fn title(&self, _state: &Self::State) -> String { - String::from("A cool iced application!") - } - - fn subscription( - &self, - _state: &Self::State, - ) -> Subscription { - Subscription::none() - } - - fn theme(&self, _state: &Self::State) -> Self::Theme { - Self::Theme::default() - } - - fn style( - &self, - _state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - application::DefaultStyle::default_style(theme) - } -} - -fn with_title( - program: P, - title: impl Title, -) -> impl Definition { - struct WithTitle { - program: P, - title: Title, - } - - impl Definition for WithTitle - where - P: Definition, - Title: self::Title, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn title(&self, state: &Self::State) -> String { - self.title.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithTitle { program, title } -} - -fn with_load( - program: P, - f: impl Fn() -> Command, -) -> impl Definition { - struct WithLoad { - program: P, - load: F, - } - - impl Definition for WithLoad - where - F: Fn() -> Command, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = executor::Default; - - fn build(&self) -> (Self::State, Command) { - let (state, command) = self.program.build(); - - (state, Command::batch([command, (self.load)()])) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithLoad { program, load: f } -} - -fn with_subscription( - program: P, - f: impl Fn(&P::State) -> Subscription, -) -> impl Definition { - struct WithSubscription { - program: P, - subscription: F, - } - - impl Definition for WithSubscription - where - F: Fn(&P::State) -> Subscription, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = executor::Default; - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - (self.subscription)(state) - } - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithSubscription { - program, - subscription: f, - } -} - -fn with_theme( - program: P, - f: impl Fn(&P::State) -> P::Theme, -) -> impl Definition { - struct WithTheme { - program: P, - theme: F, - } - - impl Definition for WithTheme - where - F: Fn(&P::State) -> P::Theme, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn theme(&self, state: &Self::State) -> Self::Theme { - (self.theme)(state) - } - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithTheme { program, theme: f } -} - -fn with_style( - program: P, - f: impl Fn(&P::State, &P::Theme) -> application::Appearance, -) -> impl Definition { - struct WithStyle { - program: P, - style: F, - } - - impl Definition for WithStyle - where - F: Fn(&P::State, &P::Theme) -> application::Appearance, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - (self.style)(state, theme) - } - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - } - - WithStyle { program, style: f } -} - -/// The title logic of some [`Program`]. -/// -/// This trait is implemented both for `&static str` and -/// any closure `Fn(&State) -> String`. -pub trait Title { - /// Produces the title of the [`Program`]. - fn title(&self, state: &State) -> String; -} - -impl Title for &'static str { - fn title(&self, _state: &State) -> String { - self.to_string() - } -} - -impl Title for T -where - T: Fn(&State) -> String, -{ - fn title(&self, state: &State) -> String { - self(state) - } -} - -/// The update logic of some [`Program`]. -/// -/// This trait allows [`application`] to take any closure that -/// returns any `Into>`. -/// -/// [`application`]: self::application() -pub trait Update { - /// Processes the message and updates the state of the [`Program`]. - fn update( - &self, - state: &mut State, - message: Message, - ) -> impl Into>; -} - -impl Update for T -where - T: Fn(&mut State, Message) -> C, - C: Into>, -{ - fn update( - &self, - state: &mut State, - message: Message, - ) -> impl Into> { - self(state, message) - } -} - -/// The view logic of some [`Program`]. -/// -/// This trait allows [`application`] to take any closure that -/// returns any `Into>`. -/// -/// [`application`]: self::application() -pub trait View<'a, State, Message> { - /// Produces the widget of the [`Program`]. - fn view(&self, state: &'a State) -> impl Into>; -} - -impl<'a, T, State, Message, Widget> View<'a, State, Message> for T -where - T: Fn(&'a State) -> Widget, - State: 'static, - Widget: Into>, -{ - fn view(&self, state: &'a State) -> impl Into> { - self(state) - } -} -- cgit From 8e1d0b51f13a5561cd20b24be9cc06bc317ea601 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:29:02 +0100 Subject: Fix documentation of `run` function --- src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index def020e0..902a5f36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -340,10 +340,8 @@ pub type Element< /// [`Application`]: crate::Application pub type Result = std::result::Result<(), Error>; -/// Runs a basic iced application with default [`Settings`] given -/// - its window title, -/// - its update logic, -/// - and its view logic. +/// Runs a basic iced application with default [`Settings`] given its title, +/// update, and view logic. /// /// # Example /// ```no_run -- cgit From a034e40f7c0e325938da92894ee34f589f372e0a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:36:42 +0100 Subject: Clarify chain nature of `run` function --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 902a5f36..d238e78a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,6 +343,10 @@ pub type Result = std::result::Result<(), Error>; /// Runs a basic iced application with default [`Settings`] given its title, /// update, and view logic. /// +/// This is equivalent to chaining [`program`] with [`Program::run`]. +/// +/// [`Program::run`]: application::Program::run +/// /// # Example /// ```no_run /// use iced::widget::{button, column, text, Column}; -- cgit From c4b4207f4768c7e254ff0a6bf95c4d76ea08ce48 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:41:34 +0100 Subject: Support custom themes in `Program` API --- src/application/program.rs | 32 +++++++++++++++++--------------- src/lib.rs | 5 +++-- 2 files changed, 20 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/application/program.rs b/src/application/program.rs index 24e00efe..d70f39cf 100644 --- a/src/application/program.rs +++ b/src/application/program.rs @@ -64,37 +64,38 @@ use std::borrow::Cow; /// ] /// } /// ``` -pub fn program( +pub fn program( title: impl Title, update: impl Update, - view: impl for<'a> self::View<'a, State, Message>, -) -> Program< - impl Definition, -> + view: impl for<'a> self::View<'a, State, Message, Theme>, +) -> Program> where State: Default + 'static, Message: Send + std::fmt::Debug, + Theme: Default + application::DefaultStyle, { use std::marker::PhantomData; - struct Application { + struct Application { update: Update, view: View, _state: PhantomData, _message: PhantomData, + _theme: PhantomData, } - impl Definition - for Application + impl Definition + for Application where State: Default, Message: Send + std::fmt::Debug, + Theme: Default + application::DefaultStyle, Update: self::Update, - View: for<'a> self::View<'a, State, Message>, + View: for<'a> self::View<'a, State, Message, Theme>, { type State = State; type Message = Message; - type Theme = crate::Theme; + type Theme = Theme; type Executor = executor::Default; fn build(&self) -> (Self::State, Command) { @@ -123,6 +124,7 @@ where view, _state: PhantomData, _message: PhantomData, + _theme: PhantomData, }, settings: Settings::default(), } @@ -793,18 +795,18 @@ where /// /// This trait allows the [`program`] builder to take any closure that /// returns any `Into>`. -pub trait View<'a, State, Message> { +pub trait View<'a, State, Message, Theme> { /// Produces the widget of the [`Program`]. - fn view(&self, state: &'a State) -> impl Into>; + fn view(&self, state: &'a State) -> impl Into>; } -impl<'a, T, State, Message, Widget> View<'a, State, Message> for T +impl<'a, T, State, Message, Theme, Widget> View<'a, State, Message, Theme> for T where T: Fn(&'a State) -> Widget, State: 'static, - Widget: Into>, + Widget: Into>, { - fn view(&self, state: &'a State) -> impl Into> { + fn view(&self, state: &'a State) -> impl Into> { self(state) } } diff --git a/src/lib.rs b/src/lib.rs index d238e78a..bc87b1a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -373,14 +373,15 @@ pub type Result = std::result::Result<(), Error>; /// ] /// } /// ``` -pub fn run( +pub fn run( title: impl application::Title + 'static, update: impl application::Update + 'static, - view: impl for<'a> application::View<'a, State, Message> + 'static, + view: impl for<'a> application::View<'a, State, Message, Theme> + 'static, ) -> Result where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, + Theme: Default + application::DefaultStyle + 'static, { program(title, update, view).run() } -- cgit From 784fa80c0d92a7d1fda8a7ff77185d50423e228a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 17:57:24 +0100 Subject: Use `Program` API in `todos` example --- src/application/program.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/application/program.rs b/src/application/program.rs index d70f39cf..2e77678e 100644 --- a/src/application/program.rs +++ b/src/application/program.rs @@ -32,7 +32,7 @@ use crate::application::{self, Application}; use crate::executor::{self, Executor}; use crate::window; -use crate::{Command, Element, Font, Result, Settings, Subscription}; +use crate::{Command, Element, Font, Result, Settings, Size, Subscription}; use std::borrow::Cow; @@ -277,6 +277,20 @@ impl Program

{ } } + /// Sets the [`window::Settings::size`] of the [`Program`]. + pub fn window_size(self, size: impl Into) -> Self { + Self { + settings: Settings { + window: window::Settings { + size: size.into(), + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + /// Sets the [`window::Settings::transparent`] of the [`Program`]. pub fn transparent(self, transparent: bool) -> Self { Self { -- cgit From 943b6c965773748f8cacaa4fe385ac4a3bfb1e69 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 19:15:31 +0100 Subject: Introduce `Program::run_with` to control the initial state --- src/application/program.rs | 79 +++++++++++++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/application/program.rs b/src/application/program.rs index 2e77678e..be635431 100644 --- a/src/application/program.rs +++ b/src/application/program.rs @@ -70,7 +70,7 @@ pub fn program( view: impl for<'a> self::View<'a, State, Message, Theme>, ) -> Program> where - State: Default + 'static, + State: 'static, Message: Send + std::fmt::Debug, Theme: Default + application::DefaultStyle, { @@ -87,7 +87,6 @@ where impl Definition for Application where - State: Default, Message: Send + std::fmt::Debug, Theme: Default + application::DefaultStyle, Update: self::Update, @@ -98,8 +97,8 @@ where type Theme = Theme; type Executor = executor::Default; - fn build(&self) -> (Self::State, Command) { - (Self::State::default(), Command::none()) + fn load(&self) -> Command { + Command::none() } fn update( @@ -151,25 +150,57 @@ pub struct Program { impl Program

{ /// Runs the underlying [`Application`] of the [`Program`]. + /// + /// The state of the [`Program`] must implement [`Default`]. + /// If your state does not implement [`Default`], use [`run_with`] + /// instead. + /// + /// [`run_with`]: Self::run_with pub fn run(self) -> Result + where + Self: 'static, + P::State: Default, + { + self.run_with(P::State::default) + } + + /// Runs the underlying [`Application`] of the [`Program`] with a + /// closure that creates the initial state. + pub fn run_with( + self, + initialize: impl Fn() -> P::State + Clone + 'static, + ) -> Result where Self: 'static, { - struct Instance { + use std::marker::PhantomData; + + struct Instance { program: P, state: P::State, + _initialize: PhantomData, } - impl Application for Instance

{ + impl P::State> Application for Instance { type Message = P::Message; type Theme = P::Theme; - type Flags = P; + type Flags = (P, I); type Executor = P::Executor; - fn new(program: Self::Flags) -> (Self, Command) { - let (state, command) = P::build(&program); - - (Self { program, state }, command) + fn new( + (program, initialize): Self::Flags, + ) -> (Self, Command) { + let state = initialize(); + let command = program.load(); + + ( + Self { + program, + state, + _initialize: PhantomData, + }, + command, + ) } fn title(&self) -> String { @@ -206,7 +237,7 @@ impl Program

{ let Self { raw, settings } = self; Instance::run(Settings { - flags: raw, + flags: (raw, initialize), id: settings.id, window: settings.window, fonts: settings.fonts, @@ -389,7 +420,7 @@ pub trait Definition: Sized { /// The executor of the program. type Executor: Executor; - fn build(&self) -> (Self::State, Command); + fn load(&self) -> Command; fn update( &self, @@ -445,8 +476,8 @@ fn with_title( type Theme = P::Theme; type Executor = P::Executor; - fn build(&self) -> (Self::State, Command) { - self.program.build() + fn load(&self) -> Command { + self.program.load() } fn title(&self, state: &Self::State) -> String { @@ -509,10 +540,8 @@ fn with_load( type Theme = P::Theme; type Executor = executor::Default; - fn build(&self) -> (Self::State, Command) { - let (state, command) = self.program.build(); - - (state, Command::batch([command, (self.load)()])) + fn load(&self) -> Command { + Command::batch([self.program.load(), (self.load)()]) } fn update( @@ -582,8 +611,8 @@ fn with_subscription( (self.subscription)(state) } - fn build(&self) -> (Self::State, Command) { - self.program.build() + fn load(&self) -> Command { + self.program.load() } fn update( @@ -646,8 +675,8 @@ fn with_theme( (self.theme)(state) } - fn build(&self) -> (Self::State, Command) { - self.program.build() + fn load(&self) -> Command { + self.program.load() } fn title(&self, state: &Self::State) -> String { @@ -714,8 +743,8 @@ fn with_style( (self.style)(state, theme) } - fn build(&self) -> (Self::State, Command) { - self.program.build() + fn load(&self) -> Command { + self.program.load() } fn title(&self, state: &Self::State) -> String { -- cgit From cdb18e610a72b4a025d7e1890140393adee5b087 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 19:38:42 +0100 Subject: Move `Application` trait to `advanced` module --- src/advanced.rs | 1 + src/application.rs | 9 +- src/application/program.rs | 855 --------------------------------------------- src/lib.rs | 25 +- src/program.rs | 851 ++++++++++++++++++++++++++++++++++++++++++++ src/settings.rs | 12 +- 6 files changed, 874 insertions(+), 879 deletions(-) delete mode 100644 src/application/program.rs create mode 100644 src/program.rs (limited to 'src') diff --git a/src/advanced.rs b/src/advanced.rs index 8e026f84..306c3559 100644 --- a/src/advanced.rs +++ b/src/advanced.rs @@ -1,4 +1,5 @@ //! Leverage advanced concepts like custom widgets. +pub use crate::application::Application; pub use crate::core::clipboard::{self, Clipboard}; pub use crate::core::image; pub use crate::core::layout::{self, Layout}; diff --git a/src/application.rs b/src/application.rs index ba60db67..8317abcb 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,12 +1,8 @@ //! Build interactive cross-platform applications. -mod program; - -pub use program::{program, Definition, Program, Title, Update, View}; - use crate::shell::application; use crate::{Command, Element, Executor, Settings, Subscription}; -pub use application::{default, Appearance, DefaultStyle}; +pub use application::{Appearance, DefaultStyle}; /// An interactive cross-platform application. /// @@ -62,8 +58,9 @@ pub use application::{default, Appearance, DefaultStyle}; /// says "Hello, world!": /// /// ```no_run +/// use iced::advanced::Application; /// use iced::executor; -/// use iced::{Application, Command, Element, Settings, Theme}; +/// use iced::{Command, Element, Settings, Theme}; /// /// pub fn main() -> iced::Result { /// Hello::run(Settings::default()) diff --git a/src/application/program.rs b/src/application/program.rs deleted file mode 100644 index be635431..00000000 --- a/src/application/program.rs +++ /dev/null @@ -1,855 +0,0 @@ -//! -//! # Example -//! ```no_run -//! use iced::widget::{button, column, text, Column}; -//! use iced::Theme; -//! -//! pub fn main() -> iced::Result { -//! iced::program("A counter", update, view) -//! .theme(|_| Theme::Dark) -//! .centered() -//! .run() -//! } -//! -//! #[derive(Debug, Clone)] -//! enum Message { -//! Increment, -//! } -//! -//! fn update(value: &mut u64, message: Message) { -//! match message { -//! Message::Increment => *value += 1, -//! } -//! } -//! -//! fn view(value: &u64) -> Column { -//! column![ -//! text(value), -//! button("+").on_press(Message::Increment), -//! ] -//! } -//! ``` -use crate::application::{self, Application}; -use crate::executor::{self, Executor}; -use crate::window; -use crate::{Command, Element, Font, Result, Settings, Size, Subscription}; - -use std::borrow::Cow; - -/// Creates an iced [`Program`] given its title, update, and view logic. -/// -/// # Example -/// ```no_run -/// use iced::widget::{button, column, text, Column}; -/// -/// pub fn main() -> iced::Result { -/// iced::program("A counter", update, view).run() -/// } -/// -/// #[derive(Debug, Clone)] -/// enum Message { -/// Increment, -/// } -/// -/// fn update(value: &mut u64, message: Message) { -/// match message { -/// Message::Increment => *value += 1, -/// } -/// } -/// -/// fn view(value: &u64) -> Column { -/// column![ -/// text(value), -/// button("+").on_press(Message::Increment), -/// ] -/// } -/// ``` -pub fn program( - title: impl Title, - update: impl Update, - view: impl for<'a> self::View<'a, State, Message, Theme>, -) -> Program> -where - State: 'static, - Message: Send + std::fmt::Debug, - Theme: Default + application::DefaultStyle, -{ - use std::marker::PhantomData; - - struct Application { - update: Update, - view: View, - _state: PhantomData, - _message: PhantomData, - _theme: PhantomData, - } - - impl Definition - for Application - where - Message: Send + std::fmt::Debug, - Theme: Default + application::DefaultStyle, - Update: self::Update, - View: for<'a> self::View<'a, State, Message, Theme>, - { - type State = State; - type Message = Message; - type Theme = Theme; - type Executor = executor::Default; - - fn load(&self) -> Command { - Command::none() - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.update.update(state, message).into() - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.view.view(state).into() - } - } - - Program { - raw: Application { - update, - view, - _state: PhantomData, - _message: PhantomData, - _theme: PhantomData, - }, - settings: Settings::default(), - } - .title(title) -} - -/// The underlying definition and configuration of an iced [`Application`]. -/// -/// You can use this API to create and run iced applications -/// step by step—without coupling your logic to a trait -/// or a specific type. -/// -/// This API is meant to be a more convenient—although less -/// powerful—alternative to the [`Application`] trait. -/// -/// You can create a [`Program`] with the [`program`] helper. -/// -/// [`run`]: Program::run -#[derive(Debug)] -pub struct Program { - raw: P, - settings: Settings, -} - -impl Program

{ - /// Runs the underlying [`Application`] of the [`Program`]. - /// - /// The state of the [`Program`] must implement [`Default`]. - /// If your state does not implement [`Default`], use [`run_with`] - /// instead. - /// - /// [`run_with`]: Self::run_with - pub fn run(self) -> Result - where - Self: 'static, - P::State: Default, - { - self.run_with(P::State::default) - } - - /// Runs the underlying [`Application`] of the [`Program`] with a - /// closure that creates the initial state. - pub fn run_with( - self, - initialize: impl Fn() -> P::State + Clone + 'static, - ) -> Result - where - Self: 'static, - { - use std::marker::PhantomData; - - struct Instance { - program: P, - state: P::State, - _initialize: PhantomData, - } - - impl P::State> Application for Instance { - type Message = P::Message; - type Theme = P::Theme; - type Flags = (P, I); - type Executor = P::Executor; - - fn new( - (program, initialize): Self::Flags, - ) -> (Self, Command) { - let state = initialize(); - let command = program.load(); - - ( - Self { - program, - state, - _initialize: PhantomData, - }, - command, - ) - } - - fn title(&self) -> String { - self.program.title(&self.state) - } - - fn update( - &mut self, - message: Self::Message, - ) -> Command { - self.program.update(&mut self.state, message) - } - - fn view( - &self, - ) -> crate::Element<'_, Self::Message, Self::Theme, crate::Renderer> - { - self.program.view(&self.state) - } - - fn subscription(&self) -> Subscription { - self.program.subscription(&self.state) - } - - fn theme(&self) -> Self::Theme { - self.program.theme(&self.state) - } - - fn style(&self, theme: &Self::Theme) -> application::Appearance { - self.program.style(&self.state, theme) - } - } - - let Self { raw, settings } = self; - - Instance::run(Settings { - flags: (raw, initialize), - id: settings.id, - window: settings.window, - fonts: settings.fonts, - default_font: settings.default_font, - default_text_size: settings.default_text_size, - antialiasing: settings.antialiasing, - }) - } - - /// Sets the [`Settings`] that will be used to run the [`Program`]. - pub fn settings(self, settings: Settings) -> Self { - Self { settings, ..self } - } - - /// Sets the [`Settings::antialiasing`] of the [`Program`]. - pub fn antialiasing(self, antialiasing: bool) -> Self { - Self { - settings: Settings { - antialiasing, - ..self.settings - }, - ..self - } - } - - /// Sets the default [`Font`] of the [`Program`]. - pub fn default_font(self, default_font: Font) -> Self { - Self { - settings: Settings { - default_font, - ..self.settings - }, - ..self - } - } - - /// Adds a font to the list of fonts that will be loaded at the start of the [`Program`]. - pub fn font(mut self, font: impl Into>) -> Self { - self.settings.fonts.push(font.into()); - self - } - - /// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Program`]. - pub fn centered(self) -> Self { - Self { - settings: Settings { - window: window::Settings { - position: window::Position::Centered, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`window::Settings::exit_on_close_request`] of the [`Program`]. - pub fn exit_on_close_request(self, exit_on_close_request: bool) -> Self { - Self { - settings: Settings { - window: window::Settings { - exit_on_close_request, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`window::Settings::size`] of the [`Program`]. - pub fn window_size(self, size: impl Into) -> Self { - Self { - settings: Settings { - window: window::Settings { - size: size.into(), - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`window::Settings::transparent`] of the [`Program`]. - pub fn transparent(self, transparent: bool) -> Self { - Self { - settings: Settings { - window: window::Settings { - transparent, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`Title`] of the [`Program`]. - pub(crate) fn title( - self, - title: impl Title, - ) -> Program< - impl Definition, - > { - Program { - raw: with_title(self.raw, title), - settings: self.settings, - } - } - - /// Runs the [`Command`] produced by the closure at startup. - pub fn load( - self, - f: impl Fn() -> Command, - ) -> Program< - impl Definition, - > { - Program { - raw: with_load(self.raw, f), - settings: self.settings, - } - } - - /// Sets the subscription logic of the [`Program`]. - pub fn subscription( - self, - f: impl Fn(&P::State) -> Subscription, - ) -> Program< - impl Definition, - > { - Program { - raw: with_subscription(self.raw, f), - settings: self.settings, - } - } - - /// Sets the theme logic of the [`Program`]. - pub fn theme( - self, - f: impl Fn(&P::State) -> P::Theme, - ) -> Program< - impl Definition, - > { - Program { - raw: with_theme(self.raw, f), - settings: self.settings, - } - } - - /// Sets the style logic of the [`Program`]. - pub fn style( - self, - f: impl Fn(&P::State, &P::Theme) -> application::Appearance, - ) -> Program< - impl Definition, - > { - Program { - raw: with_style(self.raw, f), - settings: self.settings, - } - } -} - -/// The internal definition of a [`Program`]. -/// -/// You should not need to implement this trait directly. Instead, use the -/// methods available in the [`Program`] struct. -#[allow(missing_docs)] -pub trait Definition: Sized { - /// The state of the program. - type State; - - /// The message of the program. - type Message: Send + std::fmt::Debug; - - /// The theme of the program. - type Theme: Default + application::DefaultStyle; - - /// The executor of the program. - type Executor: Executor; - - fn load(&self) -> Command; - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command; - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme>; - - fn title(&self, _state: &Self::State) -> String { - String::from("A cool iced application!") - } - - fn subscription( - &self, - _state: &Self::State, - ) -> Subscription { - Subscription::none() - } - - fn theme(&self, _state: &Self::State) -> Self::Theme { - Self::Theme::default() - } - - fn style( - &self, - _state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - application::DefaultStyle::default_style(theme) - } -} - -fn with_title( - program: P, - title: impl Title, -) -> impl Definition { - struct WithTitle { - program: P, - title: Title, - } - - impl Definition for WithTitle - where - P: Definition, - Title: self::Title, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn load(&self) -> Command { - self.program.load() - } - - fn title(&self, state: &Self::State) -> String { - self.title.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithTitle { program, title } -} - -fn with_load( - program: P, - f: impl Fn() -> Command, -) -> impl Definition { - struct WithLoad { - program: P, - load: F, - } - - impl Definition for WithLoad - where - F: Fn() -> Command, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = executor::Default; - - fn load(&self) -> Command { - Command::batch([self.program.load(), (self.load)()]) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithLoad { program, load: f } -} - -fn with_subscription( - program: P, - f: impl Fn(&P::State) -> Subscription, -) -> impl Definition { - struct WithSubscription { - program: P, - subscription: F, - } - - impl Definition for WithSubscription - where - F: Fn(&P::State) -> Subscription, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = executor::Default; - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - (self.subscription)(state) - } - - fn load(&self) -> Command { - self.program.load() - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithSubscription { - program, - subscription: f, - } -} - -fn with_theme( - program: P, - f: impl Fn(&P::State) -> P::Theme, -) -> impl Definition { - struct WithTheme { - program: P, - theme: F, - } - - impl Definition for WithTheme - where - F: Fn(&P::State) -> P::Theme, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn theme(&self, state: &Self::State) -> Self::Theme { - (self.theme)(state) - } - - fn load(&self) -> Command { - self.program.load() - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithTheme { program, theme: f } -} - -fn with_style( - program: P, - f: impl Fn(&P::State, &P::Theme) -> application::Appearance, -) -> impl Definition { - struct WithStyle { - program: P, - style: F, - } - - impl Definition for WithStyle - where - F: Fn(&P::State, &P::Theme) -> application::Appearance, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - (self.style)(state, theme) - } - - fn load(&self) -> Command { - self.program.load() - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - } - - WithStyle { program, style: f } -} - -/// The title logic of some [`Program`]. -/// -/// This trait is implemented both for `&static str` and -/// any closure `Fn(&State) -> String`. -/// -/// This trait allows the [`program`] builder to take any of them. -pub trait Title { - /// Produces the title of the [`Program`]. - fn title(&self, state: &State) -> String; -} - -impl Title for &'static str { - fn title(&self, _state: &State) -> String { - self.to_string() - } -} - -impl Title for T -where - T: Fn(&State) -> String, -{ - fn title(&self, state: &State) -> String { - self(state) - } -} - -/// The update logic of some [`Program`]. -/// -/// This trait allows the [`program`] builder to take any closure that -/// returns any `Into>`. -pub trait Update { - /// Processes the message and updates the state of the [`Program`]. - fn update( - &self, - state: &mut State, - message: Message, - ) -> impl Into>; -} - -impl Update for T -where - T: Fn(&mut State, Message) -> C, - C: Into>, -{ - fn update( - &self, - state: &mut State, - message: Message, - ) -> impl Into> { - self(state, message) - } -} - -/// The view logic of some [`Program`]. -/// -/// This trait allows the [`program`] builder to take any closure that -/// returns any `Into>`. -pub trait View<'a, State, Message, Theme> { - /// Produces the widget of the [`Program`]. - fn view(&self, state: &'a State) -> impl Into>; -} - -impl<'a, T, State, Message, Theme, Widget> View<'a, State, Message, Theme> for T -where - T: Fn(&'a State) -> Widget, - State: 'static, - Widget: Into>, -{ - fn view(&self, state: &'a State) -> impl Into> { - self(state) - } -} diff --git a/src/lib.rs b/src/lib.rs index bc87b1a3..49447418 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,11 +157,11 @@ //! 1. Draw the resulting user interface. //! //! # Usage -//! You can either use the [`program`] builder or implement the [`Application`] -//! trait directly. +//! Use [`run`] or the [`program`] builder. //! //! [Elm]: https://elm-lang.org/ //! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ +//! [`program`]: program() #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] @@ -185,9 +185,10 @@ pub use iced_futures::futures; #[cfg(feature = "highlighter")] pub use iced_highlighter as highlighter; +mod application; mod error; -pub mod application; +pub mod program; pub mod settings; pub mod time; pub mod window; @@ -315,12 +316,12 @@ pub mod widget { mod runtime {} } -pub use application::Application; pub use command::Command; pub use error::Error; pub use event::Event; pub use executor::Executor; pub use font::Font; +pub use program::Program; pub use renderer::Renderer; pub use settings::Settings; pub use subscription::Subscription; @@ -335,9 +336,7 @@ pub type Element< Renderer = crate::Renderer, > = crate::core::Element<'a, Message, Theme, Renderer>; -/// The result of running an [`Application`]. -/// -/// [`Application`]: crate::Application +/// The result of running a [`Program`]. pub type Result = std::result::Result<(), Error>; /// Runs a basic iced application with default [`Settings`] given its title, @@ -345,7 +344,7 @@ pub type Result = std::result::Result<(), Error>; /// /// This is equivalent to chaining [`program`] with [`Program::run`]. /// -/// [`Program::run`]: application::Program::run +/// [`program`]: program() /// /// # Example /// ```no_run @@ -374,17 +373,17 @@ pub type Result = std::result::Result<(), Error>; /// } /// ``` pub fn run( - title: impl application::Title + 'static, - update: impl application::Update + 'static, - view: impl for<'a> application::View<'a, State, Message, Theme> + 'static, + title: impl program::Title + 'static, + update: impl program::Update + 'static, + view: impl for<'a> program::View<'a, State, Message, Theme> + 'static, ) -> Result where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, - Theme: Default + application::DefaultStyle + 'static, + Theme: Default + program::DefaultStyle + 'static, { program(title, update, view).run() } #[doc(inline)] -pub use application::program; +pub use program::program; diff --git a/src/program.rs b/src/program.rs new file mode 100644 index 00000000..7a366585 --- /dev/null +++ b/src/program.rs @@ -0,0 +1,851 @@ +//! Create and run iced applications step by step. +//! +//! # Example +//! ```no_run +//! use iced::widget::{button, column, text, Column}; +//! use iced::Theme; +//! +//! pub fn main() -> iced::Result { +//! iced::program("A counter", update, view) +//! .theme(|_| Theme::Dark) +//! .centered() +//! .run() +//! } +//! +//! #[derive(Debug, Clone)] +//! enum Message { +//! Increment, +//! } +//! +//! fn update(value: &mut u64, message: Message) { +//! match message { +//! Message::Increment => *value += 1, +//! } +//! } +//! +//! fn view(value: &u64) -> Column { +//! column![ +//! text(value), +//! button("+").on_press(Message::Increment), +//! ] +//! } +//! ``` +use crate::application::Application; +use crate::executor::{self, Executor}; +use crate::window; +use crate::{Command, Element, Font, Result, Settings, Size, Subscription}; + +pub use crate::application::{Appearance, DefaultStyle}; + +use std::borrow::Cow; + +/// Creates an iced [`Program`] given its title, update, and view logic. +/// +/// # Example +/// ```no_run +/// use iced::widget::{button, column, text, Column}; +/// +/// pub fn main() -> iced::Result { +/// iced::program("A counter", update, view).run() +/// } +/// +/// #[derive(Debug, Clone)] +/// enum Message { +/// Increment, +/// } +/// +/// fn update(value: &mut u64, message: Message) { +/// match message { +/// Message::Increment => *value += 1, +/// } +/// } +/// +/// fn view(value: &u64) -> Column { +/// column![ +/// text(value), +/// button("+").on_press(Message::Increment), +/// ] +/// } +/// ``` +pub fn program( + title: impl Title, + update: impl Update, + view: impl for<'a> self::View<'a, State, Message, Theme>, +) -> Program> +where + State: 'static, + Message: Send + std::fmt::Debug, + Theme: Default + DefaultStyle, +{ + use std::marker::PhantomData; + + struct Application { + update: Update, + view: View, + _state: PhantomData, + _message: PhantomData, + _theme: PhantomData, + } + + impl Definition + for Application + where + Message: Send + std::fmt::Debug, + Theme: Default + DefaultStyle, + Update: self::Update, + View: for<'a> self::View<'a, State, Message, Theme>, + { + type State = State; + type Message = Message; + type Theme = Theme; + type Executor = executor::Default; + + fn load(&self) -> Command { + Command::none() + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.update.update(state, message).into() + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.view.view(state).into() + } + } + + Program { + raw: Application { + update, + view, + _state: PhantomData, + _message: PhantomData, + _theme: PhantomData, + }, + settings: Settings::default(), + } + .title(title) +} + +/// The underlying definition and configuration of an iced application. +/// +/// You can use this API to create and run iced applications +/// step by step—without coupling your logic to a trait +/// or a specific type. +/// +/// You can create a [`Program`] with the [`program`] helper. +/// +/// [`run`]: Program::run +#[derive(Debug)] +pub struct Program { + raw: P, + settings: Settings, +} + +impl Program

{ + /// Runs the underlying [`Application`] of the [`Program`]. + /// + /// The state of the [`Program`] must implement [`Default`]. + /// If your state does not implement [`Default`], use [`run_with`] + /// instead. + /// + /// [`run_with`]: Self::run_with + pub fn run(self) -> Result + where + Self: 'static, + P::State: Default, + { + self.run_with(P::State::default) + } + + /// Runs the underlying [`Application`] of the [`Program`] with a + /// closure that creates the initial state. + pub fn run_with( + self, + initialize: impl Fn() -> P::State + Clone + 'static, + ) -> Result + where + Self: 'static, + { + use std::marker::PhantomData; + + struct Instance { + program: P, + state: P::State, + _initialize: PhantomData, + } + + impl P::State> Application for Instance { + type Message = P::Message; + type Theme = P::Theme; + type Flags = (P, I); + type Executor = P::Executor; + + fn new( + (program, initialize): Self::Flags, + ) -> (Self, Command) { + let state = initialize(); + let command = program.load(); + + ( + Self { + program, + state, + _initialize: PhantomData, + }, + command, + ) + } + + fn title(&self) -> String { + self.program.title(&self.state) + } + + fn update( + &mut self, + message: Self::Message, + ) -> Command { + self.program.update(&mut self.state, message) + } + + fn view( + &self, + ) -> crate::Element<'_, Self::Message, Self::Theme, crate::Renderer> + { + self.program.view(&self.state) + } + + fn subscription(&self) -> Subscription { + self.program.subscription(&self.state) + } + + fn theme(&self) -> Self::Theme { + self.program.theme(&self.state) + } + + fn style(&self, theme: &Self::Theme) -> Appearance { + self.program.style(&self.state, theme) + } + } + + let Self { raw, settings } = self; + + Instance::run(Settings { + flags: (raw, initialize), + id: settings.id, + window: settings.window, + fonts: settings.fonts, + default_font: settings.default_font, + default_text_size: settings.default_text_size, + antialiasing: settings.antialiasing, + }) + } + + /// Sets the [`Settings`] that will be used to run the [`Program`]. + pub fn settings(self, settings: Settings) -> Self { + Self { settings, ..self } + } + + /// Sets the [`Settings::antialiasing`] of the [`Program`]. + pub fn antialiasing(self, antialiasing: bool) -> Self { + Self { + settings: Settings { + antialiasing, + ..self.settings + }, + ..self + } + } + + /// Sets the default [`Font`] of the [`Program`]. + pub fn default_font(self, default_font: Font) -> Self { + Self { + settings: Settings { + default_font, + ..self.settings + }, + ..self + } + } + + /// Adds a font to the list of fonts that will be loaded at the start of the [`Program`]. + pub fn font(mut self, font: impl Into>) -> Self { + self.settings.fonts.push(font.into()); + self + } + + /// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Program`]. + pub fn centered(self) -> Self { + Self { + settings: Settings { + window: window::Settings { + position: window::Position::Centered, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::exit_on_close_request`] of the [`Program`]. + pub fn exit_on_close_request(self, exit_on_close_request: bool) -> Self { + Self { + settings: Settings { + window: window::Settings { + exit_on_close_request, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::size`] of the [`Program`]. + pub fn window_size(self, size: impl Into) -> Self { + Self { + settings: Settings { + window: window::Settings { + size: size.into(), + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`window::Settings::transparent`] of the [`Program`]. + pub fn transparent(self, transparent: bool) -> Self { + Self { + settings: Settings { + window: window::Settings { + transparent, + ..self.settings.window + }, + ..self.settings + }, + ..self + } + } + + /// Sets the [`Title`] of the [`Program`]. + pub(crate) fn title( + self, + title: impl Title, + ) -> Program< + impl Definition, + > { + Program { + raw: with_title(self.raw, title), + settings: self.settings, + } + } + + /// Runs the [`Command`] produced by the closure at startup. + pub fn load( + self, + f: impl Fn() -> Command, + ) -> Program< + impl Definition, + > { + Program { + raw: with_load(self.raw, f), + settings: self.settings, + } + } + + /// Sets the subscription logic of the [`Program`]. + pub fn subscription( + self, + f: impl Fn(&P::State) -> Subscription, + ) -> Program< + impl Definition, + > { + Program { + raw: with_subscription(self.raw, f), + settings: self.settings, + } + } + + /// Sets the theme logic of the [`Program`]. + pub fn theme( + self, + f: impl Fn(&P::State) -> P::Theme, + ) -> Program< + impl Definition, + > { + Program { + raw: with_theme(self.raw, f), + settings: self.settings, + } + } + + /// Sets the style logic of the [`Program`]. + pub fn style( + self, + f: impl Fn(&P::State, &P::Theme) -> Appearance, + ) -> Program< + impl Definition, + > { + Program { + raw: with_style(self.raw, f), + settings: self.settings, + } + } +} + +/// The internal definition of a [`Program`]. +/// +/// You should not need to implement this trait directly. Instead, use the +/// methods available in the [`Program`] struct. +#[allow(missing_docs)] +pub trait Definition: Sized { + /// The state of the program. + type State; + + /// The message of the program. + type Message: Send + std::fmt::Debug; + + /// The theme of the program. + type Theme: Default + DefaultStyle; + + /// The executor of the program. + type Executor: Executor; + + fn load(&self) -> Command; + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command; + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme>; + + fn title(&self, _state: &Self::State) -> String { + String::from("A cool iced application!") + } + + fn subscription( + &self, + _state: &Self::State, + ) -> Subscription { + Subscription::none() + } + + fn theme(&self, _state: &Self::State) -> Self::Theme { + Self::Theme::default() + } + + fn style(&self, _state: &Self::State, theme: &Self::Theme) -> Appearance { + DefaultStyle::default_style(theme) + } +} + +fn with_title( + program: P, + title: impl Title, +) -> impl Definition { + struct WithTitle { + program: P, + title: Title, + } + + impl Definition for WithTitle + where + P: Definition, + Title: self::Title, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn load(&self) -> Command { + self.program.load() + } + + fn title(&self, state: &Self::State) -> String { + self.title.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> Appearance { + self.program.style(state, theme) + } + } + + WithTitle { program, title } +} + +fn with_load( + program: P, + f: impl Fn() -> Command, +) -> impl Definition { + struct WithLoad { + program: P, + load: F, + } + + impl Definition for WithLoad + where + F: Fn() -> Command, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = executor::Default; + + fn load(&self) -> Command { + Command::batch([self.program.load(), (self.load)()]) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> Appearance { + self.program.style(state, theme) + } + } + + WithLoad { program, load: f } +} + +fn with_subscription( + program: P, + f: impl Fn(&P::State) -> Subscription, +) -> impl Definition { + struct WithSubscription { + program: P, + subscription: F, + } + + impl Definition for WithSubscription + where + F: Fn(&P::State) -> Subscription, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = executor::Default; + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + (self.subscription)(state) + } + + fn load(&self) -> Command { + self.program.load() + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> Appearance { + self.program.style(state, theme) + } + } + + WithSubscription { + program, + subscription: f, + } +} + +fn with_theme( + program: P, + f: impl Fn(&P::State) -> P::Theme, +) -> impl Definition { + struct WithTheme { + program: P, + theme: F, + } + + impl Definition for WithTheme + where + F: Fn(&P::State) -> P::Theme, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn theme(&self, state: &Self::State) -> Self::Theme { + (self.theme)(state) + } + + fn load(&self) -> Command { + self.program.load() + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> Appearance { + self.program.style(state, theme) + } + } + + WithTheme { program, theme: f } +} + +fn with_style( + program: P, + f: impl Fn(&P::State, &P::Theme) -> Appearance, +) -> impl Definition { + struct WithStyle { + program: P, + style: F, + } + + impl Definition for WithStyle + where + F: Fn(&P::State, &P::Theme) -> Appearance, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Executor = P::Executor; + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> Appearance { + (self.style)(state, theme) + } + + fn load(&self) -> Command { + self.program.load() + } + + fn title(&self, state: &Self::State) -> String { + self.program.title(state) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.program.view(state) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn theme(&self, state: &Self::State) -> Self::Theme { + self.program.theme(state) + } + } + + WithStyle { program, style: f } +} + +/// The title logic of some [`Program`]. +/// +/// This trait is implemented both for `&static str` and +/// any closure `Fn(&State) -> String`. +/// +/// This trait allows the [`program`] builder to take any of them. +pub trait Title { + /// Produces the title of the [`Program`]. + fn title(&self, state: &State) -> String; +} + +impl Title for &'static str { + fn title(&self, _state: &State) -> String { + self.to_string() + } +} + +impl Title for T +where + T: Fn(&State) -> String, +{ + fn title(&self, state: &State) -> String { + self(state) + } +} + +/// The update logic of some [`Program`]. +/// +/// This trait allows the [`program`] builder to take any closure that +/// returns any `Into>`. +pub trait Update { + /// Processes the message and updates the state of the [`Program`]. + fn update( + &self, + state: &mut State, + message: Message, + ) -> impl Into>; +} + +impl Update for T +where + T: Fn(&mut State, Message) -> C, + C: Into>, +{ + fn update( + &self, + state: &mut State, + message: Message, + ) -> impl Into> { + self(state, message) + } +} + +/// The view logic of some [`Program`]. +/// +/// This trait allows the [`program`] builder to take any closure that +/// returns any `Into>`. +pub trait View<'a, State, Message, Theme> { + /// Produces the widget of the [`Program`]. + fn view(&self, state: &'a State) -> impl Into>; +} + +impl<'a, T, State, Message, Theme, Widget> View<'a, State, Message, Theme> for T +where + T: Fn(&'a State) -> Widget, + State: 'static, + Widget: Into>, +{ + fn view(&self, state: &'a State) -> impl Into> { + self(state) + } +} diff --git a/src/settings.rs b/src/settings.rs index 92204847..f7947841 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -4,7 +4,9 @@ use crate::{Font, Pixels}; use std::borrow::Cow; -/// The settings of an application. +/// The settings of an iced [`Program`]. +/// +/// [`Program`]: crate::Program #[derive(Debug, Clone)] pub struct Settings { /// The identifier of the application. @@ -18,9 +20,9 @@ pub struct Settings { /// They will be ignored on the Web. pub window: window::Settings, - /// The data needed to initialize the [`Application`]. + /// The data needed to initialize the [`Program`]. /// - /// [`Application`]: crate::Application + /// [`Program`]: crate::Program pub flags: Flags, /// The fonts to load on boot. @@ -49,9 +51,9 @@ pub struct Settings { } impl Settings { - /// Initialize [`Application`] settings using the given data. + /// Initialize [`Program`] settings using the given data. /// - /// [`Application`]: crate::Application + /// [`Program`]: crate::Program pub fn with_flags(flags: Flags) -> Self { let default_settings = Settings::<()>::default(); -- cgit From cab9dec6267f3e38b2dee2a0262cd04e21a7519e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 19:42:43 +0100 Subject: Remove `'static'` bound for `P::State` in `Program::run_with` --- src/application.rs | 3 ++- src/program.rs | 5 +---- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/application.rs b/src/application.rs index 8317abcb..a44d6342 100644 --- a/src/application.rs +++ b/src/application.rs @@ -192,7 +192,8 @@ where /// [`Error`]: crate::Error fn run(settings: Settings) -> crate::Result where - Self: 'static, + Self::Message: 'static, + Self::Executor: 'static, { #[allow(clippy::needless_update)] let renderer_settings = crate::renderer::Settings { diff --git a/src/program.rs b/src/program.rs index 7a366585..c423f6d0 100644 --- a/src/program.rs +++ b/src/program.rs @@ -166,10 +166,7 @@ impl Program

{ /// Runs the underlying [`Application`] of the [`Program`] with a /// closure that creates the initial state. - pub fn run_with( - self, - initialize: impl Fn() -> P::State + Clone + 'static, - ) -> Result + pub fn run_with(self, initialize: impl Fn() -> P::State + Clone) -> Result where Self: 'static, { -- cgit From eb67aa5d71172569e3d404107a1a449998d98d42 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 19:53:02 +0100 Subject: Revert "Remove `'static'` bound for `P::State` in `Program::run_with`" This reverts commit cab9dec6267f3e38b2dee2a0262cd04e21a7519e. Wasm needs the `'static'` bound since the runtime will run in a background task. --- src/application.rs | 3 +-- src/program.rs | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/application.rs b/src/application.rs index a44d6342..8317abcb 100644 --- a/src/application.rs +++ b/src/application.rs @@ -192,8 +192,7 @@ where /// [`Error`]: crate::Error fn run(settings: Settings) -> crate::Result where - Self::Message: 'static, - Self::Executor: 'static, + Self: 'static, { #[allow(clippy::needless_update)] let renderer_settings = crate::renderer::Settings { diff --git a/src/program.rs b/src/program.rs index c423f6d0..7a366585 100644 --- a/src/program.rs +++ b/src/program.rs @@ -166,7 +166,10 @@ impl Program

{ /// Runs the underlying [`Application`] of the [`Program`] with a /// closure that creates the initial state. - pub fn run_with(self, initialize: impl Fn() -> P::State + Clone) -> Result + pub fn run_with( + self, + initialize: impl Fn() -> P::State + Clone + 'static, + ) -> Result where Self: 'static, { -- cgit From f409037c0766016a9bd22abde5da14406e74959a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Mar 2024 19:15:25 +0100 Subject: Simplify message names in overview docs --- src/lib.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 49447418..0e9566e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,8 +63,8 @@ //! ``` //! #[derive(Debug, Clone, Copy)] //! pub enum Message { -//! IncrementPressed, -//! DecrementPressed, +//! Increment, +//! Decrement, //! } //! ``` //! @@ -79,8 +79,8 @@ //! # //! # #[derive(Debug, Clone, Copy)] //! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, +//! # Increment, +//! # Decrement, //! # } //! # //! use iced::widget::{button, column, text, Column}; @@ -90,15 +90,15 @@ //! // We use a column: a simple vertical layout //! column![ //! // The increment button. We tell it to produce an -//! // `IncrementPressed` message when pressed -//! button("+").on_press(Message::IncrementPressed), +//! // `Increment` message when pressed +//! button("+").on_press(Message::Increment), //! //! // We show the value of the counter here //! text(self.value).size(50), //! //! // The decrement button. We tell it to produce a -//! // `DecrementPressed` message when pressed -//! button("-").on_press(Message::DecrementPressed), +//! // `Decrement` message when pressed +//! button("-").on_press(Message::Decrement), //! ] //! } //! } @@ -115,18 +115,18 @@ //! # //! # #[derive(Debug, Clone, Copy)] //! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, +//! # Increment, +//! # Decrement, //! # } //! impl Counter { //! // ... //! //! pub fn update(&mut self, message: Message) { //! match message { -//! Message::IncrementPressed => { +//! Message::Increment => { //! self.value += 1; //! } -//! Message::DecrementPressed => { +//! Message::Decrement => { //! self.value -= 1; //! } //! } -- cgit