Skip to main content

teksilo_widgets/
text_base_direction.rs

1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4//! The base direction of a text a user types: the direction its paragraph
5//! reads in, which decides where the text starts, where an empty field puts
6//! its caret, and which way a screen reader is told it runs.
7//!
8//! A text input resolves it, every time it lays out, in this order:
9//!
10//! 1. an explicit [`TextBaseDirection::LeftToRight`] or
11//!    [`TextBaseDirection::RightToLeft`], used as given;
12//! 2. with [`TextBaseDirection::Auto`] (the default), the first strongly
13//!    directional character of the text (UAX #9, rules P2 and P3): Arabic
14//!    text reads right to left, Latin text left to right, whatever the
15//!    window's direction;
16//! 3. when the text has none (it is empty, or only digits and punctuation),
17//!    the [`content_language`](teksilo_core::widget_builder::WidgetBuilder::content_language)
18//!    declared on or above the field, through
19//!    [`LanguageTag::likely_direction`](teksilo_core::language::LanguageTag::likely_direction);
20//! 4. with no language declared, the window's layout direction.
21//!
22//! So an empty field declared Arabic opens with its caret on the right, and a
23//! field declared Japanese that holds Arabic text still reads right to left:
24//! the language never decides against the text. This is HTML's `dir="auto"`,
25//! falling back on the language before the surrounding direction.
26
27use teksilo_core::environment::LayoutDirection;
28use teksilo_core::language::LanguageTag;
29
30/// Which way a typed text's paragraph reads. See the [module](self) for how
31/// a text input resolves it.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
33pub enum TextBaseDirection {
34    /// The text decides: its first strongly directional character, else the
35    /// declared language's usual direction, else the window's.
36    #[default]
37    Auto,
38    /// Left to right, whatever the text.
39    LeftToRight,
40    /// Right to left, whatever the text.
41    RightToLeft,
42}
43
44impl TextBaseDirection {
45    /// The direction a paragraph of `text` reads in: see the [module](self).
46    /// `language` is the content language declared on or above the field,
47    /// `window` the window's layout direction.
48    pub fn resolve(
49        self,
50        text: &str,
51        language: Option<&LanguageTag>,
52        window: LayoutDirection,
53    ) -> LayoutDirection {
54        from_engine(teksilo_text::resolve_paragraph_direction(
55            text,
56            self.for_engine(),
57            to_engine(neutral_direction(language, window)),
58        ))
59    }
60
61    /// The direction the editing engine is given for blocks that store none:
62    /// `Auto` lets each block's text decide.
63    pub(crate) fn for_engine(self) -> teksilo_text::TextDirection {
64        match self {
65            Self::Auto => teksilo_text::TextDirection::Auto,
66            Self::LeftToRight => teksilo_text::TextDirection::LeftToRight,
67            Self::RightToLeft => teksilo_text::TextDirection::RightToLeft,
68        }
69    }
70}
71
72/// The direction a text with no strongly directional character takes: the
73/// declared language's usual direction, else the window's.
74pub(crate) fn neutral_direction(
75    language: Option<&LanguageTag>,
76    window: LayoutDirection,
77) -> LayoutDirection {
78    language.map_or(window, LanguageTag::likely_direction)
79}
80
81pub(crate) fn to_engine(direction: LayoutDirection) -> teksilo_text::TextDirection {
82    match direction {
83        LayoutDirection::LeftToRight => teksilo_text::TextDirection::LeftToRight,
84        LayoutDirection::RightToLeft => teksilo_text::TextDirection::RightToLeft,
85    }
86}
87
88pub(crate) fn from_engine(direction: teksilo_text::TextDirection) -> LayoutDirection {
89    match direction {
90        teksilo_text::TextDirection::RightToLeft => LayoutDirection::RightToLeft,
91        _ => LayoutDirection::LeftToRight,
92    }
93}
94
95/// The AccessKit direction of a layout direction.
96pub(crate) fn to_accesskit(direction: LayoutDirection) -> teksilo_core::accesskit::TextDirection {
97    match direction {
98        LayoutDirection::LeftToRight => teksilo_core::accesskit::TextDirection::LeftToRight,
99        LayoutDirection::RightToLeft => teksilo_core::accesskit::TextDirection::RightToLeft,
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    const LTR: LayoutDirection = LayoutDirection::LeftToRight;
108    const RTL: LayoutDirection = LayoutDirection::RightToLeft;
109
110    fn tag(s: &str) -> LanguageTag {
111        LanguageTag::parse(s).unwrap()
112    }
113
114    #[test]
115    fn auto_follows_the_text_whatever_the_language_and_the_window() {
116        let auto = TextBaseDirection::Auto;
117        assert_eq!(auto.resolve("مرحبا", None, LTR), RTL);
118        assert_eq!(auto.resolve("مرحبا", Some(&tag("ja")), LTR), RTL);
119        assert_eq!(auto.resolve("hello", Some(&tag("ar")), RTL), LTR);
120    }
121
122    #[test]
123    fn with_no_strong_character_the_language_then_the_window_decides() {
124        let auto = TextBaseDirection::Auto;
125        assert_eq!(auto.resolve("", Some(&tag("ar")), LTR), RTL);
126        assert_eq!(auto.resolve("123", Some(&tag("he")), LTR), RTL);
127        assert_eq!(auto.resolve("", Some(&tag("ja")), RTL), LTR);
128        assert_eq!(auto.resolve("", None, RTL), RTL);
129        assert_eq!(auto.resolve("", None, LTR), LTR);
130    }
131
132    #[test]
133    fn an_explicit_direction_is_used_as_given() {
134        assert_eq!(
135            TextBaseDirection::LeftToRight.resolve("مرحبا", Some(&tag("ar")), RTL),
136            LTR
137        );
138        assert_eq!(
139            TextBaseDirection::RightToLeft.resolve("hello", None, LTR),
140            RTL
141        );
142    }
143}