1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! This contains all the views that are used to construct the core of the application.
use std::path::PathBuf;

use egui::{CentralPanel, Checkbox, FontDefinitions, ScrollArea, SidePanel, Style, Ui, Widget};
use egui_theme::EguiTheme;
use serde::{Deserialize, Serialize};
mod colors;
mod fonts;
mod preview;
mod shape;
mod spacing;
mod text;

use preview::Preview;

use fonts::FontViewState;
use text::TextStyleViewState;

/// StylistFileDialogFunction is a function callback that allows the `StylistState` to open a native filedialog and get file paths for egui.
pub type StylistFileDialogFunction =
    Box<dyn Fn(StylistFileDialog, Option<(&str, &[&str])>) -> Option<PathBuf>>;

/// This determines what kind of FileDialog is desired from within the `StylistState`
pub enum StylistFileDialog {
    Open,
    Save,
}

#[derive(PartialEq, Serialize, Deserialize, Clone, Copy)]
enum StylerTab {
    Colors,
    Fonts,
    TextStyles,
    Spacing,
    Shape,
}
/// This is the framework agnostic application state that can be easily embedded directly into any `egui` integration.
///
/// This can easily be embedded into any existing egui application by calling `ui` from within the egui context such as follows:
///
/// ```ignore
/// let state = StylistState::default():
/// egui::CentralPanel::default().show(ctx, |ui| state.ui(ui));
/// ```

#[derive(Serialize, Deserialize)]
pub struct StylistState {
    current_tab: StylerTab,
    show_stylist: bool,
    show_preview: bool,
    style: Style,
    font_definitions: FontDefinitions,
    #[serde(skip)]
    font_view_state: FontViewState,
    #[serde(skip)]
    text_style_view_state: TextStyleViewState,
    preview: Preview,
    #[serde(skip)]
    pub file_dialog_function: Option<StylistFileDialogFunction>,
}

impl StylistState {
    pub fn default() -> Self {
        Self {
            current_tab: StylerTab::Colors,
            style: Style::default(),
            show_stylist: true,
            show_preview: true,
            font_definitions: FontDefinitions::default(),
            font_view_state: FontViewState::default(),
            text_style_view_state: TextStyleViewState::default(),
            preview: Preview::new(Style::default()),
            file_dialog_function: None,
        }
    }
    /// Sets `file_dialog_function` with the function call that it can use to
    pub fn set_file_dialog_function(&mut self, f: StylistFileDialogFunction) {
        self.file_dialog_function = Some(f);
    }
    /// Calls the file_dialog function and returns a path if it was found.
    pub fn file_dialog(
        &self,
        kind: StylistFileDialog,
        filter: Option<(&str, &[&str])>,
    ) -> Option<PathBuf> {
        self.file_dialog_function
            .as_ref()
            .and_then(|f| f(kind, filter))
    }

    fn tab_menu_ui(&mut self, ui: &mut Ui) {
        use egui::widgets::SelectableLabel;
        // Menu tabs
        ui.horizontal(|ui| {
            if ui
                .add(SelectableLabel::new(
                    self.current_tab == StylerTab::Colors,
                    "Colors",
                ))
                .clicked()
            {
                self.current_tab = StylerTab::Colors;
            }
            if ui
                .add(SelectableLabel::new(
                    self.current_tab == StylerTab::Fonts,
                    "Fonts",
                ))
                .clicked()
            {
                self.current_tab = StylerTab::Fonts;
            }
            if ui
                .add(SelectableLabel::new(
                    self.current_tab == StylerTab::TextStyles,
                    "TextStyles",
                ))
                .clicked()
            {
                self.current_tab = StylerTab::TextStyles;
            }

            if ui
                .add(SelectableLabel::new(
                    self.current_tab == StylerTab::Spacing,
                    "Spacing",
                ))
                .clicked()
            {
                self.current_tab = StylerTab::Spacing;
            }

            if ui
                .add(SelectableLabel::new(
                    self.current_tab == StylerTab::Shape,
                    "Shape",
                ))
                .clicked()
            {
                self.current_tab = StylerTab::Shape;
            }
            Checkbox::new(&mut self.show_stylist, "Show Stylist").ui(ui);
            Checkbox::new(&mut self.show_preview, "Show preview").ui(ui);
        });
    }
    /// Creates and displays the Stylist UI.
    /// This can be used to embed the Stylist into any application that supports it.
    pub fn ui(&mut self, ui: &mut Ui) {
        // Get the tab ui
        self.tab_menu_ui(ui);
        if self.show_stylist {
            SidePanel::left("_stylist_panel")
                .width_range(300.0..=900.0)
                .show_inside(ui, |ui| {
                    ScrollArea::vertical().show(ui, |ui| {
                        // Show the content views.
                        match self.current_tab {
                            StylerTab::Colors => colors::colors_view(&mut self.style, ui),
                            StylerTab::Fonts => fonts::fonts_view(
                                &mut self.font_view_state,
                                self.file_dialog_function.as_ref(),
                                &mut self.font_definitions,
                                &mut self.style,
                                ui,
                            ),
                            StylerTab::TextStyles => {
                                let families = self
                                    .font_definitions
                                    .families
                                    .keys()
                                    .cloned()
                                    .collect::<Vec<_>>();
                                text::text_styles_view(
                                    &mut self.text_style_view_state,
                                    &mut self.style,
                                    families,
                                    ui,
                                )
                            }
                            StylerTab::Spacing => spacing::spacing_view(&mut self.style, ui),
                            StylerTab::Shape => shape::shape_view(&mut self.style, ui),
                        };
                    });
                });
        }
        if self.show_preview {
            CentralPanel::default().show_inside(ui, |ui| {
                ScrollArea::vertical().show(ui, |ui| {
                    self.preview.set_style(self.style.clone());
                    ui.ctx().set_fonts(self.font_definitions.clone());
                    self.preview.show(ui);
                });
            });
        }
    }
    pub fn export_theme(&self) -> EguiTheme {
        EguiTheme::new(self.style.clone(), self.font_definitions.clone())
    }
    pub fn import_theme(&mut self, theme: EguiTheme) {
        let (style, font_definitions) = theme.extract();
        self.style = style;
        self.font_definitions = font_definitions;
    }
}