Implement iced::Sandbox trait for simple apps
This commit is contained in:
parent
428509c84a
commit
ba56a561b2
4 changed files with 131 additions and 86 deletions
45
src/sandbox.rs
Normal file
45
src/sandbox.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::{Application, Command, Element};
|
||||
|
||||
pub trait Sandbox {
|
||||
type Message: std::fmt::Debug + Send;
|
||||
|
||||
fn new() -> Self;
|
||||
|
||||
fn title(&self) -> String;
|
||||
|
||||
fn update(&mut self, message: Self::Message);
|
||||
|
||||
fn view(&mut self) -> Element<Self::Message>;
|
||||
|
||||
fn run()
|
||||
where
|
||||
Self: 'static + Sized,
|
||||
{
|
||||
<Self as Application>::run()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Application for T
|
||||
where
|
||||
T: Sandbox,
|
||||
{
|
||||
type Message = T::Message;
|
||||
|
||||
fn new() -> (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 view(&mut self) -> Element<T::Message> {
|
||||
T::view(self)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue