gpui-ui-kit 0.5.10

A reusable UI component library for GPUI applications
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
//! Text component
//!
//! Typography and text styling utilities.

use crate::theme::{Theme, ThemeExt};
use gpui::prelude::*;
use gpui::{Component, *};

/// Text size variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextSize {
    /// Extra small
    Xs,
    /// Small
    Sm,
    /// Medium (default)
    #[default]
    Md,
    /// Large
    Lg,
    /// Extra large
    Xl,
    /// 2X large
    Xxl,
}

/// Text weight
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TextWeight {
    /// Light
    Light,
    /// Normal (default)
    #[default]
    Normal,
    /// Medium
    Medium,
    /// Semibold
    Semibold,
    /// Bold
    Bold,
}

impl TextWeight {
    fn to_font_weight(&self) -> FontWeight {
        match self {
            TextWeight::Light => FontWeight::LIGHT,
            TextWeight::Normal => FontWeight::NORMAL,
            TextWeight::Medium => FontWeight::MEDIUM,
            TextWeight::Semibold => FontWeight::SEMIBOLD,
            TextWeight::Bold => FontWeight::BOLD,
        }
    }
}

/// A styled text component
#[derive(IntoElement)]
pub struct Text {
    content: SharedString,
    size: TextSize,
    weight: TextWeight,
    color: Option<Rgba>,
    muted: bool,
    truncate: bool,
    theme: Option<Theme>,
}

impl Text {
    /// Create new text
    pub fn new(content: impl Into<SharedString>) -> Self {
        Self {
            content: content.into(),
            size: TextSize::default(),
            weight: TextWeight::default(),
            color: None,
            muted: false,
            truncate: false,
            theme: None,
        }
    }

    /// Set theme
    pub fn with_theme(mut self, theme: Theme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// Set size
    pub fn size(mut self, size: TextSize) -> Self {
        self.size = size;
        self
    }

    /// Set weight
    pub fn weight(mut self, weight: TextWeight) -> Self {
        self.weight = weight;
        self
    }

    /// Set custom color
    pub fn color(mut self, color: Rgba) -> Self {
        self.color = Some(color);
        self
    }

    /// Make text muted (secondary color)
    pub fn muted(mut self, muted: bool) -> Self {
        self.muted = muted;
        self
    }

    /// Truncate with ellipsis
    pub fn truncate(mut self, truncate: bool) -> Self {
        self.truncate = truncate;
        self
    }

    /// Build into element with theme from App context
    pub fn build_with_cx(self, cx: &App) -> Div {
        let theme = self.theme.clone().unwrap_or_else(|| cx.theme());
        self.build_with_theme(&theme)
    }

    /// Build into element with explicit theme
    pub fn build_with_theme(self, theme: &Theme) -> Div {
        let text_color = if let Some(color) = self.color {
            color
        } else if self.muted {
            theme.text_muted
        } else {
            theme.text_secondary
        };

        let mut text = div()
            .text_color(text_color)
            .font_weight(self.weight.to_font_weight());

        // Apply size
        text = match self.size {
            TextSize::Xs => text.text_xs(),
            TextSize::Sm => text.text_sm(),
            TextSize::Md => text.text_sm(),
            TextSize::Lg => text.text_lg(),
            TextSize::Xl => text.text_xl(),
            TextSize::Xxl => text.text_2xl(),
        };

        if self.truncate {
            text = text.overflow_hidden().whitespace_nowrap();
        }

        text.child(self.content)
    }

    /// Build into element (uses default dark theme colors for backwards compatibility)
    pub fn build(self) -> Div {
        let theme = self.theme.clone().unwrap_or_else(Theme::dark);
        self.build_with_theme(&theme)
    }
}

impl RenderOnce for Text {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = self.theme.clone().unwrap_or_else(|| cx.theme());
        self.build_with_theme(&theme)
    }
}

/// A heading component
#[derive(IntoElement)]
pub struct Heading {
    content: SharedString,
    level: u8,
    theme: Option<Theme>,
}

impl Heading {
    /// Create a new heading
    pub fn new(content: impl Into<SharedString>) -> Self {
        Self {
            content: content.into(),
            level: 1,
            theme: None,
        }
    }

    /// Set theme
    pub fn with_theme(mut self, theme: Theme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// Set heading level (1-6)
    pub fn level(mut self, level: u8) -> Self {
        self.level = level.clamp(1, 6);
        self
    }

    /// Create h1
    pub fn h1(content: impl Into<SharedString>) -> Self {
        Self::new(content).level(1)
    }

    /// Create h2
    pub fn h2(content: impl Into<SharedString>) -> Self {
        Self::new(content).level(2)
    }

    /// Create h3
    pub fn h3(content: impl Into<SharedString>) -> Self {
        Self::new(content).level(3)
    }

    /// Create h4
    pub fn h4(content: impl Into<SharedString>) -> Self {
        Self::new(content).level(4)
    }

    /// Build into element with explicit theme
    pub fn build_with_theme(self, theme: &Theme) -> Div {
        let mut heading = div()
            .font_weight(FontWeight::BOLD)
            .text_color(theme.text_primary);

        heading = match self.level {
            1 => heading.text_2xl(),
            2 => heading.text_xl(),
            3 => heading.text_lg(),
            4 => heading,
            5 => heading.text_sm(),
            _ => heading.text_xs(),
        };

        heading.child(self.content)
    }

    /// Build into element (uses default dark theme colors for backwards compatibility)
    pub fn build(self) -> Div {
        let theme = self.theme.clone().unwrap_or_else(Theme::dark);
        self.build_with_theme(&theme)
    }
}

impl RenderOnce for Heading {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = self.theme.clone().unwrap_or_else(|| cx.theme());
        self.build_with_theme(&theme)
    }
}

/// A code/monospace text component
#[derive(IntoElement)]
pub struct Code {
    content: SharedString,
    inline: bool,
    theme: Option<Theme>,
}

impl Code {
    /// Create inline code
    pub fn new(content: impl Into<SharedString>) -> Self {
        Self {
            content: content.into(),
            inline: true,
            theme: None,
        }
    }

    /// Create code block
    pub fn block(content: impl Into<SharedString>) -> Self {
        Self {
            content: content.into(),
            inline: false,
            theme: None,
        }
    }

    /// Set theme
    pub fn with_theme(mut self, theme: Theme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// Build into element with explicit theme
    pub fn build_with_theme(self, theme: &Theme) -> Div {
        // Code uses a slightly different color from accent
        let code_text = match theme.variant {
            crate::theme::ThemeVariant::Light => rgb(0xc7254e),
            // Dark, Midnight, Forest, BlackAndWhite all use dark-style colors
            crate::theme::ThemeVariant::Dark
            | crate::theme::ThemeVariant::Midnight
            | crate::theme::ThemeVariant::Forest
            | crate::theme::ThemeVariant::BlackAndWhite => rgb(0xe06c75),
        };

        if self.inline {
            div()
                .px_1()
                .py(px(1.0))
                .bg(theme.surface)
                .rounded(px(3.0))
                .text_xs()
                .text_color(code_text)
                .child(self.content)
        } else {
            div()
                .p_3()
                .bg(theme.muted)
                .rounded_md()
                .text_sm()
                .text_color(theme.text_secondary)
                .overflow_hidden()
                .child(self.content)
        }
    }

    /// Build into element (uses default dark theme colors for backwards compatibility)
    pub fn build(self) -> Div {
        let theme = self.theme.clone().unwrap_or_else(Theme::dark);
        self.build_with_theme(&theme)
    }
}

impl RenderOnce for Code {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = self.theme.clone().unwrap_or_else(|| cx.theme());
        self.build_with_theme(&theme)
    }
}

/// A link component
pub struct Link {
    id: ElementId,
    content: SharedString,
    href: Option<SharedString>,
    external: bool,
    on_click: Option<Box<dyn Fn(&mut Window, &mut App) + 'static>>,
    theme: Option<Theme>,
}

impl Link {
    /// Create a new link
    pub fn new(id: impl Into<ElementId>, content: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            content: content.into(),
            href: None,
            external: false,
            on_click: None,
            theme: None,
        }
    }

    /// Set theme
    pub fn with_theme(mut self, theme: Theme) -> Self {
        self.theme = Some(theme);
        self
    }

    /// Set href
    pub fn href(mut self, href: impl Into<SharedString>) -> Self {
        self.href = Some(href.into());
        self
    }

    /// Mark as external link
    pub fn external(mut self, external: bool) -> Self {
        self.external = external;
        self
    }

    /// Set click handler
    pub fn on_click(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_click = Some(Box::new(handler));
        self
    }

    /// Build into element with explicit theme
    pub fn build_with_theme(self, theme: &Theme) -> Stateful<Div> {
        let accent = theme.accent;
        let accent_hover = theme.accent_hover;

        let mut link = div()
            .id(self.id)
            .text_color(accent)
            .cursor_pointer()
            .hover(move |s| s.text_color(accent_hover));

        if let Some(handler) = self.on_click {
            link = link.on_mouse_up(MouseButton::Left, move |_event, window, cx| {
                handler(window, cx);
            });
        }

        link = link.child(self.content);

        if self.external {
            link = link.child(div().text_xs().ml_1().child(""));
        }

        link
    }

    /// Build into element (uses default dark theme colors for backwards compatibility)
    pub fn build(self) -> Stateful<Div> {
        let theme = self.theme.clone().unwrap_or_else(Theme::dark);
        self.build_with_theme(&theme)
    }
}

impl IntoElement for Link {
    type Element = Component<Self>;

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

impl RenderOnce for Link {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = self.theme.clone().unwrap_or_else(|| cx.theme());
        self.build_with_theme(&theme)
    }
}