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
use gpui::{AnyElement, FontWeight, IntoElement, ParentElement, Styled};
use std::rc::Rc;
use crate::prelude::*;
/// A bordered, `semantic::elevated_surface`-backed section with a fieldset
/// area (arbitrary child fields, e.g. `FormField`s) followed by a `border-t`
/// footer with right-aligned Save/Cancel `Button`s.
#[derive(IntoElement, RegisterComponent)]
pub struct ActionPanel {
title: Option<SharedString>,
description: Option<SharedString>,
fields: Vec<AnyElement>,
save_label: SharedString,
cancel_label: SharedString,
on_save: Option<Rc<dyn Fn(&mut Window, &mut App) + 'static>>,
on_cancel: Option<Rc<dyn Fn(&mut Window, &mut App) + 'static>>,
}
impl ActionPanel {
pub fn new() -> Self {
Self {
title: None,
description: None,
fields: Vec::new(),
save_label: "Save".into(),
cancel_label: "Cancel".into(),
on_save: None,
on_cancel: None,
}
}
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into());
self
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
/// Adds a field element to the fieldset area (e.g. a `FormField`).
pub fn field(mut self, field: impl IntoElement) -> Self {
self.fields.push(field.into_any_element());
self
}
pub fn save_label(mut self, label: impl Into<SharedString>) -> Self {
self.save_label = label.into();
self
}
pub fn cancel_label(mut self, label: impl Into<SharedString>) -> Self {
self.cancel_label = label.into();
self
}
pub fn on_save(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_save = Some(Rc::new(handler));
self
}
pub fn on_cancel(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_cancel = Some(Rc::new(handler));
self
}
}
impl Default for ActionPanel {
fn default() -> Self {
Self::new()
}
}
impl RenderOnce for ActionPanel {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let title = self.title;
let description = self.description;
let fields = self.fields;
let save_label = self.save_label;
let cancel_label = self.cancel_label;
let on_save = self.on_save;
let on_cancel = self.on_cancel;
let mut fieldset = v_flex().gap_4().p_4();
for field in fields {
fieldset = fieldset.child(field);
}
v_flex()
.w_full()
.rounded_lg()
.border_1()
.border_color(semantic::border(cx))
.bg(semantic::elevated_surface(cx))
.overflow_hidden()
.when(title.is_some() || description.is_some(), |this| {
this.child(
v_flex()
.gap_1()
.px_4()
.pt_4()
.when_some(title, |this, title| {
this.child(
Label::new(title)
.size(LabelSize::Default)
.weight(FontWeight::MEDIUM),
)
})
.when_some(description, |this, description| {
this.child(
Label::new(description)
.size(LabelSize::Small)
.color(Color::Muted),
)
}),
)
})
.child(fieldset)
.child(
h_flex()
.justify_end()
.gap_2()
.px_4()
.py_3()
.border_t_1()
.border_color(semantic::border(cx))
.when_some(on_cancel, |this, handler| {
this.child(
// Wrapping `div` only exists so integration tests can
// locate the Cancel button's real rendered pixel bounds
// via `VisualTestContext::debug_bounds` (test-only,
// no-op in release builds — mirrors the `Tab`/
// `ContextMenu` `debug_selector` precedent) and drive a
// genuine `simulate_click`, instead of invoking
// `on_cancel` directly. It does not intercept the click:
// the inner `Button` still owns the only click handler.
div()
.id("action-panel-cancel-wrap")
.debug_selector(|| "ACTION_PANEL-cancel".into())
.child(
Button::new("action-panel-cancel", cancel_label)
.on_click(move |_, window, cx| handler(window, cx)),
),
)
})
.when_some(on_save, |this, handler| {
this.child(
// See the Cancel wrapper's comment above; same rationale.
div()
.id("action-panel-save-wrap")
.debug_selector(|| "ACTION_PANEL-save".into())
.child(
Button::new("action-panel-save", save_label)
.primary()
.on_click(move |_, window, cx| handler(window, cx)),
),
)
}),
)
}
}
impl Component for ActionPanel {
fn scope() -> ComponentScope {
ComponentScope::Input
}
fn description() -> Option<&'static str> {
Some("A fieldset section with a footer of right-aligned Save/Cancel buttons.")
}
fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
Some(
ActionPanel::new()
.title("Profile")
.description("Update your personal details.")
.field(
FormField::new(Label::new("Ada Lovelace").color(Color::Default)).label("Name"),
)
.field(
FormField::new(Label::new("ada@example.com").color(Color::Default))
.label("Email"),
)
.on_save(|_, _| {})
.on_cancel(|_, _| {})
.into_any_element(),
)
}
}