use std::{
cell::{Cell, RefCell},
rc::Rc,
};
use crate::theme::Theme;
thread_local! {
static THEME_SNAPSHOT: RefCell<Option<Rc<Theme>>> = const { RefCell::new(None) };
}
#[doc(hidden)]
pub fn set_theme_snapshot(theme: &Theme) {
THEME_SNAPSHOT.with(|snapshot| *snapshot.borrow_mut() = Some(Rc::new(theme.clone())));
}
#[must_use]
pub fn current_theme() -> Rc<Theme> {
THEME_SNAPSHOT.with(|snapshot| {
snapshot
.borrow_mut()
.get_or_insert_with(|| Rc::new(Theme::default()))
.clone()
})
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FocusRingMode {
#[default]
KeyboardOnly,
Always,
}
thread_local! {
static FOCUS_RING_MODE: Cell<FocusRingMode> = const { Cell::new(FocusRingMode::KeyboardOnly) };
static KEYBOARD_MODALITY: Cell<bool> = const { Cell::new(false) };
}
pub fn set_focus_ring_mode(mode: FocusRingMode) {
FOCUS_RING_MODE.with(|current| current.set(mode));
}
pub fn focus_visible() -> bool {
FOCUS_RING_MODE.with(Cell::get) == FocusRingMode::Always || KEYBOARD_MODALITY.with(Cell::get)
}
#[doc(hidden)]
#[must_use]
pub fn set_keyboard_modality(keyboard: bool) -> bool {
KEYBOARD_MODALITY.with(|current| current.replace(keyboard) != keyboard)
}