use egui::style::{WidgetVisuals, Widgets};
use egui::{Color32, Stroke};
use super::{ANIMATION_TIME, Theme, ThemeId, ThemeMode, effective_id};
pub fn system_is_dark(ctx: &egui::Context) -> bool {
ctx.input(|input| input.raw.system_theme)
.is_some_and(|theme| theme == egui::Theme::Dark)
}
pub fn apply(ctx: &egui::Context, mode: ThemeMode) {
apply_id(
ctx,
match mode {
ThemeMode::Light => ThemeId::GithubLight,
ThemeMode::Dark => ThemeId::GithubDark,
_ => ThemeId::GithubLight,
},
);
}
pub fn apply_theme(ctx: &egui::Context, id: ThemeId, follow_system: bool) -> ThemeId {
let effective = effective_id(id, follow_system, system_is_dark(ctx));
apply_id(ctx, effective);
effective
}
pub fn apply_id(ctx: &egui::Context, id: ThemeId) {
apply_tokens(ctx, &Theme::for_id(id));
}
pub fn apply_tokens(ctx: &egui::Context, theme: &Theme) {
let theme = *theme;
ctx.data_mut(|data| data.insert_temp(current_key(), theme));
let mut visuals = if theme.is_dark() {
egui::Visuals::dark()
} else {
egui::Visuals::light()
};
visuals.panel_fill = theme.background;
visuals.window_fill = theme.surface;
visuals.faint_bg_color = theme.hover;
visuals.extreme_bg_color = theme.sunken;
visuals.hyperlink_color = theme.link;
visuals.selection.bg_fill = theme.selection;
visuals.selection.stroke = Stroke::new(1.0, theme.text);
visuals.window_stroke = Stroke::new(1.0, theme.border);
visuals.error_fg_color = theme.danger;
visuals.warn_fg_color = theme.warning;
paint_widgets(&mut visuals.widgets, &theme);
visuals.override_text_color = Some(theme.text);
ctx.all_styles_mut(|style| {
style.visuals = visuals.clone();
style.spacing.item_spacing = egui::vec2(8.0, 4.0);
style.spacing.button_padding = egui::vec2(8.0, 3.0);
style.spacing.interact_size = egui::vec2(26.0, 24.0);
style.spacing.menu_margin = egui::Margin::same(4);
style.spacing.scroll.bar_width = 6.0;
style.spacing.scroll.floating_width = 3.0;
style.spacing.scroll.floating_allocated_width = 4.0;
style.spacing.scroll.bar_inner_margin = 2.0;
style.animation_time = ANIMATION_TIME;
});
crate::foundation::style::apply_to_egui(ctx, &crate::foundation::style::get(ctx));
}
fn paint_widgets(widgets: &mut Widgets, theme: &Theme) {
let foreground = Stroke::new(1.0, theme.focus);
let states: [(&mut WidgetVisuals, Color32); 5] = [
(&mut widgets.noninteractive, theme.background),
(&mut widgets.inactive, theme.surface),
(&mut widgets.hovered, theme.hover),
(&mut widgets.active, theme.accent),
(&mut widgets.open, theme.surface),
];
for (state, fill) in states {
state.bg_fill = fill;
state.weak_bg_fill = fill;
state.fg_stroke = foreground;
}
widgets.inactive.weak_bg_fill = theme.surface;
widgets.inactive.bg_stroke = theme.border_stroke();
widgets.hovered.bg_stroke = Stroke::new(1.0, theme.focus);
widgets.active.fg_stroke = Stroke::new(1.0, theme.text);
widgets.active.bg_stroke = Stroke::new(2.0, theme.focus);
widgets.noninteractive.bg_stroke = Stroke::new(1.0, theme.border);
}
fn current_key() -> egui::Id {
egui::Id::unique("crisol::theme::current")
}
pub fn current(ctx: &egui::Context) -> Theme {
ctx.data(|data| data.get_temp::<Theme>(current_key()))
.unwrap_or_else(|| {
if ctx.style_of(ctx.theme()).visuals.dark_mode {
Theme::dark()
} else {
Theme::light()
}
})
}
pub fn motion_enabled(ctx: &egui::Context) -> bool {
ctx.style_of(ctx.theme()).animation_time > 0.0
}
pub fn set_reduced_motion(ctx: &egui::Context, reduced: bool) {
ctx.all_styles_mut(|style| {
style.animation_time = if reduced { 0.0 } else { ANIMATION_TIME };
});
}