Merge branch 'master' into beacon

This commit is contained in:
Héctor Ramón Jiménez 2024-05-09 12:32:25 +02:00
commit aaf396256e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
284 changed files with 18747 additions and 15450 deletions

View file

@ -1,4 +1,5 @@
//! Leverage advanced concepts like custom widgets.
pub use crate::application::Application;
pub use crate::core::clipboard::{self, Clipboard};
pub use crate::core::image;
pub use crate::core::layout::{self, Layout};
@ -8,11 +9,13 @@ pub use crate::core::renderer::{self, Renderer};
pub use crate::core::svg;
pub use crate::core::text::{self, Text};
pub use crate::core::widget::{self, Widget};
pub use crate::core::{Hasher, Shell};
pub use crate::core::Shell;
pub use crate::renderer::graphics;
pub use iced_debug as debug;
pub mod subscription {
//! Write your own subscriptions.
pub use crate::runtime::futures::subscription::{EventStream, Recipe};
pub use crate::runtime::futures::subscription::{
EventStream, Hasher, Recipe,
};
}

View file

@ -1,7 +1,10 @@
//! Build interactive cross-platform applications.
use crate::core::text;
use crate::graphics::compositor;
use crate::shell::application;
use crate::{Command, Element, Executor, Settings, Subscription};
pub use crate::style::application::{Appearance, StyleSheet};
pub use application::{Appearance, DefaultStyle};
/// An interactive cross-platform application.
///
@ -13,9 +16,7 @@ pub use crate::style::application::{Appearance, StyleSheet};
/// document.
///
/// An [`Application`] can execute asynchronous actions by returning a
/// [`Command`] in some of its methods. If you do not intend to perform any
/// background work in your program, the [`Sandbox`] trait offers a simplified
/// interface.
/// [`Command`] in some of its methods.
///
/// When using an [`Application`] with the `debug` feature enabled, a debug view
/// can be toggled by pressing `F12`.
@ -59,8 +60,9 @@ pub use crate::style::application::{Appearance, StyleSheet};
/// says "Hello, world!":
///
/// ```no_run
/// use iced::advanced::Application;
/// use iced::executor;
/// use iced::{Application, Command, Element, Settings, Theme};
/// use iced::{Command, Element, Settings, Theme, Renderer};
///
/// pub fn main() -> iced::Result {
/// Hello::run(Settings::default())
@ -73,6 +75,7 @@ pub use crate::style::application::{Appearance, StyleSheet};
/// type Flags = ();
/// type Message = ();
/// type Theme = Theme;
/// type Renderer = Renderer;
///
/// fn new(_flags: ()) -> (Hello, Command<Self::Message>) {
/// (Hello, Command::none())
@ -91,7 +94,10 @@ pub use crate::style::application::{Appearance, StyleSheet};
/// }
/// }
/// ```
pub trait Application: Sized {
pub trait Application: Sized
where
Self::Theme: DefaultStyle,
{
/// The [`Executor`] that will run commands and subscriptions.
///
/// The [default executor] can be a good starting point!
@ -104,7 +110,10 @@ pub trait Application: Sized {
type Message: std::fmt::Debug + Send;
/// The theme of your [`Application`].
type Theme: Default + StyleSheet;
type Theme: Default;
/// The renderer of your [`Application`].
type Renderer: text::Renderer + compositor::Default;
/// The data needed to initialize your [`Application`].
type Flags;
@ -139,7 +148,7 @@ pub trait Application: Sized {
/// Returns the widgets to display in the [`Application`].
///
/// These widgets can produce __messages__ based on user interaction.
fn view(&self) -> Element<'_, Self::Message, Self::Theme, crate::Renderer>;
fn view(&self) -> Element<'_, Self::Message, Self::Theme, Self::Renderer>;
/// Returns the current [`Theme`] of the [`Application`].
///
@ -148,11 +157,9 @@ pub trait Application: Sized {
Self::Theme::default()
}
/// Returns the current `Style` of the [`Theme`].
///
/// [`Theme`]: Self::Theme
fn style(&self) -> <Self::Theme as StyleSheet>::Style {
<Self::Theme as StyleSheet>::Style::default()
/// Returns the current [`Appearance`] of the [`Application`].
fn style(&self, theme: &Self::Theme) -> Appearance {
theme.default_style()
}
/// Returns the event [`Subscription`] for the current state of the
@ -194,7 +201,7 @@ pub trait Application: Sized {
Self: 'static,
{
#[allow(clippy::needless_update)]
let renderer_settings = crate::renderer::Settings {
let renderer_settings = crate::graphics::Settings {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
antialiasing: if settings.antialiasing {
@ -202,26 +209,30 @@ pub trait Application: Sized {
} else {
None
},
..crate::renderer::Settings::default()
..crate::graphics::Settings::default()
};
Ok(crate::shell::application::run::<
Instance<Self>,
Self::Executor,
crate::renderer::Compositor,
<Self::Renderer as compositor::Default>::Compositor,
>(settings.into(), renderer_settings)?)
}
}
struct Instance<A: Application>(A);
struct Instance<A>(A)
where
A: Application,
A::Theme: DefaultStyle;
impl<A> crate::runtime::Program for Instance<A>
where
A: Application,
A::Theme: DefaultStyle,
{
type Message = A::Message;
type Theme = A::Theme;
type Renderer = crate::Renderer;
type Renderer = A::Renderer;
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
@ -232,9 +243,10 @@ where
}
}
impl<A> crate::shell::Application for Instance<A>
impl<A> application::Application for Instance<A>
where
A: Application,
A::Theme: DefaultStyle,
{
type Flags = A::Flags;
@ -252,8 +264,8 @@ where
self.0.theme()
}
fn style(&self) -> <A::Theme as StyleSheet>::Style {
self.0.style()
fn style(&self, theme: &A::Theme) -> Appearance {
self.0.style(theme)
}
fn subscription(&self) -> Subscription<Self::Message> {

View file

@ -51,6 +51,7 @@
//! We start by modelling the __state__ of our application:
//!
//! ```
//! #[derive(Default)]
//! struct Counter {
//! // The counter value
//! value: i32,
@ -63,8 +64,8 @@
//! ```
//! #[derive(Debug, Clone, Copy)]
//! pub enum Message {
//! IncrementPressed,
//! DecrementPressed,
//! Increment,
//! Decrement,
//! }
//! ```
//!
@ -79,8 +80,8 @@
//! #
//! # #[derive(Debug, Clone, Copy)]
//! # pub enum Message {
//! # IncrementPressed,
//! # DecrementPressed,
//! # Increment,
//! # Decrement,
//! # }
//! #
//! use iced::widget::{button, column, text, Column};
@ -90,15 +91,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 +116,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;
//! }
//! }
@ -134,8 +135,22 @@
//! }
//! ```
//!
//! 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
//! # #[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)
//! }
//! ```
//!
//! 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,26 +158,18 @@
//! 1. Draw the resulting user interface.
//!
//! # Usage
//! The [`Application`] and [`Sandbox`] traits should get you started quickly,
//! streamlining all the process described above!
//! 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"
)]
#![forbid(rust_2018_idioms, unsafe_code)]
#![deny(
missing_debug_implementations,
missing_docs,
unused_results,
rustdoc::broken_intra_doc_links
)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(docsrs, feature(doc_cfg))]
use iced_widget::graphics;
use iced_widget::renderer;
use iced_widget::style;
use iced_winit as shell;
use iced_winit::core;
use iced_winit::runtime;
@ -172,10 +179,10 @@ pub use iced_futures::futures;
#[cfg(feature = "highlighter")]
pub use iced_highlighter as highlighter;
mod application;
mod error;
mod sandbox;
pub mod application;
pub mod program;
pub mod settings;
pub mod time;
pub mod window;
@ -186,16 +193,15 @@ pub mod advanced;
#[cfg(feature = "multi-window")]
pub mod multi_window;
pub use style::theme;
pub use crate::core::alignment;
pub use crate::core::border;
pub use crate::core::color;
pub use crate::core::gradient;
pub use crate::core::theme;
pub use crate::core::{
Alignment, Background, Border, Color, ContentFit, Degrees, Gradient,
Length, Padding, Pixels, Point, Radians, Rectangle, Shadow, Size,
Transformation, Vector,
Length, Padding, Pixels, Point, Radians, Rectangle, Rotation, Shadow, Size,
Theme, Transformation, Vector,
};
pub mod clipboard {
@ -304,17 +310,15 @@ 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 sandbox::Sandbox;
pub use settings::Settings;
pub use subscription::Subscription;
pub use theme::Theme;
/// A generic widget.
///
@ -326,7 +330,55 @@ 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,
/// update, and view logic.
///
/// This is equivalent to chaining [`program`] with [`Program::run`].
///
/// [`program`]: program()
///
/// # 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, Theme, Renderer>(
title: impl program::Title<State> + 'static,
update: impl program::Update<State, Message> + 'static,
view: impl for<'a> program::View<'a, State, Message, Theme, Renderer> + 'static,
) -> Result
where
State: Default + 'static,
Message: std::fmt::Debug + Send + 'static,
Theme: Default + program::DefaultStyle + 'static,
Renderer: program::Renderer + 'static,
{
program(title, update, view).run()
}
#[doc(inline)]
pub use program::program;

View file

@ -1,4 +1,254 @@
//! Leverage multi-window support in your application.
mod application;
use crate::window;
use crate::{Command, Element, Executor, Settings, Subscription};
pub use application::Application;
pub use crate::application::{Appearance, DefaultStyle};
/// An interactive cross-platform multi-window application.
///
/// This trait is the main entrypoint of Iced. Once implemented, you can run
/// your GUI application by simply calling [`run`](#method.run).
///
/// - On native platforms, it will run in its own windows.
/// - On the web, it will take control of the `<title>` and the `<body>` of the
/// document and display only the contents of the `window::Id::MAIN` window.
///
/// An [`Application`] can execute asynchronous actions by returning a
/// [`Command`] in some of its methods.
///
/// When using an [`Application`] with the `debug` feature enabled, a debug view
/// can be toggled by pressing `F12`.
///
/// # Examples
/// See the `examples/multi-window` example to see this multi-window `Application` trait in action.
///
/// ## A simple "Hello, world!"
///
/// If you just want to get started, here is a simple [`Application`] that
/// says "Hello, world!":
///
/// ```no_run
/// use iced::{executor, window};
/// use iced::{Command, Element, Settings, Theme};
/// use iced::multi_window::{self, Application};
///
/// pub fn main() -> iced::Result {
/// Hello::run(Settings::default())
/// }
///
/// struct Hello;
///
/// impl multi_window::Application for Hello {
/// type Executor = executor::Default;
/// type Flags = ();
/// type Message = ();
/// type Theme = Theme;
///
/// fn new(_flags: ()) -> (Hello, Command<Self::Message>) {
/// (Hello, Command::none())
/// }
///
/// fn title(&self, _window: window::Id) -> String {
/// String::from("A cool application")
/// }
///
/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
/// Command::none()
/// }
///
/// fn view(&self, _window: window::Id) -> Element<Self::Message> {
/// "Hello, world!".into()
/// }
/// }
/// ```
///
/// [`Sandbox`]: crate::Sandbox
pub trait Application: Sized
where
Self::Theme: DefaultStyle,
{
/// The [`Executor`] that will run commands and subscriptions.
///
/// The [default executor] can be a good starting point!
///
/// [`Executor`]: Self::Executor
/// [default executor]: crate::executor::Default
type Executor: Executor;
/// The type of __messages__ your [`Application`] will produce.
type Message: std::fmt::Debug + Send;
/// The theme of your [`Application`].
type Theme: Default;
/// The data needed to initialize your [`Application`].
type Flags;
/// Initializes the [`Application`] with the flags provided to
/// [`run`] as part of the [`Settings`].
///
/// Here is where you should return the initial state of your app.
///
/// Additionally, you can return a [`Command`] if you need to perform some
/// async action in the background on startup. This is useful if you want to
/// load state from a file, perform an initial HTTP request, etc.
///
/// [`run`]: Self::run
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
/// Returns the current title of the `window` of the [`Application`].
///
/// This title can be dynamic! The runtime will automatically update the
/// title of your window when necessary.
fn title(&self, window: window::Id) -> String;
/// Handles a __message__ and updates the state of the [`Application`].
///
/// This is where you define your __update logic__. All the __messages__,
/// produced by either user interactions or commands, will be handled by
/// this method.
///
/// Any [`Command`] returned will be executed immediately in the background.
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
/// Returns the widgets to display in the `window` of the [`Application`].
///
/// These widgets can produce __messages__ based on user interaction.
fn view(
&self,
window: window::Id,
) -> Element<'_, Self::Message, Self::Theme, crate::Renderer>;
/// Returns the current [`Theme`] of the `window` of the [`Application`].
///
/// [`Theme`]: Self::Theme
#[allow(unused_variables)]
fn theme(&self, window: window::Id) -> Self::Theme {
Self::Theme::default()
}
/// Returns the current `Style` of the [`Theme`].
///
/// [`Theme`]: Self::Theme
fn style(&self, theme: &Self::Theme) -> Appearance {
Self::Theme::default_style(theme)
}
/// Returns the event [`Subscription`] for the current state of the
/// application.
///
/// A [`Subscription`] will be kept alive as long as you keep returning it,
/// and the __messages__ produced will be handled by
/// [`update`](#tymethod.update).
///
/// By default, this method returns an empty [`Subscription`].
fn subscription(&self) -> Subscription<Self::Message> {
Subscription::none()
}
/// Returns the scale factor of the `window` of the [`Application`].
///
/// It can be used to dynamically control the size of the UI at runtime
/// (i.e. zooming).
///
/// For instance, a scale factor of `2.0` will make widgets twice as big,
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
#[allow(unused_variables)]
fn scale_factor(&self, window: window::Id) -> f64 {
1.0
}
/// Runs the multi-window [`Application`].
///
/// On native platforms, this method will take control of the current thread
/// until the [`Application`] exits.
///
/// On the web platform, this method __will NOT return__ unless there is an
/// [`Error`] during startup.
///
/// [`Error`]: crate::Error
fn run(settings: Settings<Self::Flags>) -> crate::Result
where
Self: 'static,
{
#[allow(clippy::needless_update)]
let renderer_settings = crate::graphics::Settings {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
antialiasing: if settings.antialiasing {
Some(crate::graphics::Antialiasing::MSAAx4)
} else {
None
},
..crate::graphics::Settings::default()
};
Ok(crate::shell::multi_window::run::<
Instance<Self>,
Self::Executor,
crate::renderer::Compositor,
>(settings.into(), renderer_settings)?)
}
}
struct Instance<A>(A)
where
A: Application,
A::Theme: DefaultStyle;
impl<A> crate::runtime::multi_window::Program for Instance<A>
where
A: Application,
A::Theme: DefaultStyle,
{
type Message = A::Message;
type Theme = A::Theme;
type Renderer = crate::Renderer;
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
}
fn view(
&self,
window: window::Id,
) -> Element<'_, Self::Message, Self::Theme, Self::Renderer> {
self.0.view(window)
}
}
impl<A> crate::shell::multi_window::Application for Instance<A>
where
A: Application,
A::Theme: DefaultStyle,
{
type Flags = A::Flags;
fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
let (app, command) = A::new(flags);
(Instance(app), command)
}
fn title(&self, window: window::Id) -> String {
self.0.title(window)
}
fn theme(&self, window: window::Id) -> A::Theme {
self.0.theme(window)
}
fn style(&self, theme: &Self::Theme) -> Appearance {
self.0.style(theme)
}
fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription()
}
fn scale_factor(&self, window: window::Id) -> f64 {
self.0.scale_factor(window)
}
}

View file

@ -1,246 +0,0 @@
use crate::style::application::StyleSheet;
use crate::window;
use crate::{Command, Element, Executor, Settings, Subscription};
/// An interactive cross-platform multi-window application.
///
/// This trait is the main entrypoint of Iced. Once implemented, you can run
/// your GUI application by simply calling [`run`](#method.run).
///
/// - On native platforms, it will run in its own windows.
/// - On the web, it will take control of the `<title>` and the `<body>` of the
/// document and display only the contents of the `window::Id::MAIN` window.
///
/// An [`Application`] can execute asynchronous actions by returning a
/// [`Command`] in some of its methods. If you do not intend to perform any
/// background work in your program, the [`Sandbox`] trait offers a simplified
/// interface.
///
/// When using an [`Application`] with the `debug` feature enabled, a debug view
/// can be toggled by pressing `F12`.
///
/// # Examples
/// See the `examples/multi-window` example to see this multi-window `Application` trait in action.
///
/// ## A simple "Hello, world!"
///
/// If you just want to get started, here is a simple [`Application`] that
/// says "Hello, world!":
///
/// ```no_run
/// use iced::{executor, window};
/// use iced::{Command, Element, Settings, Theme};
/// use iced::multi_window::{self, Application};
///
/// pub fn main() -> iced::Result {
/// Hello::run(Settings::default())
/// }
///
/// struct Hello;
///
/// impl multi_window::Application for Hello {
/// type Executor = executor::Default;
/// type Flags = ();
/// type Message = ();
/// type Theme = Theme;
///
/// fn new(_flags: ()) -> (Hello, Command<Self::Message>) {
/// (Hello, Command::none())
/// }
///
/// fn title(&self, _window: window::Id) -> String {
/// String::from("A cool application")
/// }
///
/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
/// Command::none()
/// }
///
/// fn view(&self, _window: window::Id) -> Element<Self::Message> {
/// "Hello, world!".into()
/// }
/// }
/// ```
///
/// [`Sandbox`]: crate::Sandbox
pub trait Application: Sized {
/// The [`Executor`] that will run commands and subscriptions.
///
/// The [default executor] can be a good starting point!
///
/// [`Executor`]: Self::Executor
/// [default executor]: crate::executor::Default
type Executor: Executor;
/// The type of __messages__ your [`Application`] will produce.
type Message: std::fmt::Debug + Send;
/// The theme of your [`Application`].
type Theme: Default + StyleSheet;
/// The data needed to initialize your [`Application`].
type Flags;
/// Initializes the [`Application`] with the flags provided to
/// [`run`] as part of the [`Settings`].
///
/// Here is where you should return the initial state of your app.
///
/// Additionally, you can return a [`Command`] if you need to perform some
/// async action in the background on startup. This is useful if you want to
/// load state from a file, perform an initial HTTP request, etc.
///
/// [`run`]: Self::run
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
/// Returns the current title of the `window` of the [`Application`].
///
/// This title can be dynamic! The runtime will automatically update the
/// title of your window when necessary.
fn title(&self, window: window::Id) -> String;
/// Handles a __message__ and updates the state of the [`Application`].
///
/// This is where you define your __update logic__. All the __messages__,
/// produced by either user interactions or commands, will be handled by
/// this method.
///
/// Any [`Command`] returned will be executed immediately in the background.
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
/// Returns the widgets to display in the `window` of the [`Application`].
///
/// These widgets can produce __messages__ based on user interaction.
fn view(
&self,
window: window::Id,
) -> Element<'_, Self::Message, Self::Theme, crate::Renderer>;
/// Returns the current [`Theme`] of the `window` of the [`Application`].
///
/// [`Theme`]: Self::Theme
#[allow(unused_variables)]
fn theme(&self, window: window::Id) -> Self::Theme {
Self::Theme::default()
}
/// Returns the current `Style` of the [`Theme`].
///
/// [`Theme`]: Self::Theme
fn style(&self) -> <Self::Theme as StyleSheet>::Style {
<Self::Theme as StyleSheet>::Style::default()
}
/// Returns the event [`Subscription`] for the current state of the
/// application.
///
/// A [`Subscription`] will be kept alive as long as you keep returning it,
/// and the __messages__ produced will be handled by
/// [`update`](#tymethod.update).
///
/// By default, this method returns an empty [`Subscription`].
fn subscription(&self) -> Subscription<Self::Message> {
Subscription::none()
}
/// Returns the scale factor of the `window` of the [`Application`].
///
/// It can be used to dynamically control the size of the UI at runtime
/// (i.e. zooming).
///
/// For instance, a scale factor of `2.0` will make widgets twice as big,
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
#[allow(unused_variables)]
fn scale_factor(&self, window: window::Id) -> f64 {
1.0
}
/// Runs the multi-window [`Application`].
///
/// On native platforms, this method will take control of the current thread
/// until the [`Application`] exits.
///
/// On the web platform, this method __will NOT return__ unless there is an
/// [`Error`] during startup.
///
/// [`Error`]: crate::Error
fn run(settings: Settings<Self::Flags>) -> crate::Result
where
Self: 'static,
{
#[allow(clippy::needless_update)]
let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
antialiasing: if settings.antialiasing {
Some(crate::graphics::Antialiasing::MSAAx4)
} else {
None
},
..crate::renderer::Settings::default()
};
Ok(crate::shell::multi_window::run::<
Instance<Self>,
Self::Executor,
crate::renderer::Compositor,
>(settings.into(), renderer_settings)?)
}
}
struct Instance<A: Application>(A);
impl<A> crate::runtime::multi_window::Program for Instance<A>
where
A: Application,
{
type Message = A::Message;
type Theme = A::Theme;
type Renderer = crate::Renderer;
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
}
fn view(
&self,
window: window::Id,
) -> Element<'_, Self::Message, Self::Theme, Self::Renderer> {
self.0.view(window)
}
}
impl<A> crate::shell::multi_window::Application for Instance<A>
where
A: Application,
{
type Flags = A::Flags;
fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
let (app, command) = A::new(flags);
(Instance(app), command)
}
fn title(&self, window: window::Id) -> String {
self.0.title(window)
}
fn theme(&self, window: window::Id) -> A::Theme {
self.0.theme(window)
}
fn style(&self) -> <A::Theme as StyleSheet>::Style {
self.0.style()
}
fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription()
}
fn scale_factor(&self, window: window::Id) -> f64 {
self.0.scale_factor(window)
}
}

879
src/program.rs Normal file
View file

@ -0,0 +1,879 @@
//! Create and run iced applications step by step.
//!
//! # 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<Message> {
//! column![
//! text(value),
//! button("+").on_press(Message::Increment),
//! ]
//! }
//! ```
use crate::application::Application;
use crate::core::text;
use crate::executor::{self, Executor};
use crate::graphics::compositor;
use crate::window;
use crate::{Command, Element, Font, Result, Settings, Size, Subscription};
pub use crate::application::{Appearance, DefaultStyle};
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<Message> {
/// column![
/// text(value),
/// button("+").on_press(Message::Increment),
/// ]
/// }
/// ```
pub fn program<State, Message, Theme, Renderer>(
title: impl Title<State>,
update: impl Update<State, Message>,
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
) -> Program<impl Definition<State = State, Message = Message, Theme = Theme>>
where
State: 'static,
Message: Send + std::fmt::Debug,
Theme: Default + DefaultStyle,
Renderer: self::Renderer,
{
use std::marker::PhantomData;
struct Application<State, Message, Theme, Renderer, Update, View> {
update: Update,
view: View,
_state: PhantomData<State>,
_message: PhantomData<Message>,
_theme: PhantomData<Theme>,
_renderer: PhantomData<Renderer>,
}
impl<State, Message, Theme, Renderer, Update, View> Definition
for Application<State, Message, Theme, Renderer, Update, View>
where
Message: Send + std::fmt::Debug,
Theme: Default + DefaultStyle,
Renderer: self::Renderer,
Update: self::Update<State, Message>,
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
{
type State = State;
type Message = Message;
type Theme = Theme;
type Renderer = Renderer;
type Executor = executor::Default;
fn load(&self) -> Command<Self::Message> {
Command::none()
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
self.update.update(state, message).into()
}
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
self.view.view(state).into()
}
}
Program {
raw: Application {
update,
view,
_state: PhantomData,
_message: PhantomData,
_theme: PhantomData,
_renderer: 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.
///
/// You can create a [`Program`] with the [`program`] helper.
///
/// [`run`]: Program::run
#[derive(Debug)]
pub struct Program<P: Definition> {
raw: P,
settings: Settings,
}
impl<P: Definition> Program<P> {
/// Runs the underlying [`Application`] of the [`Program`].
///
/// The state of the [`Program`] must implement [`Default`].
/// If your state does not implement [`Default`], use [`run_with`]
/// instead.
///
/// [`run_with`]: Self::run_with
pub fn run(self) -> Result
where
Self: 'static,
P::State: Default,
{
self.run_with(P::State::default)
}
/// Runs the underlying [`Application`] of the [`Program`] with a
/// closure that creates the initial state.
pub fn run_with(
self,
initialize: impl Fn() -> P::State + Clone + 'static,
) -> Result
where
Self: 'static,
{
use std::marker::PhantomData;
struct Instance<P: Definition, I> {
program: P,
state: P::State,
_initialize: PhantomData<I>,
}
impl<P: Definition, I: Fn() -> P::State> Application for Instance<P, I> {
type Message = P::Message;
type Theme = P::Theme;
type Renderer = P::Renderer;
type Flags = (P, I);
type Executor = P::Executor;
fn new(
(program, initialize): Self::Flags,
) -> (Self, Command<Self::Message>) {
let state = initialize();
let command = program.load();
(
Self {
program,
state,
_initialize: PhantomData,
},
command,
)
}
fn title(&self) -> String {
self.program.title(&self.state)
}
fn update(
&mut self,
message: Self::Message,
) -> Command<Self::Message> {
self.program.update(&mut self.state, message)
}
fn view(
&self,
) -> crate::Element<'_, Self::Message, Self::Theme, Self::Renderer>
{
self.program.view(&self.state)
}
fn subscription(&self) -> Subscription<Self::Message> {
self.program.subscription(&self.state)
}
fn theme(&self) -> Self::Theme {
self.program.theme(&self.state)
}
fn style(&self, theme: &Self::Theme) -> Appearance {
self.program.style(&self.state, theme)
}
}
let Self { raw, settings } = self;
Instance::run(Settings {
flags: (raw, initialize),
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<Cow<'static, [u8]>>) -> 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::size`] of the [`Program`].
pub fn window_size(self, size: impl Into<Size>) -> Self {
Self {
settings: Settings {
window: window::Settings {
size: size.into(),
..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<P::State>,
) -> Program<
impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
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<P::Message>,
) -> Program<
impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
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<P::Message>,
) -> Program<
impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
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<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
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) -> Appearance,
) -> Program<
impl Definition<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
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 + DefaultStyle;
/// The renderer of the program.
type Renderer: Renderer;
/// The executor of the program.
type Executor: Executor;
fn load(&self) -> Command<Self::Message>;
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message>;
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer>;
fn title(&self, _state: &Self::State) -> String {
String::from("A cool iced application!")
}
fn subscription(
&self,
_state: &Self::State,
) -> Subscription<Self::Message> {
Subscription::none()
}
fn theme(&self, _state: &Self::State) -> Self::Theme {
Self::Theme::default()
}
fn style(&self, _state: &Self::State, theme: &Self::Theme) -> Appearance {
DefaultStyle::default_style(theme)
}
}
fn with_title<P: Definition>(
program: P,
title: impl Title<P::State>,
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithTitle<P, Title> {
program: P,
title: Title,
}
impl<P, Title> Definition for WithTitle<P, Title>
where
P: Definition,
Title: self::Title<P::State>,
{
type State = P::State;
type Message = P::Message;
type Theme = P::Theme;
type Renderer = P::Renderer;
type Executor = P::Executor;
fn load(&self) -> Command<Self::Message> {
self.program.load()
}
fn title(&self, state: &Self::State) -> String {
self.title.title(state)
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
self.program.update(state, message)
}
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
self.program.view(state)
}
fn theme(&self, state: &Self::State) -> Self::Theme {
self.program.theme(state)
}
fn subscription(
&self,
state: &Self::State,
) -> Subscription<Self::Message> {
self.program.subscription(state)
}
fn style(
&self,
state: &Self::State,
theme: &Self::Theme,
) -> Appearance {
self.program.style(state, theme)
}
}
WithTitle { program, title }
}
fn with_load<P: Definition>(
program: P,
f: impl Fn() -> Command<P::Message>,
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithLoad<P, F> {
program: P,
load: F,
}
impl<P: Definition, F> Definition for WithLoad<P, F>
where
F: Fn() -> Command<P::Message>,
{
type State = P::State;
type Message = P::Message;
type Theme = P::Theme;
type Renderer = P::Renderer;
type Executor = executor::Default;
fn load(&self) -> Command<Self::Message> {
Command::batch([self.program.load(), (self.load)()])
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
self.program.update(state, message)
}
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
self.program.view(state)
}
fn title(&self, state: &Self::State) -> String {
self.program.title(state)
}
fn subscription(
&self,
state: &Self::State,
) -> Subscription<Self::Message> {
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,
) -> Appearance {
self.program.style(state, theme)
}
}
WithLoad { program, load: f }
}
fn with_subscription<P: Definition>(
program: P,
f: impl Fn(&P::State) -> Subscription<P::Message>,
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithSubscription<P, F> {
program: P,
subscription: F,
}
impl<P: Definition, F> Definition for WithSubscription<P, F>
where
F: Fn(&P::State) -> Subscription<P::Message>,
{
type State = P::State;
type Message = P::Message;
type Theme = P::Theme;
type Renderer = P::Renderer;
type Executor = executor::Default;
fn subscription(
&self,
state: &Self::State,
) -> Subscription<Self::Message> {
(self.subscription)(state)
}
fn load(&self) -> Command<Self::Message> {
self.program.load()
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
self.program.update(state, message)
}
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
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,
) -> Appearance {
self.program.style(state, theme)
}
}
WithSubscription {
program,
subscription: f,
}
}
fn with_theme<P: Definition>(
program: P,
f: impl Fn(&P::State) -> P::Theme,
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithTheme<P, F> {
program: P,
theme: F,
}
impl<P: Definition, F> Definition for WithTheme<P, F>
where
F: Fn(&P::State) -> P::Theme,
{
type State = P::State;
type Message = P::Message;
type Theme = P::Theme;
type Renderer = P::Renderer;
type Executor = P::Executor;
fn theme(&self, state: &Self::State) -> Self::Theme {
(self.theme)(state)
}
fn load(&self) -> Command<Self::Message> {
self.program.load()
}
fn title(&self, state: &Self::State) -> String {
self.program.title(state)
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
self.program.update(state, message)
}
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
self.program.view(state)
}
fn subscription(
&self,
state: &Self::State,
) -> Subscription<Self::Message> {
self.program.subscription(state)
}
fn style(
&self,
state: &Self::State,
theme: &Self::Theme,
) -> Appearance {
self.program.style(state, theme)
}
}
WithTheme { program, theme: f }
}
fn with_style<P: Definition>(
program: P,
f: impl Fn(&P::State, &P::Theme) -> Appearance,
) -> impl Definition<State = P::State, Message = P::Message, Theme = P::Theme> {
struct WithStyle<P, F> {
program: P,
style: F,
}
impl<P: Definition, F> Definition for WithStyle<P, F>
where
F: Fn(&P::State, &P::Theme) -> Appearance,
{
type State = P::State;
type Message = P::Message;
type Theme = P::Theme;
type Renderer = P::Renderer;
type Executor = P::Executor;
fn style(
&self,
state: &Self::State,
theme: &Self::Theme,
) -> Appearance {
(self.style)(state, theme)
}
fn load(&self) -> Command<Self::Message> {
self.program.load()
}
fn title(&self, state: &Self::State) -> String {
self.program.title(state)
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
self.program.update(state, message)
}
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
self.program.view(state)
}
fn subscription(
&self,
state: &Self::State,
) -> Subscription<Self::Message> {
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<State> {
/// Produces the title of the [`Program`].
fn title(&self, state: &State) -> String;
}
impl<State> Title<State> for &'static str {
fn title(&self, _state: &State) -> String {
self.to_string()
}
}
impl<T, State> Title<State> 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<Command<Message>>`.
pub trait Update<State, Message> {
/// Processes the message and updates the state of the [`Program`].
fn update(
&self,
state: &mut State,
message: Message,
) -> impl Into<Command<Message>>;
}
impl<T, State, Message, C> Update<State, Message> for T
where
T: Fn(&mut State, Message) -> C,
C: Into<Command<Message>>,
{
fn update(
&self,
state: &mut State,
message: Message,
) -> impl Into<Command<Message>> {
self(state, message)
}
}
/// The view logic of some [`Program`].
///
/// This trait allows the [`program`] builder to take any closure that
/// returns any `Into<Element<'_, Message>>`.
pub trait View<'a, State, Message, Theme, Renderer> {
/// Produces the widget of the [`Program`].
fn view(
&self,
state: &'a State,
) -> impl Into<Element<'a, Message, Theme, Renderer>>;
}
impl<'a, T, State, Message, Theme, Renderer, Widget>
View<'a, State, Message, Theme, Renderer> for T
where
T: Fn(&'a State) -> Widget,
State: 'static,
Widget: Into<Element<'a, Message, Theme, Renderer>>,
{
fn view(
&self,
state: &'a State,
) -> impl Into<Element<'a, Message, Theme, Renderer>> {
self(state)
}
}
/// The renderer of some [`Program`].
pub trait Renderer: text::Renderer + compositor::Default {}
impl<T> Renderer for T where T: text::Renderer + compositor::Default {}

View file

@ -1,199 +0,0 @@
use crate::theme::{self, Theme};
use crate::{Application, Command, Element, Error, Settings, Subscription};
/// A sandboxed [`Application`].
///
/// If you are a just getting started with the library, this trait offers a
/// simpler interface than [`Application`].
///
/// Unlike an [`Application`], a [`Sandbox`] cannot run any asynchronous
/// actions or be initialized with some external flags. However, both traits
/// are very similar and upgrading from a [`Sandbox`] is very straightforward.
///
/// Therefore, it is recommended to always start by implementing this trait and
/// upgrade only once necessary.
///
/// # Examples
/// [The repository has a bunch of examples] that use the [`Sandbox`] trait:
///
/// - [`bezier_tool`], a Paint-like tool for drawing Bézier curves using the
/// [`Canvas widget`].
/// - [`counter`], the classic counter example explained in [the overview].
/// - [`custom_widget`], a demonstration of how to build a custom widget that
/// draws a circle.
/// - [`geometry`], a custom widget showcasing how to draw geometry with the
/// `Mesh2D` primitive in [`iced_wgpu`].
/// - [`pane_grid`], a grid of panes that can be split, resized, and
/// reorganized.
/// - [`progress_bar`], a simple progress bar that can be filled by using a
/// slider.
/// - [`styling`], an example showcasing custom styling with a light and dark
/// theme.
/// - [`svg`], an application that renders the [Ghostscript Tiger] by leveraging
/// the [`Svg` widget].
/// - [`tour`], a simple UI tour that can run both on native platforms and the
/// web!
///
/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.12/examples
/// [`bezier_tool`]: https://github.com/iced-rs/iced/tree/0.12/examples/bezier_tool
/// [`counter`]: https://github.com/iced-rs/iced/tree/0.12/examples/counter
/// [`custom_widget`]: https://github.com/iced-rs/iced/tree/0.12/examples/custom_widget
/// [`geometry`]: https://github.com/iced-rs/iced/tree/0.12/examples/geometry
/// [`pane_grid`]: https://github.com/iced-rs/iced/tree/0.12/examples/pane_grid
/// [`progress_bar`]: https://github.com/iced-rs/iced/tree/0.12/examples/progress_bar
/// [`styling`]: https://github.com/iced-rs/iced/tree/0.12/examples/styling
/// [`svg`]: https://github.com/iced-rs/iced/tree/0.12/examples/svg
/// [`tour`]: https://github.com/iced-rs/iced/tree/0.12/examples/tour
/// [`Canvas widget`]: crate::widget::Canvas
/// [the overview]: index.html#overview
/// [`iced_wgpu`]: https://github.com/iced-rs/iced/tree/0.12/wgpu
/// [`Svg` widget]: crate::widget::Svg
/// [Ghostscript Tiger]: https://commons.wikimedia.org/wiki/File:Ghostscript_Tiger.svg
///
/// ## A simple "Hello, world!"
///
/// If you just want to get started, here is a simple [`Sandbox`] that
/// says "Hello, world!":
///
/// ```no_run
/// use iced::{Element, Sandbox, Settings};
///
/// pub fn main() -> iced::Result {
/// Hello::run(Settings::default())
/// }
///
/// struct Hello;
///
/// impl Sandbox for Hello {
/// type Message = ();
///
/// fn new() -> Hello {
/// Hello
/// }
///
/// fn title(&self) -> String {
/// String::from("A cool application")
/// }
///
/// fn update(&mut self, _message: Self::Message) {
/// // This application has no interactions
/// }
///
/// fn view(&self) -> Element<Self::Message> {
/// "Hello, world!".into()
/// }
/// }
/// ```
pub trait Sandbox {
/// The type of __messages__ your [`Sandbox`] will produce.
type Message: std::fmt::Debug + Send;
/// Initializes the [`Sandbox`].
///
/// Here is where you should return the initial state of your app.
fn new() -> Self;
/// Returns the current title of the [`Sandbox`].
///
/// This title can be dynamic! The runtime will automatically update the
/// title of your application when necessary.
fn title(&self) -> String;
/// Handles a __message__ and updates the state of the [`Sandbox`].
///
/// This is where you define your __update logic__. All the __messages__,
/// produced by user interactions, will be handled by this method.
fn update(&mut self, message: Self::Message);
/// Returns the widgets to display in the [`Sandbox`].
///
/// These widgets can produce __messages__ based on user interaction.
fn view(&self) -> Element<'_, Self::Message>;
/// Returns the current [`Theme`] of the [`Sandbox`].
///
/// If you want to use your own custom theme type, you will have to use an
/// [`Application`].
///
/// By default, it returns [`Theme::default`].
fn theme(&self) -> Theme {
Theme::default()
}
/// Returns the current style variant of [`theme::Application`].
///
/// By default, it returns [`theme::Application::default`].
fn style(&self) -> theme::Application {
theme::Application::default()
}
/// Returns the scale factor of the [`Sandbox`].
///
/// It can be used to dynamically control the size of the UI at runtime
/// (i.e. zooming).
///
/// For instance, a scale factor of `2.0` will make widgets twice as big,
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
fn scale_factor(&self) -> f64 {
1.0
}
/// Runs the [`Sandbox`].
///
/// On native platforms, this method will take control of the current thread
/// and __will NOT return__.
///
/// It should probably be that last thing you call in your `main` function.
fn run(settings: Settings<()>) -> Result<(), Error>
where
Self: 'static + Sized,
{
<Self as Application>::run(settings)
}
}
impl<T> Application for T
where
T: Sandbox,
{
type Executor = iced_futures::backend::null::Executor;
type Flags = ();
type Message = T::Message;
type Theme = Theme;
fn new(_flags: ()) -> (Self, Command<T::Message>) {
(T::new(), Command::none())
}
fn title(&self) -> String {
T::title(self)
}
fn update(&mut self, message: T::Message) -> Command<T::Message> {
T::update(self, message);
Command::none()
}
fn view(&self) -> Element<'_, T::Message> {
T::view(self)
}
fn theme(&self) -> Self::Theme {
T::theme(self)
}
fn style(&self) -> theme::Application {
T::style(self)
}
fn subscription(&self) -> Subscription<T::Message> {
Subscription::none()
}
fn scale_factor(&self) -> f64 {
T::scale_factor(self)
}
}

View file

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

View file

@ -1,5 +1,5 @@
//! Listen and react to time.
pub use iced_core::time::{Duration, Instant, SystemTime};
pub use crate::core::time::{Duration, Instant, SystemTime};
#[allow(unused_imports)]
#[cfg_attr(

View file

@ -54,7 +54,7 @@ pub enum Error {
InvalidError(#[from] icon::Error),
/// The underlying OS failed to create the icon.
#[error("The underlying OS failted to create the window icon: {0}")]
#[error("The underlying OS failed to create the window icon: {0}")]
OsError(#[from] io::Error),
/// The `image` crate reported an error.