summaryrefslogtreecommitdiffstats
path: root/src/application.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/application.rs')
-rw-r--r--src/application.rs132
1 files changed, 74 insertions, 58 deletions
diff --git a/src/application.rs b/src/application.rs
index d46cd2ac..78280e98 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -1,5 +1,7 @@
use crate::window;
-use crate::{Color, Command, Element, Executor, Settings, Subscription};
+use crate::{
+ Clipboard, Color, Command, Element, Executor, Menu, Settings, Subscription,
+};
/// An interactive cross-platform application.
///
@@ -11,15 +13,13 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// document.
///
/// An [`Application`] can execute asynchronous actions by returning a
-/// [`Command`](struct.Command.html) in some of its methods. If
-/// you do not intend to perform any background work in your program, the
-/// [`Sandbox`](trait.Sandbox.html) trait offers a simplified interface.
+/// [`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`.
///
-/// [`Application`]: trait.Application.html
-///
/// # Examples
/// [The repository has a bunch of examples] that use the [`Application`] trait:
///
@@ -29,6 +29,8 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// a dummy file of 100 MB and tracks the download progress.
/// - [`events`], a log of native events displayed using a conditional
/// [`Subscription`].
+/// - [`game_of_life`], an interactive version of the [Game of Life], invented
+/// by [John Horton Conway].
/// - [`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
@@ -37,17 +39,18 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// to listen to time.
/// - [`todos`], a todos tracker inspired by [TodoMVC].
///
-/// [The repository has a bunch of examples]: https://github.com/hecrj/iced/tree/0.1/examples
-/// [`clock`]: https://github.com/hecrj/iced/tree/0.1/examples/clock
-/// [`download_progress`]: https://github.com/hecrj/iced/tree/0.1/examples/download_progress
-/// [`events`]: https://github.com/hecrj/iced/tree/0.1/examples/events
-/// [`pokedex`]: https://github.com/hecrj/iced/tree/0.1/examples/pokedex
-/// [`solar_system`]: https://github.com/hecrj/iced/tree/0.1/examples/solar_system
-/// [`stopwatch`]: https://github.com/hecrj/iced/tree/0.1/examples/stopwatch
-/// [`todos`]: https://github.com/hecrj/iced/tree/0.1/examples/todos
-/// [`Canvas`]: widget/canvas/struct.Canvas.html
+/// [The repository has a bunch of examples]: https://github.com/hecrj/iced/tree/0.3/examples
+/// [`clock`]: https://github.com/hecrj/iced/tree/0.3/examples/clock
+/// [`download_progress`]: https://github.com/hecrj/iced/tree/0.3/examples/download_progress
+/// [`events`]: https://github.com/hecrj/iced/tree/0.3/examples/events
+/// [`game_of_life`]: https://github.com/hecrj/iced/tree/0.3/examples/game_of_life
+/// [`pokedex`]: https://github.com/hecrj/iced/tree/0.3/examples/pokedex
+/// [`solar_system`]: https://github.com/hecrj/iced/tree/0.3/examples/solar_system
+/// [`stopwatch`]: https://github.com/hecrj/iced/tree/0.3/examples/stopwatch
+/// [`todos`]: https://github.com/hecrj/iced/tree/0.3/examples/todos
+/// [`Sandbox`]: crate::Sandbox
+/// [`Canvas`]: crate::widget::Canvas
/// [PokéAPI]: https://pokeapi.co/
-/// [`Subscription`]: type.Subscription.html
/// [TodoMVC]: http://todomvc.com/
///
/// ## A simple "Hello, world!"
@@ -56,7 +59,7 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// says "Hello, world!":
///
/// ```no_run
-/// use iced::{executor, Application, Command, Element, Settings, Text};
+/// use iced::{executor, Application, Clipboard, Command, Element, Settings, Text};
///
/// pub fn main() -> iced::Result {
/// Hello::run(Settings::default())
@@ -65,7 +68,7 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// struct Hello;
///
/// impl Application for Hello {
-/// type Executor = executor::Null;
+/// type Executor = executor::Default;
/// type Message = ();
/// type Flags = ();
///
@@ -77,7 +80,7 @@ use crate::{Color, Command, Element, Executor, Settings, Subscription};
/// String::from("A cool application")
/// }
///
-/// fn update(&mut self, _message: Self::Message) -> Command<Self::Message> {
+/// fn update(&mut self, _message: Self::Message, _clipboard: &mut Clipboard) -> Command<Self::Message> {
/// Command::none()
/// }
///
@@ -91,18 +94,14 @@ pub trait Application: Sized {
///
/// The [default executor] can be a good starting point!
///
- /// [`Executor`]: trait.Executor.html
- /// [default executor]: executor/struct.Default.html
+ /// [`Executor`]: Self::Executor
+ /// [default executor]: crate::executor::Default
type Executor: Executor;
/// The type of __messages__ your [`Application`] will produce.
- ///
- /// [`Application`]: trait.Application.html
- type Message: std::fmt::Debug + Send;
+ type Message: std::fmt::Debug + Clone + Send;
/// The data needed to initialize your [`Application`].
- ///
- /// [`Application`]: trait.Application.html
type Flags;
/// Initializes the [`Application`] with the flags provided to
@@ -110,22 +109,17 @@ pub trait Application: Sized {
///
/// Here is where you should return the initial state of your app.
///
- /// Additionally, you can return a [`Command`](struct.Command.html) 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.
+ /// 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.
///
- /// [`Application`]: trait.Application.html
- /// [`run`]: #method.run.html
- /// [`Settings`]: struct.Settings.html
+ /// [`run`]: Self::run
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
/// Returns the current title of the [`Application`].
///
/// This title can be dynamic! The runtime will automatically update the
/// title of your application when necessary.
- ///
- /// [`Application`]: trait.Application.html
fn title(&self) -> String;
/// Handles a __message__ and updates the state of the [`Application`].
@@ -135,10 +129,11 @@ pub trait Application: Sized {
/// this method.
///
/// Any [`Command`] returned will be executed immediately in the background.
- ///
- /// [`Application`]: trait.Application.html
- /// [`Command`]: struct.Command.html
- fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
+ fn update(
+ &mut self,
+ message: Self::Message,
+ clipboard: &mut Clipboard,
+ ) -> Command<Self::Message>;
/// Returns the event [`Subscription`] for the current state of the
/// application.
@@ -148,8 +143,6 @@ pub trait Application: Sized {
/// [`update`](#tymethod.update).
///
/// By default, this method returns an empty [`Subscription`].
- ///
- /// [`Subscription`]: struct.Subscription.html
fn subscription(&self) -> Subscription<Self::Message> {
Subscription::none()
}
@@ -157,8 +150,6 @@ pub trait Application: Sized {
/// Returns the widgets to display in the [`Application`].
///
/// These widgets can produce __messages__ based on user interaction.
- ///
- /// [`Application`]: trait.Application.html
fn view(&mut self) -> Element<'_, Self::Message>;
/// Returns the current [`Application`] mode.
@@ -169,8 +160,6 @@ pub trait Application: Sized {
/// Currently, the mode only has an effect in native platforms.
///
/// By default, an application will run in windowed mode.
- ///
- /// [`Application`]: trait.Application.html
fn mode(&self) -> window::Mode {
window::Mode::Windowed
}
@@ -178,9 +167,6 @@ pub trait Application: Sized {
/// Returns the background color of the [`Application`].
///
/// By default, it returns [`Color::WHITE`].
- ///
- /// [`Application`]: trait.Application.html
- /// [`Color::WHITE`]: struct.Color.html#const.WHITE
fn background_color(&self) -> Color {
Color::WHITE
}
@@ -194,12 +180,24 @@ pub trait Application: Sized {
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
- ///
- /// [`Application`]: trait.Application.html
fn scale_factor(&self) -> f64 {
1.0
}
+ /// Returns whether the [`Application`] should be terminated.
+ ///
+ /// By default, it returns `false`.
+ fn should_exit(&self) -> bool {
+ false
+ }
+
+ /// Returns the current system [`Menu`] of the [`Application`].
+ ///
+ /// By default, it returns an empty [`Menu`].
+ fn menu(&self) -> Menu<Self::Message> {
+ Menu::new()
+ }
+
/// Runs the [`Application`].
///
/// On native platforms, this method will take control of the current thread
@@ -207,8 +205,7 @@ pub trait Application: Sized {
///
/// It should probably be that last thing you call in your `main` function.
///
- /// [`Application`]: trait.Application.html
- /// [`Error`]: enum.Error.html
+ /// [`Error`]: crate::Error
fn run(settings: Settings<Self::Flags>) -> crate::Result
where
Self: 'static,
@@ -218,12 +215,13 @@ pub trait Application: Sized {
let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
+ text_multithreading: settings.text_multithreading,
antialiasing: if settings.antialiasing {
Some(crate::renderer::settings::Antialiasing::MSAAx4)
} else {
None
},
- ..crate::renderer::Settings::default()
+ ..crate::renderer::Settings::from_env()
};
Ok(crate::runtime::application::run::<
@@ -251,9 +249,14 @@ where
{
type Renderer = crate::renderer::Renderer;
type Message = A::Message;
-
- fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
- self.0.update(message)
+ type Clipboard = iced_winit::Clipboard;
+
+ fn update(
+ &mut self,
+ message: Self::Message,
+ clipboard: &mut iced_winit::Clipboard,
+ ) -> Command<Self::Message> {
+ self.0.update(message, clipboard)
}
fn view(&mut self) -> Element<'_, Self::Message> {
@@ -282,6 +285,7 @@ where
match self.0.mode() {
window::Mode::Windowed => iced_winit::Mode::Windowed,
window::Mode::Fullscreen => iced_winit::Mode::Fullscreen,
+ window::Mode::Hidden => iced_winit::Mode::Hidden,
}
}
@@ -296,6 +300,14 @@ where
fn scale_factor(&self) -> f64 {
self.0.scale_factor()
}
+
+ fn should_exit(&self) -> bool {
+ self.0.should_exit()
+ }
+
+ fn menu(&self) -> Menu<Self::Message> {
+ self.0.menu()
+ }
}
#[cfg(target_arch = "wasm32")]
@@ -317,8 +329,12 @@ where
self.0.title()
}
- fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
- self.0.update(message)
+ fn update(
+ &mut self,
+ message: Self::Message,
+ clipboard: &mut Clipboard,
+ ) -> Command<Self::Message> {
+ self.0.update(message, clipboard)
}
fn subscription(&self) -> Subscription<Self::Message> {