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
//! Dropdown module.
//!
//! This public module implements the Liora dropdown menu component backed by Liora popover behavior. It keeps the reusable
//! component logic inside `liora-components` rather than Gallery or Docs so
//! downstream GPUI applications can compose the same behavior with their own
//! app state, assets, and release policy.
//!
//! ## 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::Popover;
use crate::gpui_compat::element_id;
use gpui::{
AnyElement, App, Component, IntoElement, RenderOnce, SharedString, Window, div, prelude::*, px,
};
use liora_core::{Config, Placement, clear_popover, stable_unique_id};
use std::sync::Arc;
/// Data model used by dropdown item rendering.
pub struct DropdownItem {
/// User-facing label rendered for this item.
pub label: SharedString,
/// Callback invoked when the component is activated by pointer or keyboard input.
pub on_click: Arc<dyn Fn(&mut Window, &mut App) + 'static>,
}
/// Fluent native GPUI component for rendering Liora dropdown.
pub struct Dropdown {
trigger: AnyElement,
items: Vec<DropdownItem>,
placement: Placement,
close_on_click_outside: bool,
close_on_escape: bool,
id: Option<SharedString>,
}
impl Dropdown {
/// Creates `Dropdown` initialized from the supplied trigger.
pub fn new(trigger: impl IntoElement) -> Self {
Self {
trigger: trigger.into_any_element(),
items: vec![],
placement: Placement::BottomStart,
close_on_click_outside: true,
close_on_escape: true,
id: None,
}
}
/// Performs the item operation used by this component.
pub fn item(
mut self,
label: impl Into<SharedString>,
on_click: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
self.items.push(DropdownItem {
label: label.into(),
on_click: Arc::new(on_click),
});
self
}
/// Selects the popup, label, or overlay placement.
pub fn placement(mut self, p: Placement) -> Self {
self.placement = p;
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
}
/// 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
}
}
impl RenderOnce for Dropdown {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.global::<Config>().theme.clone();
let items = self.items;
let close_on_escape = self.close_on_escape;
let dropdown_id = self.id.clone().unwrap_or_else(|| {
stable_unique_id(format!("dropdown:{}", items.len()), "dropdown", _window, cx)
});
let close_on_click_outside = self.close_on_click_outside;
Popover::new(self.trigger)
.id(dropdown_id.clone())
.placement(self.placement)
.offset(px(4.0))
.close_on_click_outside(close_on_click_outside)
.close_on_escape(close_on_escape)
.content(move |_window, _cx| {
let theme = theme.clone();
div()
.id(element_id(format!("{}-menu", dropdown_id)))
.cursor_default()
.occlude()
.flex()
.flex_col()
.py_1()
.min_w(px(168.0))
.max_h(px(200.0))
.children(items.iter().enumerate().map(|(i, item)| {
let on_click = item.on_click.clone();
let label = item.label.clone();
let dropdown_id = dropdown_id.clone();
let item_id = format!("{}-item-{}", dropdown_id, i);
div()
.id(element_id(item_id))
.cursor_pointer()
.flex()
.items_center()
.min_h(px(34.0))
.px_3()
.py_2()
.text_size(px(theme.font_size.md))
.text_color(theme.neutral.text_1)
.hover(|s| {
s.cursor_pointer()
.bg(theme.neutral.hover)
.text_color(theme.primary.base)
})
.on_click(move |_, window, cx| {
on_click(window, cx);
clear_popover(&dropdown_id, cx);
})
.child(div().text_sm().child(label))
}))
})
}
}
impl IntoElement for Dropdown {
type Element = Component<Self>;
fn into_element(self) -> Self::Element {
Component::new(self)
}
}
#[cfg(test)]
mod tests {
#[test]
fn dropdown_inherits_popover_motion_shell() {
let source = include_str!("dropdown.rs")
.split("#[cfg(test)]")
.next()
.unwrap();
assert!(source.contains("Popover::new(self.trigger)"));
assert!(source.contains(".content(move |_window, _cx|"));
}
}