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
424
425
426
427
428
429
430
//! Accordion component
//!
//! Collapsible content sections with support for both vertical and horizontal orientations.

use crate::ComponentTheme;
use crate::theme::{ThemeExt, glow_shadow};
use gpui::prelude::*;
use gpui::*;

/// Theme colors for accordion styling
#[derive(Debug, Clone, ComponentTheme)]
pub struct AccordionTheme {
    #[theme(default = 0x252525, from = muted)]
    pub header_bg: Rgba,
    #[theme(default = 0x2a2a2a, from = surface_hover)]
    pub header_hover_bg: Rgba,
    #[theme(default = 0x1e1e1e, from = background)]
    pub content_bg: Rgba,
    #[theme(default = 0x3a3a3a, from = border)]
    pub border: Rgba,
    #[theme(default = 0xffffff, from = text_primary)]
    pub title_color: Rgba,
    #[theme(default = 0x888888, from = text_muted)]
    pub indicator_color: Rgba,
}

/// A single accordion item
pub struct AccordionItem {
    id: SharedString,
    title: SharedString,
    content: Option<AnyElement>,
    disabled: bool,
}

impl AccordionItem {
    /// Create a new accordion item
    pub fn new(id: impl Into<SharedString>, title: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            title: title.into(),
            content: None,
            disabled: false,
        }
    }

    /// Set content
    pub fn content(mut self, content: impl IntoElement) -> Self {
        self.content = Some(content.into_any_element());
        self
    }

    /// Set disabled state
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Get the item ID
    pub fn id(&self) -> &SharedString {
        &self.id
    }
}

/// Accordion behavior mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AccordionMode {
    /// Only one item can be open at a time
    #[default]
    Single,
    /// Multiple items can be open
    Multiple,
}

/// Accordion orientation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AccordionOrientation {
    /// Vertical layout: headers stacked vertically, content expands downward (default)
    #[default]
    Vertical,
    /// Horizontal layout: headers arranged horizontally, content expands downward
    Horizontal,
    /// Side layout: headers stacked vertically on left, content expands to right
    Side,
}

/// An accordion component
pub struct Accordion {
    items: Vec<AccordionItem>,
    expanded: Vec<SharedString>,
    mode: AccordionMode,
    orientation: AccordionOrientation,
    theme: Option<AccordionTheme>,
    on_change: Option<Box<dyn Fn(&SharedString, bool, &mut Window, &mut App) + 'static>>,
}

impl Accordion {
    /// Create a new accordion
    pub fn new() -> Self {
        Self {
            items: Vec::new(),
            expanded: Vec::new(),
            mode: AccordionMode::default(),
            orientation: AccordionOrientation::default(),
            theme: None,
            on_change: None,
        }
    }

    /// Set items
    pub fn items(mut self, items: Vec<AccordionItem>) -> Self {
        self.items = items;
        self
    }

    /// Add a single item
    pub fn item(mut self, item: AccordionItem) -> Self {
        self.items.push(item);
        self
    }

    /// Set expanded item IDs
    pub fn expanded(mut self, expanded: Vec<SharedString>) -> Self {
        self.expanded = expanded;
        self
    }

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

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

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

    /// Set change handler (receives item ID and new expanded state)
    pub fn on_change(
        mut self,
        handler: impl Fn(&SharedString, bool, &mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_change = Some(Box::new(handler));
        self
    }

    /// Build into element with theme
    pub fn build_with_theme(self, theme: &AccordionTheme) -> Div {
        // Use self.theme if provided, otherwise clone the passed theme
        let theme = self.theme.unwrap_or_else(|| theme.clone());

        // Handle Side layout separately since it needs different structure
        let is_side = matches!(self.orientation, AccordionOrientation::Side);
        if is_side {
            let Accordion {
                items,
                expanded,
                on_change,
                ..
            } = self;
            let on_change = on_change.map(|h| std::rc::Rc::new(h));
            return Self::build_side_layout_static(items, expanded, theme, on_change);
        }

        let on_change = self.on_change.map(|h| std::rc::Rc::new(h));
        let is_vertical = matches!(self.orientation, AccordionOrientation::Vertical);

        let mut container = div()
            .flex()
            .border_1()
            .border_color(theme.border)
            .rounded_lg();

        // Set flex direction based on orientation
        container = if is_vertical {
            container.flex_col()
        } else {
            container.flex_row()
        };

        for (idx, item) in self.items.into_iter().enumerate() {
            let is_expanded = self.expanded.contains(&item.id);
            let item_id = item.id.clone();
            let is_first = idx == 0;

            // Create item wrapper for horizontal layout
            let mut item_wrapper = div();
            if !is_vertical {
                item_wrapper = item_wrapper.flex().flex_col();
            }

            // Header
            let mut header = div()
                .id(SharedString::from(format!("accordion-header-{}", item_id)))
                .flex()
                .items_center()
                .justify_between()
                .px_4()
                .py_3()
                .bg(theme.header_bg)
                .cursor_pointer();

            // Add border based on orientation
            if !is_first {
                header = if is_vertical {
                    header.border_t_1().border_color(theme.border)
                } else {
                    header.border_l_1().border_color(theme.border)
                };
            }

            if item.disabled {
                header = header.opacity(0.5).cursor_not_allowed();
            } else {
                let hover_bg = theme.header_hover_bg;
                header =
                    header.hover(move |style| style.bg(hover_bg).shadow(glow_shadow(hover_bg)));

                // Click handler
                if let Some(handler) = on_change.clone() {
                    let id = item_id.clone();
                    let new_state = !is_expanded;
                    header = header.on_mouse_up(MouseButton::Left, move |_event, window, cx| {
                        (handler)(&id, new_state, window, cx);
                    });
                }
            }

            // Title
            header = header.child(
                div()
                    .text_sm()
                    .font_weight(FontWeight::MEDIUM)
                    .text_color(theme.title_color)
                    .child(item.title),
            );

            // Expand/collapse indicator (different for vertical vs horizontal)
            let indicator = if is_vertical {
                if is_expanded { "â–¼" } else { "â–¶" }
            } else if is_expanded {
                "â–¼"
            } else {
                "â–²"
            };
            header = header.child(
                div()
                    .text_xs()
                    .text_color(theme.indicator_color)
                    .child(indicator),
            );

            item_wrapper = item_wrapper.child(header);

            // Content (only if expanded)
            if is_expanded && let Some(content) = item.content {
                let content_div = div()
                    .px_4()
                    .py_3()
                    .bg(theme.content_bg)
                    .border_t_1()
                    .border_color(theme.border);

                item_wrapper = item_wrapper.child(content_div.child(content));
            }

            container = container.child(item_wrapper);
        }

        container
    }

    /// Build side layout: headers vertically on left, content expands to right
    fn build_side_layout_static(
        items: Vec<AccordionItem>,
        expanded: Vec<SharedString>,
        theme: AccordionTheme,
        on_change: Option<
            std::rc::Rc<Box<dyn Fn(&SharedString, bool, &mut Window, &mut App) + 'static>>,
        >,
    ) -> Div {
        let mut container = div()
            .flex()
            .flex_row()
            .border_1()
            .border_color(theme.border)
            .rounded_lg();

        // Left side: vertical header tabs
        let mut headers_container = div()
            .flex()
            .flex_col()
            .border_r_1()
            .border_color(theme.border);

        for (idx, item) in items.iter().enumerate() {
            let is_expanded = expanded.contains(&item.id);
            let item_id = item.id.clone();
            let is_first = idx == 0;

            let mut header = div()
                .id(SharedString::from(format!(
                    "accordion-header-side-{}",
                    item_id
                )))
                .flex()
                .items_center()
                .justify_center()
                .w(px(40.0))
                .py_4()
                .bg(theme.header_bg)
                .cursor_pointer();

            if !is_first {
                header = header.border_t_1().border_color(theme.border);
            }

            if is_expanded {
                header = header.bg(theme.header_hover_bg);
            }

            if item.disabled {
                header = header.opacity(0.5).cursor_not_allowed();
            } else {
                let hover_bg = theme.header_hover_bg;
                header =
                    header.hover(move |style| style.bg(hover_bg).shadow(glow_shadow(hover_bg)));

                // Click handler
                if let Some(handler) = on_change.clone() {
                    let id = item_id.clone();
                    let new_state = !is_expanded;
                    header = header.on_mouse_up(MouseButton::Left, move |_event, window, cx| {
                        (handler)(&id, new_state, window, cx);
                    });
                }
            }

            // Vertical text - display each character on its own line
            let mut text_container = div().flex().flex_col().items_center().gap_1();

            // Show full text vertically when expanded, abbreviated when closed
            if is_expanded {
                // Show full text vertically
                for ch in item.title.chars() {
                    text_container = text_container.child(
                        div()
                            .text_xs()
                            .font_weight(FontWeight::MEDIUM)
                            .text_color(theme.title_color)
                            .child(ch.to_string()),
                    );
                }
            } else {
                // Show first character only when closed
                let label_text = if !item.title.is_empty() {
                    item.title.chars().next().unwrap().to_string()
                } else {
                    String::from("?")
                };
                text_container = text_container.child(
                    div()
                        .text_sm()
                        .font_weight(FontWeight::MEDIUM)
                        .text_color(theme.title_color)
                        .child(label_text),
                );
            }

            header = header.child(text_container);

            headers_container = headers_container.child(header);
        }

        container = container.child(headers_container);

        // Right side: content area - show all expanded items side by side
        let mut content_container = div().flex().flex_row().flex_1();

        for item in items.into_iter() {
            let is_expanded = expanded.contains(&item.id);

            if is_expanded && let Some(content) = item.content {
                let content_div = div()
                    .flex_1()
                    .px_4()
                    .py_3()
                    .bg(theme.content_bg)
                    .border_r_1()
                    .border_color(theme.border)
                    .child(content);

                content_container = content_container.child(content_div);
            }
        }

        container = container.child(content_container);

        container
    }
}

impl Default for Accordion {
    fn default() -> Self {
        Self::new()
    }
}

impl RenderOnce for Accordion {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let global_theme = cx.theme();
        let accordion_theme = AccordionTheme::from(&global_theme);
        self.build_with_theme(&accordion_theme)
    }
}

impl IntoElement for Accordion {
    type Element = gpui::Component<Self>;

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