use oxiui_core::UiCtx;
pub const BUILTIN_THEMES: &[&str] = &["light", "dark", "cooljapan_default"];
pub fn theme_picker(ui: &mut dyn UiCtx, current: &mut &'static str) -> bool {
let mut changed = false;
for &name in BUILTIN_THEMES {
let resp = ui.button(name);
if resp.clicked && *current != name {
*current = name;
changed = true;
}
}
changed
}
pub fn by_name(name: &str) -> Box<dyn oxiui_core::Theme> {
match name {
"dark" => oxiui_theme::dark(),
"cooljapan_default" => oxiui_theme::cooljapan_default(),
_ => oxiui_theme::light(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use oxiui_core::{ButtonResponse, UiCtx};
struct NullUiCtx;
impl UiCtx for NullUiCtx {
fn heading(&mut self, _text: &str) {}
fn label(&mut self, _text: &str) {}
fn button(&mut self, _label: &str) -> ButtonResponse {
ButtonResponse::default()
}
}
#[test]
fn test_theme_picker_no_panic() {
let mut ctx = NullUiCtx;
let mut current = "light";
let changed = theme_picker(&mut ctx, &mut current);
assert!(!changed, "null context should return false");
assert_eq!(current, "light", "selection unchanged");
}
#[test]
fn test_theme_picker_selection_changes() {
struct ClickCtx {
target: &'static str,
}
impl UiCtx for ClickCtx {
fn heading(&mut self, _text: &str) {}
fn label(&mut self, _text: &str) {}
fn button(&mut self, label: &str) -> ButtonResponse {
ButtonResponse {
clicked: label == self.target,
hovered: false,
}
}
}
let mut ctx = ClickCtx { target: "dark" };
let mut current = "light";
let changed = theme_picker(&mut ctx, &mut current);
assert!(changed, "clicking 'dark' must return true");
assert_eq!(current, "dark");
}
#[test]
fn test_by_name_all_variants() {
for &name in BUILTIN_THEMES {
let _theme = by_name(name);
}
let _theme = by_name("unknown");
}
}