Move Program to application module

This commit is contained in:
Héctor Ramón Jiménez 2024-03-17 14:16:38 +01:00
parent 7e1ef7d150
commit 54f44754eb
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
29 changed files with 58 additions and 80 deletions

View file

@ -7,7 +7,7 @@ use iced::widget::canvas::{
use iced::{Element, Length, Point, Rectangle, Renderer, Subscription, Theme}; use iced::{Element, Length, Point, Rectangle, Renderer, Subscription, Theme};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application("Arc - Iced", Arc::update, Arc::view) iced::program("Arc - Iced", Arc::update, Arc::view)
.subscription(Arc::subscription) .subscription(Arc::subscription)
.theme(|_| Theme::Dark) .theme(|_| Theme::Dark)
.antialiasing(true) .antialiasing(true)

View file

@ -3,7 +3,7 @@ use iced::widget::{button, column, text};
use iced::{Alignment, Element, Length}; use iced::{Alignment, Element, Length};
pub fn main() -> iced::Result { 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) .antialiasing(true)
.run() .run()
} }

View file

@ -4,7 +4,7 @@ use iced::{Element, Font, Length};
const ICON_FONT: Font = Font::with_name("icons"); const ICON_FONT: Font = Font::with_name("icons");
pub fn main() -> iced::Result { 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()) .font(include_bytes!("../fonts/icons.ttf").as_slice())
.run() .run()
} }

View file

@ -8,7 +8,7 @@ use iced::{
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application("Clock - Iced", Clock::update, Clock::view) iced::program("Clock - Iced", Clock::update, Clock::view)
.subscription(Clock::subscription) .subscription(Clock::subscription)
.theme(Clock::theme) .theme(Clock::theme)
.antialiasing(true) .antialiasing(true)

View file

@ -13,7 +13,7 @@ use std::marker::PhantomData;
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application( iced::program(
"Color Palette - Iced", "Color Palette - Iced",
ColorPalette::update, ColorPalette::update,
ColorPalette::view, ColorPalette::view,

View file

@ -9,13 +9,9 @@ use iced::window;
use iced::{Alignment, Color, Element, Length, Subscription}; use iced::{Alignment, Color, Element, Length, Subscription};
fn main() -> iced::Result { fn main() -> iced::Result {
iced::application( iced::program("Custom Shader - Iced", IcedCubes::update, IcedCubes::view)
"Custom Shader - Iced", .subscription(IcedCubes::subscription)
IcedCubes::update, .run()
IcedCubes::view,
)
.subscription(IcedCubes::subscription)
.run()
} }
struct IcedCubes { struct IcedCubes {

View file

@ -4,13 +4,9 @@ use iced::widget::{button, column, container, progress_bar, text, Column};
use iced::{Alignment, Element, Length, Subscription}; use iced::{Alignment, Element, Length, Subscription};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application( iced::program("Download Progress - Iced", Example::update, Example::view)
"Download Progress - Iced", .subscription(Example::subscription)
Example::update, .run()
Example::view,
)
.subscription(Example::subscription)
.run()
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -5,7 +5,7 @@ use iced::window;
use iced::{Alignment, Command, Element, Length, Subscription}; use iced::{Alignment, Command, Element, Length, Subscription};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application("Events - Iced", Events::update, Events::view) iced::program("Events - Iced", Events::update, Events::view)
.subscription(Events::subscription) .subscription(Events::subscription)
.exit_on_close_request(false) .exit_on_close_request(false)
.run() .run()

View file

@ -3,7 +3,7 @@ use iced::window;
use iced::{Alignment, Command, Element, Length}; use iced::{Alignment, Command, Element, Length};
pub fn main() -> iced::Result { 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)] #[derive(Default)]

View file

@ -8,7 +8,7 @@ use iced::{Alignment, Color, Element, Length, Radians, Theme};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::application("Gradient - Iced", Gradient::update, Gradient::view) iced::program("Gradient - Iced", Gradient::update, Gradient::view)
.style(Gradient::style) .style(Gradient::style)
.transparent(true) .transparent(true)
.run() .run()

View file

@ -10,7 +10,7 @@ use iced::{
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application(Layout::title, Layout::update, Layout::view) iced::program(Layout::title, Layout::update, Layout::view)
.subscription(Layout::subscription) .subscription(Layout::subscription)
.theme(Layout::theme) .theme(Layout::theme)
.run() .run()

View file

@ -11,7 +11,7 @@ use circular::Circular;
use linear::Linear; use linear::Linear;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application( iced::program(
"Loading Spinners - Iced", "Loading Spinners - Iced",
LoadingSpinners::update, LoadingSpinners::update,
LoadingSpinners::view, LoadingSpinners::view,

View file

@ -13,7 +13,7 @@ use std::collections::HashMap;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::application("Multitouch - Iced", Multitouch::update, Multitouch::view) iced::program("Multitouch - Iced", Multitouch::update, Multitouch::view)
.antialiasing(true) .antialiasing(true)
.centered() .centered()
.run() .run()

View file

@ -7,7 +7,7 @@ use iced::widget::{
use iced::{Color, Element, Length, Size, Subscription}; use iced::{Color, Element, Length, Size, Subscription};
pub fn main() -> iced::Result { 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) .subscription(Example::subscription)
.run() .run()
} }

View file

@ -3,7 +3,7 @@ use iced::widget::{self, column, container, image, row, text};
use iced::{Alignment, Command, Element, Length}; use iced::{Alignment, Command, Element, Length};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application(Pokedex::title, Pokedex::update, Pokedex::view) iced::program(Pokedex::title, Pokedex::update, Pokedex::view)
.load(Pokedex::search) .load(Pokedex::search)
.run() .run()
} }

View file

@ -4,7 +4,7 @@ use iced::widget::{
use iced::{Alignment, Element, Length, Theme}; use iced::{Alignment, Element, Length, Theme};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application( iced::program(
"QR Code Generator - Iced", "QR Code Generator - Iced",
QRGenerator::update, QRGenerator::update,
QRGenerator::view, QRGenerator::view,

View file

@ -13,7 +13,7 @@ use ::image::ColorType;
fn main() -> iced::Result { fn main() -> iced::Result {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::application("Screenshot - Iced", Example::update, Example::view) iced::program("Screenshot - Iced", Example::update, Example::view)
.subscription(Example::subscription) .subscription(Example::subscription)
.run() .run()
} }

View file

@ -10,7 +10,7 @@ use once_cell::sync::Lazy;
static SCROLLABLE_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique); static SCROLLABLE_ID: Lazy<scrollable::Id> = Lazy::new(scrollable::Id::unique);
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application( iced::program(
"Scrollable - Iced", "Scrollable - Iced",
ScrollableDemo::update, ScrollableDemo::update,
ScrollableDemo::view, ScrollableDemo::view,

View file

@ -8,7 +8,7 @@ use rand::Rng;
use std::fmt::Debug; use std::fmt::Debug;
fn main() -> iced::Result { fn main() -> iced::Result {
iced::application( iced::program(
"Sierpinski Triangle - Iced", "Sierpinski Triangle - Iced",
SierpinskiEmulator::update, SierpinskiEmulator::update,
SierpinskiEmulator::view, SierpinskiEmulator::view,

View file

@ -22,7 +22,7 @@ use std::time::Instant;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::application( iced::program(
"Solar System - Iced", "Solar System - Iced",
SolarSystem::update, SolarSystem::update,
SolarSystem::view, SolarSystem::view,

View file

@ -7,7 +7,7 @@ use iced::{Alignment, Element, Length, Subscription, Theme};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application("Stopwatch - Iced", Stopwatch::update, Stopwatch::view) iced::program("Stopwatch - Iced", Stopwatch::update, Stopwatch::view)
.subscription(Stopwatch::subscription) .subscription(Stopwatch::subscription)
.theme(Stopwatch::theme) .theme(Stopwatch::theme)
.run() .run()

View file

@ -6,7 +6,7 @@ use iced::widget::{
use iced::{Alignment, Element, Length, Theme}; use iced::{Alignment, Element, Length, Theme};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application("Styling - Iced", Styling::update, Styling::view) iced::program("Styling - Iced", Styling::update, Styling::view)
.theme(Styling::theme) .theme(Styling::theme)
.run() .run()
} }

View file

@ -2,12 +2,8 @@ use iced::widget::{button, column, container, text};
use iced::{system, Command, Element, Length}; use iced::{system, Command, Element, Length};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application( iced::program("System Information - Iced", Example::update, Example::view)
"System Information - Iced", .run()
Example::update,
Example::view,
)
.run()
} }
#[derive(Default)] #[derive(Default)]

View file

@ -16,7 +16,7 @@ pub fn main() -> iced::Result {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::application(Tour::title, Tour::update, Tour::view) iced::program(Tour::title, Tour::update, Tour::view)
.centered() .centered()
.run() .run()
} }

View file

@ -3,7 +3,7 @@ use iced::widget::{container, text};
use iced::{Element, Length, Subscription}; use iced::{Element, Length, Subscription};
pub fn main() -> iced::Result { 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) .subscription(App::subscription)
.run() .run()
} }

View file

@ -6,7 +6,7 @@ use iced::widget::{
use iced::{Element, Length, Point, Rectangle, Renderer, Theme, Vector}; use iced::{Element, Length, Point, Rectangle, Renderer, Theme, Vector};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application( iced::program(
"Vectorial Text - Iced", "Vectorial Text - Iced",
VectorialText::update, VectorialText::update,
VectorialText::view, VectorialText::view,

View file

@ -1,7 +1,10 @@
//! Build interactive cross-platform applications. //! 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::shell::application;
use crate::{Command, Element, Executor, Settings, Subscription};
pub use application::{default, Appearance, DefaultStyle}; pub use application::{default, Appearance, DefaultStyle};

View file

@ -1,13 +1,3 @@
//! 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 //! # Example
//! ```no_run //! ```no_run
@ -15,7 +5,7 @@
//! use iced::Theme; //! use iced::Theme;
//! //!
//! pub fn main() -> iced::Result { //! pub fn main() -> iced::Result {
//! iced::application("A counter", update, view) //! iced::program("A counter", update, view)
//! .theme(|_| Theme::Dark) //! .theme(|_| Theme::Dark)
//! .centered() //! .centered()
//! .run() //! .run()
@ -53,7 +43,7 @@ use std::borrow::Cow;
/// use iced::widget::{button, column, text, Column}; /// use iced::widget::{button, column, text, Column};
/// ///
/// pub fn main() -> iced::Result { /// pub fn main() -> iced::Result {
/// iced::application("A counter", update, view).run() /// iced::program("A counter", update, view).run()
/// } /// }
/// ///
/// #[derive(Debug, Clone)] /// #[derive(Debug, Clone)]
@ -74,7 +64,7 @@ use std::borrow::Cow;
/// ] /// ]
/// } /// }
/// ``` /// ```
pub fn application<State, Message>( pub fn program<State, Message>(
title: impl Title<State>, title: impl Title<State>,
update: impl Update<State, Message>, update: impl Update<State, Message>,
view: impl for<'a> self::View<'a, State, Message>, view: impl for<'a> self::View<'a, State, Message>,
@ -139,14 +129,18 @@ where
.title(title) .title(title)
} }
/// A fully functioning and configured iced application. /// The underlying definition and configuration of an iced [`Application`].
/// ///
/// It can be [`run`]! /// 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.
/// ///
/// Create one with the [`application`] helper. /// 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 /// [`run`]: Program::run
/// [`application`]: self::application()
#[derive(Debug)] #[derive(Debug)]
pub struct Program<P: Definition> { pub struct Program<P: Definition> {
raw: P, raw: P,
@ -154,7 +148,7 @@ pub struct Program<P: Definition> {
} }
impl<P: Definition> Program<P> { impl<P: Definition> Program<P> {
/// Runs the [`Program`]. /// Runs the underlying [`Application`] of the [`Program`].
pub fn run(self) -> Result pub fn run(self) -> Result
where where
Self: 'static, Self: 'static,
@ -364,9 +358,7 @@ impl<P: Definition> Program<P> {
/// The internal definition of a [`Program`]. /// The internal definition of a [`Program`].
/// ///
/// You should not need to implement this trait directly. Instead, use the /// You should not need to implement this trait directly. Instead, use the
/// helper functions available in the [`program`] module and the [`Program`] struct. /// methods available in the [`Program`] struct.
///
/// [`program`]: crate::program
#[allow(missing_docs)] #[allow(missing_docs)]
pub trait Definition: Sized { pub trait Definition: Sized {
/// The state of the program. /// The state of the program.
@ -748,6 +740,8 @@ fn with_style<P: Definition>(
/// ///
/// This trait is implemented both for `&static str` and /// This trait is implemented both for `&static str` and
/// any closure `Fn(&State) -> String`. /// any closure `Fn(&State) -> String`.
///
/// This trait allows the [`program`] builder to take any of them.
pub trait Title<State> { pub trait Title<State> {
/// Produces the title of the [`Program`]. /// Produces the title of the [`Program`].
fn title(&self, state: &State) -> String; fn title(&self, state: &State) -> String;
@ -770,10 +764,8 @@ where
/// The update logic of some [`Program`]. /// The update logic of some [`Program`].
/// ///
/// This trait allows [`application`] to take any closure that /// This trait allows the [`program`] builder to take any closure that
/// returns any `Into<Command<Message>>`. /// returns any `Into<Command<Message>>`.
///
/// [`application`]: self::application()
pub trait Update<State, Message> { pub trait Update<State, Message> {
/// Processes the message and updates the state of the [`Program`]. /// Processes the message and updates the state of the [`Program`].
fn update( fn update(
@ -799,10 +791,8 @@ where
/// The view logic of some [`Program`]. /// The view logic of some [`Program`].
/// ///
/// This trait allows [`application`] to take any closure that /// This trait allows the [`program`] builder to take any closure that
/// returns any `Into<Element<'_, Message>>`. /// returns any `Into<Element<'_, Message>>`.
///
/// [`application`]: self::application()
pub trait View<'a, State, Message> { pub trait View<'a, State, Message> {
/// Produces the widget of the [`Program`]. /// Produces the widget of the [`Program`].
fn view(&self, state: &'a State) -> impl Into<Element<'a, Message>>; fn view(&self, state: &'a State) -> impl Into<Element<'a, Message>>;

View file

@ -157,12 +157,11 @@
//! 1. Draw the resulting user interface. //! 1. Draw the resulting user interface.
//! //!
//! # Usage //! # 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. //! trait directly.
//! //!
//! [Elm]: https://elm-lang.org/ //! [Elm]: https://elm-lang.org/
//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ //! [The Elm Architecture]: https://guide.elm-lang.org/architecture/
//! [`application`]: application()
#![doc( #![doc(
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" 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; mod error;
pub mod application; pub mod application;
pub mod program;
pub mod settings; pub mod settings;
pub mod time; pub mod time;
pub mod window; pub mod window;
@ -323,7 +321,6 @@ pub use error::Error;
pub use event::Event; pub use event::Event;
pub use executor::Executor; pub use executor::Executor;
pub use font::Font; pub use font::Font;
pub use program::Program;
pub use renderer::Renderer; pub use renderer::Renderer;
pub use settings::Settings; pub use settings::Settings;
pub use subscription::Subscription; pub use subscription::Subscription;
@ -375,16 +372,16 @@ pub type Result = std::result::Result<(), Error>;
/// } /// }
/// ``` /// ```
pub fn run<State, Message>( pub fn run<State, Message>(
title: impl program::Title<State> + 'static, title: impl application::Title<State> + 'static,
update: impl program::Update<State, Message> + 'static, update: impl application::Update<State, Message> + 'static,
view: impl for<'a> program::View<'a, State, Message> + 'static, view: impl for<'a> application::View<'a, State, Message> + 'static,
) -> Result ) -> Result
where where
State: Default + 'static, State: Default + 'static,
Message: std::fmt::Debug + Send + 'static, Message: std::fmt::Debug + Send + 'static,
{ {
application(title, update, view).run() program(title, update, view).run()
} }
#[doc(inline)] #[doc(inline)]
pub use program::application; pub use application::program;