Expose iced_pure through a pure feature in iced
Besides exposing the `iced_pure` crate, enabling the `pure` feature also
provides pure versions of both the `Application` and `Sandbox` traits!
🎉
This commit is contained in:
parent
897188317b
commit
66d69b5c9a
7 changed files with 344 additions and 14 deletions
|
|
@ -44,6 +44,8 @@ async-std = ["iced_futures/async-std"]
|
||||||
smol = ["iced_futures/smol"]
|
smol = ["iced_futures/smol"]
|
||||||
# Enables advanced color conversion via `palette`
|
# Enables advanced color conversion via `palette`
|
||||||
palette = ["iced_core/palette"]
|
palette = ["iced_core/palette"]
|
||||||
|
# Enables pure, virtual widgets in the `pure` module
|
||||||
|
pure = ["iced_pure"]
|
||||||
|
|
||||||
[badges]
|
[badges]
|
||||||
maintenance = { status = "actively-developed" }
|
maintenance = { status = "actively-developed" }
|
||||||
|
|
@ -98,6 +100,7 @@ iced_futures = { version = "0.3", path = "futures" }
|
||||||
iced_winit = { version = "0.3", path = "winit" }
|
iced_winit = { version = "0.3", path = "winit" }
|
||||||
iced_glutin = { version = "0.2", path = "glutin", optional = true }
|
iced_glutin = { version = "0.2", path = "glutin", optional = true }
|
||||||
iced_glow = { version = "0.2", path = "glow", optional = true }
|
iced_glow = { version = "0.2", path = "glow", optional = true }
|
||||||
|
iced_pure = { version = "0.1", path = "pure", optional = true }
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
|
|
||||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,4 @@ edition = "2021"
|
||||||
publish = false
|
publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced = { path = "../../.." }
|
iced = { path = "../../..", features = ["pure"] }
|
||||||
iced_pure = { path = "../../../pure" }
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use iced::{Alignment, Element, Sandbox, Settings};
|
use iced::pure::{Button, Column, Element, Sandbox, Text};
|
||||||
use iced_pure::{Button, Column, Pure, State, Text};
|
use iced::{Alignment, Settings};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
Counter::run(Settings::default())
|
Counter::run(Settings::default())
|
||||||
|
|
@ -7,7 +7,6 @@ pub fn main() -> iced::Result {
|
||||||
|
|
||||||
struct Counter {
|
struct Counter {
|
||||||
value: i32,
|
value: i32,
|
||||||
state: State<Message, iced::Renderer>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
|
@ -20,10 +19,7 @@ impl Sandbox for Counter {
|
||||||
type Message = Message;
|
type Message = Message;
|
||||||
|
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self { value: 0 }
|
||||||
value: 0,
|
|
||||||
state: State::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self) -> String {
|
fn title(&self) -> String {
|
||||||
|
|
@ -41,14 +37,13 @@ impl Sandbox for Counter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<'_, Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
let content = Column::new()
|
Column::new()
|
||||||
.padding(20)
|
.padding(20)
|
||||||
.align_items(Alignment::Center)
|
.align_items(Alignment::Center)
|
||||||
.push(Button::new("Increment").on_press(Message::IncrementPressed))
|
.push(Button::new("Increment").on_press(Message::IncrementPressed))
|
||||||
.push(Text::new(self.value.to_string()).size(50))
|
.push(Text::new(self.value.to_string()).size(50))
|
||||||
.push(Button::new("Decrement").on_press(Message::DecrementPressed));
|
.push(Button::new("Decrement").on_press(Message::DecrementPressed))
|
||||||
|
.into()
|
||||||
Pure::new(&mut self.state, content).into()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -195,6 +195,9 @@ pub mod time;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
pub mod window;
|
pub mod window;
|
||||||
|
|
||||||
|
#[cfg(feature = "pure")]
|
||||||
|
pub mod pure;
|
||||||
|
|
||||||
#[cfg(all(not(feature = "glow"), feature = "wgpu"))]
|
#[cfg(all(not(feature = "glow"), feature = "wgpu"))]
|
||||||
use iced_winit as runtime;
|
use iced_winit as runtime;
|
||||||
|
|
||||||
|
|
|
||||||
29
src/pure.rs
Normal file
29
src/pure.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
//! Leverage pure, virtual widgets in your application.
|
||||||
|
//!
|
||||||
|
//! The widgets found in this module are completely stateless versions of
|
||||||
|
//! [the original widgets].
|
||||||
|
//!
|
||||||
|
//! Effectively, this means that, as a user of the library, you do not need to
|
||||||
|
//! keep track of the local state of each widget (e.g. [`button::State`]).
|
||||||
|
//! Instead, the runtime will keep track of everything for you!
|
||||||
|
//!
|
||||||
|
//! You can embed pure widgets anywhere in your [impure `Application`] using the
|
||||||
|
//! [`Pure`] widget and some [`State`].
|
||||||
|
//!
|
||||||
|
//! In case you want to only use pure widgets in your application, this module
|
||||||
|
//! offers an alternate [`Application`] trait with a completely pure `view`
|
||||||
|
//! method.
|
||||||
|
//!
|
||||||
|
//! [the original widgets]: crate::widget
|
||||||
|
//! [`button::State`]: crate::widget::button::State
|
||||||
|
//! [impure `Application`]: crate::Application
|
||||||
|
pub use iced_pure::{Element as _, *};
|
||||||
|
|
||||||
|
/// A generic, pure [`Widget`].
|
||||||
|
pub type Element<Message> = iced_pure::Element<Message, crate::Renderer>;
|
||||||
|
|
||||||
|
mod application;
|
||||||
|
mod sandbox;
|
||||||
|
|
||||||
|
pub use application::Application;
|
||||||
|
pub use sandbox::Sandbox;
|
||||||
182
src/pure/application.rs
Normal file
182
src/pure/application.rs
Normal file
|
|
@ -0,0 +1,182 @@
|
||||||
|
use crate::pure::{self, Pure};
|
||||||
|
use crate::window;
|
||||||
|
use crate::{Color, Command, Executor, Settings, Subscription};
|
||||||
|
|
||||||
|
/// A pure version of [`Application`].
|
||||||
|
///
|
||||||
|
/// Unlike the impure version, the `view` method of this trait takes an
|
||||||
|
/// immutable reference to `self` and returns a pure [`Element`].
|
||||||
|
///
|
||||||
|
/// [`Application`]: crate::Application
|
||||||
|
/// [`Element`]: pure::Element
|
||||||
|
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 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 [`Application`].
|
||||||
|
///
|
||||||
|
/// 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 [`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 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 widgets to display in the [`Application`].
|
||||||
|
///
|
||||||
|
/// These widgets can produce __messages__ based on user interaction.
|
||||||
|
fn view(&self) -> pure::Element<Self::Message>;
|
||||||
|
|
||||||
|
/// Returns the current [`Application`] mode.
|
||||||
|
///
|
||||||
|
/// The runtime will automatically transition your application if a new mode
|
||||||
|
/// is returned.
|
||||||
|
///
|
||||||
|
/// Currently, the mode only has an effect in native platforms.
|
||||||
|
///
|
||||||
|
/// By default, an application will run in windowed mode.
|
||||||
|
fn mode(&self) -> window::Mode {
|
||||||
|
window::Mode::Windowed
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the background color of the [`Application`].
|
||||||
|
///
|
||||||
|
/// By default, it returns [`Color::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`.
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Runs the [`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,
|
||||||
|
{
|
||||||
|
<Instance<Self> as crate::Application>::run(settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Instance<A: Application> {
|
||||||
|
application: A,
|
||||||
|
state: pure::State<A::Message, crate::Renderer>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A> crate::Application for Instance<A>
|
||||||
|
where
|
||||||
|
A: Application,
|
||||||
|
A::Message: 'static,
|
||||||
|
{
|
||||||
|
type Executor = A::Executor;
|
||||||
|
type Message = A::Message;
|
||||||
|
type Flags = A::Flags;
|
||||||
|
|
||||||
|
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>) {
|
||||||
|
let (application, command) = A::new(flags);
|
||||||
|
|
||||||
|
(
|
||||||
|
Instance {
|
||||||
|
application,
|
||||||
|
state: pure::State::new(),
|
||||||
|
},
|
||||||
|
command,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn title(&self) -> String {
|
||||||
|
A::title(&self.application)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
||||||
|
A::update(&mut self.application, message)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&mut self) -> crate::Element<'_, Self::Message> {
|
||||||
|
let content = A::view(&self.application);
|
||||||
|
|
||||||
|
Pure::new(&mut self.state, content).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn mode(&self) -> window::Mode {
|
||||||
|
A::mode(&self.application)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn background_color(&self) -> Color {
|
||||||
|
A::background_color(&self.application)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scale_factor(&self) -> f64 {
|
||||||
|
A::scale_factor(&self.application)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_exit(&self) -> bool {
|
||||||
|
A::should_exit(&self.application)
|
||||||
|
}
|
||||||
|
}
|
||||||
119
src/pure/sandbox.rs
Normal file
119
src/pure/sandbox.rs
Normal file
|
|
@ -0,0 +1,119 @@
|
||||||
|
use crate::pure;
|
||||||
|
use crate::{Color, Command, Error, Settings, Subscription};
|
||||||
|
|
||||||
|
/// A pure version of [`Sandbox`].
|
||||||
|
///
|
||||||
|
/// Unlike the impure version, the `view` method of this trait takes an
|
||||||
|
/// immutable reference to `self` and returns a pure [`Element`].
|
||||||
|
///
|
||||||
|
/// [`Sandbox`]: crate::Sandbox
|
||||||
|
/// [`Element`]: pure::Element
|
||||||
|
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) -> pure::Element<Self::Message>;
|
||||||
|
|
||||||
|
/// Returns the background color of the [`Sandbox`].
|
||||||
|
///
|
||||||
|
/// By default, it returns [`Color::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`.
|
||||||
|
fn scale_factor(&self) -> f64 {
|
||||||
|
1.0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether the [`Sandbox`] should be terminated.
|
||||||
|
///
|
||||||
|
/// By default, it returns `false`.
|
||||||
|
fn should_exit(&self) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 pure::Application>::run(settings)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> pure::Application for T
|
||||||
|
where
|
||||||
|
T: Sandbox,
|
||||||
|
{
|
||||||
|
type Executor = iced_futures::backend::null::Executor;
|
||||||
|
type Flags = ();
|
||||||
|
type Message = T::Message;
|
||||||
|
|
||||||
|
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 subscription(&self) -> Subscription<T::Message> {
|
||||||
|
Subscription::none()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&self) -> pure::Element<T::Message> {
|
||||||
|
T::view(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn background_color(&self) -> Color {
|
||||||
|
T::background_color(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn scale_factor(&self) -> f64 {
|
||||||
|
T::scale_factor(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn should_exit(&self) -> bool {
|
||||||
|
T::should_exit(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue