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
//! Collapsible component - Expandable/collapsible section with trigger and content.
use gpui::{prelude::FluentBuilder as _, *};
use std::rc::Rc;
use crate::capabilities::foundation::theme::use_theme;
#[derive(IntoElement)]
pub struct Collapsible {
id: Option<ElementId>,
trigger: Option<AnyElement>,
content: Option<AnyElement>,
is_open: bool,
disabled: bool,
show_icon: bool,
on_toggle: Option<Rc<dyn Fn(bool, &mut Window, &mut App)>>,
style: StyleRefinement,
}
impl Collapsible {
pub fn new() -> Self {
Self {
id: None,
trigger: None,
content: None,
is_open: false,
disabled: false,
show_icon: true,
on_toggle: None,
style: StyleRefinement::default(),
}
}
/// Set the trigger element id. Required to enable keyboard toggling
/// (Enter/Space) and focus support.
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
pub fn trigger(mut self, trigger: impl IntoElement) -> Self {
self.trigger = Some(trigger.into_any_element());
self
}
pub fn content(mut self, content: impl IntoElement) -> Self {
self.content = Some(content.into_any_element());
self
}
pub fn open(mut self, open: bool) -> Self {
self.is_open = open;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn show_icon(mut self, show: bool) -> Self {
self.show_icon = show;
self
}
pub fn on_toggle<F>(mut self, handler: F) -> Self
where
F: Fn(bool, &mut Window, &mut App) + 'static,
{
self.on_toggle = Some(Rc::new(handler));
self
}
}
impl Default for Collapsible {
fn default() -> Self {
Self::new()
}
}
impl Styled for Collapsible {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl RenderOnce for Collapsible {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = use_theme(cx);
let user_style = self.style;
let Collapsible {
id,
trigger,
content,
is_open,
disabled,
show_icon,
on_toggle,
style: _,
} = self;
let focus_handle = id.as_ref().map(|id| {
window
.use_keyed_state(id.clone(), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone()
});
div()
.flex()
.flex_col()
.w_full()
.when_some(trigger, |this: Div, trigger| {
let row = div()
.flex()
.items_center()
.gap(px(8.0))
.cursor(if disabled {
CursorStyle::Arrow
} else {
CursorStyle::PointingHand
})
.when(!disabled, |this: Div| {
this.hover(|style| style.bg(theme.tokens.muted.opacity(0.5)))
})
.when(disabled, |this: Div| this.opacity(0.5))
.when(show_icon, |this: Div| {
this.child(
div()
.flex()
.items_center()
.justify_center()
.text_size(px(16.0))
.text_color(theme.tokens.muted_foreground)
.when(is_open, |this: Div| this.child("▼"))
.when(!is_open, |this: Div| this.child("▶")),
)
})
.child(div().flex_1().child(trigger));
if let (Some(id), Some(focus_handle)) = (id.clone(), focus_handle.clone()) {
this.child(
row.id(id)
.role(Role::Button)
.aria_expanded(is_open)
.aria_disabled(disabled)
.when(!disabled, |this| {
this.track_focus(&focus_handle.clone().tab_index(0).tab_stop(true))
})
.when(!disabled, {
let on_toggle = on_toggle.clone();
move |this| {
this.when_some(on_toggle, |this, handler| {
this.on_click({
let handler = handler.clone();
move |_, window, cx| handler(!is_open, window, cx)
})
.on_key_down(
move |event: &KeyDownEvent, window, cx| {
if event.keystroke.key == "space"
|| event.keystroke.key == "enter"
{
(handler)(!is_open, window, cx);
cx.stop_propagation();
}
},
)
})
}
}),
)
} else {
this.child(row.when(!disabled, {
let on_toggle = on_toggle.clone();
move |this| {
this.when_some(on_toggle, |this, handler| {
this.on_mouse_down(MouseButton::Left, move |_, window, cx| {
(handler)(!is_open, window, cx)
})
})
}
}))
}
})
.when(is_open, |this: Div| {
this.when_some(content, |this: Div, content| {
this.child(div().overflow_hidden().child(content))
})
})
.map(|this| {
let mut div = this;
div.style().refine(&user_style);
div
})
}
}