summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/application.rs79
-rw-r--r--src/element.rs2
-rw-r--r--src/keyboard.rs6
-rw-r--r--src/lib.rs39
-rw-r--r--src/mouse.rs2
-rw-r--r--src/sandbox.rs37
-rw-r--r--src/settings.rs34
-rw-r--r--src/time.rs14
-rw-r--r--src/widget.rs31
-rw-r--r--src/window/settings.rs24
10 files changed, 205 insertions, 63 deletions
diff --git a/src/application.rs b/src/application.rs
index 689332f1..47b89dbd 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -1,4 +1,6 @@
-use crate::{window, Command, Element, Executor, Settings, Subscription};
+use crate::{
+ window, Color, Command, Element, Executor, Settings, Subscription,
+};
/// An interactive cross-platform application.
///
@@ -174,6 +176,31 @@ pub trait Application: Sized {
window::Mode::Windowed
}
+ /// 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
+ }
+
+ /// 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`.
+ ///
+ /// [`Application`]: trait.Application.html
+ fn scale_factor(&self) -> f64 {
+ 1.0
+ }
+
/// Runs the [`Application`].
///
/// On native platforms, this method will take control of the current thread
@@ -188,20 +215,22 @@ pub trait Application: Sized {
{
#[cfg(not(target_arch = "wasm32"))]
{
- let wgpu_settings = iced_wgpu::Settings {
+ let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font,
+ default_text_size: settings.default_text_size,
antialiasing: if settings.antialiasing {
- Some(iced_wgpu::settings::Antialiasing::MSAAx4)
+ Some(crate::renderer::settings::Antialiasing::MSAAx4)
} else {
None
},
- ..iced_wgpu::Settings::default()
+ ..crate::renderer::Settings::default()
};
- <Instance<Self> as iced_winit::Application>::run(
- settings.into(),
- wgpu_settings,
- );
+ crate::runtime::application::run::<
+ Instance<Self>,
+ Self::Executor,
+ crate::renderer::window::Compositor,
+ >(settings.into(), renderer_settings);
}
#[cfg(target_arch = "wasm32")]
@@ -212,15 +241,29 @@ pub trait Application: Sized {
struct Instance<A: Application>(A);
#[cfg(not(target_arch = "wasm32"))]
-impl<A> iced_winit::Application for Instance<A>
+impl<A> iced_winit::Program for Instance<A>
where
A: Application,
{
- type Backend = iced_wgpu::window::Backend;
- type Executor = A::Executor;
- type Flags = A::Flags;
+ type Renderer = crate::renderer::Renderer;
type Message = A::Message;
+ fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
+ self.0.update(message)
+ }
+
+ fn view(&mut self) -> Element<'_, Self::Message> {
+ self.0.view()
+ }
+}
+
+#[cfg(not(target_arch = "wasm32"))]
+impl<A> crate::runtime::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);
@@ -238,16 +281,16 @@ where
}
}
- fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
- self.0.update(message)
- }
-
fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription()
}
- fn view(&mut self) -> Element<'_, Self::Message> {
- self.0.view()
+ fn background_color(&self) -> Color {
+ self.0.background_color()
+ }
+
+ fn scale_factor(&self) -> f64 {
+ self.0.scale_factor()
}
}
diff --git a/src/element.rs b/src/element.rs
index e5356fb6..6f47c701 100644
--- a/src/element.rs
+++ b/src/element.rs
@@ -3,7 +3,7 @@
/// This is an alias of an `iced_native` element with a default `Renderer`.
#[cfg(not(target_arch = "wasm32"))]
pub type Element<'a, Message> =
- iced_winit::Element<'a, Message, iced_wgpu::Renderer>;
+ crate::runtime::Element<'a, Message, crate::renderer::Renderer>;
#[cfg(target_arch = "wasm32")]
pub use iced_web::Element;
diff --git a/src/keyboard.rs b/src/keyboard.rs
index 181dd974..0b3e894d 100644
--- a/src/keyboard.rs
+++ b/src/keyboard.rs
@@ -1,6 +1,2 @@
//! Listen and react to keyboard events.
-#[cfg(not(target_arch = "wasm32"))]
-pub use iced_winit::input::keyboard::{KeyCode, ModifiersState};
-
-#[cfg(target_arch = "wasm32")]
-pub use iced_web::keyboard::{KeyCode, ModifiersState};
+pub use crate::runtime::keyboard::{Event, KeyCode, ModifiersState};
diff --git a/src/lib.rs b/src/lib.rs
index 4f66cc73..d08b39cf 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -185,10 +185,41 @@ mod sandbox;
pub mod executor;
pub mod keyboard;
+pub mod mouse;
pub mod settings;
pub mod widget;
pub mod window;
+#[cfg(all(
+ any(feature = "tokio", feature = "async-std"),
+ not(target_arch = "wasm32")
+))]
+#[cfg_attr(docsrs, doc(cfg(any(feature = "tokio", feature = "async-std"))))]
+pub mod time;
+
+#[cfg(all(
+ not(target_arch = "wasm32"),
+ not(feature = "glow"),
+ feature = "wgpu"
+))]
+use iced_winit as runtime;
+
+#[cfg(all(not(target_arch = "wasm32"), feature = "glow"))]
+use iced_glutin as runtime;
+
+#[cfg(all(
+ not(target_arch = "wasm32"),
+ not(feature = "glow"),
+ feature = "wgpu"
+))]
+use iced_wgpu as renderer;
+
+#[cfg(all(not(target_arch = "wasm32"), feature = "glow"))]
+use iced_glow as renderer;
+
+#[cfg(target_arch = "wasm32")]
+use iced_web as runtime;
+
#[doc(no_inline)]
pub use widget::*;
@@ -198,13 +229,7 @@ pub use executor::Executor;
pub use sandbox::Sandbox;
pub use settings::Settings;
-#[cfg(not(target_arch = "wasm32"))]
-use iced_winit as runtime;
-
-#[cfg(target_arch = "wasm32")]
-use iced_web as runtime;
-
pub use runtime::{
futures, Align, Background, Color, Command, Font, HorizontalAlignment,
- Length, Point, Size, Subscription, Vector, VerticalAlignment,
+ Length, Point, Rectangle, Size, Subscription, Vector, VerticalAlignment,
};
diff --git a/src/mouse.rs b/src/mouse.rs
new file mode 100644
index 00000000..d61ed09a
--- /dev/null
+++ b/src/mouse.rs
@@ -0,0 +1,2 @@
+//! Listen and react to mouse events.
+pub use crate::runtime::mouse::{Button, Event, Interaction, ScrollDelta};
diff --git a/src/sandbox.rs b/src/sandbox.rs
index c6fa45d0..6a73eab0 100644
--- a/src/sandbox.rs
+++ b/src/sandbox.rs
@@ -1,4 +1,6 @@
-use crate::{executor, Application, Command, Element, Settings, Subscription};
+use crate::{
+ executor, Application, Color, Command, Element, Settings, Subscription,
+};
/// A sandboxed [`Application`].
///
@@ -124,6 +126,31 @@ pub trait Sandbox {
/// [`Sandbox`]: trait.Sandbox.html
fn view(&mut self) -> Element<'_, Self::Message>;
+ /// Returns the background color of the [`Sandbox`].
+ ///
+ /// By default, it returns [`Color::WHITE`].
+ ///
+ /// [`Sandbox`]: trait.Sandbox.html
+ /// [`Color::WHITE`]: struct.Color.html#const.WHITE
+ fn background_color(&self) -> Color {
+ Color::WHITE
+ }
+
+ /// 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`.
+ ///
+ /// [`Sandbox`]: trait.Sandbox.html
+ fn scale_factor(&self) -> f64 {
+ 1.0
+ }
+
/// Runs the [`Sandbox`].
///
/// On native platforms, this method will take control of the current thread
@@ -169,4 +196,12 @@ where
fn view(&mut self) -> Element<'_, T::Message> {
T::view(self)
}
+
+ fn background_color(&self) -> Color {
+ T::background_color(self)
+ }
+
+ fn scale_factor(&self) -> f64 {
+ T::scale_factor(self)
+ }
}
diff --git a/src/settings.rs b/src/settings.rs
index 01ad0ee0..d7ff4cab 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -2,7 +2,7 @@
use crate::window;
/// The settings of an application.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Settings<Flags> {
/// The window settings.
///
@@ -22,6 +22,11 @@ pub struct Settings<Flags> {
// TODO: Add `name` for web compatibility
pub default_font: Option<&'static [u8]>,
+ /// The text size that will be used by default.
+ ///
+ /// The default value is 20.
+ pub default_text_size: u16,
+
/// If set to true, the renderer will try to perform antialiasing for some
/// primitives.
///
@@ -39,12 +44,28 @@ impl<Flags> Settings<Flags> {
///
/// [`Application`]: ../trait.Application.html
pub fn with_flags(flags: Flags) -> Self {
+ let default_settings = Settings::<()>::default();
+
Self {
flags,
- // not using ..Default::default() struct update syntax since it is more permissive to
- // allow initializing with flags without trait bound on Default
+ antialiasing: default_settings.antialiasing,
+ default_font: default_settings.default_font,
+ default_text_size: default_settings.default_text_size,
+ window: default_settings.window,
+ }
+ }
+}
+
+impl<Flags> Default for Settings<Flags>
+where
+ Flags: Default,
+{
+ fn default() -> Self {
+ Self {
+ flags: Default::default(),
antialiasing: Default::default(),
default_font: Default::default(),
+ default_text_size: 20,
window: Default::default(),
}
}
@@ -54,12 +75,7 @@ impl<Flags> Settings<Flags> {
impl<Flags> From<Settings<Flags>> for iced_winit::Settings<Flags> {
fn from(settings: Settings<Flags>) -> iced_winit::Settings<Flags> {
iced_winit::Settings {
- window: iced_winit::settings::Window {
- size: settings.window.size,
- resizable: settings.window.resizable,
- decorations: settings.window.decorations,
- platform_specific: Default::default(),
- },
+ window: settings.window.into(),
flags: settings.flags,
}
}
diff --git a/src/time.rs b/src/time.rs
new file mode 100644
index 00000000..cd442461
--- /dev/null
+++ b/src/time.rs
@@ -0,0 +1,14 @@
+//! Listen and react to time.
+use crate::Subscription;
+
+/// Returns a [`Subscription`] that produces messages at a set interval.
+///
+/// The first message is produced after a `duration`, and then continues to
+/// produce more messages every `duration` after that.
+///
+/// [`Subscription`]: ../subscription/struct.Subscription.html
+pub fn every(
+ duration: std::time::Duration,
+) -> Subscription<std::time::Instant> {
+ iced_futures::time::every(duration)
+}
diff --git a/src/widget.rs b/src/widget.rs
index 03e3192b..3e4d4788 100644
--- a/src/widget.rs
+++ b/src/widget.rs
@@ -18,29 +18,30 @@
//! [`text_input::State`]: text_input/struct.State.html
#[cfg(not(target_arch = "wasm32"))]
mod platform {
- pub use iced_wgpu::widget::{
+ pub use crate::renderer::widget::{
button, checkbox, container, pane_grid, progress_bar, radio,
- scrollable, slider, text_input,
+ scrollable, slider, text_input, Column, Row, Space, Text,
};
- #[cfg(feature = "canvas")]
- #[cfg_attr(docsrs, doc(cfg(feature = "canvas")))]
- pub use iced_wgpu::widget::canvas;
+ #[cfg(any(feature = "canvas", feature = "glow_canvas"))]
+ #[cfg_attr(
+ docsrs,
+ doc(cfg(any(feature = "canvas", feature = "glow_canvas")))
+ )]
+ pub use crate::renderer::widget::canvas;
#[cfg_attr(docsrs, doc(cfg(feature = "image")))]
pub mod image {
//! Display images in your user interface.
- pub use iced_winit::image::{Handle, Image};
+ pub use crate::runtime::image::{Handle, Image};
}
#[cfg_attr(docsrs, doc(cfg(feature = "svg")))]
pub mod svg {
//! Display vector graphics in your user interface.
- pub use iced_winit::svg::{Handle, Svg};
+ pub use crate::runtime::svg::{Handle, Svg};
}
- pub use iced_winit::{Space, Text};
-
#[doc(no_inline)]
pub use {
button::Button, checkbox::Checkbox, container::Container, image::Image,
@@ -52,18 +53,6 @@ mod platform {
#[cfg(feature = "canvas")]
#[doc(no_inline)]
pub use canvas::Canvas;
-
- /// A container that distributes its contents vertically.
- ///
- /// This is an alias of an `iced_native` column with a default `Renderer`.
- pub type Column<'a, Message> =
- iced_winit::Column<'a, Message, iced_wgpu::Renderer>;
-
- /// A container that distributes its contents horizontally.
- ///
- /// This is an alias of an `iced_native` row with a default `Renderer`.
- pub type Row<'a, Message> =
- iced_winit::Row<'a, Message, iced_wgpu::Renderer>;
}
#[cfg(target_arch = "wasm32")]
diff --git a/src/window/settings.rs b/src/window/settings.rs
index a31d2af2..eb997899 100644
--- a/src/window/settings.rs
+++ b/src/window/settings.rs
@@ -1,9 +1,15 @@
/// The window settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Settings {
- /// The size of the window.
+ /// The initial size of the window.
pub size: (u32, u32),
+ /// The minimum size of the window.
+ pub min_size: Option<(u32, u32)>,
+
+ /// The maximum size of the window.
+ pub max_size: Option<(u32, u32)>,
+
/// Whether the window should be resizable or not.
pub resizable: bool,
@@ -15,8 +21,24 @@ impl Default for Settings {
fn default() -> Settings {
Settings {
size: (1024, 768),
+ min_size: None,
+ max_size: None,
resizable: true,
decorations: true,
}
}
}
+
+#[cfg(not(target_arch = "wasm32"))]
+impl From<Settings> for iced_winit::settings::Window {
+ fn from(settings: Settings) -> Self {
+ Self {
+ size: settings.size,
+ min_size: settings.min_size,
+ max_size: settings.max_size,
+ resizable: settings.resizable,
+ decorations: settings.decorations,
+ platform_specific: Default::default(),
+ }
+ }
+}