rok-ui 0.9.2

shadcn/ui-style component system for GPUI desktop apps, with a React-like developer experience.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Direction: left-to-right or right-to-left layout (shadcn/ui's `<DirectionProvider>`).
//!
//! In RTL, layouts mirror the way CSS `dir="rtl"` mirrors them:
//! - horizontal flex rows flow right to left (components and `styles!` rows);
//! - logical spacing and positions (`ps`, `pe`, `start`, `end`, `padding_start`
//!   in `styles!`) land on the reading side;
//! - text aligns to the right, and directional icons (chevrons, arrows) flip;
//! - floating surfaces align to the trigger's right edge, sheets swap sides,
//!   sliders and progress bars fill from the right.
//!
//! GPUI shapes each line of text left to right, so this does not reorder
//! characters inside mixed-direction text.
//!
//! ```no_run
//! # use rok_ui::prelude::*;
//! # fn example(window: &mut Window, cx: &mut App) {
//! # #[component] fn Settings() -> impl IntoElement { div() }
//! set_text_direction(TextDirection::from_locale("ar-EG"), cx); // the whole app
//!
//! let subtree = Direction::new(TextDirection::Rtl).child(Settings::new()); // one subtree
//! let built = Direction::build(TextDirection::Rtl, || view! { div { "Built inside" } });
//! # }
//! ```

use std::cell::Cell;

use gpui::{
    div, prelude::*, px, AnyElement, App, DefiniteLength, GlobalElementId, InspectorElementId,
    LayoutId, Length, Pixels, Window,
};

use crate::icon::IconName;

/// Reading direction.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TextDirection {
    /// Left to right (English, French).
    #[default]
    Ltr,
    /// Right to left (Arabic, Hebrew, Persian).
    Rtl,
}

impl TextDirection {
    /// Whether this is right to left.
    #[must_use]
    pub fn is_rtl(self) -> bool {
        self == TextDirection::Rtl
    }

    /// The direction of a BCP 47 locale or language code: `"ar"`, `"he-IL"`, `"fa_IR"`.
    #[must_use]
    pub fn from_locale(locale: &str) -> Self {
        const RTL_LANGUAGES: &[&str] = &[
            "ar", "arc", "ckb", "dv", "fa", "ha", "he", "iw", "khw", "ks", "ku", "ps", "sd", "ug",
            "ur", "yi",
        ];
        let language = locale
            .split(['-', '_'])
            .next()
            .unwrap_or_default()
            .to_ascii_lowercase();
        if RTL_LANGUAGES.contains(&language.as_str()) {
            TextDirection::Rtl
        } else {
            TextDirection::Ltr
        }
    }
}

thread_local! {
    static CURRENT: Cell<TextDirection> = const { Cell::new(TextDirection::Ltr) };
}

/// The direction in effect right now (while rendering: the enclosing
/// [`Direction`], else the app direction).
pub fn current_direction() -> TextDirection {
    CURRENT.with(Cell::get)
}

/// `current_direction().is_rtl()`.
#[must_use]
pub fn is_rtl() -> bool {
    current_direction().is_rtl()
}

/// Run `body` with `direction` in effect, then restore the previous one.
pub fn with_direction<R>(direction: TextDirection, body: impl FnOnce() -> R) -> R {
    let previous = CURRENT.with(|current| current.replace(direction));
    let result = body();
    CURRENT.with(|current| current.set(previous));
    result
}

/// Set the direction for the whole app (outside any [`Direction`] element).
pub fn set_text_direction(direction: TextDirection, cx: &mut App) {
    CURRENT.with(|current| current.set(direction));
    cx.refresh_windows();
}

/// Read the direction while rendering: `cx.direction().is_rtl()`.
pub trait ActiveDirection {
    /// The text direction in effect.
    fn direction(&self) -> TextDirection;
}

impl ActiveDirection for App {
    fn direction(&self) -> TextDirection {
        current_direction()
    }
}

impl IconName {
    /// The icon pointing the other way: left and right chevrons and arrows swap.
    #[must_use]
    pub fn mirrored(self) -> IconName {
        match self {
            IconName::ChevronLeft => IconName::ChevronRight,
            IconName::ChevronRight => IconName::ChevronLeft,
            IconName::ChevronsLeft => IconName::ChevronsRight,
            IconName::ChevronsRight => IconName::ChevronsLeft,
            IconName::ArrowLeft => IconName::ArrowRight,
            IconName::ArrowRight => IconName::ArrowLeft,
            other => other,
        }
    }

    /// Mirrored in RTL, unchanged in LTR. Use it for "forward" / "back" icons.
    #[must_use]
    pub fn for_direction(self) -> IconName {
        if is_rtl() {
            self.mirrored()
        } else {
            self
        }
    }
}

/// Direction-aware styling for GPUI elements and rok-ui components. These read
/// the direction when called, so call them while rendering.
pub trait DirectionalStyled: Styled + Sized {
    /// `flex()` with the row flowing in the reading direction (right to left
    /// in RTL). A later `flex_col()` still makes it a column.
    #[must_use]
    fn flex_dir(self) -> Self {
        let element = self.flex();
        if is_rtl() {
            element.flex_row_reverse()
        } else {
            element
        }
    }

    /// A row that always flows left to right (codes, numbers, charts).
    #[must_use]
    fn flex_ltr(self) -> Self {
        self.flex().flex_row()
    }

    /// Padding on the starting side (left in LTR, right in RTL).
    #[must_use]
    fn ps(self, length: impl Into<DefiniteLength> + Clone) -> Self {
        if is_rtl() {
            self.pr(length)
        } else {
            self.pl(length)
        }
    }

    /// Padding on the ending side.
    #[must_use]
    fn pe(self, length: impl Into<DefiniteLength> + Clone) -> Self {
        if is_rtl() {
            self.pl(length)
        } else {
            self.pr(length)
        }
    }

    /// Margin on the starting side.
    #[must_use]
    fn ms(self, length: impl Into<Length> + Clone) -> Self {
        if is_rtl() {
            self.mr(length)
        } else {
            self.ml(length)
        }
    }

    /// Margin on the ending side.
    #[must_use]
    fn me(self, length: impl Into<Length> + Clone) -> Self {
        if is_rtl() {
            self.ml(length)
        } else {
            self.mr(length)
        }
    }

    /// Offset from the starting edge (absolute / relative positioning).
    #[must_use]
    fn inset_start(self, length: impl Into<Length> + Clone) -> Self {
        if is_rtl() {
            self.right(length)
        } else {
            self.left(length)
        }
    }

    /// Offset from the ending edge.
    #[must_use]
    fn inset_end(self, length: impl Into<Length> + Clone) -> Self {
        if is_rtl() {
            self.left(length)
        } else {
            self.right(length)
        }
    }

    /// 1px border on the starting side.
    #[must_use]
    fn border_s_1(self) -> Self {
        if is_rtl() {
            self.border_r_1()
        } else {
            self.border_l_1()
        }
    }

    /// 1px border on the ending side.
    #[must_use]
    fn border_e_1(self) -> Self {
        if is_rtl() {
            self.border_l_1()
        } else {
            self.border_r_1()
        }
    }

    /// Round the starting corners.
    #[must_use]
    fn rounded_s(self, radius: impl Into<gpui::AbsoluteLength> + Clone) -> Self {
        if is_rtl() {
            self.rounded_r(radius)
        } else {
            self.rounded_l(radius)
        }
    }

    /// Round the ending corners.
    #[must_use]
    fn rounded_e(self, radius: impl Into<gpui::AbsoluteLength> + Clone) -> Self {
        if is_rtl() {
            self.rounded_l(radius)
        } else {
            self.rounded_r(radius)
        }
    }

    /// Square off the starting corners.
    #[must_use]
    fn rounded_s_none(self) -> Self {
        self.rounded_s(px(0.))
    }

    /// Square off the ending corners.
    #[must_use]
    fn rounded_e_none(self) -> Self {
        self.rounded_e(px(0.))
    }

    /// Align text to the starting side.
    #[must_use]
    fn text_start(self) -> Self {
        if is_rtl() {
            self.text_right()
        } else {
            self.text_left()
        }
    }

    /// Align text to the ending side.
    #[must_use]
    fn text_end(self) -> Self {
        if is_rtl() {
            self.text_left()
        } else {
            self.text_right()
        }
    }
}

impl<E: Styled + Sized> DirectionalStyled for E {}

/// Applies a direction to everything rendered inside it.
///
/// Components and views inside render in this direction. Plain elements you
/// build in the same expression are built before the `Direction` is laid out;
/// build them with [`Direction::build`] so they see it too.
pub struct Direction {
    value: TextDirection,
    children: Vec<AnyElement>,
    content: Option<AnyElement>,
}

impl Direction {
    /// Render children in `direction`.
    #[must_use]
    pub fn new(direction: TextDirection) -> Self {
        Self {
            value: direction,
            children: Vec::new(),
            content: None,
        }
    }

    /// Build `content` with `direction` in effect (so `.sx()`, `flex_dir()` and
    /// friends mirror), and render it inside a `Direction`.
    pub fn build<E: IntoElement>(direction: TextDirection, content: impl FnOnce() -> E) -> Self {
        let element = with_direction(direction, || content().into_any_element());
        Self::new(direction).child(element)
    }
}

impl ParentElement for Direction {
    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
        self.children.extend(elements);
    }
}

impl IntoElement for Direction {
    type Element = Self;

    fn into_element(self) -> Self::Element {
        self
    }
}

impl Element for Direction {
    type RequestLayoutState = ();
    type PrepaintState = ();

    fn id(&self) -> Option<gpui::ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, Self::RequestLayoutState) {
        let is_rtl = self.value.is_rtl();
        let mut content = div()
            .flex()
            .flex_col()
            .when(is_rtl, |content| content.text_right().items_end())
            .children(std::mem::take(&mut self.children))
            .into_any_element();
        // Components render while their layout is requested, so the direction
        // is installed for this step (and for prepaint and paint, where lists
        // and portals render too).
        let layout_id = with_direction(self.value, || content.request_layout(window, cx));
        self.content = Some(content);
        (layout_id, ())
    }

    fn prepaint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        _bounds: gpui::Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        window: &mut Window,
        cx: &mut App,
    ) -> Self::PrepaintState {
        if let Some(content) = self.content.as_mut() {
            with_direction(self.value, || content.prepaint(window, cx));
        }
    }

    fn paint(
        &mut self,
        _id: Option<&GlobalElementId>,
        _inspector_id: Option<&InspectorElementId>,
        _bounds: gpui::Bounds<Pixels>,
        _request_layout: &mut Self::RequestLayoutState,
        _prepaint: &mut Self::PrepaintState,
        window: &mut Window,
        cx: &mut App,
    ) {
        if let Some(content) = self.content.as_mut() {
            with_direction(self.value, || content.paint(window, cx));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn locales_map_to_directions() {
        assert_eq!(TextDirection::from_locale("ar"), TextDirection::Rtl);
        assert_eq!(TextDirection::from_locale("he-IL"), TextDirection::Rtl);
        assert_eq!(TextDirection::from_locale("fa_IR"), TextDirection::Rtl);
        assert_eq!(TextDirection::from_locale("ur-PK"), TextDirection::Rtl);
        assert_eq!(TextDirection::from_locale("en-US"), TextDirection::Ltr);
        assert_eq!(TextDirection::from_locale("fr"), TextDirection::Ltr);
        assert_eq!(TextDirection::from_locale(""), TextDirection::Ltr);
    }

    #[test]
    fn with_direction_restores_the_previous_direction() {
        assert_eq!(current_direction(), TextDirection::Ltr);
        with_direction(TextDirection::Rtl, || {
            assert!(is_rtl());
            assert_eq!(
                IconName::ChevronRight.for_direction(),
                IconName::ChevronLeft
            );
            with_direction(TextDirection::Ltr, || assert!(!is_rtl()));
            assert!(is_rtl());
        });
        assert!(!is_rtl());
        assert_eq!(
            IconName::ChevronRight.for_direction(),
            IconName::ChevronRight
        );
    }
}