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
//! Popover: a click-opened floating surface with interactive content →
//! Carbon `Popover` + `PopoverContent` (`.cds--popover`, `.cu-popover-content`).
//!
//! - **Behavior**: pressing the trigger (pointer, Enter or Space) toggles it; a
//! press outside or Escape closes it; focus moves into the surface on open
//! and back to where it was on close. Focus leaving the surface and its
//! trigger closes it, and Tab runs in the reference's DOM order, content
//! right after the trigger ([`super::overlay::TabFlow`]). Positioning is Base's `Popup` via
//! [`super::floating`]. Base's own `Popover` is not used: it draws no caret
//! and cannot place Carbon's 12 alignments.
//! - **Motion**: none; it shows and hides at once (see [`super::floating`]).
//! - **Appearance**: `layer-01` surface, 2px radius
//! (`--popover-border-radius: 2px`), Carbon's popover drop shadow
//! (`0 0.125rem 0.125rem rgba(0,0,0,.2)`) when `drop_shadow`,
//! a 12×6 caret and a 0.625rem offset when `caret`, content padded 1rem,
//! 12–22rem wide, body-01.
//!
//! ```ignore
//! Popover::new("dims", Button::new("t", "Dimensions").variant(ButtonVariant::Outline))
//! .align(Align::BottomLeft)
//! .child(div().child("Set the layer size."))
//! ```
use super::floating::{self, Align, POPOVER_OFFSET};
use super::overlay::{self, OpenChange, rem};
use crate::{
theme::color,
tokens::{ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, ElementId, Hsla, InteractiveElement, IntoElement, KeyDownEvent, MouseButton,
ParentElement, RenderOnce, Styled, Window, div, prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
/// The popover's colors.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PopoverLook {
/// `--popover-background-color: var(--layer)` (layer-01 on the page).
pub background: Hsla,
/// `--popover-text-color: var(--text-primary)`.
pub text: Hsla,
}
impl PopoverLook {
/// The look on the active theme.
pub fn current() -> Self {
Self {
background: color(ColorRole::Layer01),
text: color(ColorRole::TextPrimary),
}
}
}
/// `.cu-popover-content { min-width: 12rem; max-width: 22rem; padding: spacing-05 }`.
pub const CONTENT_MIN_WIDTH: f32 = 192.;
/// See [`CONTENT_MIN_WIDTH`].
pub const CONTENT_MAX_WIDTH: f32 = 352.;
/// `--popover-border-radius: 2px`.
pub const RADIUS: f32 = 2.;
/// A Carbon popover. See the module docs.
#[derive(IntoElement)]
pub struct Popover {
id: ElementId,
trigger: AnyElement,
children: Vec<AnyElement>,
align: Align,
open: Option<bool>,
default_open: bool,
on_open_change: Option<OpenChange>,
caret: bool,
drop_shadow: bool,
}
impl Popover {
/// A popover with a stable `id`, opened by `trigger`.
pub fn new(id: impl Into<ElementId>, trigger: impl IntoElement) -> Self {
Self {
id: id.into(),
trigger: trigger.into_any_element(),
children: Vec::new(),
align: Align::Bottom,
open: None,
default_open: false,
on_open_change: None,
caret: true,
drop_shadow: true,
}
}
/// One of Carbon's 12 alignments (default `bottom`).
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
/// Controlled open state.
pub fn open(mut self, open: bool) -> Self {
self.open = Some(open);
self
}
/// Initial open state when uncontrolled.
pub fn default_open(mut self, open: bool) -> Self {
self.default_open = open;
self
}
/// Called whenever the popover asks to open or close.
pub fn on_open_change(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_open_change = Some(Rc::new(f));
self
}
/// Draw the caret (default true).
pub fn caret(mut self, caret: bool) -> Self {
self.caret = caret;
self
}
/// Draw the floating shadow (default true).
pub fn drop_shadow(mut self, shadow: bool) -> Self {
self.drop_shadow = shadow;
self
}
}
impl ParentElement for Popover {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Popover {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let openness = overlay::openness(
&id,
self.open,
self.default_open,
self.on_open_change.clone(),
window,
cx,
);
let open = openness.open;
let set_open = openness.setter();
let focus = overlay::surface_focus(&id, open, None, window, cx);
// Carbon `Popover` closes on `focusout` to outside it and its
// trigger; Tab runs in its DOM order (content right after the
// trigger).
let flow = overlay::TabFlow::new(&id, open, &focus, &set_open, window, cx);
let trigger = {
let toggle = set_open.clone();
let wrapper = overlay::trigger_wrapper(
(id.clone(), "trigger"),
self.trigger,
Rc::new(move |window, cx| toggle(!open, window, cx)),
);
flow.trigger(wrapper, open, &focus, &set_open)
.into_any_element()
};
let look = PopoverLook::current();
let surface = open.then(|| {
let set_escape = set_open.clone();
let set_outside = set_open.clone();
let surface = div().id((id.clone(), "surface"));
flow.surface(surface, &focus, &set_open)
.track_focus(&focus)
.tab_group()
.rounded(px(RADIUS))
.bg(look.background)
.text_color(look.text)
.when(self.drop_shadow, |this| {
this.shadow(overlay::popover_drop_shadow())
})
.min_w(rem(CONTENT_MIN_WIDTH))
.max_w(rem(CONTENT_MAX_WIDTH))
.p(rem(16.))
.type_style(TypeStyle::Body01)
.on_key_down(move |event: &KeyDownEvent, window, cx| {
if event.keystroke.key == "escape" && open {
set_escape(false, window, cx);
cx.stop_propagation();
}
})
.on_mouse_down_out(move |_, window, cx| {
if open {
set_outside(false, window, cx);
}
})
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.children(self.children)
});
floating::anchored(
(id, "popup"),
trigger,
surface,
self.align,
self.caret.then_some(look.background),
POPOVER_OFFSET,
window,
cx,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
#[test]
fn look_and_geometry_follow_popover_css() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(hex(PopoverLook::current().background), "#f4f4f4");
crate::theme::tests_set(CarbonTheme::G90);
assert_eq!(hex(PopoverLook::current().background), "#393939");
assert_eq!(
(CONTENT_MIN_WIDTH, CONTENT_MAX_WIDTH, RADIUS),
(192., 352., 2.)
);
let p = Popover::new("p", div());
assert_eq!(
(p.align, p.caret, p.drop_shadow),
(Align::Bottom, true, true)
);
}
}