Skip to content

Commit

Permalink
feat: add funtction to apply theme to a Style
Browse files Browse the repository at this point in the history
* feat: get egui::Visuals directly from the theme

* feat: add function to set egui::Style
  • Loading branch information
lampsitter authored Aug 22, 2023
1 parent a46c7fc commit 8db4c01
Showing 1 changed file with 63 additions and 40 deletions.
103 changes: 63 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 8db4c01

Please sign in to comment.