Skip to main content

egui_pop_api/
stories.rs

1//! Gallery stories: a plugin contributes a component shown in its states,
2//! with an optional playground of its options. A gallery application
3//! lists and draws them; tests reuse them as fixtures.
4
5use std::fmt;
6
7use egui::Ui;
8use egui_pop_model::ids::StoryId;
9use serde::Serialize;
10
11use crate::app::AppHandle;
12
13/// Draws a story or its playground.
14pub type StoryFn = Box<dyn FnMut(&mut Ui, &AppHandle)>;
15
16/// A story as contributed.
17pub struct Story {
18    pub id: StoryId,
19    pub title: String,
20    pub description: String,
21    /// Stories of a group are listed together.
22    pub group: String,
23    /// Lighter first within a group.
24    pub weight: i32,
25    pub show: StoryFn,
26    pub playground: Option<StoryFn>,
27}
28
29impl fmt::Debug for Story {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.debug_struct("Story")
32            .field("id", &self.id)
33            .field("title", &self.title)
34            .finish()
35    }
36}
37
38impl Story {
39    pub fn new(id: StoryId, title: &str, show: impl FnMut(&mut Ui, &AppHandle) + 'static) -> Story {
40        Story {
41            id,
42            title: title.into(),
43            description: String::new(),
44            group: String::new(),
45            weight: 0,
46            show: Box::new(show),
47            playground: None,
48        }
49    }
50
51    pub fn description(mut self, text: &str) -> Story {
52        self.description = text.into();
53        self
54    }
55
56    pub fn group(mut self, group: &str, weight: i32) -> Story {
57        self.group = group.into();
58        self.weight = weight;
59        self
60    }
61
62    pub fn playground(mut self, f: impl FnMut(&mut Ui, &AppHandle) + 'static) -> Story {
63        self.playground = Some(Box::new(f));
64        self
65    }
66}
67
68/// What the gallery lists.
69#[derive(Clone, Debug, PartialEq, Serialize)]
70pub struct StoryInfo {
71    pub id: StoryId,
72    pub title: String,
73    pub description: String,
74    pub group: String,
75    pub weight: i32,
76    pub has_playground: bool,
77    pub owner: egui_pop_model::ids::OwnerId,
78}