use crate::pure::{self, Pure};
use crate::window;
use crate::{Color, Command, Executor, Settings, Subscription};
pub trait Application: Sized {
type Executor: Executor;
type Message: std::fmt::Debug + Send;
type Flags;
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
fn title(&self) -> String;
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
fn subscription(&self) -> Subscription<Self::Message> {
Subscription::none()
}
fn view(&self) -> pure::Element<'_, Self::Message>;
fn mode(&self) -> window::Mode {
window::Mode::Windowed
}
fn background_color(&self) -> Color {
Color::WHITE
}
fn scale_factor(&self) -> f64 {
1.0
}
fn should_exit(&self) -> bool {
false
}
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,
}
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 subscription(&self) -> Subscription<Self::Message> {
A::subscription(&self.application)
}
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)
}
}