Add program::Preset and emulator::Mode
This commit is contained in:
parent
927d5b7cba
commit
73f5569f28
17 changed files with 305 additions and 39 deletions
|
|
@ -4,6 +4,12 @@ pub use iced_runtime as runtime;
|
|||
pub use iced_runtime::core;
|
||||
pub use iced_runtime::futures;
|
||||
|
||||
#[cfg(feature = "test")]
|
||||
mod preset;
|
||||
|
||||
#[cfg(feature = "test")]
|
||||
pub use preset::Preset;
|
||||
|
||||
use crate::core::renderer;
|
||||
use crate::core::text;
|
||||
use crate::core::theme;
|
||||
|
|
@ -100,6 +106,11 @@ pub trait Program: Sized {
|
|||
fn scale_factor(&self, _state: &Self::State, _window: window::Id) -> f64 {
|
||||
1.0
|
||||
}
|
||||
|
||||
#[cfg(feature = "test")]
|
||||
fn presets(&self) -> &[Preset<Self::State, Self::Message>] {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
||||
/// Decorates a [`Program`] with the given title function.
|
||||
|
|
|
|||
42
program/src/preset.rs
Normal file
42
program/src/preset.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use crate::runtime::Task;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
|
||||
/// A specific boot strategy for a [`Program`].
|
||||
pub struct Preset<State, Message> {
|
||||
name: Cow<'static, str>,
|
||||
boot: Box<dyn Fn() -> (State, Task<Message>)>,
|
||||
}
|
||||
|
||||
impl<State, Message> Preset<State, Message> {
|
||||
/// Creates a new [`Preset`] with the given name and boot strategy.
|
||||
pub fn new(
|
||||
name: impl Into<Cow<'static, str>>,
|
||||
boot: impl Fn() -> (State, Task<Message>) + 'static,
|
||||
) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
boot: Box::new(boot),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the name of the [`Preset`].
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
/// Boots the [`Preset`], returning the initial [`Program`] state and
|
||||
/// a [`Task`] for concurrent booting.
|
||||
pub fn boot(&self) -> (State, Task<Message>) {
|
||||
(self.boot)()
|
||||
}
|
||||
}
|
||||
|
||||
impl<State, Message> fmt::Debug for Preset<State, Message> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Preset")
|
||||
.field("name", &self.name)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue