diff --git a/src/lib.rs b/src/lib.rs index e2a08f9..d4c89df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,46 +33,24 @@ use egui::{epaint, style, Color32}; /// Apply the given theme to a [`Context`](egui::Context). pub fn set_theme(ctx: &egui::Context, theme: Theme) { let old = ctx.style().visuals.clone(); - ctx.set_visuals(egui::Visuals { - override_text_color: Some(theme.text), - hyperlink_color: theme.rosewater, - faint_bg_color: theme.surface0, - extreme_bg_color: theme.crust, - code_bg_color: theme.mantle, - warn_fg_color: theme.peach, - error_fg_color: theme.maroon, - window_fill: theme.base, - panel_fill: theme.base, - window_stroke: egui::Stroke { - color: theme.overlay1, - ..old.window_stroke - }, - widgets: style::Widgets { - noninteractive: make_widget_visual(old.widgets.noninteractive, &theme, theme.base), - inactive: make_widget_visual(old.widgets.inactive, &theme, theme.surface0), - hovered: make_widget_visual(old.widgets.hovered, &theme, theme.surface2), - active: make_widget_visual(old.widgets.active, &theme, theme.surface1), - open: make_widget_visual(old.widgets.open, &theme, theme.surface0), - }, - selection: style::Selection { - bg_fill: theme - .blue - .linear_multiply(if theme == LATTE { 0.4 } else { 0.2 }), - stroke: egui::Stroke { - color: theme.overlay1, - ..old.selection.stroke - }, - }, - window_shadow: epaint::Shadow { - color: theme.base, - ..old.window_shadow - }, - popup_shadow: epaint::Shadow { - color: theme.base, - ..old.popup_shadow - }, - ..old - }); + ctx.set_visuals(theme.visuals(old)); +} + +/// Apply the given theme to a [`Style`](egui::Style). +/// +/// # Example +/// +/// ```rust +/// # use egui::__run_test_ctx; +/// # __run_test_ctx(|ctx| { +/// let mut style = (*ctx.style()).clone(); +/// catppuccin_egui::set_style_theme(&mut style, catppuccin_egui::MACCHIATO); +/// ctx.set_style(style); +/// # }); +/// ``` +pub fn set_style_theme(style: &mut egui::Style, theme: Theme) { + let old = style.visuals.clone(); + style.visuals = theme.visuals(old); } fn make_widget_visual( @@ -126,6 +104,51 @@ pub struct Theme { pub crust: Color32, } +impl Theme { + fn visuals(&self, old: egui::Visuals) -> egui::Visuals { + egui::Visuals { + override_text_color: Some(self.text), + hyperlink_color: self.rosewater, + faint_bg_color: self.surface0, + extreme_bg_color: self.crust, + code_bg_color: self.mantle, + warn_fg_color: self.peach, + error_fg_color: self.maroon, + window_fill: self.base, + panel_fill: self.base, + window_stroke: egui::Stroke { + color: self.overlay1, + ..old.window_stroke + }, + widgets: style::Widgets { + noninteractive: make_widget_visual(old.widgets.noninteractive, self, self.base), + inactive: make_widget_visual(old.widgets.inactive, self, self.surface0), + hovered: make_widget_visual(old.widgets.hovered, self, self.surface2), + active: make_widget_visual(old.widgets.active, self, self.surface1), + open: make_widget_visual(old.widgets.open, self, self.surface0), + }, + selection: style::Selection { + bg_fill: self + .blue + .linear_multiply(if *self == LATTE { 0.4 } else { 0.2 }), + stroke: egui::Stroke { + color: self.overlay1, + ..old.selection.stroke + }, + }, + window_shadow: epaint::Shadow { + color: self.base, + ..old.window_shadow + }, + popup_shadow: epaint::Shadow { + color: self.base, + ..old.popup_shadow + }, + ..old + } + } +} + pub const LATTE: Theme = Theme { rosewater: Color32::from_rgb(220, 138, 120), flamingo: Color32::from_rgb(221, 120, 120),