use crate::reactive::Signal;
use std::cell::RefCell;
thread_local! {
static VIEWPORT_DIMENSIONS: RefCell<Signal<(f32, f32)>> = RefCell::new(Signal::new((1920.0, 1080.0)));
static COLOR_SCHEME: RefCell<Signal<ColorScheme>> = RefCell::new(Signal::new(ColorScheme::Dark));
static REDUCED_MOTION: RefCell<Signal<bool>> = RefCell::new(Signal::new(false));
static CONTRAST_PREFERENCE: RefCell<Signal<ContrastPreference>> = RefCell::new(Signal::new(ContrastPreference::Normal));
static ACTIVE_MEDIA_QUERIES: RefCell<Vec<(Vec<MediaCondition>, Signal<bool>)>> = const { RefCell::new(Vec::new()) };
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorScheme {
Light,
#[default]
Dark,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ContrastPreference {
#[default]
Normal,
High,
Low,
}
fn reevaluate_active_queries() {
let (width, height) = VIEWPORT_DIMENSIONS.with(|d| d.borrow().get());
let cs = COLOR_SCHEME.with(|cs| cs.borrow().get());
let rm = REDUCED_MOTION.with(|rm| rm.borrow().get());
ACTIVE_MEDIA_QUERIES.with(|queries| {
for (conditions, sig) in queries.borrow().iter() {
let matched = conditions.iter().all(|c| c.evaluate(width, height, cs, rm));
sig.set(matched);
}
});
}
pub fn set_viewport_dimensions(width: f32, height: f32) {
VIEWPORT_DIMENSIONS.with(|dims| {
dims.borrow().set((width, height));
});
reevaluate_active_queries();
}
pub fn set_system_color_scheme(scheme: ColorScheme) {
COLOR_SCHEME.with(|cs| {
cs.borrow().set(scheme);
});
reevaluate_active_queries();
}
pub fn set_system_reduced_motion(reduced: bool) {
REDUCED_MOTION.with(|rm| {
rm.borrow().set(reduced);
});
reevaluate_active_queries();
}
pub fn set_system_contrast(contrast: ContrastPreference) {
CONTRAST_PREFERENCE.with(|cp| {
cp.borrow().set(contrast);
});
}
pub fn use_color_scheme() -> Signal<ColorScheme> {
COLOR_SCHEME.with(|cs| cs.borrow().clone())
}
pub fn use_reduced_motion() -> Signal<bool> {
REDUCED_MOTION.with(|rm| rm.borrow().clone())
}
pub fn use_contrast() -> Signal<ContrastPreference> {
CONTRAST_PREFERENCE.with(|cp| cp.borrow().clone())
}
#[derive(Debug, Clone, PartialEq)]
enum MediaCondition {
MinWidth(f32),
MaxWidth(f32),
MinHeight(f32),
MaxHeight(f32),
OrientationPortrait,
OrientationLandscape,
PrefersColorScheme(ColorScheme),
PrefersReducedMotion(bool),
}
impl MediaCondition {
fn evaluate(
&self,
width: f32,
height: f32,
color_scheme: ColorScheme,
reduced_motion: bool,
) -> bool {
match self {
Self::MinWidth(min) => width >= *min,
Self::MaxWidth(max) => width <= *max,
Self::MinHeight(min) => height >= *min,
Self::MaxHeight(max) => height <= *max,
Self::OrientationPortrait => height >= width,
Self::OrientationLandscape => width > height,
Self::PrefersColorScheme(cs) => color_scheme == *cs,
Self::PrefersReducedMotion(rm) => reduced_motion == *rm,
}
}
}
fn parse_media_query(query: &str) -> Vec<MediaCondition> {
let mut conditions = Vec::new();
let cleaned = query
.trim()
.trim_start_matches('@')
.trim_start_matches("media")
.trim();
for part in cleaned.split("and") {
let p = part.trim().trim_matches(|c| c == '(' || c == ')').trim();
if let Some((prop, val)) = p.split_once(':') {
let prop_name = prop.trim().to_ascii_lowercase();
let val_str = val.trim().to_ascii_lowercase();
let val_clean = val_str.trim_end_matches("px").trim();
match prop_name.as_str() {
"min-width" => {
if let Ok(num) = val_clean.parse::<f32>() {
conditions.push(MediaCondition::MinWidth(num));
}
}
"max-width" => {
if let Ok(num) = val_clean.parse::<f32>() {
conditions.push(MediaCondition::MaxWidth(num));
}
}
"min-height" => {
if let Ok(num) = val_clean.parse::<f32>() {
conditions.push(MediaCondition::MinHeight(num));
}
}
"max-height" => {
if let Ok(num) = val_clean.parse::<f32>() {
conditions.push(MediaCondition::MaxHeight(num));
}
}
"orientation" => {
if val_str == "portrait" {
conditions.push(MediaCondition::OrientationPortrait);
} else if val_str == "landscape" {
conditions.push(MediaCondition::OrientationLandscape);
}
}
"prefers-color-scheme" => {
if val_str == "light" {
conditions.push(MediaCondition::PrefersColorScheme(ColorScheme::Light));
} else if val_str == "dark" {
conditions.push(MediaCondition::PrefersColorScheme(ColorScheme::Dark));
}
}
"prefers-reduced-motion" => {
if val_str == "reduce" {
conditions.push(MediaCondition::PrefersReducedMotion(true));
} else if val_str == "no-preference" {
conditions.push(MediaCondition::PrefersReducedMotion(false));
}
}
_ => {}
}
}
}
conditions
}
pub fn use_media_query(query: &str) -> Signal<bool> {
let conditions = parse_media_query(query);
let (w, h) = VIEWPORT_DIMENSIONS.with(|d| d.borrow().get());
let cs = COLOR_SCHEME.with(|cs| cs.borrow().get());
let rm = REDUCED_MOTION.with(|rm| rm.borrow().get());
let initial_match = conditions.iter().all(|c| c.evaluate(w, h, cs, rm));
let result_sig = Signal::new(initial_match);
ACTIVE_MEDIA_QUERIES.with(|queries| {
queries.borrow_mut().push((conditions, result_sig.clone()));
});
result_sig
}