use crate::window;
use crate::{Color, Command, Element, 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(&mut self) -> 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,
{
let renderer_settings = crate::renderer::Settings {
default_font: settings.default_font,
default_text_size: settings.default_text_size,
text_multithreading: settings.text_multithreading,
antialiasing: if settings.antialiasing {
Some(crate::renderer::settings::Antialiasing::MSAAx4)
} else {
None
},
..crate::renderer::Settings::from_env()
};
Ok(crate::runtime::application::run::<
Instance<Self>,
Self::Executor,
crate::renderer::window::Compositor,
>(settings.into(), renderer_settings)?)
}
}
struct Instance<A: Application>(A);
impl<A> iced_winit::Program for Instance<A>
where
A: Application,
{
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()
}
}
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);
(Instance(app), command)
}
fn title(&self) -> String {
self.0.title()
}
fn mode(&self) -> iced_winit::Mode {
match self.0.mode() {
window::Mode::Windowed => iced_winit::Mode::Windowed,
window::Mode::Fullscreen => iced_winit::Mode::Fullscreen,
window::Mode::Hidden => iced_winit::Mode::Hidden,
}
}
fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription()
}
fn background_color(&self) -> Color {
self.0.background_color()
}
fn scale_factor(&self) -> f64 {
self.0.scale_factor()
}
fn should_exit(&self) -> bool {
self.0.should_exit()
}
}