From f9ee3229c1e641b451f18d6f1b0a75a608a6b023 Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Wed, 18 Sep 2024 01:27:35 -0400 Subject: Allow specifying a custom executor --- src/application.rs | 18 +++++++++++++ src/program.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/src/application.rs b/src/application.rs index d0f77304..d175cf85 100644 --- a/src/application.rs +++ b/src/application.rs @@ -30,6 +30,8 @@ //! ] //! } //! ``` +use iced_futures::Executor; + use crate::program::{self, Program}; use crate::window; use crate::{Element, Font, Result, Settings, Size, Subscription, Task}; @@ -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/program.rs b/src/program.rs index 2b697fbe..68efab88 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 theme( + &self, + state: &Self::State, + window: window::Id, + ) -> Self::Theme { + self.program.theme(state, window) + } + + 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 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 {} -- cgit From d20ce8d82cb4936602c57064a896f7ed686529be Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 18 Sep 2024 21:19:18 +0200 Subject: Import `Executor` directly from `crate` --- src/application.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/application.rs b/src/application.rs index d175cf85..2ba764be 100644 --- a/src/application.rs +++ b/src/application.rs @@ -30,11 +30,11 @@ //! ] //! } //! ``` -use iced_futures::Executor; - 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; -- cgit From 93068836182cb2a6527a267453f280eb5c0d34a3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 18 Sep 2024 21:19:33 +0200 Subject: Fix order of `Program::theme` implementation --- src/program.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/program.rs b/src/program.rs index 68efab88..94cb9a7d 100644 --- a/src/program.rs +++ b/src/program.rs @@ -570,14 +570,6 @@ pub fn with_executor( type Renderer = P::Renderer; type Executor = E; - fn theme( - &self, - state: &Self::State, - window: window::Id, - ) -> Self::Theme { - self.program.theme(state, window) - } - fn title(&self, state: &Self::State, window: window::Id) -> String { self.program.title(state, window) } @@ -605,6 +597,14 @@ pub fn with_executor( 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, -- cgit From 7f4a73e1856134821e3e0af11f21469cdc4544a2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 18 Sep 2024 21:21:02 +0200 Subject: Implement `executor` method for `Daemon` --- src/daemon.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/daemon.rs b/src/daemon.rs index 6a6ad133..81254bf9 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`]. -- cgit