liora-components 0.1.10

Enterprise-style native GPUI component library for Liora 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Dropdown Button module.
//!
//! This public module implements a Liora split-capable dropdown button composed from native GPUI Button/Popover
//! behavior. It keeps the reusable component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose command menus without app-specific resources.
//!
//! ## Usage model
//!
//! Components in this module render native GPUI element trees. Stateless builder
//! values can be constructed inline, while controls with focus, selection,
//! popup, drag, or editing state should be stored as `gpui::Entity<T>` fields in
//! the parent view so state survives GPUI render passes.
//!
//! ## Design contract
//!
//! The implementation should use Liora theme tokens from `liora-core` and
//! `liora-theme`, keep accessibility-oriented keyboard/pointer behavior close to
//! the component, and avoid app-specific Gallery/Docs resources in this SDK
//! crate.

use crate::{Button, Popover, gpui_compat::element_id};
use gpui::{
    AnyElement, App, Component, Hsla, IntoElement, MouseButton, RenderOnce, SharedString, Window,
    div, prelude::*, px,
};
use liora_core::{Config, Placement, clear_popover, stable_unique_id};
use liora_icons::Icon;
use liora_icons_lucide::IconName;
use liora_theme::{ButtonSize, ButtonVariant};
use std::sync::Arc;

type DropdownButtonCallback = Arc<dyn Fn(&mut Window, &mut App) + 'static>;

/// Data model used by dropdown button item rendering.
#[derive(Clone)]
pub struct DropdownButtonItem {
    /// User-facing label rendered for this item.
    pub label: SharedString,
    /// Optional leading Lucide icon.
    pub icon: Option<IconName>,
    /// Whether the item should be inert and styled as disabled.
    pub disabled: bool,
    /// Whether the item should use danger semantics.
    pub danger: bool,
    /// Callback invoked when the item is activated.
    pub on_click: DropdownButtonCallback,
}

impl DropdownButtonItem {
    /// Creates `DropdownButtonItem` initialized from the supplied label and callback.
    pub fn new(
        label: impl Into<SharedString>,
        on_click: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        Self {
            label: label.into(),
            icon: None,
            disabled: false,
            danger: false,
            on_click: Arc::new(on_click),
        }
    }

    /// Sets the leading icon used by the item.
    pub fn icon(mut self, icon: IconName) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Toggles the disabled state and suppresses activation when enabled.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Applies the danger semantic treatment.
    pub fn danger(mut self) -> Self {
        self.danger = true;
        self
    }
}

/// Fluent native GPUI component for rendering Liora dropdown button.
pub struct DropdownButton {
    label: SharedString,
    items: Vec<DropdownButtonItem>,
    placement: Placement,
    close_on_click_outside: bool,
    close_on_escape: bool,
    id: Option<SharedString>,
    variant: ButtonVariant,
    size: ButtonSize,
    secondary: bool,
    disabled: bool,
    split: bool,
    leading_icon: Option<IconName>,
    on_click: Option<DropdownButtonCallback>,
}

impl DropdownButton {
    /// Creates `DropdownButton` initialized from the supplied label.
    pub fn new(label: impl Into<SharedString>) -> Self {
        Self {
            label: label.into(),
            items: Vec::new(),
            placement: Placement::BottomStart,
            close_on_click_outside: true,
            close_on_escape: true,
            id: None,
            variant: ButtonVariant::Default,
            size: ButtonSize::Default,
            secondary: false,
            disabled: false,
            split: false,
            leading_icon: None,
            on_click: None,
        }
    }

    /// Adds an enabled menu item.
    pub fn item(
        mut self,
        label: impl Into<SharedString>,
        on_click: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        self.items.push(DropdownButtonItem::new(label, on_click));
        self
    }

    /// Adds a preconfigured menu item.
    pub fn menu_item(mut self, item: DropdownButtonItem) -> Self {
        self.items.push(item);
        self
    }

    /// Adds a disabled menu item.
    pub fn disabled_item(mut self, label: impl Into<SharedString>) -> Self {
        self.items
            .push(DropdownButtonItem::new(label, |_, _| {}).disabled(true));
        self
    }

    /// Adds a danger menu item.
    pub fn danger_item(
        mut self,
        label: impl Into<SharedString>,
        on_click: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        self.items
            .push(DropdownButtonItem::new(label, on_click).danger());
        self
    }

    /// Assigns a stable element id used by GPUI state, hit testing, and automated interaction tests.
    pub fn id(mut self, id: impl Into<SharedString>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Selects the popup placement.
    pub fn placement(mut self, placement: Placement) -> Self {
        self.placement = placement;
        self
    }

    /// Toggles whether the popup closes when escape occurs.
    pub fn close_on_escape(mut self, close: bool) -> Self {
        self.close_on_escape = close;
        self
    }

    /// Toggles whether the popup closes when click outside occurs.
    pub fn close_on_click_outside(mut self, close: bool) -> Self {
        self.close_on_click_outside = close;
        self
    }

    /// Applies the primary semantic visual variant.
    pub fn primary(mut self) -> Self {
        self.variant = ButtonVariant::Primary;
        self
    }

    /// Applies the informational semantic visual variant.
    pub fn info(mut self) -> Self {
        self.variant = ButtonVariant::Info;
        self
    }

    /// Applies the success semantic visual variant.
    pub fn success(mut self) -> Self {
        self.variant = ButtonVariant::Success;
        self
    }

    /// Applies the warning semantic visual variant.
    pub fn warning(mut self) -> Self {
        self.variant = ButtonVariant::Warning;
        self
    }

    /// Applies the danger semantic visual variant.
    pub fn danger(mut self) -> Self {
        self.variant = ButtonVariant::Danger;
        self
    }

    /// Uses the compact size preset.
    pub fn small(mut self) -> Self {
        self.size = ButtonSize::Small;
        self
    }

    /// Uses the large size preset.
    pub fn large(mut self) -> Self {
        self.size = ButtonSize::Large;
        self
    }

    /// Toggles the disabled state and suppresses trigger/menu interaction when enabled.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Applies the secondary visual treatment.
    pub fn secondary(mut self) -> Self {
        self.secondary = true;
        self
    }

    /// Sets the leading icon rendered before the button label.
    pub fn icon_start(mut self, icon: IconName) -> Self {
        self.leading_icon = Some(icon);
        self
    }

    /// Enables split button behavior: left side invokes `on_click`, right side opens the menu.
    pub fn split(mut self, split: bool) -> Self {
        self.split = split;
        self
    }

    /// Registers the primary action used by split button mode.
    pub fn on_click(mut self, cb: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_click = Some(Arc::new(cb));
        self
    }

    fn resolved_button(&self, label: impl Into<SharedString>) -> Button {
        let mut button = Button::new(label).variant(self.variant).size(self.size);
        if self.secondary {
            button = button.secondary();
        }
        if self.disabled {
            button = button.disabled(true);
        }
        button
    }

    fn menu_trigger(&self, id: &SharedString) -> AnyElement {
        if self.split {
            split_trigger(self, id)
        } else {
            let mut button = self
                .resolved_button(self.label.clone())
                .icon_end(IconName::ChevronDown);
            if let Some(icon) = self.leading_icon {
                button = button.icon_start(icon);
            }
            if self.disabled {
                button.into_any_element()
            } else {
                button.into_any_element()
            }
        }
    }
}

impl RenderOnce for DropdownButton {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.global::<Config>().theme.clone();
        let dropdown_id = self.id.clone().unwrap_or_else(|| {
            stable_unique_id(
                format!(
                    "dropdown-button:{}:{}:{}:{:?}:{:?}",
                    self.label,
                    self.items.len(),
                    self.split,
                    self.variant,
                    self.size
                ),
                "dropdown-button",
                window,
                cx,
            )
        });
        let trigger = self.menu_trigger(&dropdown_id);
        if self.disabled {
            return trigger;
        }

        let items = self.items;
        let disabled = self.disabled;

        Popover::new(trigger)
            .id(dropdown_id.clone())
            .placement(self.placement)
            .offset(px(4.0))
            .close_on_click_outside(self.close_on_click_outside)
            .close_on_escape(self.close_on_escape)
            .content(move |_window, _cx| {
                dropdown_button_menu(dropdown_id.clone(), items.clone(), theme.clone(), disabled)
            })
            .into_any_element()
    }
}

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

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

fn split_trigger(button: &DropdownButton, id: &SharedString) -> AnyElement {
    let main_id = element_id(format!("{id}-main-action"));
    let toggle_id = element_id(format!("{id}-toggle"));
    let mut primary = button.resolved_button(button.label.clone()).id(main_id);
    if let Some(icon) = button.leading_icon {
        primary = primary.icon_start(icon);
    }
    if !button.disabled
        && let Some(on_click) = button.on_click.clone()
    {
        primary = primary.on_click(move |_, window, cx| {
            on_click(window, cx);
            cx.stop_propagation();
        });
    }

    let toggle = button
        .resolved_button("")
        .id(toggle_id)
        .icon_only(IconName::ChevronDown);

    div()
        .id(element_id(format!("{id}-split-trigger")))
        .flex()
        .flex_row()
        .items_center()
        .gap_1()
        .child(primary)
        .child(toggle)
        .into_any_element()
}

fn dropdown_button_menu(
    dropdown_id: SharedString,
    items: Vec<DropdownButtonItem>,
    theme: liora_theme::Theme,
    disabled: bool,
) -> AnyElement {
    div()
        .id(element_id(format!("{dropdown_id}-menu")))
        .cursor_default()
        .occlude()
        .flex()
        .flex_col()
        .py_1()
        .min_w(px(188.0))
        .max_h(px(240.0))
        .children(items.into_iter().enumerate().map(|(index, item)| {
            dropdown_button_item(dropdown_id.clone(), index, item, theme.clone(), disabled)
        }))
        .into_any_element()
}

fn dropdown_button_item(
    dropdown_id: SharedString,
    index: usize,
    item: DropdownButtonItem,
    theme: liora_theme::Theme,
    menu_disabled: bool,
) -> AnyElement {
    let disabled = menu_disabled || item.disabled;
    let item_id = element_id(format!("{dropdown_id}-item-{index}"));
    let label = item.label;
    let on_click = item.on_click;
    let icon = item.icon;
    let text_color = item_text_color(&theme, disabled, item.danger);
    let hover_color = if item.danger {
        theme.danger.base
    } else {
        theme.primary.base
    };

    div()
        .id(item_id)
        .flex()
        .items_center()
        .gap_2()
        .min_h(px(34.0))
        .px_3()
        .py_2()
        .text_size(px(theme.font_size.md))
        .text_color(text_color)
        .when(disabled, |s| s.cursor_not_allowed())
        .when(!disabled, |s| {
            s.cursor_pointer()
                .hover(move |s| s.bg(theme.neutral.hover).text_color(hover_color))
        })
        .when_some(icon, |s, icon| {
            s.child(Icon::new(icon).size(px(14.0)).color(text_color))
        })
        .child(div().text_sm().child(label))
        .when(!disabled, |s| {
            s.on_mouse_down(MouseButton::Left, move |_, window, cx| {
                on_click(window, cx);
                clear_popover(&dropdown_id, cx);
                cx.stop_propagation();
            })
        })
        .into_any_element()
}

fn item_text_color(theme: &liora_theme::Theme, disabled: bool, danger: bool) -> Hsla {
    if disabled {
        theme.neutral.text_disabled
    } else if danger {
        theme.danger.base
    } else {
        theme.neutral.text_1
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dropdown_button_builders_track_state() {
        let button = DropdownButton::new("Deploy")
            .id("deploy-menu")
            .primary()
            .large()
            .secondary()
            .split(true)
            .icon_start(IconName::Rocket)
            .on_click(|_, _| {})
            .placement(Placement::TopEnd)
            .close_on_click_outside(false)
            .close_on_escape(false)
            .item("Preview", |_, _| {})
            .menu_item(DropdownButtonItem::new("Rollback", |_, _| {}).icon(IconName::Undo2))
            .disabled_item("Locked")
            .danger_item("Delete", |_, _| {});

        assert_eq!(button.id.as_ref().map(AsRef::as_ref), Some("deploy-menu"));
        assert_eq!(button.variant, ButtonVariant::Primary);
        assert_eq!(button.size, ButtonSize::Large);
        assert!(button.secondary);
        assert!(button.split);
        assert_eq!(button.leading_icon, Some(IconName::Rocket));
        assert_eq!(button.placement, Placement::TopEnd);
        assert!(!button.close_on_click_outside);
        assert!(!button.close_on_escape);
        assert_eq!(button.items.len(), 4);
        assert!(button.items[1].icon.is_some());
        assert!(button.items[2].disabled);
        assert!(button.items[3].danger);
    }

    #[test]
    fn dropdown_button_reuses_popover_and_exposes_split_trigger() {
        let source = include_str!("dropdown_button.rs")
            .split("#[cfg(test)]")
            .next()
            .unwrap();

        assert!(source.contains("Popover::new(trigger)"));
        assert!(source.contains("fn split_trigger"));
        assert!(source.contains("IconName::ChevronDown"));
        assert!(source.contains("clear_popover(&dropdown_id, cx)"));
        assert!(source.contains("cx.stop_propagation();"));
    }
}