Skip to main content

declarative_gpui/
lib.rs

1//! Declarative `ui!` macro for building GPUI element trees.
2//!
3//! All parsing and expansion logic lives in `declarative-gpui-core` — a
4//! normal library crate, so it can be unit-tested, benchmarked, and fuzzed
5//! (proc-macro crates export nothing but their macros, and the `proc_macro`
6//! API is only usable inside a real expansion). This crate is just the entry
7//! point converting at the `proc_macro` / `proc_macro2` boundary.
8
9use proc_macro::TokenStream;
10
11/// Build a GPUI element tree declaratively.
12///
13/// Elements take style tokens, `key = value` builder calls, and call-style
14/// args; children go in braces, and `if`/`if let`/`for`/`match` expand to
15/// real Rust control flow. See the crate README for the full token reference.
16///
17/// ```ignore
18/// use declarative_gpui::ui;
19/// use gpui::prelude::*;
20///
21/// let view = ui! {
22///     col(gap_4 p_16 bg_1c1a17 rounded_lg) {
23///         text("Hello" text_lg semibold text_f5f0e8)
24///         for item in items {
25///             row(gap_2) { text(item.name) }
26///         }
27///     }
28/// };
29/// ```
30#[proc_macro]
31pub fn ui(input: TokenStream) -> TokenStream {
32    declarative_gpui_core::ui_impl(input.into()).into()
33}
34
35/// Convert a color to a `gpui::Hsla` literal at compile time — the same
36/// conversion the `ui!` style tokens use, exposed standalone so theme
37/// palettes can be `const`. Accepts 3/4/6/8-digit hex (optional leading `#`,
38/// optionally quoted) or a named color.
39///
40/// ```ignore
41/// use declarative_gpui::color;
42/// use gpui::Hsla;
43///
44/// struct Theme { panel: Hsla, text: Hsla }
45/// const DARK: Theme = Theme { panel: color!(1c1a17), text: color!("#f5f0e8") };
46/// ```
47///
48/// Storing theme tokens as `Hsla` (rather than hex `u32`s converted through
49/// `rgb()` per frame) makes a themed `bg = (th.panel)` a plain field copy —
50/// zero color math at render time.
51#[proc_macro]
52pub fn color(input: TokenStream) -> TokenStream {
53    declarative_gpui_core::color_impl(input.into()).into()
54}
55
56/// Like [`ui!`], but instead of building the element tree, expand to a
57/// `&'static str` of the pretty-printed GPUI builder code the same input
58/// generates — `cargo expand` scoped to one invocation, with no nightly or
59/// extra tooling.
60///
61/// ```ignore
62/// println!("{}", ui_expand! { row(px_8 bg_1c1a17) { text("hi") } });
63/// ```
64#[proc_macro]
65pub fn ui_expand(input: TokenStream) -> TokenStream {
66    declarative_gpui_core::ui_expand_impl(input.into()).into()
67}