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
//! Accordion component for displaying multiple collapsible sections.
//!
//! # Example
//!
//! ```
//! # use gpui::{Context, IntoElement, Render, Window, prelude::*};
//! use gpuikit::elements::accordion::{AccordionState, accordion, accordion_item};
//! # struct D;
//! # impl Render for D { fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
//! // Create an accordion with multiple items
//! let accordion_state = cx.new(|_cx| {
//! AccordionState::new(
//! accordion("my-accordion")
//! .item(accordion_item("section-1", "Section 1").content("Content for section 1"))
//! .item(accordion_item("section-2", "Section 2").content("Content for section 2"))
//! .item(accordion_item("section-3", "Section 3").content("Content for section 3"))
//! )
//! });
//!
//! // For single mode (only one item open at a time):
//! let _ = accordion("my-accordion").single();
//!
//! // For multiple mode (default, multiple items can be open):
//! let _ = accordion("my-accordion").multiple();
//! # accordion_state
//! # }}
//! # let mut tcx = gpui::TestAppContext::single();
//! # tcx.update(gpuikit::init);
//! # let _ = tcx.add_window_view(|_, _| D);
//! ```
use crate::icons::Icons;
use crate::theme::{ActiveTheme, Themeable};
use gpui::{
Context, ElementId, EventEmitter, IntoElement, ParentElement, Render, SharedString, Styled,
Window, div, prelude::*, px, rems,
};
use std::collections::HashSet;
/// Event emitted when accordion items are expanded or collapsed.
pub struct AccordionChanged {
/// The ID of the item that was toggled.
pub item_id: ElementId,
/// Whether the item is now expanded.
pub expanded: bool,
}
/// Mode for accordion behavior.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AccordionMode {
/// Only one item can be open at a time.
Single,
/// Multiple items can be open simultaneously.
#[default]
Multiple,
}
/// A single item within an accordion.
pub struct AccordionItem {
id: ElementId,
header: SharedString,
content: Option<SharedString>,
disabled: bool,
}
impl AccordionItem {
/// Create a new accordion item with a header.
pub fn new(id: impl Into<ElementId>, header: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
header: header.into(),
content: None,
disabled: false,
}
}
/// Set the content of the accordion item.
pub fn content(mut self, content: impl Into<SharedString>) -> Self {
self.content = Some(content.into());
self
}
/// Set whether this item is disabled.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
/// Creates a new accordion item.
pub fn accordion_item(id: impl Into<ElementId>, header: impl Into<SharedString>) -> AccordionItem {
AccordionItem::new(id, header)
}
/// Builder for creating an accordion component.
pub struct Accordion {
id: ElementId,
items: Vec<AccordionItem>,
mode: AccordionMode,
default_expanded: HashSet<ElementId>,
}
impl Accordion {
/// Create a new accordion.
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
items: Vec::new(),
mode: AccordionMode::Multiple,
default_expanded: HashSet::new(),
}
}
/// Add an item to the accordion.
pub fn item(mut self, item: AccordionItem) -> Self {
self.items.push(item);
self
}
/// Set the accordion to single mode (only one item open at a time).
pub fn single(mut self) -> Self {
self.mode = AccordionMode::Single;
self
}
/// Set the accordion to multiple mode (multiple items can be open).
pub fn multiple(mut self) -> Self {
self.mode = AccordionMode::Multiple;
self
}
/// Set the mode of the accordion.
pub fn mode(mut self, mode: AccordionMode) -> Self {
self.mode = mode;
self
}
/// Set an item to be expanded by default.
pub fn default_expanded(mut self, item_id: impl Into<ElementId>) -> Self {
self.default_expanded.insert(item_id.into());
self
}
}
/// Creates a new accordion builder.
pub fn accordion(id: impl Into<ElementId>) -> Accordion {
Accordion::new(id)
}
/// Stateful accordion component that manages expanded/collapsed state.
pub struct AccordionState {
id: ElementId,
items: Vec<AccordionItem>,
mode: AccordionMode,
expanded: HashSet<ElementId>,
}
impl EventEmitter<AccordionChanged> for AccordionState {}
impl AccordionState {
/// Create a new accordion state from an accordion builder.
pub fn new(accordion: Accordion) -> Self {
Self {
id: accordion.id,
items: accordion.items,
mode: accordion.mode,
expanded: accordion.default_expanded,
}
}
/// Check if an item is expanded.
pub fn is_expanded(&self, item_id: &ElementId) -> bool {
self.expanded.contains(item_id)
}
/// Toggle an item's expanded state.
pub fn toggle(&mut self, item_id: ElementId, cx: &mut Context<Self>) {
// Find the item and check if it's disabled
let item = self.items.iter().find(|i| i.id == item_id);
if let Some(item) = item {
if item.disabled {
return;
}
}
let was_expanded = self.expanded.contains(&item_id);
if was_expanded {
self.expanded.remove(&item_id);
} else {
if self.mode == AccordionMode::Single {
self.expanded.clear();
}
self.expanded.insert(item_id.clone());
}
cx.emit(AccordionChanged {
item_id,
expanded: !was_expanded,
});
cx.notify();
}
/// Expand an item.
pub fn expand(&mut self, item_id: ElementId, cx: &mut Context<Self>) {
if !self.expanded.contains(&item_id) {
if self.mode == AccordionMode::Single {
self.expanded.clear();
}
self.expanded.insert(item_id.clone());
cx.emit(AccordionChanged {
item_id,
expanded: true,
});
cx.notify();
}
}
/// Collapse an item.
pub fn collapse(&mut self, item_id: ElementId, cx: &mut Context<Self>) {
if self.expanded.remove(&item_id) {
cx.emit(AccordionChanged {
item_id,
expanded: false,
});
cx.notify();
}
}
/// Collapse all items.
pub fn collapse_all(&mut self, cx: &mut Context<Self>) {
self.expanded.clear();
cx.notify();
}
/// Expand all items (only works in multiple mode).
pub fn expand_all(&mut self, cx: &mut Context<Self>) {
if self.mode == AccordionMode::Multiple {
for item in &self.items {
if !item.disabled {
self.expanded.insert(item.id.clone());
}
}
cx.notify();
}
}
}
impl Render for AccordionState {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
div()
.id(self.id.clone())
.flex()
.flex_col()
.w_full()
.border_1()
.border_color(theme.border())
.rounded(rems(0.5))
.overflow_hidden()
.children(self.items.iter().enumerate().map(|(index, item)| {
let is_expanded = self.expanded.contains(&item.id);
let is_first = index == 0;
let is_last = index == self.items.len() - 1;
let item_id = item.id.clone();
let header = item.header.clone();
let content = item.content.clone();
let disabled = item.disabled;
let theme = cx.theme();
div()
.flex()
.flex_col()
.when(!is_first, |this| {
this.border_t_1().border_color(theme.border_subtle())
})
.child(
// Header
div()
.id(item_id.clone())
.flex()
.items_center()
.justify_between()
.px(rems(0.75))
.py(rems(0.5))
.bg(theme.surface())
.when(!disabled, |this| {
this.cursor_pointer()
.hover(|style| style.bg(theme.surface_secondary()))
.on_click(cx.listener(move |this, _, _window, cx| {
this.toggle(item_id.clone(), cx);
}))
})
.when(disabled, |this| this.cursor_not_allowed().opacity(0.5))
.child(
div()
.text_sm()
.font_weight(gpui::FontWeight::MEDIUM)
.text_color(if disabled {
theme.fg_disabled()
} else {
theme.fg()
})
.child(header),
)
.child(
div().flex().items_center().justify_center().child(
if is_expanded {
Icons::chevron_down()
} else {
Icons::chevron_right()
}
.size(px(14.))
.text_color(theme.fg_muted()),
),
),
)
.when(is_expanded, |this| {
this.child(
// Content
div()
.px(rems(0.75))
.py(rems(0.5))
.bg(theme.surface())
.border_t_1()
.border_color(theme.border_subtle())
.when(!is_last, |this| this.border_b_0())
.child(
div()
.text_sm()
.text_color(theme.fg_muted())
.when_some(content, |this, content| this.child(content)),
),
)
})
}))
}
}