Move Application trait to advanced module

This commit is contained in:
Héctor Ramón Jiménez 2024-03-17 19:38:42 +01:00
parent 943b6c9657
commit cdb18e610a
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
6 changed files with 48 additions and 51 deletions

View file

@ -1,5 +1,5 @@
use iced::application;
use iced::gradient; use iced::gradient;
use iced::program;
use iced::widget::{ use iced::widget::{
checkbox, column, container, horizontal_space, row, slider, text, checkbox, column, container, horizontal_space, row, slider, text,
}; };
@ -95,14 +95,16 @@ impl Gradient {
.into() .into()
} }
fn style(&self, theme: &Theme) -> application::Appearance { fn style(&self, theme: &Theme) -> program::Appearance {
use program::DefaultStyle;
if self.transparent { if self.transparent {
application::Appearance { program::Appearance {
background_color: Color::TRANSPARENT, background_color: Color::TRANSPARENT,
text_color: theme.palette().text, text_color: theme.palette().text,
} }
} else { } else {
application::default(theme) Theme::default_style(theme)
} }
} }
} }

View file

@ -1,4 +1,5 @@
//! Leverage advanced concepts like custom widgets. //! Leverage advanced concepts like custom widgets.
pub use crate::application::Application;
pub use crate::core::clipboard::{self, Clipboard}; pub use crate::core::clipboard::{self, Clipboard};
pub use crate::core::image; pub use crate::core::image;
pub use crate::core::layout::{self, Layout}; pub use crate::core::layout::{self, Layout};

View file

@ -1,12 +1,8 @@
//! Build interactive cross-platform applications. //! Build interactive cross-platform applications.
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}; use crate::{Command, Element, Executor, Settings, Subscription};
pub use application::{default, Appearance, DefaultStyle}; pub use application::{Appearance, DefaultStyle};
/// An interactive cross-platform application. /// An interactive cross-platform application.
/// ///
@ -62,8 +58,9 @@ pub use application::{default, Appearance, DefaultStyle};
/// says "Hello, world!": /// says "Hello, world!":
/// ///
/// ```no_run /// ```no_run
/// use iced::advanced::Application;
/// use iced::executor; /// use iced::executor;
/// use iced::{Application, Command, Element, Settings, Theme}; /// use iced::{Command, Element, Settings, Theme};
/// ///
/// pub fn main() -> iced::Result { /// pub fn main() -> iced::Result {
/// Hello::run(Settings::default()) /// Hello::run(Settings::default())

View file

@ -157,11 +157,11 @@
//! 1. Draw the resulting user interface. //! 1. Draw the resulting user interface.
//! //!
//! # Usage //! # Usage
//! You can either use the [`program`] builder or implement the [`Application`] //! Use [`run`] or the [`program`] builder.
//! 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/
//! [`program`]: program()
#![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"
)] )]
@ -185,9 +185,10 @@ pub use iced_futures::futures;
#[cfg(feature = "highlighter")] #[cfg(feature = "highlighter")]
pub use iced_highlighter as highlighter; pub use iced_highlighter as highlighter;
mod application;
mod error; mod error;
pub mod application; pub mod program;
pub mod settings; pub mod settings;
pub mod time; pub mod time;
pub mod window; pub mod window;
@ -315,12 +316,12 @@ pub mod widget {
mod runtime {} mod runtime {}
} }
pub use application::Application;
pub use command::Command; pub use command::Command;
pub use error::Error; 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;
@ -335,9 +336,7 @@ pub type Element<
Renderer = crate::Renderer, Renderer = crate::Renderer,
> = crate::core::Element<'a, Message, Theme, Renderer>; > = crate::core::Element<'a, Message, Theme, Renderer>;
/// The result of running an [`Application`]. /// The result of running a [`Program`].
///
/// [`Application`]: crate::Application
pub type Result = std::result::Result<(), Error>; pub type Result = std::result::Result<(), Error>;
/// Runs a basic iced application with default [`Settings`] given its title, /// 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`]. /// This is equivalent to chaining [`program`] with [`Program::run`].
/// ///
/// [`Program::run`]: application::Program::run /// [`program`]: program()
/// ///
/// # Example /// # Example
/// ```no_run /// ```no_run
@ -374,17 +373,17 @@ pub type Result = std::result::Result<(), Error>;
/// } /// }
/// ``` /// ```
pub fn run<State, Message, Theme>( pub fn run<State, Message, Theme>(
title: impl application::Title<State> + 'static, title: impl program::Title<State> + 'static,
update: impl application::Update<State, Message> + 'static, update: impl program::Update<State, Message> + 'static,
view: impl for<'a> application::View<'a, State, Message, Theme> + 'static, view: impl for<'a> program::View<'a, State, Message, Theme> + 'static,
) -> Result ) -> Result
where where
State: Default + 'static, State: Default + 'static,
Message: std::fmt::Debug + Send + 'static, Message: std::fmt::Debug + Send + 'static,
Theme: Default + application::DefaultStyle + 'static, Theme: Default + program::DefaultStyle + 'static,
{ {
program(title, update, view).run() program(title, update, view).run()
} }
#[doc(inline)] #[doc(inline)]
pub use application::program; pub use program::program;

View file

@ -1,3 +1,4 @@
//! Create and run iced applications step by step.
//! //!
//! # Example //! # Example
//! ```no_run //! ```no_run
@ -29,11 +30,13 @@
//! ] //! ]
//! } //! }
//! ``` //! ```
use crate::application::{self, Application}; use crate::application::Application;
use crate::executor::{self, Executor}; use crate::executor::{self, Executor};
use crate::window; use crate::window;
use crate::{Command, Element, Font, Result, Settings, Size, Subscription}; use crate::{Command, Element, Font, Result, Settings, Size, Subscription};
pub use crate::application::{Appearance, DefaultStyle};
use std::borrow::Cow; use std::borrow::Cow;
/// Creates an iced [`Program`] given its title, update, and view logic. /// Creates an iced [`Program`] given its title, update, and view logic.
@ -72,7 +75,7 @@ pub fn program<State, Message, Theme>(
where where
State: 'static, State: 'static,
Message: Send + std::fmt::Debug, Message: Send + std::fmt::Debug,
Theme: Default + application::DefaultStyle, Theme: Default + DefaultStyle,
{ {
use std::marker::PhantomData; use std::marker::PhantomData;
@ -88,7 +91,7 @@ where
for Application<State, Message, Theme, Update, View> for Application<State, Message, Theme, Update, View>
where where
Message: Send + std::fmt::Debug, Message: Send + std::fmt::Debug,
Theme: Default + application::DefaultStyle, Theme: Default + DefaultStyle,
Update: self::Update<State, Message>, Update: self::Update<State, Message>,
View: for<'a> self::View<'a, State, Message, Theme>, View: for<'a> self::View<'a, State, Message, Theme>,
{ {
@ -130,15 +133,12 @@ where
.title(title) .title(title)
} }
/// The underlying definition and configuration of an iced [`Application`]. /// The underlying definition and configuration of an iced application.
/// ///
/// You can use this API to create and run iced applications /// You can use this API to create and run iced applications
/// step by step—without coupling your logic to a trait /// step by step—without coupling your logic to a trait
/// or a specific type. /// 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. /// You can create a [`Program`] with the [`program`] helper.
/// ///
/// [`run`]: Program::run /// [`run`]: Program::run
@ -229,7 +229,7 @@ impl<P: Definition> Program<P> {
self.program.theme(&self.state) self.program.theme(&self.state)
} }
fn style(&self, theme: &Self::Theme) -> application::Appearance { fn style(&self, theme: &Self::Theme) -> Appearance {
self.program.style(&self.state, theme) self.program.style(&self.state, theme)
} }
} }
@ -391,7 +391,7 @@ impl<P: Definition> Program<P> {
/// Sets the style logic of the [`Program`]. /// Sets the style logic of the [`Program`].
pub fn style( pub fn style(
self, self,
f: impl Fn(&P::State, &P::Theme) -> application::Appearance, f: impl Fn(&P::State, &P::Theme) -> Appearance,
) -> Program< ) -> Program<
impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>, impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>,
> { > {
@ -415,7 +415,7 @@ pub trait Definition: Sized {
type Message: Send + std::fmt::Debug; type Message: Send + std::fmt::Debug;
/// The theme of the program. /// The theme of the program.
type Theme: Default + application::DefaultStyle; type Theme: Default + DefaultStyle;
/// The executor of the program. /// The executor of the program.
type Executor: Executor; type Executor: Executor;
@ -448,12 +448,8 @@ pub trait Definition: Sized {
Self::Theme::default() Self::Theme::default()
} }
fn style( fn style(&self, _state: &Self::State, theme: &Self::Theme) -> Appearance {
&self, DefaultStyle::default_style(theme)
_state: &Self::State,
theme: &Self::Theme,
) -> application::Appearance {
application::DefaultStyle::default_style(theme)
} }
} }
@ -514,7 +510,7 @@ fn with_title<P: Definition>(
&self, &self,
state: &Self::State, state: &Self::State,
theme: &Self::Theme, theme: &Self::Theme,
) -> application::Appearance { ) -> Appearance {
self.program.style(state, theme) self.program.style(state, theme)
} }
} }
@ -578,7 +574,7 @@ fn with_load<P: Definition>(
&self, &self,
state: &Self::State, state: &Self::State,
theme: &Self::Theme, theme: &Self::Theme,
) -> application::Appearance { ) -> Appearance {
self.program.style(state, theme) self.program.style(state, theme)
} }
} }
@ -642,7 +638,7 @@ fn with_subscription<P: Definition>(
&self, &self,
state: &Self::State, state: &Self::State,
theme: &Self::Theme, theme: &Self::Theme,
) -> application::Appearance { ) -> Appearance {
self.program.style(state, theme) self.program.style(state, theme)
} }
} }
@ -709,7 +705,7 @@ fn with_theme<P: Definition>(
&self, &self,
state: &Self::State, state: &Self::State,
theme: &Self::Theme, theme: &Self::Theme,
) -> application::Appearance { ) -> Appearance {
self.program.style(state, theme) self.program.style(state, theme)
} }
} }
@ -719,7 +715,7 @@ fn with_theme<P: Definition>(
fn with_style<P: Definition>( fn with_style<P: Definition>(
program: P, program: P,
f: impl Fn(&P::State, &P::Theme) -> application::Appearance, f: impl Fn(&P::State, &P::Theme) -> Appearance,
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> { ) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithStyle<P, F> { struct WithStyle<P, F> {
program: P, program: P,
@ -728,7 +724,7 @@ fn with_style<P: Definition>(
impl<P: Definition, F> Definition for WithStyle<P, F> impl<P: Definition, F> Definition for WithStyle<P, F>
where where
F: Fn(&P::State, &P::Theme) -> application::Appearance, F: Fn(&P::State, &P::Theme) -> Appearance,
{ {
type State = P::State; type State = P::State;
type Message = P::Message; type Message = P::Message;
@ -739,7 +735,7 @@ fn with_style<P: Definition>(
&self, &self,
state: &Self::State, state: &Self::State,
theme: &Self::Theme, theme: &Self::Theme,
) -> application::Appearance { ) -> Appearance {
(self.style)(state, theme) (self.style)(state, theme)
} }

View file

@ -4,7 +4,9 @@ use crate::{Font, Pixels};
use std::borrow::Cow; use std::borrow::Cow;
/// The settings of an application. /// The settings of an iced [`Program`].
///
/// [`Program`]: crate::Program
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Settings<Flags = ()> { pub struct Settings<Flags = ()> {
/// The identifier of the application. /// The identifier of the application.
@ -18,9 +20,9 @@ pub struct Settings<Flags = ()> {
/// They will be ignored on the Web. /// They will be ignored on the Web.
pub window: window::Settings, 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, pub flags: Flags,
/// The fonts to load on boot. /// The fonts to load on boot.
@ -49,9 +51,9 @@ pub struct Settings<Flags = ()> {
} }
impl<Flags> Settings<Flags> { impl<Flags> Settings<Flags> {
/// 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 { pub fn with_flags(flags: Flags) -> Self {
let default_settings = Settings::<()>::default(); let default_settings = Settings::<()>::default();