Remove sandbox by making application more generic 🎉

This commit is contained in:
Héctor Ramón Jiménez 2024-03-16 19:14:13 +01:00
parent 0a24611ccd
commit 28a27f08ed
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
23 changed files with 80 additions and 111 deletions

View file

@ -7,7 +7,7 @@ use iced::widget::canvas::{
use iced::{Element, Length, Point, Rectangle, Renderer, Subscription, Theme}; use iced::{Element, Length, Point, Rectangle, Renderer, Subscription, Theme};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Arc - Iced", Arc::update, Arc::view) iced::application("Arc - Iced", Arc::update, Arc::view)
.subscription(Arc::subscription) .subscription(Arc::subscription)
.theme(|_| Theme::Dark) .theme(|_| Theme::Dark)
.antialiased() .antialiased()

View file

@ -3,7 +3,7 @@ use iced::widget::{button, column, text};
use iced::{Alignment, Element, Length}; use iced::{Alignment, Element, Length};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Bezier Tool - Iced", Example::update, Example::view) iced::application("Bezier Tool - Iced", Example::update, Example::view)
.antialiased() .antialiased()
.run() .run()
} }

View file

@ -4,7 +4,7 @@ use iced::{Element, Font, Length};
const ICON_FONT: Font = Font::with_name("icons"); const ICON_FONT: Font = Font::with_name("icons");
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Checkbox - Iced", Example::update, Example::view) iced::application("Checkbox - Iced", Example::update, Example::view)
.font(include_bytes!("../fonts/icons.ttf").as_slice()) .font(include_bytes!("../fonts/icons.ttf").as_slice())
.run() .run()
} }

View file

@ -8,7 +8,7 @@ use iced::{
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Clock - Iced", Clock::update, Clock::view) iced::application("Clock - Iced", Clock::update, Clock::view)
.subscription(Clock::subscription) .subscription(Clock::subscription)
.theme(Clock::theme) .theme(Clock::theme)
.antialiased() .antialiased()

View file

@ -13,7 +13,7 @@ use std::marker::PhantomData;
use std::ops::RangeInclusive; use std::ops::RangeInclusive;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox( iced::application(
"Color Palette - Iced", "Color Palette - Iced",
ColorPalette::update, ColorPalette::update,
ColorPalette::view, ColorPalette::view,

View file

@ -9,7 +9,11 @@ use iced::window;
use iced::{Alignment, Color, Element, Length, Subscription}; use iced::{Alignment, Color, Element, Length, Subscription};
fn main() -> iced::Result { fn main() -> iced::Result {
iced::sandbox("Custom Shader - Iced", IcedCubes::update, IcedCubes::view) iced::application(
"Custom Shader - Iced",
IcedCubes::update,
IcedCubes::view,
)
.subscription(IcedCubes::subscription) .subscription(IcedCubes::subscription)
.run() .run()
} }

View file

@ -4,7 +4,11 @@ use iced::widget::{button, column, container, progress_bar, text, Column};
use iced::{Alignment, Element, Length, Subscription}; use iced::{Alignment, Element, Length, Subscription};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Download Progress - Iced", Example::update, Example::view) iced::application(
"Download Progress - Iced",
Example::update,
Example::view,
)
.subscription(Example::subscription) .subscription(Example::subscription)
.run() .run()
} }

View file

@ -151,7 +151,7 @@ use iced::{Element, Length};
use rainbow::rainbow; use rainbow::rainbow;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::run("Custom 2D Geometry - Iced", |_, _| {}, view) iced::run("Custom 2D Geometry - Iced", |_: &mut _, _| {}, view)
} }
fn view(_state: &()) -> Element<'_, ()> { fn view(_state: &()) -> Element<'_, ()> {

View file

@ -10,7 +10,7 @@ use iced::{
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox(Layout::title, Layout::update, Layout::view) iced::application(Layout::title, Layout::update, Layout::view)
.subscription(Layout::subscription) .subscription(Layout::subscription)
.theme(Layout::theme) .theme(Layout::theme)
.run() .run()

View file

@ -11,7 +11,7 @@ use circular::Circular;
use linear::Linear; use linear::Linear;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox( iced::application(
"Loading Spinners - Iced", "Loading Spinners - Iced",
LoadingSpinners::update, LoadingSpinners::update,
LoadingSpinners::view, LoadingSpinners::view,

View file

@ -13,7 +13,7 @@ use std::collections::HashMap;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::sandbox("Multitouch - Iced", Multitouch::update, Multitouch::view) iced::application("Multitouch - Iced", Multitouch::update, Multitouch::view)
.antialiased() .antialiased()
.centered() .centered()
.run() .run()

View file

@ -7,7 +7,7 @@ use iced::widget::{
use iced::{Color, Element, Length, Size, Subscription}; use iced::{Color, Element, Length, Size, Subscription};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Pane Grid - Iced", Example::update, Example::view) iced::application("Pane Grid - Iced", Example::update, Example::view)
.subscription(Example::subscription) .subscription(Example::subscription)
.run() .run()
} }

View file

@ -4,7 +4,7 @@ use iced::widget::{
use iced::{Alignment, Element, Length, Theme}; use iced::{Alignment, Element, Length, Theme};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox( iced::application(
"QR Code Generator - Iced", "QR Code Generator - Iced",
QRGenerator::update, QRGenerator::update,
QRGenerator::view, QRGenerator::view,

View file

@ -8,7 +8,7 @@ use rand::Rng;
use std::fmt::Debug; use std::fmt::Debug;
fn main() -> iced::Result { fn main() -> iced::Result {
iced::sandbox( iced::application(
"Sierpinski Triangle - Iced", "Sierpinski Triangle - Iced",
SierpinskiEmulator::update, SierpinskiEmulator::update,
SierpinskiEmulator::view, SierpinskiEmulator::view,

View file

@ -22,7 +22,7 @@ use std::time::Instant;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::sandbox( iced::application(
"Solar System - Iced", "Solar System - Iced",
SolarSystem::update, SolarSystem::update,
SolarSystem::view, SolarSystem::view,

View file

@ -7,7 +7,7 @@ use iced::{Alignment, Element, Length, Subscription, Theme};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Stopwatch - Iced", Stopwatch::update, Stopwatch::view) iced::application("Stopwatch - Iced", Stopwatch::update, Stopwatch::view)
.subscription(Stopwatch::subscription) .subscription(Stopwatch::subscription)
.theme(Stopwatch::theme) .theme(Stopwatch::theme)
.run() .run()

View file

@ -6,7 +6,7 @@ use iced::widget::{
use iced::{Alignment, Element, Length, Theme}; use iced::{Alignment, Element, Length, Theme};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("Styling - Iced", Styling::update, Styling::view) iced::application("Styling - Iced", Styling::update, Styling::view)
.theme(Styling::theme) .theme(Styling::theme)
.run() .run()
} }

View file

@ -16,7 +16,7 @@ pub fn main() -> iced::Result {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
iced::sandbox(Tour::title, Tour::update, Tour::view) iced::application(Tour::title, Tour::update, Tour::view)
.centered() .centered()
.run() .run()
} }

View file

@ -3,7 +3,7 @@ use iced::widget::{container, text};
use iced::{Element, Length, Subscription}; use iced::{Element, Length, Subscription};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox("URL Handler - Iced", App::update, App::view) iced::application("URL Handler - Iced", App::update, App::view)
.subscription(App::subscription) .subscription(App::subscription)
.run() .run()
} }

View file

@ -6,7 +6,7 @@ use iced::widget::{
use iced::{Element, Length, Point, Rectangle, Renderer, Theme, Vector}; use iced::{Element, Length, Point, Rectangle, Renderer, Theme, Vector};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::sandbox( iced::application(
"Vectorial Text - Iced", "Vectorial Text - Iced",
VectorialText::update, VectorialText::update,
VectorialText::view, VectorialText::view,

View file

@ -112,6 +112,12 @@ impl<T> Command<T> {
} }
} }
impl<Message> From<()> for Command<Message> {
fn from(_value: ()) -> Self {
Self::none()
}
}
impl<T> fmt::Debug for Command<T> { impl<T> fmt::Debug for Command<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Command(command) = self; let Command(command) = self;

View file

@ -363,15 +363,15 @@ pub type Result = std::result::Result<(), Error>;
/// ``` /// ```
pub fn run<State, Message>( pub fn run<State, Message>(
title: impl program::Title<State> + 'static, title: impl program::Title<State> + 'static,
update: impl Fn(&mut State, Message) + 'static, update: impl program::Update<State, Message> + 'static,
view: impl for<'a> program::View<'a, State, Message> + 'static, view: impl for<'a> program::View<'a, State, Message> + 'static,
) -> Result ) -> Result
where where
State: Default + 'static, State: Default + 'static,
Message: std::fmt::Debug + Send + 'static, Message: std::fmt::Debug + Send + 'static,
{ {
sandbox(title, update, view).run() application(title, update, view).run()
} }
#[doc(inline)] #[doc(inline)]
pub use program::{application, sandbox}; pub use program::application;

View file

@ -15,7 +15,7 @@
//! use iced::Theme; //! use iced::Theme;
//! //!
//! pub fn main() -> iced::Result { //! pub fn main() -> iced::Result {
//! iced::sandbox("A counter", update, view) //! iced::application("A counter", update, view)
//! .theme(|_| Theme::Dark) //! .theme(|_| Theme::Dark)
//! .centered() //! .centered()
//! .run() //! .run()
@ -46,14 +46,14 @@ use crate::{Command, Element, Font, Result, Settings, Subscription};
use std::borrow::Cow; use std::borrow::Cow;
/// Creates the most basic kind of [`Program`] from some update and view logic. /// Creates an iced [`Program`] given its title, update, and view logic.
/// ///
/// # Example /// # Example
/// ```no_run /// ```no_run
/// use iced::widget::{button, column, text, Column}; /// use iced::widget::{button, column, text, Column};
/// ///
/// pub fn main() -> iced::Result { /// pub fn main() -> iced::Result {
/// iced::sandbox("A counter", update, view).run() /// iced::application("A counter", update, view).run()
/// } /// }
/// ///
/// #[derive(Debug, Clone)] /// #[derive(Debug, Clone)]
@ -74,78 +74,9 @@ use std::borrow::Cow;
/// ] /// ]
/// } /// }
/// ``` /// ```
pub fn sandbox<State, Message>(
title: impl Title<State>,
update: impl Fn(&mut State, Message),
view: impl for<'a> self::View<'a, State, Message>,
) -> Program<
impl Definition<State = State, Message = Message, Theme = crate::Theme>,
>
where
State: Default + 'static,
Message: Send + std::fmt::Debug,
{
use std::marker::PhantomData;
struct Sandbox<State, Message, Update, View> {
update: Update,
view: View,
_state: PhantomData<State>,
_message: PhantomData<Message>,
}
impl<State, Message, Update, View> Definition
for Sandbox<State, Message, Update, View>
where
State: Default + 'static,
Message: Send + std::fmt::Debug,
Update: Fn(&mut State, Message),
View: for<'a> self::View<'a, State, Message>,
{
type State = State;
type Message = Message;
type Theme = crate::Theme;
type Executor = iced_futures::backend::null::Executor;
fn build(&self) -> (Self::State, Command<Self::Message>) {
(State::default(), Command::none())
}
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Command<Self::Message> {
(self.update)(state, message);
Command::none()
}
fn view<'a>(
&self,
state: &'a Self::State,
) -> Element<'a, Self::Message, Self::Theme> {
self.view.view(state).into()
}
}
Program {
raw: Sandbox {
update,
view,
_state: PhantomData,
_message: PhantomData,
},
settings: Settings::default(),
}
.title(title)
}
/// Creates a [`Program`] that can leverage the [`Command`] API for
/// concurrent operations.
pub fn application<State, Message>( pub fn application<State, Message>(
title: impl Title<State>, title: impl Title<State>,
update: impl Fn(&mut State, Message) -> Command<Message>, update: impl Update<State, Message>,
view: impl for<'a> self::View<'a, State, Message>, view: impl for<'a> self::View<'a, State, Message>,
) -> Program< ) -> Program<
impl Definition<State = State, Message = Message, Theme = crate::Theme>, impl Definition<State = State, Message = Message, Theme = crate::Theme>,
@ -168,7 +99,7 @@ where
where where
State: Default, State: Default,
Message: Send + std::fmt::Debug, Message: Send + std::fmt::Debug,
Update: Fn(&mut State, Message) -> Command<Message>, Update: self::Update<State, Message>,
View: for<'a> self::View<'a, State, Message>, View: for<'a> self::View<'a, State, Message>,
{ {
type State = State; type State = State;
@ -185,7 +116,7 @@ where
state: &mut Self::State, state: &mut Self::State,
message: Self::Message, message: Self::Message,
) -> Command<Self::Message> { ) -> Command<Self::Message> {
(self.update)(state, message) self.update.update(state, message).into()
} }
fn view<'a>( fn view<'a>(
@ -704,18 +635,44 @@ where
} }
} }
/// The update logic of some [`Program`].
///
/// This trait allows [`application`] to take any closure that
/// returns any `Into<Command<Message>>`.
///
/// [`application`]: self::application()
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`]. /// The view logic of some [`Program`].
/// ///
/// This trait allows [`sandbox`] and [`application`] to /// This trait allows [`application`] to take any closure that
/// take any closure that returns any `Into<Element<'_, Message>>`. /// returns any `Into<Element<'_, Message>>`.
/// ///
/// [`application`]: self::application() /// [`application`]: self::application()
pub trait View<'a, State, Message> { pub trait View<'a, State, Message> {
/// The widget returned by the view logic.
type Widget: Into<Element<'a, Message>>;
/// Produces the widget of the [`Program`]. /// Produces the widget of the [`Program`].
fn view(&self, state: &'a State) -> Self::Widget; fn view(&self, state: &'a State) -> impl Into<Element<'a, Message>>;
} }
impl<'a, T, State, Message, Widget> View<'a, State, Message> for T impl<'a, T, State, Message, Widget> View<'a, State, Message> for T
@ -724,9 +681,7 @@ where
State: 'static, State: 'static,
Widget: Into<Element<'a, Message>>, Widget: Into<Element<'a, Message>>,
{ {
type Widget = Widget; fn view(&self, state: &'a State) -> impl Into<Element<'a, Message>> {
fn view(&self, state: &'a State) -> Self::Widget {
self(state) self(state)
} }
} }