Skip to main content

cranpose_ui/
layout_direction.rs

1//! Which way the interface reads.
2//!
3//! Layout direction is what turns "start" and "end" into "left" and "right".
4//! It is a composition local rather than a global so a screen can pin a
5//! direction — a code block, a phone number, a language picker — without
6//! reversing the rest of the interface with it.
7
8use cranpose_core::{CompositionLocalProvider, StaticCompositionLocal, staticCompositionLocalOf};
9pub use cranpose_ui_layout::LayoutDirection;
10
11/// The [`StaticCompositionLocal`] carrying the current layout direction.
12///
13/// Compose's `LocalLayoutDirection`, static as Compose's is: a read
14/// subscribes nothing, and a provider given another direction recomposes
15/// everything under it. Every layout reads it, and it seldom changes.
16pub fn local_layout_direction() -> StaticCompositionLocal<LayoutDirection> {
17    crate::thread_state::with_environment(|locals| {
18        locals
19            .direction
20            .get_or_init(|| staticCompositionLocalOf(LayoutDirection::default))
21            .clone()
22    })
23}
24
25/// The layout direction in force here.
26pub fn layout_direction() -> LayoutDirection {
27    crate::thread_state::with_environment(|locals| {
28        locals
29            .direction
30            .get_or_init(|| staticCompositionLocalOf(LayoutDirection::default))
31            .current()
32    })
33}
34
35/// Runs `content` in `direction`.
36#[expect(non_snake_case, reason = "names follow Compose's casing")]
37#[track_caller]
38pub fn ProvideLayoutDirection(direction: LayoutDirection, content: impl FnOnce()) {
39    CompositionLocalProvider([local_layout_direction().provides(direction)], content);
40}
41
42#[cfg(test)]
43#[path = "tests/layout_direction_tests.rs"]
44mod tests;