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 --- examples/gradient/src/main.rs | 10 +- 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 +- 7 files changed, 880 insertions(+), 883 deletions(-) delete mode 100644 src/application/program.rs create mode 100644 src/program.rs diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index 9c48f4b9..22c21cdd 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -1,5 +1,5 @@ -use iced::application; use iced::gradient; +use iced::program; use iced::widget::{ checkbox, column, container, horizontal_space, row, slider, text, }; @@ -95,14 +95,16 @@ impl Gradient { .into() } - fn style(&self, theme: &Theme) -> application::Appearance { + fn style(&self, theme: &Theme) -> program::Appearance { + use program::DefaultStyle; + if self.transparent { - application::Appearance { + program::Appearance { background_color: Color::TRANSPARENT, text_color: theme.palette().text, } } else { - application::default(theme) + Theme::default_style(theme) } } } 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