use cranpose_core::{CompositionLocal, CompositionLocalProvider, compositionLocalOf};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum LayoutDirection {
#[default]
Ltr,
Rtl,
}
impl LayoutDirection {
pub fn is_rtl(self) -> bool {
matches!(self, LayoutDirection::Rtl)
}
pub fn reversed(self) -> Self {
match self {
LayoutDirection::Ltr => LayoutDirection::Rtl,
LayoutDirection::Rtl => LayoutDirection::Ltr,
}
}
pub fn resolve(self, start: f32, end: f32) -> (f32, f32) {
match self {
LayoutDirection::Ltr => (start, end),
LayoutDirection::Rtl => (end, start),
}
}
}
pub fn local_layout_direction() -> CompositionLocal<LayoutDirection> {
thread_local! {
static LOCAL: std::cell::RefCell<Option<CompositionLocal<LayoutDirection>>> =
const { std::cell::RefCell::new(None) };
}
LOCAL.with(|cell| {
cell.borrow_mut()
.get_or_insert_with(|| compositionLocalOf(LayoutDirection::default))
.clone()
})
}
pub fn layout_direction() -> LayoutDirection {
local_layout_direction().current()
}
#[expect(non_snake_case)]
#[track_caller]
pub fn ProvideLayoutDirection(direction: LayoutDirection, content: impl FnOnce()) {
CompositionLocalProvider(vec![local_layout_direction().provides(direction)], content);
}
#[cfg(test)]
#[path = "tests/layout_direction_tests.rs"]
mod tests;