From c22269bff3085012d326a0df77bf27ad5bcb41b7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 05:33:47 +0100 Subject: Introduce `Program` API --- src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index c596f2a6..f42a845e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -175,6 +175,7 @@ mod error; mod sandbox; pub mod application; +pub mod program; pub mod settings; pub mod time; pub mod window; @@ -308,6 +309,7 @@ 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 sandbox::Sandbox; pub use settings::Settings; @@ -327,3 +329,49 @@ pub type Element< /// /// [`Application`]: crate::Application pub type Result = std::result::Result<(), Error>; + +/// Runs a basic iced application with default [`Settings`] given +/// - its window title, +/// - its update logic, +/// - and its view logic. +/// +/// # Example +/// ```no_run +/// use iced::widget::{button, column, text, Column}; +/// +/// pub fn main() -> iced::Result { +/// iced::run("A counter", update, view) +/// } +/// +/// #[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 run( + title: &'static str, + update: impl Fn(&mut State, Message) + 'static, + view: impl for<'a> program::View<'a, State, Message> + 'static, +) -> Result +where + State: Default + 'static, + Message: std::fmt::Debug + Send + 'static, +{ + sandbox(update, view).title(title).run() +} + +#[doc(inline)] +pub use program::{application, sandbox}; -- cgit From 93ae790da14544667176ecdbdd6a4eaaa98a248a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 15:53:03 +0100 Subject: Implement `Program::load` to specify startup `Command` --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index f42a845e..19815f0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -362,7 +362,7 @@ pub type Result = std::result::Result<(), Error>; /// } /// ``` pub fn run( - title: &'static str, + title: impl program::Title + 'static, update: impl Fn(&mut State, Message) + 'static, view: impl for<'a> program::View<'a, State, Message> + 'static, ) -> Result -- cgit From bb71e8481ed59f991b9bd9dc55ea7e011ba0aac6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 16:12:07 +0100 Subject: Make `sandbox` helper take a `title` as well --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 19815f0f..cda5341c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -370,7 +370,7 @@ where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, { - sandbox(update, view).title(title).run() + sandbox(title, update, view).run() } #[doc(inline)] -- cgit From 28a27f08edccd53e06ad693e63b0a62dae921da5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 16 Mar 2024 19:14:13 +0100 Subject: Remove `sandbox` by making `application` more generic :tada: --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index cda5341c..ae6bb344 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -363,15 +363,15 @@ pub type Result = std::result::Result<(), Error>; /// ``` pub fn run( title: impl program::Title + 'static, - update: impl Fn(&mut State, Message) + 'static, + update: impl program::Update + 'static, view: impl for<'a> program::View<'a, State, Message> + 'static, ) -> Result where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, { - sandbox(title, update, view).run() + application(title, update, view).run() } #[doc(inline)] -pub use program::{application, sandbox}; +pub use program::application; -- cgit From 846d76cd3f3f2faae5efbb3fda2a2bcb3b064481 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 13:46:52 +0100 Subject: Remove `Sandbox` trait :tada: --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index ae6bb344..060cc6c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,7 +172,6 @@ pub use iced_futures::futures; pub use iced_highlighter as highlighter; mod error; -mod sandbox; pub mod application; pub mod program; @@ -311,7 +310,6 @@ pub use executor::Executor; pub use font::Font; pub use program::Program; pub use renderer::Renderer; -pub use sandbox::Sandbox; pub use settings::Settings; pub use subscription::Subscription; -- cgit From 179e8863b3c7a1f056eef5e06fbf4f3796a641ba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 13:55:15 +0100 Subject: Fix broken intra-doc links to `Sandbox` --- src/lib.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 060cc6c5..1f2b8c93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,8 +134,15 @@ //! } //! ``` //! -//! And that's everything! We just wrote a whole user interface. Iced is now -//! able to: +//! And that's everything! We just wrote a whole user interface. Let's run it: +//! +//! ```no_run +//! fn main() -> iced::Result { +//! iced::run("A cool counter", Counter::update, Counter::view) +//! } +//! ``` +//! +//! Iced will automatically: //! //! 1. Take the result of our __view logic__ and layout its widgets. //! 1. Process events from our system and produce __messages__ for our @@ -143,11 +150,12 @@ //! 1. Draw the resulting user interface. //! //! # Usage -//! The [`Application`] and [`Sandbox`] traits should get you started quickly, -//! streamlining all the process described above! +//! You can either use the [`application`] 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" )] -- cgit From 7e1ef7d150aa3d4d05942eea2706348f20d61d64 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 13:57:53 +0100 Subject: Fix new doc test in root module --- src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 1f2b8c93..c2177484 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,6 +137,13 @@ //! And that's everything! We just wrote a whole user interface. Let's run it: //! //! ```no_run +//! # #[derive(Default)] +//! # struct Counter; +//! # impl Counter { +//! # fn update(&mut self, _message: ()) {} +//! # fn view(&self) -> iced::Element<()> { unimplemented!() } +//! # } +//! # //! fn main() -> iced::Result { //! iced::run("A cool counter", Counter::update, Counter::view) //! } -- cgit From 54f44754eb216d4b2c08cd2a7c3582f1dc295205 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:16:38 +0100 Subject: Move `Program` to `application` module --- src/lib.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'src/lib.rs') 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; -- cgit From 8e1d0b51f13a5561cd20b24be9cc06bc317ea601 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:29:02 +0100 Subject: Fix documentation of `run` function --- src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index def020e0..902a5f36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -340,10 +340,8 @@ pub type Element< /// [`Application`]: crate::Application pub type Result = std::result::Result<(), Error>; -/// Runs a basic iced application with default [`Settings`] given -/// - its window title, -/// - its update logic, -/// - and its view logic. +/// Runs a basic iced application with default [`Settings`] given its title, +/// update, and view logic. /// /// # Example /// ```no_run -- cgit From a034e40f7c0e325938da92894ee34f589f372e0a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:36:42 +0100 Subject: Clarify chain nature of `run` function --- src/lib.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 902a5f36..d238e78a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,6 +343,10 @@ pub type Result = std::result::Result<(), Error>; /// Runs a basic iced application with default [`Settings`] given its title, /// update, and view logic. /// +/// This is equivalent to chaining [`program`] with [`Program::run`]. +/// +/// [`Program::run`]: application::Program::run +/// /// # Example /// ```no_run /// use iced::widget::{button, column, text, Column}; -- cgit From c4b4207f4768c7e254ff0a6bf95c4d76ea08ce48 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 14:41:34 +0100 Subject: Support custom themes in `Program` API --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index d238e78a..bc87b1a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -373,14 +373,15 @@ pub type Result = std::result::Result<(), Error>; /// ] /// } /// ``` -pub fn run( +pub fn run( title: impl application::Title + 'static, update: impl application::Update + 'static, - view: impl for<'a> application::View<'a, State, Message> + 'static, + view: impl for<'a> application::View<'a, State, Message, Theme> + 'static, ) -> Result where State: Default + 'static, Message: std::fmt::Debug + Send + 'static, + Theme: Default + application::DefaultStyle + 'static, { program(title, update, view).run() } -- cgit From cdb18e610a72b4a025d7e1890140393adee5b087 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 17 Mar 2024 19:38:42 +0100 Subject: Move `Application` trait to `advanced` module --- src/lib.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src/lib.rs') 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; -- cgit From f409037c0766016a9bd22abde5da14406e74959a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Mar 2024 19:15:25 +0100 Subject: Simplify message names in overview docs --- src/lib.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 49447418..0e9566e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,8 +63,8 @@ //! ``` //! #[derive(Debug, Clone, Copy)] //! pub enum Message { -//! IncrementPressed, -//! DecrementPressed, +//! Increment, +//! Decrement, //! } //! ``` //! @@ -79,8 +79,8 @@ //! # //! # #[derive(Debug, Clone, Copy)] //! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, +//! # Increment, +//! # Decrement, //! # } //! # //! use iced::widget::{button, column, text, Column}; @@ -90,15 +90,15 @@ //! // We use a column: a simple vertical layout //! column![ //! // The increment button. We tell it to produce an -//! // `IncrementPressed` message when pressed -//! button("+").on_press(Message::IncrementPressed), +//! // `Increment` message when pressed +//! button("+").on_press(Message::Increment), //! //! // We show the value of the counter here //! text(self.value).size(50), //! //! // The decrement button. We tell it to produce a -//! // `DecrementPressed` message when pressed -//! button("-").on_press(Message::DecrementPressed), +//! // `Decrement` message when pressed +//! button("-").on_press(Message::Decrement), //! ] //! } //! } @@ -115,18 +115,18 @@ //! # //! # #[derive(Debug, Clone, Copy)] //! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, +//! # Increment, +//! # Decrement, //! # } //! impl Counter { //! // ... //! //! pub fn update(&mut self, message: Message) { //! match message { -//! Message::IncrementPressed => { +//! Message::Increment => { //! self.value += 1; //! } -//! Message::DecrementPressed => { +//! Message::Decrement => { //! self.value -= 1; //! } //! } -- cgit