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 --- examples/arc/src/main.rs | 2 +- examples/bezier_tool/src/main.rs | 2 +- examples/checkbox/src/main.rs | 2 +- examples/clock/src/main.rs | 2 +- examples/color_palette/src/main.rs | 2 +- examples/custom_shader/src/main.rs | 10 +- examples/download_progress/src/main.rs | 10 +- examples/events/src/main.rs | 2 +- examples/exit/src/main.rs | 2 +- examples/gradient/src/main.rs | 2 +- examples/layout/src/main.rs | 2 +- examples/loading_spinners/src/main.rs | 2 +- examples/multitouch/src/main.rs | 2 +- examples/pane_grid/src/main.rs | 2 +- examples/pokedex/src/main.rs | 2 +- examples/qr_code/src/main.rs | 2 +- examples/screenshot/src/main.rs | 2 +- examples/scrollable/src/main.rs | 2 +- examples/sierpinski_triangle/src/main.rs | 2 +- examples/solar_system/src/main.rs | 2 +- examples/stopwatch/src/main.rs | 2 +- examples/styling/src/main.rs | 2 +- examples/system_information/src/main.rs | 8 +- examples/tour/src/main.rs | 2 +- examples/url_handler/src/main.rs | 2 +- examples/vectorial_text/src/main.rs | 2 +- src/application.rs | 5 +- src/application/program.rs | 810 ++++++++++++++++++++++++++++++ src/lib.rs | 15 +- src/program.rs | 820 ------------------------------- 30 files changed, 851 insertions(+), 873 deletions(-) create mode 100644 src/application/program.rs delete mode 100644 src/program.rs diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index b1e8402a..4576404f 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -7,7 +7,7 @@ use iced::widget::canvas::{ use iced::{Element, Length, Point, Rectangle, Renderer, Subscription, Theme}; pub fn main() -> iced::Result { - iced::application("Arc - Iced", Arc::update, Arc::view) + iced::program("Arc - Iced", Arc::update, Arc::view) .subscription(Arc::subscription) .theme(|_| Theme::Dark) .antialiasing(true) diff --git a/examples/bezier_tool/src/main.rs b/examples/bezier_tool/src/main.rs index d822c69b..cf70bd40 100644 --- a/examples/bezier_tool/src/main.rs +++ b/examples/bezier_tool/src/main.rs @@ -3,7 +3,7 @@ use iced::widget::{button, column, text}; use iced::{Alignment, Element, Length}; pub fn main() -> iced::Result { - iced::application("Bezier Tool - Iced", Example::update, Example::view) + iced::program("Bezier Tool - Iced", Example::update, Example::view) .antialiasing(true) .run() } diff --git a/examples/checkbox/src/main.rs b/examples/checkbox/src/main.rs index 6a3b9464..38949336 100644 --- a/examples/checkbox/src/main.rs +++ b/examples/checkbox/src/main.rs @@ -4,7 +4,7 @@ use iced::{Element, Font, Length}; const ICON_FONT: Font = Font::with_name("icons"); pub fn main() -> iced::Result { - iced::application("Checkbox - Iced", Example::update, Example::view) + iced::program("Checkbox - Iced", Example::update, Example::view) .font(include_bytes!("../fonts/icons.ttf").as_slice()) .run() } diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index ebb22cef..897f8f1b 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -8,7 +8,7 @@ use iced::{ }; pub fn main() -> iced::Result { - iced::application("Clock - Iced", Clock::update, Clock::view) + iced::program("Clock - Iced", Clock::update, Clock::view) .subscription(Clock::subscription) .theme(Clock::theme) .antialiasing(true) diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index 2cded2db..29337508 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -13,7 +13,7 @@ use std::marker::PhantomData; use std::ops::RangeInclusive; pub fn main() -> iced::Result { - iced::application( + iced::program( "Color Palette - Iced", ColorPalette::update, ColorPalette::view, diff --git a/examples/custom_shader/src/main.rs b/examples/custom_shader/src/main.rs index 4457a15a..aa3dafe9 100644 --- a/examples/custom_shader/src/main.rs +++ b/examples/custom_shader/src/main.rs @@ -9,13 +9,9 @@ use iced::window; use iced::{Alignment, Color, Element, Length, Subscription}; fn main() -> iced::Result { - iced::application( - "Custom Shader - Iced", - IcedCubes::update, - IcedCubes::view, - ) - .subscription(IcedCubes::subscription) - .run() + iced::program("Custom Shader - Iced", IcedCubes::update, IcedCubes::view) + .subscription(IcedCubes::subscription) + .run() } struct IcedCubes { diff --git a/examples/download_progress/src/main.rs b/examples/download_progress/src/main.rs index 8d7994c8..9f4769e0 100644 --- a/examples/download_progress/src/main.rs +++ b/examples/download_progress/src/main.rs @@ -4,13 +4,9 @@ use iced::widget::{button, column, container, progress_bar, text, Column}; use iced::{Alignment, Element, Length, Subscription}; pub fn main() -> iced::Result { - iced::application( - "Download Progress - Iced", - Example::update, - Example::view, - ) - .subscription(Example::subscription) - .run() + iced::program("Download Progress - Iced", Example::update, Example::view) + .subscription(Example::subscription) + .run() } #[derive(Debug)] diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index e18fe299..bf568c94 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -5,7 +5,7 @@ use iced::window; use iced::{Alignment, Command, Element, Length, Subscription}; pub fn main() -> iced::Result { - iced::application("Events - Iced", Events::update, Events::view) + iced::program("Events - Iced", Events::update, Events::view) .subscription(Events::subscription) .exit_on_close_request(false) .run() diff --git a/examples/exit/src/main.rs b/examples/exit/src/main.rs index 4948bd0f..7bed272d 100644 --- a/examples/exit/src/main.rs +++ b/examples/exit/src/main.rs @@ -3,7 +3,7 @@ use iced::window; use iced::{Alignment, Command, Element, Length}; pub fn main() -> iced::Result { - iced::application("Exit - Iced", Exit::update, Exit::view).run() + iced::program("Exit - Iced", Exit::update, Exit::view).run() } #[derive(Default)] diff --git a/examples/gradient/src/main.rs b/examples/gradient/src/main.rs index 6cfe395a..9c48f4b9 100644 --- a/examples/gradient/src/main.rs +++ b/examples/gradient/src/main.rs @@ -8,7 +8,7 @@ use iced::{Alignment, Color, Element, Length, Radians, Theme}; pub fn main() -> iced::Result { tracing_subscriber::fmt::init(); - iced::application("Gradient - Iced", Gradient::update, Gradient::view) + iced::program("Gradient - Iced", Gradient::update, Gradient::view) .style(Gradient::style) .transparent(true) .run() diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index e6b521b2..713e2b70 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -10,7 +10,7 @@ use iced::{ }; pub fn main() -> iced::Result { - iced::application(Layout::title, Layout::update, Layout::view) + iced::program(Layout::title, Layout::update, Layout::view) .subscription(Layout::subscription) .theme(Layout::theme) .run() diff --git a/examples/loading_spinners/src/main.rs b/examples/loading_spinners/src/main.rs index 3261bdcd..eaa4d57e 100644 --- a/examples/loading_spinners/src/main.rs +++ b/examples/loading_spinners/src/main.rs @@ -11,7 +11,7 @@ use circular::Circular; use linear::Linear; pub fn main() -> iced::Result { - iced::application( + iced::program( "Loading Spinners - Iced", LoadingSpinners::update, LoadingSpinners::view, diff --git a/examples/multitouch/src/main.rs b/examples/multitouch/src/main.rs index 69717310..2453c7f5 100644 --- a/examples/multitouch/src/main.rs +++ b/examples/multitouch/src/main.rs @@ -13,7 +13,7 @@ use std::collections::HashMap; pub fn main() -> iced::Result { tracing_subscriber::fmt::init(); - iced::application("Multitouch - Iced", Multitouch::update, Multitouch::view) + iced::program("Multitouch - Iced", Multitouch::update, Multitouch::view) .antialiasing(true) .centered() .run() diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 439e9ee5..829996d8 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -7,7 +7,7 @@ use iced::widget::{ use iced::{Color, Element, Length, Size, Subscription}; pub fn main() -> iced::Result { - iced::application("Pane Grid - Iced", Example::update, Example::view) + iced::program("Pane Grid - Iced", Example::update, Example::view) .subscription(Example::subscription) .run() } diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index c737b87e..0811c08d 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -3,7 +3,7 @@ use iced::widget::{self, column, container, image, row, text}; use iced::{Alignment, Command, Element, Length}; pub fn main() -> iced::Result { - iced::application(Pokedex::title, Pokedex::update, Pokedex::view) + iced::program(Pokedex::title, Pokedex::update, Pokedex::view) .load(Pokedex::search) .run() } diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 1a23331d..b93adf04 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -4,7 +4,7 @@ use iced::widget::{ use iced::{Alignment, Element, Length, Theme}; pub fn main() -> iced::Result { - iced::application( + iced::program( "QR Code Generator - Iced", QRGenerator::update, QRGenerator::view, diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 296a7f54..d887c41b 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -13,7 +13,7 @@ use ::image::ColorType; fn main() -> iced::Result { tracing_subscriber::fmt::init(); - iced::application("Screenshot - Iced", Example::update, Example::view) + iced::program("Screenshot - Iced", Example::update, Example::view) .subscription(Example::subscription) .run() } diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index a6f3c689..240ae908 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -10,7 +10,7 @@ use once_cell::sync::Lazy; static SCROLLABLE_ID: Lazy = Lazy::new(scrollable::Id::unique); pub fn main() -> iced::Result { - iced::application( + iced::program( "Scrollable - Iced", ScrollableDemo::update, ScrollableDemo::view, diff --git a/examples/sierpinski_triangle/src/main.rs b/examples/sierpinski_triangle/src/main.rs index c7287516..07ae05d6 100644 --- a/examples/sierpinski_triangle/src/main.rs +++ b/examples/sierpinski_triangle/src/main.rs @@ -8,7 +8,7 @@ use rand::Rng; use std::fmt::Debug; fn main() -> iced::Result { - iced::application( + iced::program( "Sierpinski Triangle - Iced", SierpinskiEmulator::update, SierpinskiEmulator::view, diff --git a/examples/solar_system/src/main.rs b/examples/solar_system/src/main.rs index 40fd6b12..b5228f09 100644 --- a/examples/solar_system/src/main.rs +++ b/examples/solar_system/src/main.rs @@ -22,7 +22,7 @@ use std::time::Instant; pub fn main() -> iced::Result { tracing_subscriber::fmt::init(); - iced::application( + iced::program( "Solar System - Iced", SolarSystem::update, SolarSystem::view, diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 2496b85b..b9eb19cf 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -7,7 +7,7 @@ use iced::{Alignment, Element, Length, Subscription, Theme}; use std::time::{Duration, Instant}; pub fn main() -> iced::Result { - iced::application("Stopwatch - Iced", Stopwatch::update, Stopwatch::view) + iced::program("Stopwatch - Iced", Stopwatch::update, Stopwatch::view) .subscription(Stopwatch::subscription) .theme(Stopwatch::theme) .run() diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index ce14aacb..73268da0 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -6,7 +6,7 @@ use iced::widget::{ use iced::{Alignment, Element, Length, Theme}; pub fn main() -> iced::Result { - iced::application("Styling - Iced", Styling::update, Styling::view) + iced::program("Styling - Iced", Styling::update, Styling::view) .theme(Styling::theme) .run() } diff --git a/examples/system_information/src/main.rs b/examples/system_information/src/main.rs index 75a4d8d6..a6ac27a6 100644 --- a/examples/system_information/src/main.rs +++ b/examples/system_information/src/main.rs @@ -2,12 +2,8 @@ use iced::widget::{button, column, container, text}; use iced::{system, Command, Element, Length}; pub fn main() -> iced::Result { - iced::application( - "System Information - Iced", - Example::update, - Example::view, - ) - .run() + iced::program("System Information - Iced", Example::update, Example::view) + .run() } #[derive(Default)] diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 97ce761c..be6e24d5 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -16,7 +16,7 @@ pub fn main() -> iced::Result { #[cfg(not(target_arch = "wasm32"))] tracing_subscriber::fmt::init(); - iced::application(Tour::title, Tour::update, Tour::view) + iced::program(Tour::title, Tour::update, Tour::view) .centered() .run() } diff --git a/examples/url_handler/src/main.rs b/examples/url_handler/src/main.rs index 540d927e..df705b6c 100644 --- a/examples/url_handler/src/main.rs +++ b/examples/url_handler/src/main.rs @@ -3,7 +3,7 @@ use iced::widget::{container, text}; use iced::{Element, Length, Subscription}; pub fn main() -> iced::Result { - iced::application("URL Handler - Iced", App::update, App::view) + iced::program("URL Handler - Iced", App::update, App::view) .subscription(App::subscription) .run() } diff --git a/examples/vectorial_text/src/main.rs b/examples/vectorial_text/src/main.rs index 7b7814ae..a7391e23 100644 --- a/examples/vectorial_text/src/main.rs +++ b/examples/vectorial_text/src/main.rs @@ -6,7 +6,7 @@ use iced::widget::{ use iced::{Element, Length, Point, Rectangle, Renderer, Theme, Vector}; pub fn main() -> iced::Result { - iced::application( + iced::program( "Vectorial Text - Iced", VectorialText::update, VectorialText::view, diff --git a/src/application.rs b/src/application.rs index 48fc672a..ba60db67 100644 --- a/src/application.rs +++ b/src/application.rs @@ -1,7 +1,10 @@ //! Build interactive cross-platform applications. -use crate::{Command, Element, Executor, Settings, Subscription}; +mod program; + +pub use program::{program, Definition, Program, Title, Update, View}; use crate::shell::application; +use crate::{Command, Element, Executor, Settings, Subscription}; pub use application::{default, Appearance, DefaultStyle}; diff --git a/src/application/program.rs b/src/application/program.rs new file mode 100644 index 00000000..24e00efe --- /dev/null +++ b/src/application/program.rs @@ -0,0 +1,810 @@ +//! +//! # Example +//! ```no_run +//! use iced::widget::{button, column, text, Column}; +//! use iced::Theme; +//! +//! pub fn main() -> iced::Result { +//! iced::program("A counter", update, view) +//! .theme(|_| Theme::Dark) +//! .centered() +//! .run() +//! } +//! +//! #[derive(Debug, Clone)] +//! enum Message { +//! Increment, +//! } +//! +//! fn update(value: &mut u64, message: Message) { +//! match message { +//! Message::Increment => *value += 1, +//! } +//! } +//! +//! fn view(value: &u64) -> Column { +//! column![ +//! text(value), +//! button("+").on_press(Message::Increment), +//! ] +//! } +//! ``` +use crate::application::{self, Application}; +use crate::executor::{self, Executor}; +use crate::window; +use crate::{Command, Element, Font, Result, Settings, Subscription}; + +use std::borrow::Cow; + +/// Creates an iced [`Program`] given its title, update, and view logic. +/// +/// # Example +/// ```no_run +/// use iced::widget::{button, column, text, Column}; +/// +/// pub fn main() -> iced::Result { +/// iced::program("A counter", update, view).run() +/// } +/// +/// #[derive(Debug, Clone)] +/// enum Message { +/// Increment, +/// } +/// +/// fn update(value: &mut u64, message: Message) { +/// match message { +/// Message::Increment => *value += 1, +/// } +/// } +/// +/// fn view(value: &u64) -> Column { +/// column![ +/// text(value), +/// button("+").on_press(Message::Increment), +/// ] +/// } +/// ``` +pub fn program( + title: impl Title, + update: impl Update, + view: impl for<'a> self::View<'a, State, Message>, +) -> Program< + impl Definition, +> +where + State: Default + 'static, + Message: Send + std::fmt::Debug, +{ + use std::marker::PhantomData; + + struct Application { + update: Update, + view: View, + _state: PhantomData, + _message: PhantomData, + } + + impl Definition + for Application + where + State: Default, + Message: Send + std::fmt::Debug, + Update: self::Update, + View: for<'a> self::View<'a, State, Message>, + { + type State = State; + type Message = Message; + type Theme = crate::Theme; + type Executor = executor::Default; + + fn build(&self) -> (Self::State, Command) { + (Self::State::default(), Command::none()) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Command { + self.update.update(state, message).into() + } + + fn view<'a>( + &self, + state: &'a Self::State, + ) -> Element<'a, Self::Message, Self::Theme> { + self.view.view(state).into() + } + } + + Program { + raw: Application { + update, + view, + _state: PhantomData, + _message: PhantomData, + }, + settings: Settings::default(), + } + .title(title) +} + +/// The underlying definition and configuration of an iced [`Application`]. +/// +/// You can use this API to create and run iced applications +/// step by step—without coupling your logic to a trait +/// or a specific type. +/// +/// This API is meant to be a more convenient—although less +/// powerful—alternative to the [`Application`] trait. +/// +/// You can create a [`Program`] with the [`program`] helper. +/// +/// [`run`]: Program::run +#[derive(Debug)] +pub struct Program { + raw: P, + settings: Settings, +} + +impl Program

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

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

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

{ - type Message = P::Message; - type Theme = P::Theme; - type Flags = P; - type Executor = P::Executor; - - fn new(program: Self::Flags) -> (Self, Command) { - let (state, command) = P::build(&program); - - (Self { program, state }, command) - } - - fn title(&self) -> String { - self.program.title(&self.state) - } - - fn update( - &mut self, - message: Self::Message, - ) -> Command { - self.program.update(&mut self.state, message) - } - - fn view( - &self, - ) -> crate::Element<'_, Self::Message, Self::Theme, crate::Renderer> - { - self.program.view(&self.state) - } - - fn subscription(&self) -> Subscription { - self.program.subscription(&self.state) - } - - fn theme(&self) -> Self::Theme { - self.program.theme(&self.state) - } - - fn style(&self, theme: &Self::Theme) -> application::Appearance { - self.program.style(&self.state, theme) - } - } - - let Self { raw, settings } = self; - - Instance::run(Settings { - flags: raw, - id: settings.id, - window: settings.window, - fonts: settings.fonts, - default_font: settings.default_font, - default_text_size: settings.default_text_size, - antialiasing: settings.antialiasing, - }) - } - - /// Sets the [`Settings`] that will be used to run the [`Program`]. - pub fn settings(self, settings: Settings) -> Self { - Self { settings, ..self } - } - - /// Sets the [`Settings::antialiasing`] of the [`Program`]. - pub fn antialiasing(self, antialiasing: bool) -> Self { - Self { - settings: Settings { - antialiasing, - ..self.settings - }, - ..self - } - } - - /// Sets the default [`Font`] of the [`Program`]. - pub fn default_font(self, default_font: Font) -> Self { - Self { - settings: Settings { - default_font, - ..self.settings - }, - ..self - } - } - - /// Adds a font to the list of fonts that will be loaded at the start of the [`Program`]. - pub fn font(mut self, font: impl Into>) -> Self { - self.settings.fonts.push(font.into()); - self - } - - /// Sets the [`window::Settings::position`] to [`window::Position::Centered`] in the [`Program`]. - pub fn centered(self) -> Self { - Self { - settings: Settings { - window: window::Settings { - position: window::Position::Centered, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`window::Settings::exit_on_close_request`] of the [`Program`]. - pub fn exit_on_close_request(self, exit_on_close_request: bool) -> Self { - Self { - settings: Settings { - window: window::Settings { - exit_on_close_request, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`window::Settings::transparent`] of the [`Program`]. - pub fn transparent(self, transparent: bool) -> Self { - Self { - settings: Settings { - window: window::Settings { - transparent, - ..self.settings.window - }, - ..self.settings - }, - ..self - } - } - - /// Sets the [`Title`] of the [`Program`]. - pub(crate) fn title( - self, - title: impl Title, - ) -> Program< - impl Definition, - > { - Program { - raw: with_title(self.raw, title), - settings: self.settings, - } - } - - /// Runs the [`Command`] produced by the closure at startup. - pub fn load( - self, - f: impl Fn() -> Command, - ) -> Program< - impl Definition, - > { - Program { - raw: with_load(self.raw, f), - settings: self.settings, - } - } - - /// Sets the subscription logic of the [`Program`]. - pub fn subscription( - self, - f: impl Fn(&P::State) -> Subscription, - ) -> Program< - impl Definition, - > { - Program { - raw: with_subscription(self.raw, f), - settings: self.settings, - } - } - - /// Sets the theme logic of the [`Program`]. - pub fn theme( - self, - f: impl Fn(&P::State) -> P::Theme, - ) -> Program< - impl Definition, - > { - Program { - raw: with_theme(self.raw, f), - settings: self.settings, - } - } - - /// Sets the style logic of the [`Program`]. - pub fn style( - self, - f: impl Fn(&P::State, &P::Theme) -> application::Appearance, - ) -> Program< - impl Definition, - > { - Program { - raw: with_style(self.raw, f), - settings: self.settings, - } - } -} - -/// The internal definition of a [`Program`]. -/// -/// You should not need to implement this trait directly. Instead, use the -/// helper functions available in the [`program`] module and the [`Program`] struct. -/// -/// [`program`]: crate::program -#[allow(missing_docs)] -pub trait Definition: Sized { - /// The state of the program. - type State; - - /// The message of the program. - type Message: Send + std::fmt::Debug; - - /// The theme of the program. - type Theme: Default + application::DefaultStyle; - - /// The executor of the program. - type Executor: Executor; - - fn build(&self) -> (Self::State, Command); - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command; - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme>; - - fn title(&self, _state: &Self::State) -> String { - String::from("A cool iced application!") - } - - fn subscription( - &self, - _state: &Self::State, - ) -> Subscription { - Subscription::none() - } - - fn theme(&self, _state: &Self::State) -> Self::Theme { - Self::Theme::default() - } - - fn style( - &self, - _state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - application::DefaultStyle::default_style(theme) - } -} - -fn with_title( - program: P, - title: impl Title, -) -> impl Definition { - struct WithTitle { - program: P, - title: Title, - } - - impl Definition for WithTitle - where - P: Definition, - Title: self::Title, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn title(&self, state: &Self::State) -> String { - self.title.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithTitle { program, title } -} - -fn with_load( - program: P, - f: impl Fn() -> Command, -) -> impl Definition { - struct WithLoad { - program: P, - load: F, - } - - impl Definition for WithLoad - where - F: Fn() -> Command, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = executor::Default; - - fn build(&self) -> (Self::State, Command) { - let (state, command) = self.program.build(); - - (state, Command::batch([command, (self.load)()])) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithLoad { program, load: f } -} - -fn with_subscription( - program: P, - f: impl Fn(&P::State) -> Subscription, -) -> impl Definition { - struct WithSubscription { - program: P, - subscription: F, - } - - impl Definition for WithSubscription - where - F: Fn(&P::State) -> Subscription, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = executor::Default; - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - (self.subscription)(state) - } - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithSubscription { - program, - subscription: f, - } -} - -fn with_theme( - program: P, - f: impl Fn(&P::State) -> P::Theme, -) -> impl Definition { - struct WithTheme { - program: P, - theme: F, - } - - impl Definition for WithTheme - where - F: Fn(&P::State) -> P::Theme, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn theme(&self, state: &Self::State) -> Self::Theme { - (self.theme)(state) - } - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - self.program.style(state, theme) - } - } - - WithTheme { program, theme: f } -} - -fn with_style( - program: P, - f: impl Fn(&P::State, &P::Theme) -> application::Appearance, -) -> impl Definition { - struct WithStyle { - program: P, - style: F, - } - - impl Definition for WithStyle - where - F: Fn(&P::State, &P::Theme) -> application::Appearance, - { - type State = P::State; - type Message = P::Message; - type Theme = P::Theme; - type Executor = P::Executor; - - fn style( - &self, - state: &Self::State, - theme: &Self::Theme, - ) -> application::Appearance { - (self.style)(state, theme) - } - - fn build(&self) -> (Self::State, Command) { - self.program.build() - } - - fn title(&self, state: &Self::State) -> String { - self.program.title(state) - } - - fn update( - &self, - state: &mut Self::State, - message: Self::Message, - ) -> Command { - self.program.update(state, message) - } - - fn view<'a>( - &self, - state: &'a Self::State, - ) -> Element<'a, Self::Message, Self::Theme> { - self.program.view(state) - } - - fn subscription( - &self, - state: &Self::State, - ) -> Subscription { - self.program.subscription(state) - } - - fn theme(&self, state: &Self::State) -> Self::Theme { - self.program.theme(state) - } - } - - WithStyle { program, style: f } -} - -/// The title logic of some [`Program`]. -/// -/// This trait is implemented both for `&static str` and -/// any closure `Fn(&State) -> String`. -pub trait Title { - /// Produces the title of the [`Program`]. - fn title(&self, state: &State) -> String; -} - -impl Title for &'static str { - fn title(&self, _state: &State) -> String { - self.to_string() - } -} - -impl Title for T -where - T: Fn(&State) -> String, -{ - fn title(&self, state: &State) -> String { - self(state) - } -} - -/// The update logic of some [`Program`]. -/// -/// This trait allows [`application`] to take any closure that -/// returns any `Into>`. -/// -/// [`application`]: self::application() -pub trait Update { - /// Processes the message and updates the state of the [`Program`]. - fn update( - &self, - state: &mut State, - message: Message, - ) -> impl Into>; -} - -impl Update for T -where - T: Fn(&mut State, Message) -> C, - C: Into>, -{ - fn update( - &self, - state: &mut State, - message: Message, - ) -> impl Into> { - self(state, message) - } -} - -/// The view logic of some [`Program`]. -/// -/// This trait allows [`application`] to take any closure that -/// returns any `Into>`. -/// -/// [`application`]: self::application() -pub trait View<'a, State, Message> { - /// Produces the widget of the [`Program`]. - fn view(&self, state: &'a State) -> impl Into>; -} - -impl<'a, T, State, Message, Widget> View<'a, State, Message> for T -where - T: Fn(&'a State) -> Widget, - State: 'static, - Widget: Into>, -{ - fn view(&self, state: &'a State) -> impl Into> { - self(state) - } -} -- cgit