use crate::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum LayoutDirection {
#[default]
Ltr,
Rtl,
}
impl LayoutDirection {
pub fn as_str(&self) -> &'static str {
match self {
LayoutDirection::Ltr => "ltr",
LayoutDirection::Rtl => "rtl",
}
}
pub fn is_rtl(&self) -> bool {
matches!(self, LayoutDirection::Rtl)
}
pub fn start(&self) -> &'static str {
match self {
LayoutDirection::Ltr => "left",
LayoutDirection::Rtl => "right",
}
}
pub fn end(&self) -> &'static str {
match self {
LayoutDirection::Ltr => "right",
LayoutDirection::Rtl => "left",
}
}
}
#[derive(Clone)]
pub struct ThemeContext {
pub palette: String,
pub colors: Palette,
pub direction: LayoutDirection,
pub set_theme: Callback<String>,
}
impl Default for ThemeContext {
fn default() -> Self {
ThemeContext {
palette: "hikari".to_string(),
colors: themes::Hikari::palette(),
direction: LayoutDirection::Ltr,
set_theme: Callback::new(|_| {}),
}
}
}
pub fn use_theme() -> ThemeContext {
let ctx = use_context::<ThemeContext>().expect("ThemeContext not found");
ctx.get().clone()
}
pub fn try_use_theme() -> Option<ThemeContext> {
use_context::<ThemeContext>().map(|ctx| ctx.get().clone())
}
pub fn use_layout_direction() -> LayoutDirection {
use_context::<ThemeContext>()
.map(|ctx| ctx.get().direction)
.unwrap_or_default()
}