use egui::{Id, Response, Ui, Widget};
use crate::theme::BuiltInTheme;
use crate::Select;
#[must_use = "Add with `ui.add(...)`."]
pub struct ThemeSwitcher<'a> {
current: &'a mut BuiltInTheme,
id_salt: Id,
width: f32,
auto_install: bool,
}
impl<'a> std::fmt::Debug for ThemeSwitcher<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ThemeSwitcher")
.field("current", &*self.current)
.field("id_salt", &self.id_salt)
.field("width", &self.width)
.field("auto_install", &self.auto_install)
.finish()
}
}
impl<'a> ThemeSwitcher<'a> {
pub fn new(current: &'a mut BuiltInTheme) -> Self {
Self {
current,
id_salt: Id::new("elegance_theme_switcher"),
width: 110.0,
auto_install: true,
}
}
pub fn id_salt(mut self, id_salt: impl std::hash::Hash) -> Self {
self.id_salt = Id::new(id_salt);
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = width;
self
}
pub fn auto_install(mut self, auto_install: bool) -> Self {
self.auto_install = auto_install;
self
}
}
impl Widget for ThemeSwitcher<'_> {
fn ui(self, ui: &mut Ui) -> Response {
let Self {
current,
id_salt,
width,
auto_install,
} = self;
let options = BuiltInTheme::all().into_iter().map(|t| (t, t.label()));
let response = ui.add(
Select::new(id_salt, &mut *current)
.options(options)
.width(width),
);
if auto_install {
current.theme().install(ui.ctx());
}
response
}
}