iced_program/
preset.rs

1use crate::runtime::Task;
2
3use std::borrow::Cow;
4use std::fmt;
5
6/// A specific boot strategy for a [`Program`](crate::Program).
7pub struct Preset<State, Message> {
8    name: Cow<'static, str>,
9    boot: Box<dyn Fn() -> (State, Task<Message>)>,
10}
11
12impl<State, Message> Preset<State, Message> {
13    /// Creates a new [`Preset`] with the given name and boot strategy.
14    pub fn new(
15        name: impl Into<Cow<'static, str>>,
16        boot: impl Fn() -> (State, Task<Message>) + 'static,
17    ) -> Self {
18        Self {
19            name: name.into(),
20            boot: Box::new(boot),
21        }
22    }
23
24    /// Returns the name of the [`Preset`].
25    pub fn name(&self) -> &str {
26        &self.name
27    }
28
29    /// Boots the [`Preset`], returning the initial [`Program`](crate::Program) state and
30    /// a [`Task`] for concurrent booting.
31    pub fn boot(&self) -> (State, Task<Message>) {
32        (self.boot)()
33    }
34}
35
36impl<State, Message> fmt::Debug for Preset<State, Message> {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        f.debug_struct("Preset")
39            .field("name", &self.name)
40            .finish_non_exhaustive()
41    }
42}