Skip to main content

rok_ui/
lib.rs

1//! # rok-ui
2//!
3//! A shadcn/ui-style component system for [GPUI](https://gpui.rs) desktop apps,
4//! with a React-like developer experience:
5//!
6//! | React / shadcn/ui                    | rok-ui                                              |
7//! |--------------------------------------|-----------------------------------------------------|
8//! | `function Card(props) { … }`         | `#[component] fn Card(…) -> impl IntoElement { … }` |
9//! | `<Button variant="outline" size="sm">` | `Button::new("id").outline().small()`             |
10//! | `props.children`                     | `#[children] children: Vec<AnyElement>`             |
11//! | `className="w-full mt-4"`            | `.w_full().mt_4()` on any component                 |
12//! | `onClick={() => …}`                  | `.on_click(\|event, window, cx\| …)`                 |
13//! | `const [n, setN] = useState(0)`      | `let n = use_state(window, cx, \|\| 0)`              |
14//! | CSS variables (`--primary`)          | `cx.theme().colors.primary`                         |
15//!
16//! ```no_run
17//! use rok_ui::prelude::*;
18//!
19//! struct MyView;
20//!
21//! impl Render for MyView {
22//!     fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
23//!         AppRoot::new().child(Button::new("hello").label("Hello"))
24//!     }
25//! }
26//!
27//! fn main() {
28//!     Application::new().with_assets(rok_ui::Assets).run(|cx: &mut App| {
29//!         rok_ui::init(cx);
30//!         cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_| MyView)).unwrap();
31//!     });
32//! }
33//! ```
34
35#![cfg_attr(docsrs, feature(doc_cfg))]
36
37// Lets `#[component]` expand to `::rok_ui::…` paths inside this crate too.
38extern crate self as rok_ui;
39
40pub mod bidi;
41#[cfg(feature = "bloc")]
42pub mod bloc;
43pub mod components;
44pub mod context;
45pub mod cx;
46#[cfg(feature = "db")]
47pub mod db;
48#[cfg(feature = "devtools")]
49pub mod devtools;
50pub mod fonts;
51#[cfg(feature = "form")]
52pub mod form;
53pub mod hooks;
54#[cfg(feature = "http")]
55pub mod http;
56pub mod icon;
57pub mod keyed;
58pub mod motion;
59#[cfg(feature = "persist")]
60pub mod persist;
61pub mod platform;
62pub mod prelude;
63#[cfg(feature = "query")]
64pub mod query;
65#[cfg(feature = "router")]
66pub mod router;
67#[cfg(feature = "runtime")]
68pub mod runtime;
69mod scope;
70#[cfg(feature = "state")]
71pub mod state;
72pub mod styles;
73pub mod sx;
74pub mod theme;
75
76pub use cx::Cx;
77pub use gpui;
78pub use icon::{Assets, AssetsWithFallback, Icon, IconName};
79pub use keyed::Keyed;
80pub use rok_ui_macros::{children, component, keyframes, style, styles, view};
81#[cfg(feature = "query")]
82pub use rok_ui_macros::{memoize, procedure};
83
84/// Support code for the `view!` and `children!` macros. Not public API.
85#[doc(hidden)]
86pub mod __private {
87    use std::cell::Cell;
88
89    use gpui::{AnyElement, IntoElement, SharedString};
90
91    /// A child expression from markup. Text (anything `Into<SharedString>`) becomes
92    /// a [`crate::components::BidiText`] through [`TextChild`]; other elements go
93    /// through [`ElementChild`]. Method resolution tries `TextChild` first because
94    /// it needs one reference less (autoref specialization).
95    pub struct Child<T>(Cell<Option<T>>);
96
97    impl<T> Child<T> {
98        pub fn new(value: T) -> Self {
99            Self(Cell::new(Some(value)))
100        }
101
102        fn take(&self) -> T {
103            self.0.take().expect("a markup child is converted once")
104        }
105    }
106
107    pub trait TextChild {
108        fn take_child(&self) -> AnyElement;
109    }
110
111    impl<T: Into<SharedString>> TextChild for Child<T> {
112        fn take_child(&self) -> AnyElement {
113            crate::components::BidiText::new(self.take()).into_any_element()
114        }
115    }
116
117    pub trait ElementChild {
118        fn take_child(&self) -> AnyElement;
119    }
120
121    impl<T: IntoElement> ElementChild for &Child<T> {
122        fn take_child(&self) -> AnyElement {
123            self.take().into_any_element()
124        }
125    }
126}
127
128/// Install the default theme and the key bindings rok-ui components rely on
129/// (text editing, Tab focus navigation). Call once at startup.
130pub fn init(cx: &mut gpui::App) {
131    theme::init(cx);
132    // Keyboard input makes focus rings visible (see `sx::focus_visible`).
133    cx.observe_keystrokes(|_, window, _| {
134        if sx::set_keyboard_modality(true) {
135            window.refresh();
136        }
137    })
138    .detach();
139    components::app_root::bind_focus_navigation_keys(cx);
140    #[cfg(feature = "input")]
141    components::input::bind_text_editing_keys(cx);
142    #[cfg(feature = "router")]
143    router::init(cx);
144    #[cfg(feature = "state")]
145    state::init(cx);
146    #[cfg(feature = "devtools")]
147    devtools::init(cx);
148}