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/program.rs | 669 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 669 insertions(+) create mode 100644 src/program.rs (limited to 'src/program.rs') 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) + } +} -- 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/program.rs') 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/program.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 79 insertions(+), 9 deletions(-) (limited to 'src/program.rs') 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/program.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/program.rs') 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/program.rs') 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/program.rs') 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/program.rs | 123 ++++++++++++++++++--------------------------------------- 1 file changed, 39 insertions(+), 84 deletions(-) (limited to 'src/program.rs') 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/program.rs') 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/program.rs | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 146 insertions(+), 11 deletions(-) (limited to 'src/program.rs') 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 -- 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/program.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/program.rs') 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 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/program.rs | 820 --------------------------------------------------------- 1 file changed, 820 deletions(-) delete mode 100644 src/program.rs (limited to 'src/program.rs') 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 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/program.rs | 851 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 851 insertions(+) create mode 100644 src/program.rs (limited to 'src/program.rs') 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) + } +} -- 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/program.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/program.rs') 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/program.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/program.rs') 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