use gpui_kit::{
AnyElement, App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement,
LayoutId, ParentElement, Pixels, Styled as _, Window, div, prelude::FluentBuilder as _,
};
use std::cell::RefCell;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Direction {
#[default]
Ltr,
Rtl,
}
impl Direction {
pub const ALL: [Direction; 2] = [Self::Ltr, Self::Rtl];
pub const fn name(self) -> &'static str {
match self {
Self::Ltr => "ltr",
Self::Rtl => "rtl",
}
}
pub const fn is_rtl(self) -> bool {
matches!(self, Self::Rtl)
}
}
thread_local! {
static STACK: RefCell<Vec<Direction>> = const { RefCell::new(Vec::new()) };
}
pub fn use_direction() -> Direction {
STACK.with(|s| s.borrow().last().copied().unwrap_or_default())
}
pub fn current() -> Direction {
use_direction()
}
pub fn is_rtl() -> bool {
use_direction().is_rtl()
}
pub fn inline_sides<T>(direction: Direction, start: T, end: T) -> (T, T) {
if direction.is_rtl() {
(end, start)
} else {
(start, end)
}
}
pub fn with_direction<R>(direction: Direction, f: impl FnOnce() -> R) -> R {
STACK.with(|s| s.borrow_mut().push(direction));
struct Pop;
impl Drop for Pop {
fn drop(&mut self) {
STACK.with(|s| {
s.borrow_mut().pop();
});
}
}
let _pop = Pop;
f()
}
pub struct DirectionProvider {
direction: Direction,
children: Vec<AnyElement>,
}
impl DirectionProvider {
pub fn new(direction: Direction) -> Self {
Self {
direction,
children: Vec::new(),
}
}
}
impl ParentElement for DirectionProvider {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl IntoElement for DirectionProvider {
type Element = ProviderElement;
fn into_element(self) -> Self::Element {
ProviderElement {
direction: self.direction,
child: div()
.flex()
.flex_col()
.when(self.direction.is_rtl(), |d| d.items_end().text_right())
.children(self.children)
.into_any_element(),
}
}
}
pub struct ProviderElement {
direction: Direction,
child: AnyElement,
}
impl Element for ProviderElement {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, ()) {
let child = &mut self.child;
let id = with_direction(self.direction, || child.request_layout(window, cx));
(id, ())
}
fn prepaint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
_: &mut (),
window: &mut Window,
cx: &mut App,
) {
let child = &mut self.child;
with_direction(self.direction, || {
child.prepaint(window, cx);
});
}
fn paint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<Pixels>,
_: &mut (),
_: &mut (),
window: &mut Window,
cx: &mut App,
) {
let child = &mut self.child;
with_direction(self.direction, || child.paint(window, cx));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn providers_nest_and_default_to_ltr() {
assert_eq!(use_direction(), Direction::Ltr);
with_direction(Direction::Rtl, || {
assert_eq!(use_direction(), Direction::Rtl);
with_direction(Direction::Ltr, || assert_eq!(current(), Direction::Ltr));
assert_eq!(use_direction(), Direction::Rtl);
});
assert_eq!(use_direction(), Direction::Ltr);
assert_eq!(Direction::Rtl.name(), "rtl");
assert!(Direction::Rtl.is_rtl());
with_direction(Direction::Rtl, || assert!(is_rtl()));
assert!(!is_rtl());
assert_eq!(inline_sides(Direction::Ltr, 16, 40), (16, 40));
assert_eq!(inline_sides(Direction::Rtl, 16, 40), (40, 16));
}
}