use crate::application;
use crate::program::{self, Program};
#[cfg(feature = "winit")]
pub use crate::shell::program::{Appearance, DefaultStyle};
use crate::window;
use crate::{Element, Executor, Font, Settings, Subscription, Task};
#[cfg(not(feature = "winit"))]
use crate::runtime::{Appearance, DefaultStyle};
use std::borrow::Cow;
pub fn daemon<State, Message, Theme, Renderer>(
title: impl Title<State>,
update: impl application::Update<State, Message>,
view: impl for<'a> self::View<'a, State, Message, Theme, Renderer>,
) -> Daemon<impl Program<State = State, Message = Message, Theme = Theme>>
where
State: 'static,
Message: Send + std::fmt::Debug + 'static,
Theme: Default + DefaultStyle,
Renderer: program::Renderer,
{
use std::marker::PhantomData;
struct Instance<State, Message, Theme, Renderer, Update, View> {
update: Update,
view: View,
_state: PhantomData<State>,
_message: PhantomData<Message>,
_theme: PhantomData<Theme>,
_renderer: PhantomData<Renderer>,
}
impl<State, Message, Theme, Renderer, Update, View> Program
for Instance<State, Message, Theme, Renderer, Update, View>
where
Message: Send + std::fmt::Debug + 'static,
Theme: Default + DefaultStyle,
Renderer: program::Renderer,
Update: application::Update<State, Message>,
View: for<'a> self::View<'a, State, Message, Theme, Renderer>,
{
type State = State;
type Message = Message;
type Theme = Theme;
type Renderer = Renderer;
type Executor = iced_futures::backend::default::Executor;
fn update(
&self,
state: &mut Self::State,
message: Self::Message,
) -> Task<Self::Message> {
self.update.update(state, message).into()
}
fn view<'a>(
&self,
state: &'a Self::State,
window: window::Id,
) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> {
self.view.view(state, window).into()
}
}
Daemon {
raw: Instance {
update,
view,
_state: PhantomData,
_message: PhantomData,
_theme: PhantomData,
_renderer: PhantomData,
},
settings: Settings::default(),
}
.title(title)
}
#[derive(Debug)]
pub struct Daemon<P: Program> {
raw: P,
settings: Settings,
}
impl<P: Program> Daemon<P> {
#[cfg(feature = "winit")]
pub fn run(self) -> crate::Result
where
Self: 'static,
P::State: Default,
{
self.raw.run(self.settings, None)
}
#[cfg(feature = "winit")]
pub fn run_with<I>(self, initialize: I) -> crate::Result
where
Self: 'static,
I: FnOnce() -> (P::State, Task<P::Message>) + 'static,
{
self.raw.run_with(self.settings, None, initialize)
}
pub fn settings(self, settings: Settings) -> Self {
Self { settings, ..self }
}
pub fn antialiasing(self, antialiasing: bool) -> Self {
Self {
settings: Settings {
antialiasing,
..self.settings
},
..self
}
}
pub fn default_font(self, default_font: Font) -> Self {
Self {
settings: Settings {
default_font,
..self.settings
},
..self
}
}
pub fn font(mut self, font: impl Into<Cow<'static, [u8]>>) -> Self {
self.settings.fonts.push(font.into());
self
}
pub(crate) fn title(
self,
title: impl Title<P::State>,
) -> Daemon<
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
Daemon {
raw: program::with_title(self.raw, move |state, window| {
title.title(state, window)
}),
settings: self.settings,
}
}
pub fn subscription(
self,
f: impl Fn(&P::State) -> Subscription<P::Message>,
) -> Daemon<
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
Daemon {
raw: program::with_subscription(self.raw, f),
settings: self.settings,
}
}
pub fn theme(
self,
f: impl Fn(&P::State, window::Id) -> P::Theme,
) -> Daemon<
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
Daemon {
raw: program::with_theme(self.raw, f),
settings: self.settings,
}
}
pub fn style(
self,
f: impl Fn(&P::State, &P::Theme) -> Appearance,
) -> Daemon<
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
Daemon {
raw: program::with_style(self.raw, f),
settings: self.settings,
}
}
pub fn scale_factor(
self,
f: impl Fn(&P::State, window::Id) -> f64,
) -> Daemon<
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
> {
Daemon {
raw: program::with_scale_factor(self.raw, f),
settings: self.settings,
}
}
pub fn executor<E>(
self,
) -> Daemon<
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
>
where
E: Executor,
{
Daemon {
raw: program::with_executor::<P, E>(self.raw),
settings: self.settings,
}
}
}
pub trait Title<State> {
fn title(&self, state: &State, window: window::Id) -> String;
}
impl<State> Title<State> for &'static str {
fn title(&self, _state: &State, _window: window::Id) -> String {
self.to_string()
}
}
impl<T, State> Title<State> for T
where
T: Fn(&State, window::Id) -> String,
{
fn title(&self, state: &State, window: window::Id) -> String {
self(state, window)
}
}
pub trait View<'a, State, Message, Theme, Renderer> {
fn view(
&self,
state: &'a State,
window: window::Id,
) -> impl Into<Element<'a, Message, Theme, Renderer>>;
}
impl<'a, T, State, Message, Theme, Renderer, Widget>
View<'a, State, Message, Theme, Renderer> for T
where
T: Fn(&'a State, window::Id) -> Widget,
State: 'static,
Widget: Into<Element<'a, Message, Theme, Renderer>>,
{
fn view(
&self,
state: &'a State,
window: window::Id,
) -> impl Into<Element<'a, Message, Theme, Renderer>> {
self(state, window)
}
}