Skip to main content

wde_editor/
theme.rs

1//! The editor's egui theme: a flat dark palette with a single accent color, the Inter font and compact spacing so
2//! debug windows leave most of the screen to the game.
3//!
4//! Installed on the egui context by the [`EditorPlugin`](crate::EditorPlugin). Panels can use the [`palette`] to
5//! stay consistent with it, and an app can still replace it with `ctx.set_style` / `ctx.set_fonts` at startup.
6use std::sync::Arc;
7
8use wde_egui::prelude::egui::{
9    self, Color32, CornerRadius, FontFamily, FontId, Margin, Shadow, Stroke, TextStyle,
10    style::WidgetVisuals
11};
12
13pub mod palette {
14    use wde_egui::prelude::egui::Color32;
15
16    // Backgrounds, darkest to lightest
17    pub const BG_EXTREME: Color32 = Color32::from_rgb(14, 14, 15);
18    pub const BG_PANEL: Color32 = Color32::from_rgb(23, 23, 24);
19    pub const BG_CARD: Color32 = Color32::from_rgb(30, 30, 31);
20    pub const BG_WIDGET: Color32 = Color32::from_rgb(37, 37, 39);
21    pub const BG_WIDGET_HOVERED: Color32 = Color32::from_rgb(50, 50, 53);
22    pub const BG_WIDGET_ACTIVE: Color32 = Color32::from_rgb(66, 66, 70);
23    /// Slider rails and checkbox boxes, kept lighter than [`BG_WIDGET`] so they stand out from the window.
24    pub const BG_RAIL: Color32 = Color32::from_rgb(52, 52, 55);
25
26    // Borders and separators
27    pub const SEPARATOR: Color32 = Color32::from_rgb(38, 38, 40);
28    pub const BORDER: Color32 = Color32::from_rgb(48, 48, 51);
29    pub const BORDER_STRONG: Color32 = Color32::from_rgb(72, 72, 76);
30
31    // Text
32    pub const TEXT: Color32 = Color32::from_rgb(240, 240, 240);
33    pub const TEXT_MUTED: Color32 = Color32::from_rgb(200, 200, 202);
34    pub const TEXT_WEAK: Color32 = Color32::from_rgb(130, 130, 134);
35    pub const TEXT_DISABLED: Color32 = Color32::from_rgb(96, 96, 100);
36
37    // The one interactive color, used sparingly: menu title, open windows, selection, hyperlinks.
38    pub const ACCENT: Color32 = Color32::from_rgb(128, 186, 255);
39    pub const ACCENT_HOVERED: Color32 = Color32::from_rgb(168, 210, 255);
40    pub const ACCENT_MUTED: Color32 = Color32::from_rgb(70, 95, 135);
41    pub const ACCENT_BG: Color32 = Color32::from_rgb(58, 88, 128);
42
43    // Semantic, muted to match the accent
44    pub const ERROR: Color32 = Color32::from_rgb(214, 112, 112);
45    pub const WARNING: Color32 = Color32::from_rgb(219, 179, 92);
46    pub const SUCCESS: Color32 = Color32::from_rgb(132, 188, 118);
47    pub const INFO: Color32 = Color32::from_rgb(104, 189, 178);
48}
49
50pub mod layout {
51    /// Windows and menus.
52    pub const WINDOW_ROUNDING: u8 = 6;
53    /// Buttons, sliders, text fields.
54    pub const WIDGET_ROUNDING: u8 = 3;
55}
56
57pub mod fonts {
58    pub const FONT_REGULAR: &str = "Inter";
59    pub const FONT_SEMIBOLD: &str = "Inter-SemiBold";
60
61    pub const SIZE_SMALL: f32 = 10.5;
62    pub const SIZE_BODY: f32 = 12.0;
63    pub const SIZE_HEADING: f32 = 12.5;
64    pub const SIZE_MONOSPACE: f32 = 11.5;
65}
66
67use palette::*;
68
69pub fn body_font(size: f32) -> FontId {
70    FontId::new(size, FontFamily::Proportional)
71}
72
73pub fn heading_font(size: f32) -> FontId {
74    FontId::new(size, FontFamily::Name(fonts::FONT_SEMIBOLD.into()))
75}
76
77/// Installs the fonts and style on the context.
78pub fn install(ctx: &egui::Context) {
79    ctx.set_fonts(font_definitions());
80    ctx.set_global_style(style());
81}
82
83fn font_definitions() -> egui::FontDefinitions {
84    let mut defs = egui::FontDefinitions::default();
85    defs.font_data.insert(
86        fonts::FONT_REGULAR.to_owned(),
87        Arc::new(egui::FontData::from_static(include_bytes!(
88            "../fonts/Inter-Light.otf"
89        )))
90    );
91    defs.font_data.insert(
92        fonts::FONT_SEMIBOLD.to_owned(),
93        Arc::new(egui::FontData::from_static(include_bytes!(
94            "../fonts/Inter-SemiBold.otf"
95        )))
96    );
97
98    // egui's own fonts stay as fallbacks for symbols and emojis
99    let proportional = defs.families.entry(FontFamily::Proportional).or_default();
100    proportional.insert(0, fonts::FONT_REGULAR.to_owned());
101    let mut semibold = proportional.clone();
102    semibold.insert(0, fonts::FONT_SEMIBOLD.to_owned());
103    defs.families
104        .insert(FontFamily::Name(fonts::FONT_SEMIBOLD.into()), semibold);
105    defs
106}
107
108pub fn style() -> egui::Style {
109    let mut style = egui::Style {
110        text_styles: [
111            (TextStyle::Small, body_font(fonts::SIZE_SMALL)),
112            (TextStyle::Body, body_font(fonts::SIZE_BODY)),
113            (TextStyle::Button, body_font(fonts::SIZE_BODY)),
114            (TextStyle::Heading, heading_font(fonts::SIZE_HEADING)),
115            (
116                TextStyle::Monospace,
117                FontId::new(fonts::SIZE_MONOSPACE, FontFamily::Monospace)
118            )
119        ]
120        .into(),
121        ..Default::default()
122    };
123
124    let spacing = &mut style.spacing;
125    spacing.item_spacing = egui::vec2(8.0, 5.0);
126    spacing.window_margin = Margin::symmetric(10, 8);
127    spacing.menu_margin = Margin::same(5);
128    spacing.button_padding = egui::vec2(8.0, 3.0);
129    spacing.indent = 16.0;
130    spacing.interact_size = egui::vec2(36.0, 20.0);
131    spacing.slider_width = 130.0;
132    spacing.slider_rail_height = 4.0;
133    spacing.combo_width = 120.0;
134    spacing.text_edit_width = 160.0;
135    spacing.icon_width = 13.0;
136    spacing.icon_width_inner = 7.0;
137    spacing.icon_spacing = 5.0;
138    spacing.menu_spacing = 2.0;
139    spacing.scroll = egui::style::ScrollStyle {
140        bar_width: 6.0,
141        floating_allocated_width: 2.0,
142        ..egui::style::ScrollStyle::floating()
143    };
144
145    let visuals = &mut style.visuals;
146    visuals.dark_mode = true;
147    visuals.override_text_color = None;
148    visuals.weak_text_color = Some(TEXT_WEAK);
149    visuals.hyperlink_color = ACCENT;
150    visuals.warn_fg_color = WARNING;
151    visuals.error_fg_color = ERROR;
152    visuals.extreme_bg_color = BG_EXTREME;
153    visuals.text_edit_bg_color = Some(BG_EXTREME);
154    visuals.faint_bg_color = BG_CARD;
155    visuals.code_bg_color = BG_EXTREME;
156    visuals.panel_fill = BG_PANEL;
157    visuals.selection.bg_fill = ACCENT_BG;
158    visuals.selection.stroke = Stroke::new(1.0, ACCENT_HOVERED);
159
160    visuals.window_fill = BG_PANEL;
161    visuals.window_stroke = Stroke::new(1.0, BORDER);
162    visuals.window_corner_radius = CornerRadius::same(layout::WINDOW_ROUNDING);
163    visuals.window_shadow = Shadow {
164        offset: [0, 4],
165        blur: 14,
166        spread: 0,
167        color: Color32::from_black_alpha(90)
168    };
169    visuals.menu_corner_radius = CornerRadius::same(layout::WINDOW_ROUNDING);
170    visuals.popup_shadow = Shadow {
171        offset: [0, 3],
172        blur: 10,
173        spread: 0,
174        color: Color32::from_black_alpha(110)
175    };
176    visuals.resize_corner_size = 8.0;
177    visuals.collapsing_header_frame = false;
178    visuals.indent_has_left_vline = true;
179    visuals.striped = false;
180    visuals.slider_trailing_fill = true;
181    visuals.handle_shape = egui::style::HandleShape::Rect { aspect_ratio: 0.5 };
182
183    let base = WidgetVisuals {
184        bg_fill: BG_WIDGET,
185        weak_bg_fill: BG_WIDGET,
186        bg_stroke: Stroke::NONE,
187        fg_stroke: Stroke::new(1.0, TEXT_MUTED),
188        corner_radius: CornerRadius::same(layout::WIDGET_ROUNDING),
189        expansion: 0.0
190    };
191    let widgets = &mut visuals.widgets;
192    widgets.noninteractive = WidgetVisuals {
193        bg_fill: BG_CARD,
194        weak_bg_fill: BG_CARD,
195        // Separators, group frames and panel borders
196        bg_stroke: Stroke::new(1.0, SEPARATOR),
197        ..base
198    };
199    widgets.inactive = WidgetVisuals {
200        bg_fill: BG_RAIL,
201        ..base
202    };
203    widgets.hovered = WidgetVisuals {
204        bg_fill: BG_WIDGET_ACTIVE,
205        weak_bg_fill: BG_WIDGET_HOVERED,
206        fg_stroke: Stroke::new(1.0, TEXT),
207        ..base
208    };
209    widgets.active = WidgetVisuals {
210        bg_fill: BG_WIDGET_ACTIVE,
211        weak_bg_fill: BG_WIDGET_ACTIVE,
212        fg_stroke: Stroke::new(1.0, TEXT),
213        ..base
214    };
215    widgets.open = WidgetVisuals {
216        bg_fill: BG_WIDGET,
217        weak_bg_fill: BG_EXTREME,
218        fg_stroke: Stroke::new(1.0, TEXT),
219        ..base
220    };
221
222    style
223}
224
225/// Style of the menu bar and its menus: frameless buttons that only light up when hovered.
226pub fn menu_style() -> egui::Style {
227    let mut style = style();
228    style.spacing.button_padding = egui::vec2(8.0, 4.0);
229    style.spacing.item_spacing = egui::vec2(2.0, 1.0);
230    let widgets = &mut style.visuals.widgets;
231    widgets.inactive.weak_bg_fill = Color32::TRANSPARENT;
232    widgets.hovered.weak_bg_fill = BG_WIDGET_HOVERED;
233    widgets.open.weak_bg_fill = BG_WIDGET_HOVERED;
234    style
235}