use crate::{Command, Element, Executor, Settings, Subscription};
pub use iced_native::application::{Appearance, StyleSheet};
pub trait Application: Sized {
type Executor: Executor;
type Message: std::fmt::Debug + Send;
type Theme: Default + StyleSheet;
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 view(&self) -> Element<'_, Self::Message, crate::Renderer<Self::Theme>>;
fn theme(&self) -> Self::Theme {
Self::Theme::default()
}
fn style(&self) -> <Self::Theme as StyleSheet>::Style {
<Self::Theme as StyleSheet>::Style::default()
}
fn subscription(&self) -> Subscription<Self::Message> {
Subscription::none()
}
fn scale_factor(&self) -> f64 {
1.0
}
fn run(settings: Settings<Self::Flags>) -> crate::Result
where
Self: 'static,
{
#[allow(clippy::needless_update)]
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<Self::Theme>,
>(settings.into(), renderer_settings)?)
}
}
struct Instance<A: Application>(A);
impl<A> iced_winit::Program for Instance<A>
where
A: Application,
{
type Renderer = crate::Renderer<A::Theme>;
type Message = A::Message;
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
}
fn view(&self) -> Element<'_, Self::Message, Self::Renderer> {
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 theme(&self) -> A::Theme {
self.0.theme()
}
fn style(&self) -> <A::Theme as StyleSheet>::Style {
self.0.style()
}
fn subscription(&self) -> Subscription<Self::Message> {
self.0.subscription()
}
fn scale_factor(&self) -> f64 {
self.0.scale_factor()
}
}