diff --git a/src/application.rs b/src/application.rs index d0f7730447..2ba764bed6 100644 --- a/src/application.rs +++ b/src/application.rs @@ -32,7 +32,9 @@ //! ``` use crate::program::{self, Program}; use crate::window; -use crate::{Element, Font, Result, Settings, Size, Subscription, Task}; +use crate::{ + Element, Executor, Font, Result, Settings, Size, Subscription, Task, +}; use std::borrow::Cow; @@ -376,6 +378,22 @@ impl Application

{ window: self.window, } } + + /// Sets the executor of the [`Application`]. + pub fn executor( + self, + ) -> Application< + impl Program, + > + where + E: Executor, + { + Application { + raw: program::with_executor::(self.raw), + settings: self.settings, + window: self.window, + } + } } /// The title logic of some [`Application`]. diff --git a/src/daemon.rs b/src/daemon.rs index 6a6ad13327..81254bf947 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -2,7 +2,7 @@ use crate::application; use crate::program::{self, Program}; use crate::window; -use crate::{Element, Font, Result, Settings, Subscription, Task}; +use crate::{Element, Executor, Font, Result, Settings, Subscription, Task}; use std::borrow::Cow; @@ -223,6 +223,21 @@ impl Daemon

{ settings: self.settings, } } + + /// Sets the executor of the [`Daemon`]. + pub fn executor( + self, + ) -> Daemon< + impl Program, + > + where + E: Executor, + { + Daemon { + raw: program::with_executor::(self.raw), + settings: self.settings, + } + } } /// The title logic of some [`Daemon`]. diff --git a/src/program.rs b/src/program.rs index 2b697fbede..94cb9a7d33 100644 --- a/src/program.rs +++ b/src/program.rs @@ -550,6 +550,80 @@ pub fn with_scale_factor( } } +pub fn with_executor( + program: P, +) -> impl Program { + use std::marker::PhantomData; + + struct WithExecutor { + program: P, + executor: PhantomData, + } + + impl Program for WithExecutor + where + E: Executor, + { + type State = P::State; + type Message = P::Message; + type Theme = P::Theme; + type Renderer = P::Renderer; + type Executor = E; + + fn title(&self, state: &Self::State, window: window::Id) -> String { + self.program.title(state, window) + } + + fn update( + &self, + state: &mut Self::State, + message: Self::Message, + ) -> Task { + self.program.update(state, message) + } + + fn view<'a>( + &self, + state: &'a Self::State, + window: window::Id, + ) -> Element<'a, Self::Message, Self::Theme, Self::Renderer> { + self.program.view(state, window) + } + + fn subscription( + &self, + state: &Self::State, + ) -> Subscription { + self.program.subscription(state) + } + + fn theme( + &self, + state: &Self::State, + window: window::Id, + ) -> Self::Theme { + self.program.theme(state, window) + } + + fn style( + &self, + state: &Self::State, + theme: &Self::Theme, + ) -> Appearance { + self.program.style(state, theme) + } + + fn scale_factor(&self, state: &Self::State, window: window::Id) -> f64 { + self.program.scale_factor(state, window) + } + } + + WithExecutor { + program, + executor: PhantomData::, + } +} + /// The renderer of some [`Program`]. pub trait Renderer: text::Renderer + compositor::Default {}