Skip to content

Commit

Permalink
feat: get egui::Visuals directly from the theme
Browse files Browse the repository at this point in the history
  • Loading branch information
lampsitter committed Aug 18, 2023
1 parent a46c7fc commit 5ca0073
Showing 1 changed file with 56 additions and 40 deletions.
96 changes: 56 additions & 40 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,7 @@ 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));
}

fn make_widget_visual(
Expand Down Expand Up @@ -126,6 +87,61 @@ pub struct Theme {
pub crust: Color32,
}

impl Theme {
/// Get the theme as [`Visuals`](egui::Visuals)
///
/// # Example
/// ```rust
/// # use egui::__run_test_ctx;
/// # __run_test_ctx(|ctx| {
/// let old = ctx.style().visuals.clone();
/// ctx.set_visuals(catppuccin_egui::MACCHIATO.visuals(old));
/// # });
/// ```
pub 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 5ca0073

Please sign in to comment.