Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying a custom executor #2580

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -376,6 +378,22 @@ impl<P: Program> Application<P> {
window: self.window,
}
}

/// Sets the executor of the [`Application`].
pub fn executor<E>(
self,
) -> Application<
impl Program<State = P::State, Message = P::Message, Theme = P::Theme>,
>
where
E: Executor,
{
Application {
raw: program::with_executor::<P, E>(self.raw),
settings: self.settings,
window: self.window,
}
}
}

/// The title logic of some [`Application`].
Expand Down
17 changes: 16 additions & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -223,6 +223,21 @@ impl<P: Program> Daemon<P> {
settings: self.settings,
}
}

/// Sets the executor of the [`Daemon`].
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,
}
}
}

/// The title logic of some [`Daemon`].
Expand Down
74 changes: 74 additions & 0 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,80 @@ pub fn with_scale_factor<P: Program>(
}
}

pub fn with_executor<P: Program, E: Executor>(
program: P,
) -> impl Program<State = P::State, Message = P::Message, Theme = P::Theme> {
use std::marker::PhantomData;

struct WithExecutor<P, E> {
program: P,
executor: PhantomData<E>,
}

impl<P: Program, E> Program for WithExecutor<P, E>
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::Message> {
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::Message> {
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::<E>,
}
}

/// The renderer of some [`Program`].
pub trait Renderer: text::Renderer + compositor::Default {}

Expand Down
Loading