Introduce Program API

This commit is contained in:
Héctor Ramón Jiménez 2024-03-16 05:33:47 +01:00
parent 0524e9b457
commit c22269bff3
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
43 changed files with 1141 additions and 831 deletions

View file

@ -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<Message> {
/// column![
/// text(value),
/// button("+").on_press(Message::Increment),
/// ]
/// }
/// ```
pub fn run<State, Message>(
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};