2022-07-08 23:20:25 +02:00
|
|
|
//! Build interactive cross-platform applications.
|
2024-03-22 07:09:51 +01:00
|
|
|
use crate::core::text;
|
|
|
|
|
use crate::graphics::compositor;
|
2024-03-06 21:27:03 +01:00
|
|
|
use crate::shell::application;
|
2024-03-17 14:16:38 +01:00
|
|
|
use crate::{Command, Element, Executor, Settings, Subscription};
|
2024-03-06 21:27:03 +01:00
|
|
|
|
2024-03-17 19:38:42 +01:00
|
|
|
pub use application::{Appearance, DefaultStyle};
|
2022-05-26 19:02:15 +02:00
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// An interactive cross-platform 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 window.
|
|
|
|
|
/// - On the web, it will take control of the `<title>` and the `<body>` of the
|
|
|
|
|
/// document.
|
|
|
|
|
///
|
2020-04-01 04:35:24 +02:00
|
|
|
/// An [`Application`] can execute asynchronous actions by returning a
|
2024-03-17 13:55:15 +01:00
|
|
|
/// [`Command`] in some of its methods.
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
2020-04-02 03:33:30 +02:00
|
|
|
/// When using an [`Application`] with the `debug` feature enabled, a debug view
|
|
|
|
|
/// can be toggled by pressing `F12`.
|
|
|
|
|
///
|
2020-04-01 04:35:24 +02:00
|
|
|
/// # Examples
|
|
|
|
|
/// [The repository has a bunch of examples] that use the [`Application`] trait:
|
|
|
|
|
///
|
|
|
|
|
/// - [`clock`], an application that uses the [`Canvas`] widget to draw a clock
|
|
|
|
|
/// and its hands to display the current time.
|
|
|
|
|
/// - [`download_progress`], a basic application that asynchronously downloads
|
|
|
|
|
/// a dummy file of 100 MB and tracks the download progress.
|
|
|
|
|
/// - [`events`], a log of native events displayed using a conditional
|
|
|
|
|
/// [`Subscription`].
|
2020-11-26 07:32:31 +01:00
|
|
|
/// - [`game_of_life`], an interactive version of the [Game of Life], invented
|
|
|
|
|
/// by [John Horton Conway].
|
2020-04-01 04:35:24 +02:00
|
|
|
/// - [`pokedex`], an application that displays a random Pokédex entry (sprite
|
|
|
|
|
/// included!) by using the [PokéAPI].
|
|
|
|
|
/// - [`solar_system`], an animated solar system drawn using the [`Canvas`] widget
|
|
|
|
|
/// and showcasing how to compose different transforms.
|
|
|
|
|
/// - [`stopwatch`], a watch with start/stop and reset buttons showcasing how
|
|
|
|
|
/// to listen to time.
|
|
|
|
|
/// - [`todos`], a todos tracker inspired by [TodoMVC].
|
|
|
|
|
///
|
2024-02-15 03:22:53 +01:00
|
|
|
/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.12/examples
|
|
|
|
|
/// [`clock`]: https://github.com/iced-rs/iced/tree/0.12/examples/clock
|
|
|
|
|
/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.12/examples/download_progress
|
|
|
|
|
/// [`events`]: https://github.com/iced-rs/iced/tree/0.12/examples/events
|
|
|
|
|
/// [`game_of_life`]: https://github.com/iced-rs/iced/tree/0.12/examples/game_of_life
|
|
|
|
|
/// [`pokedex`]: https://github.com/iced-rs/iced/tree/0.12/examples/pokedex
|
|
|
|
|
/// [`solar_system`]: https://github.com/iced-rs/iced/tree/0.12/examples/solar_system
|
|
|
|
|
/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.12/examples/stopwatch
|
|
|
|
|
/// [`todos`]: https://github.com/iced-rs/iced/tree/0.12/examples/todos
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Sandbox`]: crate::Sandbox
|
|
|
|
|
/// [`Canvas`]: crate::widget::Canvas
|
2020-04-01 04:35:24 +02:00
|
|
|
/// [PokéAPI]: https://pokeapi.co/
|
|
|
|
|
/// [TodoMVC]: http://todomvc.com/
|
|
|
|
|
///
|
|
|
|
|
/// ## A simple "Hello, world!"
|
|
|
|
|
///
|
|
|
|
|
/// If you just want to get started, here is a simple [`Application`] that
|
|
|
|
|
/// says "Hello, world!":
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
|
|
|
|
/// ```no_run
|
2024-03-17 19:38:42 +01:00
|
|
|
/// use iced::advanced::Application;
|
2022-07-27 06:49:20 +02:00
|
|
|
/// use iced::executor;
|
2024-03-22 07:09:51 +01:00
|
|
|
/// use iced::{Command, Element, Settings, Theme, Renderer};
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
2020-09-08 00:35:17 +02:00
|
|
|
/// pub fn main() -> iced::Result {
|
2020-04-01 04:35:24 +02:00
|
|
|
/// Hello::run(Settings::default())
|
2019-11-22 19:36:57 +01:00
|
|
|
/// }
|
|
|
|
|
///
|
2020-04-01 04:35:24 +02:00
|
|
|
/// struct Hello;
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
2020-04-01 04:35:24 +02:00
|
|
|
/// impl Application for Hello {
|
2020-10-17 08:46:16 +02:00
|
|
|
/// type Executor = executor::Default;
|
2020-03-30 18:00:15 +02:00
|
|
|
/// type Flags = ();
|
2022-05-14 01:47:55 +02:00
|
|
|
/// type Message = ();
|
|
|
|
|
/// type Theme = Theme;
|
2024-03-22 07:09:51 +01:00
|
|
|
/// type Renderer = Renderer;
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
2020-04-01 04:35:24 +02:00
|
|
|
/// fn new(_flags: ()) -> (Hello, Command<Self::Message>) {
|
|
|
|
|
/// (Hello, Command::none())
|
2019-11-22 19:36:57 +01:00
|
|
|
/// }
|
|
|
|
|
///
|
|
|
|
|
/// fn title(&self) -> String {
|
2020-04-01 04:35:24 +02:00
|
|
|
/// String::from("A cool application")
|
2019-11-22 19:36:57 +01:00
|
|
|
/// }
|
|
|
|
|
///
|
2021-09-01 19:21:49 +07:00
|
|
|
/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Command::none()
|
|
|
|
|
/// }
|
|
|
|
|
///
|
2022-07-27 06:49:20 +02:00
|
|
|
/// fn view(&self) -> Element<Self::Message> {
|
|
|
|
|
/// "Hello, world!".into()
|
2019-11-22 19:36:57 +01:00
|
|
|
/// }
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2024-03-06 21:27:03 +01:00
|
|
|
pub trait Application: Sized
|
|
|
|
|
where
|
2024-03-07 20:11:32 +01:00
|
|
|
Self::Theme: DefaultStyle,
|
2024-03-06 21:27:03 +01:00
|
|
|
{
|
2020-01-20 04:47:36 +01:00
|
|
|
/// The [`Executor`] that will run commands and subscriptions.
|
|
|
|
|
///
|
2020-04-01 00:25:33 +02:00
|
|
|
/// The [default executor] can be a good starting point!
|
2020-01-20 04:47:36 +01:00
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Executor`]: Self::Executor
|
|
|
|
|
/// [default executor]: crate::executor::Default
|
2020-01-20 04:47:36 +01:00
|
|
|
type Executor: Executor;
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// The type of __messages__ your [`Application`] will produce.
|
2021-09-15 15:31:40 +07:00
|
|
|
type Message: std::fmt::Debug + Send;
|
2019-11-21 18:00:27 +01:00
|
|
|
|
2022-05-14 01:47:55 +02:00
|
|
|
/// The theme of your [`Application`].
|
2024-03-06 21:27:03 +01:00
|
|
|
type Theme: Default;
|
2022-05-14 01:47:55 +02:00
|
|
|
|
2024-03-22 07:09:51 +01:00
|
|
|
/// The renderer of your [`Application`].
|
2024-03-22 19:35:19 +01:00
|
|
|
type Renderer: text::Renderer + compositor::Default;
|
2024-03-22 07:09:51 +01:00
|
|
|
|
2020-03-30 18:00:15 +02:00
|
|
|
/// The data needed to initialize your [`Application`].
|
|
|
|
|
type Flags;
|
|
|
|
|
|
2024-05-10 20:08:09 +02:00
|
|
|
/// Returns the unique name of the [`Application`].
|
2024-05-13 13:21:10 +02:00
|
|
|
fn name() -> &'static str {
|
|
|
|
|
std::any::type_name::<Self>()
|
|
|
|
|
}
|
2024-05-10 20:08:09 +02:00
|
|
|
|
2020-03-30 18:00:15 +02:00
|
|
|
/// Initializes the [`Application`] with the flags provided to
|
2020-04-01 04:35:24 +02:00
|
|
|
/// [`run`] as part of the [`Settings`].
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
|
|
|
|
/// Here is where you should return the initial state of your app.
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// 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.
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`run`]: Self::run
|
2020-03-30 18:00:15 +02:00
|
|
|
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
|
2019-11-21 18:00:27 +01:00
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Returns the current title of the [`Application`].
|
|
|
|
|
///
|
|
|
|
|
/// This title can be dynamic! The runtime will automatically update the
|
|
|
|
|
/// title of your application when necessary.
|
2019-11-21 18:00:27 +01:00
|
|
|
fn title(&self) -> String;
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// 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.
|
2021-09-01 19:21:49 +07:00
|
|
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
2019-11-21 18:00:27 +01:00
|
|
|
|
2022-05-14 01:47:55 +02:00
|
|
|
/// Returns the widgets to display in the [`Application`].
|
|
|
|
|
///
|
|
|
|
|
/// These widgets can produce __messages__ based on user interaction.
|
2024-03-22 07:09:51 +01:00
|
|
|
fn view(&self) -> Element<'_, Self::Message, Self::Theme, Self::Renderer>;
|
2022-05-14 01:47:55 +02:00
|
|
|
|
|
|
|
|
/// Returns the current [`Theme`] of the [`Application`].
|
2022-07-08 20:07:33 +02:00
|
|
|
///
|
|
|
|
|
/// [`Theme`]: Self::Theme
|
2022-05-14 01:47:55 +02:00
|
|
|
fn theme(&self) -> Self::Theme {
|
|
|
|
|
Self::Theme::default()
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 20:11:32 +01:00
|
|
|
/// Returns the current [`Appearance`] of the [`Application`].
|
2024-03-06 21:27:03 +01:00
|
|
|
fn style(&self, theme: &Self::Theme) -> Appearance {
|
2024-03-07 20:11:32 +01:00
|
|
|
theme.default_style()
|
2022-07-08 20:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-14 05:56:46 +01:00
|
|
|
/// 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> {
|
2019-12-05 06:10:13 +01:00
|
|
|
Subscription::none()
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 19:17:05 +02:00
|
|
|
/// Returns the scale factor 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`.
|
|
|
|
|
fn scale_factor(&self) -> f64 {
|
|
|
|
|
1.0
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 19:36:57 +01:00
|
|
|
/// Runs the [`Application`].
|
|
|
|
|
///
|
2020-04-01 00:25:33 +02:00
|
|
|
/// On native platforms, this method will take control of the current thread
|
2021-11-29 14:44:19 +07:00
|
|
|
/// until the [`Application`] exits.
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
2021-11-29 14:44:19 +07:00
|
|
|
/// On the web platform, this method __will NOT return__ unless there is an
|
|
|
|
|
/// [`Error`] during startup.
|
2019-11-22 19:36:57 +01:00
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Error`]: crate::Error
|
2020-09-08 00:35:17 +02:00
|
|
|
fn run(settings: Settings<Self::Flags>) -> crate::Result
|
2019-11-21 18:00:27 +01:00
|
|
|
where
|
2024-03-17 19:53:02 +01:00
|
|
|
Self: 'static,
|
2019-11-21 18:00:27 +01:00
|
|
|
{
|
2022-07-09 18:42:41 +02:00
|
|
|
#[allow(clippy::needless_update)]
|
2024-03-22 07:09:51 +01:00
|
|
|
let renderer_settings = crate::graphics::Settings {
|
2022-01-28 16:47:50 +07:00
|
|
|
default_font: settings.default_font,
|
|
|
|
|
default_text_size: settings.default_text_size,
|
|
|
|
|
antialiasing: if settings.antialiasing {
|
2023-03-04 05:37:11 +01:00
|
|
|
Some(crate::graphics::Antialiasing::MSAAx4)
|
2022-01-28 16:47:50 +07:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
},
|
2024-03-22 07:09:51 +01:00
|
|
|
..crate::graphics::Settings::default()
|
2022-01-28 16:47:50 +07:00
|
|
|
};
|
|
|
|
|
|
2024-05-07 15:50:18 +02:00
|
|
|
Ok(crate::shell::application::run::<
|
2022-01-28 16:47:50 +07:00
|
|
|
Instance<Self>,
|
|
|
|
|
Self::Executor,
|
2024-03-22 19:35:19 +01:00
|
|
|
<Self::Renderer as compositor::Default>::Compositor,
|
2024-05-07 15:50:18 +02:00
|
|
|
>(settings.into(), renderer_settings)?)
|
2019-11-21 18:00:27 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 21:27:03 +01:00
|
|
|
struct Instance<A>(A)
|
|
|
|
|
where
|
|
|
|
|
A: Application,
|
2024-03-07 20:11:32 +01:00
|
|
|
A::Theme: DefaultStyle;
|
2019-11-21 18:00:27 +01:00
|
|
|
|
2023-03-05 06:35:20 +01:00
|
|
|
impl<A> crate::runtime::Program for Instance<A>
|
2020-05-21 04:27:31 +02:00
|
|
|
where
|
|
|
|
|
A: Application,
|
2024-03-07 20:11:32 +01:00
|
|
|
A::Theme: DefaultStyle,
|
2020-05-21 04:27:31 +02:00
|
|
|
{
|
|
|
|
|
type Message = A::Message;
|
2024-01-21 17:56:01 +01:00
|
|
|
type Theme = A::Theme;
|
2024-03-22 07:09:51 +01:00
|
|
|
type Renderer = A::Renderer;
|
2021-09-01 19:21:49 +07:00
|
|
|
|
|
|
|
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
|
|
|
|
self.0.update(message)
|
2020-05-21 04:27:31 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-21 17:56:01 +01:00
|
|
|
fn view(&self) -> Element<'_, Self::Message, Self::Theme, Self::Renderer> {
|
2020-05-21 04:27:31 +02:00
|
|
|
self.0.view()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 21:27:03 +01:00
|
|
|
impl<A> application::Application for Instance<A>
|
2019-11-21 18:00:27 +01:00
|
|
|
where
|
|
|
|
|
A: Application,
|
2024-03-07 20:11:32 +01:00
|
|
|
A::Theme: DefaultStyle,
|
2019-11-21 18:00:27 +01:00
|
|
|
{
|
2020-03-30 18:00:15 +02:00
|
|
|
type Flags = A::Flags;
|
2019-11-21 18:00:27 +01:00
|
|
|
|
2024-05-10 20:08:09 +02:00
|
|
|
fn name() -> &'static str {
|
|
|
|
|
A::name()
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-30 18:00:15 +02:00
|
|
|
fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
|
|
|
|
|
let (app, command) = A::new(flags);
|
2019-11-21 18:00:27 +01:00
|
|
|
|
|
|
|
|
(Instance(app), command)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn title(&self) -> String {
|
|
|
|
|
self.0.title()
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-14 01:47:55 +02:00
|
|
|
fn theme(&self) -> A::Theme {
|
|
|
|
|
self.0.theme()
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-06 21:27:03 +01:00
|
|
|
fn style(&self, theme: &A::Theme) -> Appearance {
|
|
|
|
|
self.0.style(theme)
|
2022-07-08 20:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-14 05:56:46 +01:00
|
|
|
fn subscription(&self) -> Subscription<Self::Message> {
|
|
|
|
|
self.0.subscription()
|
2019-12-05 06:10:13 +01:00
|
|
|
}
|
2020-06-12 22:12:15 +02:00
|
|
|
|
2020-06-19 19:17:05 +02:00
|
|
|
fn scale_factor(&self) -> f64 {
|
|
|
|
|
self.0.scale_factor()
|
|
|
|
|
}
|
2019-11-21 18:00:27 +01:00
|
|
|
}
|