use std::rc::Rc;
use std::time::Instant;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, Bounds, BoxShadow, ClickEvent, ContentMask, Corner, Div, Element, ElementId,
FontWeight, GlobalElementId, Hsla, InspectorElementId, InteractiveElement, IntoElement,
LayoutId, Length, ParentElement, Pixels, RenderOnce, SharedString,
StatefulInteractiveElement, Styled, Window, anchored, deferred, div, point, px, relative,
};
use super::{ClickHandler, Icon, Radio, RoundedExt, all, child_id, corners};
use crate::interaction::{interactive, state_layer, use_interaction};
use crate::motion::{
MotionSettings, Spring, animate_color, animate_f32, elapsed, element_state, entrance, key,
replay, request_frame,
};
use crate::shapes::Tone;
use crate::theme::{MONO_FONT_FAMILY, Scheme, Theme, alpha, font_family, mix};
pub fn shadow_pop(s: &Scheme) -> Vec<BoxShadow> {
vec![
BoxShadow {
color: s.shadow,
offset: point(px(0.), px(2.)),
blur_radius: px(6.),
spread_radius: px(0.),
},
BoxShadow {
color: alpha(s.shadow, 0.85),
offset: point(px(0.), px(10.)),
blur_radius: px(28.),
spread_radius: px(0.),
},
]
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum PopDirection {
#[default]
Up,
Down,
}
pub fn pop_in(el: Div, id: &ElementId, delay: f32, window: &mut Window, cx: &mut App) -> Div {
pop_in_toward(el, id, delay, PopDirection::Up, window, cx)
}
pub fn pop_in_toward(
el: Div,
id: &ElementId,
delay: f32,
direction: PopDirection,
window: &mut Window,
cx: &mut App,
) -> Div {
let p = entrance(key(id, "pop"), Spring::SPATIAL_FAST, delay, window, cx);
let sign = match direction {
PopDirection::Up => 1.,
PopDirection::Down => -1.,
};
el.relative()
.top(px((1. - p) * 12. * sign))
.opacity(p.clamp(0., 1.))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RevealOrigin {
Edge(PopDirection),
Corner(Corner),
}
impl From<PopDirection> for RevealOrigin {
fn from(direction: PopDirection) -> Self {
RevealOrigin::Edge(direction)
}
}
impl From<Corner> for RevealOrigin {
fn from(corner: Corner) -> Self {
RevealOrigin::Corner(corner)
}
}
const REVEAL_SHADOW: f32 = 40.;
const REVEAL_WIDTH_FROM: f32 = 0.5;
fn reveal_mask(bounds: Bounds<Pixels>, origin: RevealOrigin, progress: f32) -> Bounds<Pixels> {
let p = progress.clamp(0., 1.);
let m = REVEAL_SHADOW;
let (left, top) = (f32::from(bounds.left()), f32::from(bounds.top()));
let (right, bottom) = (f32::from(bounds.right()), f32::from(bounds.bottom()));
let (w, h) = (right - left, bottom - top);
let (from_top, from_left) = match origin {
RevealOrigin::Edge(direction) => (direction == PopDirection::Down, None),
RevealOrigin::Corner(corner) => (
matches!(corner, Corner::TopLeft | Corner::TopRight),
Some(matches!(corner, Corner::TopLeft | Corner::BottomLeft)),
),
};
let reach_y = p * (h + m);
let (y0, y1) = if from_top {
(top - m, top + reach_y)
} else {
(bottom - reach_y, bottom + m)
};
let reach_x = (REVEAL_WIDTH_FROM + (1. - REVEAL_WIDTH_FROM) * p) * (w + m);
let (x0, x1) = match from_left {
None => (left - m, right + m),
Some(true) => (left - m, left + reach_x),
Some(false) => (right - reach_x, right + m),
};
Bounds::from_corners(point(px(x0), px(y0)), point(px(x1), px(y1.max(y0))))
}
struct RevealState {
start: Instant,
opened: bool,
}
#[track_caller]
pub fn reveal<E: Styled + IntoElement>(
el: E,
id: &ElementId,
origin: impl Into<RevealOrigin>,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let (size, fade) = reveal_progress(id, window, cx);
reveal_with(el, origin.into(), size, fade)
}
pub fn reveal_at<E: Styled + IntoElement>(
el: E,
origin: impl Into<RevealOrigin>,
progress: f32,
) -> AnyElement {
let progress = progress.clamp(0., 1.);
reveal_with(el, origin.into(), progress, progress)
}
fn reveal_with<E: Styled + IntoElement>(
el: E,
origin: RevealOrigin,
size: f32,
fade: f32,
) -> AnyElement {
if size >= 1. && fade >= 1. {
return el.into_any_element();
}
RevealMask {
origin,
progress: size,
child: Some(el.opacity(fade.clamp(0., 1.)).into_any_element()),
}
.into_any_element()
}
#[track_caller]
fn reveal_progress(id: &ElementId, window: &mut Window, cx: &mut App) -> (f32, f32) {
if MotionSettings::get(cx).reduced {
return (1., 1.);
}
let (t, opened) = element_state(key(id, "reveal"), window, |state: Option<RevealState>, _| {
let mut s = state.unwrap_or(RevealState { start: Instant::now(), opened: false });
let t = elapsed(s.start, cx);
s.opened |= Spring::SPATIAL_FAST.step(t) >= 1.;
((t, s.opened), s)
});
let size = if opened { 1. } else { Spring::SPATIAL_FAST.step(t) };
let faded = t >= Spring::EFFECTS.settle_time().as_secs_f32();
let fade = if faded { 1. } else { Spring::EFFECTS.step(t) };
if !(opened && faded) {
request_frame(window);
}
(size, fade)
}
struct RevealMask {
origin: RevealOrigin,
progress: f32,
child: Option<AnyElement>,
}
impl IntoElement for RevealMask {
type Element = Self;
fn into_element(self) -> Self {
self
}
}
impl Element for RevealMask {
type RequestLayoutState = AnyElement;
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, AnyElement) {
let mut child = self.child.take().expect("laid out once");
let id = child.request_layout(window, cx);
(id, child)
}
fn prepaint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
bounds: Bounds<Pixels>,
child: &mut AnyElement,
window: &mut Window,
cx: &mut App,
) {
let mask = ContentMask { bounds: reveal_mask(bounds, self.origin, self.progress) };
window.with_content_mask(Some(mask), |window| child.prepaint(window, cx));
}
fn paint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
bounds: Bounds<Pixels>,
child: &mut AnyElement,
_: &mut (),
window: &mut Window,
cx: &mut App,
) {
let mask = ContentMask { bounds: reveal_mask(bounds, self.origin, self.progress) };
window.with_content_mask(Some(mask), |window| child.paint(window, cx));
}
}
fn content_switch(
el: Div,
id: &ElementId,
generation: u64,
window: &mut Window,
cx: &mut App,
) -> Div {
match replay(key(id, "switch"), generation, 0.25, window, cx) {
Some(t) => {
let p = Spring::EFFECTS.step(t).clamp(0., 1.);
el.relative().top(px((1. - p) * 6.)).opacity(p)
}
None => el,
}
}
#[derive(IntoElement)]
pub struct Popover {
id: ElementId,
width: Option<Pixels>,
padding: Pixels,
radius: Pixels,
gap: Pixels,
max_height: Option<Pixels>,
direction: PopDirection,
anchor: Option<Corner>,
origin: Option<Corner>,
on_dismiss: Option<ClickHandler>,
children: Vec<AnyElement>,
}
impl Popover {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
width: None,
padding: px(16.),
radius: px(28.),
gap: px(12.),
max_height: None,
direction: PopDirection::Up,
anchor: None,
origin: None,
on_dismiss: None,
children: Vec::new(),
}
}
pub fn anchor(mut self, corner: Corner) -> Self {
self.anchor = Some(corner);
self.direction = match corner {
Corner::TopLeft | Corner::TopRight => PopDirection::Down,
Corner::BottomLeft | Corner::BottomRight => PopDirection::Up,
};
self
}
pub fn on_dismiss(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_dismiss = Some(Rc::new(f));
self
}
pub fn width(mut self, width: Pixels) -> Self {
self.width = Some(width);
self
}
pub fn padding(mut self, padding: Pixels) -> Self {
self.padding = padding;
self
}
pub fn radius(mut self, radius: Pixels) -> Self {
self.radius = radius;
self
}
pub fn gap(mut self, gap: Pixels) -> Self {
self.gap = gap;
self
}
pub fn max_h(mut self, max_height: Pixels) -> Self {
self.max_height = Some(max_height);
self
}
pub fn direction(mut self, direction: PopDirection) -> Self {
self.direction = direction;
self
}
pub fn origin(mut self, corner: Corner) -> Self {
self.origin = Some(corner);
self.direction = match corner {
Corner::TopLeft | Corner::TopRight => PopDirection::Down,
Corner::BottomLeft | Corner::BottomRight => PopDirection::Up,
};
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 s = *Theme::of(cx);
let content = div()
.flex()
.flex_col()
.flex_none()
.min_w_0()
.gap(self.gap)
.p(self.padding)
.children(self.children);
let surface = div()
.flex()
.flex_col()
.when_some(self.width, |d, w| d.w(w).max_w_full())
.rounded(self.radius)
.bg(s.surface_container)
.shadow(shadow_pop(&s))
.font_family(font_family())
.text_size(px(13.))
.text_color(s.on_surface);
let surface = match self.max_height {
Some(max_height) => surface.child(
div()
.id(child_id(&self.id, "scroll"))
.max_h(max_height)
.overflow_y_scroll()
.child(content),
),
None => surface.child(content),
};
let mut surface = surface.id(child_id(&self.id, "surface"));
if let Some(f) = self.on_dismiss {
surface = surface.on_mouse_down_out(move |event, w, cx| {
let click = ClickEvent::Mouse(gpui::MouseClickEvent {
down: event.clone(),
up: gpui::MouseUpEvent {
button: event.button,
position: event.position,
modifiers: event.modifiers,
click_count: event.click_count,
},
});
f(&click, w, cx)
});
}
let origin = match self.origin.or(self.anchor) {
Some(corner) => RevealOrigin::Corner(corner),
None => RevealOrigin::Edge(self.direction),
};
let surface = reveal(surface, &self.id, origin, window, cx);
match self.anchor {
Some(corner) => deferred(
anchored()
.anchor(corner)
.snap_to_window_with_margin(px(8.))
.child(surface),
)
.with_priority(1)
.into_any_element(),
None => surface.into_any_element(),
}
}
}
#[derive(IntoElement)]
pub struct Menu {
id: ElementId,
groups: Vec<Vec<AnyElement>>,
grouped: bool,
width: f32,
max_height: Option<Pixels>,
padding: Pixels,
radius: Pixels,
direction: PopDirection,
origin: Option<Corner>,
header: Option<AnyElement>,
footer: Option<AnyElement>,
back: Option<(SharedString, ClickHandler)>,
content_key: Option<u64>,
stagger: bool,
}
const ITEM_STAGGER: f32 = 0.025;
impl Menu {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
groups: vec![Vec::new()],
grouped: false,
width: 260.,
max_height: None,
padding: px(6.),
radius: px(24.),
direction: PopDirection::Up,
origin: None,
header: None,
footer: None,
back: None,
content_key: None,
stagger: true,
}
}
pub fn stagger(mut self, stagger: bool) -> Self {
self.stagger = stagger;
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = width;
self
}
pub fn padding(mut self, padding: Pixels) -> Self {
self.padding = padding;
self
}
pub fn radius(mut self, radius: Pixels) -> Self {
self.radius = radius;
self
}
pub fn direction(mut self, direction: PopDirection) -> Self {
self.direction = direction;
self
}
pub fn origin(mut self, corner: Corner) -> Self {
self.origin = Some(corner);
self.direction = match corner {
Corner::TopLeft | Corner::TopRight => PopDirection::Down,
Corner::BottomLeft | Corner::BottomRight => PopDirection::Up,
};
self
}
pub fn header(mut self, header: impl IntoElement) -> Self {
self.header = Some(header.into_any_element());
self
}
pub fn footer(mut self, footer: impl IntoElement) -> Self {
self.footer = Some(footer.into_any_element());
self
}
pub fn back(
mut self,
title: impl Into<SharedString>,
on_back: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.back = Some((title.into(), Rc::new(on_back)));
self
}
pub fn content_key(mut self, key: u64) -> Self {
self.content_key = Some(key);
self
}
pub fn max_h(mut self, max_height: Pixels) -> Self {
self.max_height = Some(max_height);
self
}
pub fn grouped(mut self, grouped: bool) -> Self {
self.grouped = grouped;
self
}
pub fn item(mut self, item: impl IntoElement) -> Self {
self.groups
.last_mut()
.unwrap()
.push(item.into_any_element());
self
}
pub fn section(self, title: impl Into<SharedString>) -> Self {
self.item(MenuSection(title.into()))
}
pub fn separator(mut self) -> Self {
self.groups.push(Vec::new());
self
}
}
#[derive(IntoElement)]
struct MenuSection(SharedString);
impl RenderOnce for MenuSection {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
div()
.pt(px(6.))
.pb(px(2.))
.px(px(12.))
.text_size(px(12.))
.font_weight(FontWeight(700.))
.text_color(Theme::of(cx).primary)
.child(self.0)
}
}
impl RenderOnce for Menu {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let origin = match self.origin {
Some(corner) => RevealOrigin::Corner(corner),
None => RevealOrigin::Edge(self.direction),
};
let base = div()
.flex()
.flex_col()
.w(px(self.width))
.max_w_full()
.font_family(font_family())
.text_size(px(13.))
.text_color(s.on_surface);
if self.grouped {
let mut col = base.gap(px(4.));
for (i, group) in self.groups.into_iter().enumerate() {
let gid = ElementId::NamedInteger(format!("{}-group", self.id).into(), i as u64);
let card = div()
.flex()
.flex_col()
.p(px(4.))
.rounded(px(18.))
.bg(s.surface_container)
.shadow(shadow_pop(&s))
.children(group);
let p = entrance(key(&gid, "fade"), Spring::EFFECTS, i as f32 * 0.035, window, cx);
col = col.child(card.opacity(p.clamp(0., 1.)));
}
let menu = match self.max_height {
Some(max_height) => div().child(
div()
.id(child_id(&self.id, "scroll"))
.max_h(max_height)
.overflow_y_scroll()
.child(col.flex_none()),
),
None => col,
};
reveal(menu, &self.id, origin, window, cx)
} else {
let has_header = self.header.is_some() || self.back.is_some();
let mut content = div().flex().flex_col().p(self.padding);
if has_header {
content = content.pt(px(2.));
}
if self.footer.is_some() {
content = content.pb(px(2.));
}
let mut n = 0usize;
for (i, group) in self.groups.into_iter().enumerate() {
if i > 0 {
content =
content.child(div().my(px(4.)).mx(px(10.)).h(px(1.)).bg(s.outline_variant));
}
if !self.stagger {
content = content.children(group);
continue;
}
for item in group {
let id = ElementId::NamedChild(
Box::new(self.id.clone()),
format!("item-{n}").into(),
);
let delay = n.min(4) as f32 * ITEM_STAGGER;
let p = entrance(id, Spring::EFFECTS, delay, window, cx);
n += 1;
content = content.child(div().opacity(p.clamp(0., 1.)).child(item));
}
}
if let Some(generation) = self.content_key {
content = content_switch(content, &self.id, generation, window, cx);
}
let mut card = base
.rounded(self.radius)
.bg(s.surface_container)
.shadow(shadow_pop(&s));
if has_header {
let mut header = div()
.flex()
.flex_col()
.flex_none()
.px(self.padding)
.pt(self.padding);
if let Some((title, on_back)) = self.back {
header = header.child(BackRow {
id: child_id(&self.id, "back"),
title,
on_back,
});
}
card = card.child(header.children(self.header));
}
let card = match self.max_height {
Some(max_height) => card.child(
div()
.id(child_id(&self.id, "scroll"))
.max_h(max_height)
.overflow_y_scroll()
.child(content.flex_none()),
),
None => card.child(content),
};
let card = match self.footer {
Some(footer) => card.child(
div()
.flex()
.flex_col()
.flex_none()
.px(self.padding)
.pb(self.padding)
.child(footer),
),
None => card,
};
reveal(card, &self.id, origin, window, cx)
}
}
}
#[derive(IntoElement)]
struct BackRow {
id: ElementId,
title: SharedString,
on_back: ClickHandler,
}
impl RenderOnce for BackRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let state = use_interaction(&self.id, window, cx);
let layer = state_layer(&self.id, &state, all(px(19.)), s.on_surface, window, cx);
let row = div()
.id(self.id.clone())
.relative()
.flex()
.items_center()
.gap(px(9.))
.min_h(px(34.))
.px(px(9.))
.mb(px(4.))
.rounded(px(19.))
.bg(s.surface_container_high)
.font_weight(FontWeight(600.))
.child(layer)
.child(
Icon::new("chevron-right")
.size(px(16.))
.color(s.on_surface_variant)
.rotate(std::f32::consts::PI),
)
.child(self.title);
let on_back = self.on_back;
interactive(row, &state, true, true).on_click(move |e, w, cx| on_back(e, w, cx))
}
}
#[derive(IntoElement)]
pub struct MenuItem {
id: ElementId,
label: SharedString,
sublabel: Option<SharedString>,
icon: Option<SharedString>,
leading: Option<AnyElement>,
trailing: Option<AnyElement>,
shortcut: Option<SharedString>,
selected: bool,
highlighted: bool,
radio: Option<bool>,
disabled: bool,
dense: bool,
static_row: bool,
inline_sublabel: bool,
danger: bool,
hover_fill: bool,
highlights: Vec<std::ops::Range<usize>>,
confirm: Option<SharedString>,
actions: Vec<AnyElement>,
line_clamp: Option<usize>,
on_click: Option<ClickHandler>,
}
impl MenuItem {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
sublabel: None,
icon: None,
leading: None,
trailing: None,
shortcut: None,
selected: false,
highlighted: false,
radio: None,
disabled: false,
dense: false,
static_row: false,
inline_sublabel: false,
danger: false,
hover_fill: true,
highlights: Vec::new(),
confirm: None,
actions: Vec::new(),
line_clamp: None,
on_click: None,
}
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
pub fn actions(mut self, actions: impl IntoIterator<Item = impl IntoElement>) -> Self {
self.actions
.extend(actions.into_iter().map(IntoElement::into_any_element));
self
}
pub fn line_clamp(mut self, lines: usize) -> Self {
self.line_clamp = Some(lines.max(1));
self
}
pub fn hover_fill(mut self, fill: bool) -> Self {
self.hover_fill = fill;
self
}
pub fn label_highlights(mut self, ranges: Vec<std::ops::Range<usize>>) -> Self {
self.highlights = ranges;
self
}
pub fn danger(mut self, danger: bool) -> Self {
self.danger = danger;
self
}
pub fn confirm(mut self, label: impl Into<SharedString>) -> Self {
self.confirm = Some(label.into());
self
}
pub fn dense(mut self, dense: bool) -> Self {
self.dense = dense;
self
}
pub fn highlighted(mut self, highlighted: bool) -> Self {
self.highlighted = highlighted;
self
}
pub fn radio(mut self, selected: bool) -> Self {
self.radio = Some(selected);
self
}
pub fn static_row(mut self, static_row: bool) -> Self {
self.static_row = static_row;
self
}
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
pub fn inline_sublabel(mut self, inline: bool) -> Self {
self.inline_sublabel = inline;
self
}
pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn leading(mut self, el: impl IntoElement) -> Self {
self.leading = Some(el.into_any_element());
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn sublabel(mut self, sublabel: impl Into<SharedString>) -> Self {
self.sublabel = Some(sublabel.into());
self
}
pub fn trailing(mut self, el: impl IntoElement) -> Self {
self.trailing = Some(el.into_any_element());
self
}
pub fn on_click(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl RenderOnce for MenuItem {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let state = use_interaction(&self.id, window, cx);
let enabled = !self.disabled;
let reactive = enabled && !self.static_row;
let hovered = (state.read(cx).hovered && reactive && self.hover_fill) || self.highlighted;
let armed_state = self
.confirm
.as_ref()
.map(|_| window.use_keyed_state(key(&self.id, "armed"), cx, |_, _| false));
let armed = match &armed_state {
Some(armed) if *armed.read(cx) && !hovered => {
armed.update(cx, |armed, _| *armed = false);
false
}
Some(armed) => *armed.read(cx),
None => false,
};
let (min_h, pad, gap, rest_r, hover_r, icon_size, text_size) = if self.dense {
(30., 9., 9., 8., 15., 16., Some(13.))
} else {
(36., 10., 10., 10., 18., 17., None)
};
let radius = px(animate_f32(
key(&self.id, "radius"),
Spring::SPATIAL_FAST,
if hovered || self.selected {
hover_r
} else {
rest_r
},
window,
cx,
));
let (bg, fg, muted) = if self.selected && self.radio.is_none() {
(
s.secondary_container,
s.on_secondary_container,
s.on_secondary_container,
)
} else if hovered {
(
s.surface_container_highest,
s.on_surface,
s.on_surface_variant,
)
} else {
(
alpha(s.surface_container_highest, 0.),
s.on_surface,
s.on_surface_variant,
)
};
let (bg, fg, muted) = match (self.danger, armed, hovered) {
(_, true, _) => (
s.error_container,
s.on_error_container,
s.on_error_container,
),
(true, false, true) => (s.error_container, s.error, s.error),
(true, false, false) => (bg, s.error, s.error),
_ => (bg, fg, muted),
};
let bg = animate_color(key(&self.id, "bg"), Spring::EFFECTS, bg, window, cx);
let fg = animate_color(key(&self.id, "fg"), Spring::EFFECTS, fg, window, cx);
let layer = state_layer(&self.id, &state, all(radius), fg, window, cx);
let highlighted_label = |label: SharedString| -> AnyElement {
if self.highlights.is_empty() {
return label.into_any_element();
}
let style = gpui::HighlightStyle {
color: Some(s.primary),
font_weight: Some(FontWeight(700.)),
..Default::default()
};
let ranges: Vec<_> = self
.highlights
.iter()
.filter(|r| {
r.end <= label.len()
&& label.is_char_boundary(r.start)
&& label.is_char_boundary(r.end)
})
.map(|r| (r.clone(), style))
.collect();
gpui::StyledText::new(label)
.with_highlights(ranges)
.into_any_element()
};
let label = match (&self.confirm, armed) {
(Some(confirm), true) => confirm.clone().into_any_element(),
_ => highlighted_label(self.label),
};
let has_actions = !self.actions.is_empty();
let label = match self.line_clamp {
Some(lines) => div()
.overflow_hidden()
.text_ellipsis()
.line_clamp(lines)
.child(label)
.into_any_element(),
None => label,
};
let mut row = div()
.id(self.id.clone())
.relative()
.flex()
.when(!has_actions, |d| d.items_center())
.when(has_actions, |d| d.items_start())
.gap(px(gap))
.min_h(px(min_h))
.px(px(pad))
.py(px(if has_actions {
6.
} else if self.dense {
0.
} else {
4.
}))
.corners(all(radius))
.bg(bg)
.text_color(fg)
.when_some(text_size, |d, size| d.text_size(px(size)))
.when(self.selected, |d| d.font_weight(FontWeight(600.)))
.when(!enabled, |d| d.opacity(0.38))
.child(layer);
if let Some(on) = self.radio {
row = row.child(Radio::new(child_id(&self.id, "radio"), on));
} else if let Some(leading) = self.leading {
row = row.child(leading);
} else if let Some(icon) = self.icon {
row = row.child(Icon::new(icon).size(px(icon_size)).color(muted));
}
let sub = self.sublabel.map(|sub| {
div()
.text_size(px(11.5))
.font_weight(FontWeight(400.))
.text_color(muted)
.child(sub)
});
let text = if self.inline_sublabel {
div()
.flex()
.items_baseline()
.gap(px(8.))
.flex_1()
.min_w_0()
.child(div().flex_none().child(label))
.children(sub.map(|sub| {
sub.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
}))
} else {
div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.py(px(if self.dense { 4. } else { 0. }))
.line_height(gpui::relative(1.25))
.child(label)
.children(sub)
.when(has_actions, |d| {
d.child(
div()
.flex()
.flex_wrap()
.gap(px(4.))
.mt(px(6.))
.children(self.actions),
)
})
};
row = row.child(text);
let trailing = match (self.trailing, self.selected && self.radio.is_none()) {
(Some(trailing), _) => Some(trailing),
(None, true) => Some(
Icon::new("check")
.size(px(17.))
.color(fg)
.into_any_element(),
),
(None, false) => None,
};
if let Some(trailing) = trailing {
row = row.child(
div()
.flex_none()
.text_size(px(11.5))
.text_color(muted)
.child(trailing),
);
}
if let Some(shortcut) = self.shortcut {
row = row.child(
div()
.flex_none()
.font_family(MONO_FONT_FAMILY)
.text_size(px(11.))
.font_weight(FontWeight(400.))
.text_color(s.outline)
.child(shortcut),
);
}
let mut row = interactive(row, &state, reactive, reactive);
if let (Some(f), true) = (self.on_click, enabled) {
row = row.on_click(move |e, w, cx| {
if let Some(armed) = &armed_state
&& !armed.read(cx)
{
armed.update(cx, |armed, cx| {
*armed = true;
cx.notify();
});
return;
}
if let Some(armed) = &armed_state {
armed.update(cx, |armed, _| *armed = false);
}
f(e, w, cx)
});
}
row
}
}
#[derive(IntoElement)]
pub struct Card {
id: Option<ElementId>,
tone: Option<Tone>,
background: Option<Hsla>,
padding: (Pixels, Pixels),
radius: Pixels,
gap: Pixels,
entrance: bool,
on_click: Option<ClickHandler>,
children: Vec<AnyElement>,
}
impl Card {
pub fn tonal(tone: Tone) -> Self {
Self {
tone: Some(tone),
..Self::plain()
}
}
pub fn plain() -> Self {
Self {
id: None,
tone: None,
background: None,
padding: (px(22.), px(22.)),
radius: px(28.),
gap: px(12.),
entrance: false,
on_click: None,
children: Vec::new(),
}
}
pub fn padding(mut self, padding: Pixels) -> Self {
self.padding = (padding, padding);
self
}
pub fn padding_xy(mut self, x: Pixels, y: Pixels) -> Self {
self.padding = (x, y);
self
}
pub fn radius(mut self, radius: Pixels) -> Self {
self.radius = radius;
self
}
pub fn gap(mut self, gap: Pixels) -> Self {
self.gap = gap;
self
}
pub fn bg(mut self, background: Hsla) -> Self {
self.background = Some(background);
self
}
pub fn entrance(mut self, entrance: bool) -> Self {
self.entrance = entrance;
self
}
pub fn id(mut self, id: impl Into<ElementId>) -> Self {
self.id = Some(id.into());
self
}
pub fn on_click(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl ParentElement for Card {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Card {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let (bg, fg) = match self.tone {
Some(tone) => tone.colors(&s),
None => (mix(s.primary, s.surface_container, 0.06), s.on_surface),
};
let bg = self.background.unwrap_or(bg);
debug_assert!(
(self.on_click.is_none() && !self.entrance) || self.id.is_some(),
"Card::on_click and Card::entrance need Card::id"
);
let progress = match (&self.id, self.entrance) {
(Some(id), true) => entrance(key(id, "enter"), Spring::SPATIAL_SLOW, 0., window, cx),
_ => 1.,
};
let radius = self.radius + (px(48.) - self.radius) * (1. - progress).max(0.);
let card = div()
.relative()
.flex()
.flex_col()
.gap(self.gap)
.px(self.padding.0)
.py(self.padding.1)
.rounded(radius)
.bg(bg)
.text_color(fg)
.font_family(font_family())
.when(progress < 1., |d| {
d.top(px((1. - progress) * 24.))
.opacity(progress.clamp(0., 1.))
});
let (Some(id), Some(f)) = (self.id, self.on_click) else {
return card.children(self.children).into_any_element();
};
let state = use_interaction(&id, window, cx);
let layer = state_layer(&id, &state, all(radius), fg, window, cx);
let card = card.id(id).child(layer).children(self.children);
interactive(card, &state, true, true)
.on_click(move |e, w, cx| f(e, w, cx))
.into_any_element()
}
}
#[derive(IntoElement)]
pub struct ListGroup {
title: Option<SharedString>,
rows: Vec<AnyElement>,
}
impl ListGroup {
pub fn new() -> Self {
Self {
title: None,
rows: Vec::new(),
}
}
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into());
self
}
pub fn row(mut self, label: impl Into<SharedString>, trailing: impl IntoElement) -> Self {
self.rows.push(
div()
.flex()
.items_center()
.justify_between()
.gap(px(12.))
.w_full()
.child(div().font_weight(FontWeight(500.)).child(label.into()))
.child(trailing)
.into_any_element(),
);
self
}
pub fn custom_row(mut self, content: impl IntoElement) -> Self {
self.rows.push(content.into_any_element());
self
}
}
impl Default for ListGroup {
fn default() -> Self {
Self::new()
}
}
impl RenderOnce for ListGroup {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let bg = mix(s.primary, s.surface_container, 0.07);
let n = self.rows.len();
let mut col = div()
.flex()
.flex_col()
.gap(px(2.))
.font_family(font_family())
.text_size(px(13.5))
.text_color(s.on_surface);
if let Some(title) = self.title {
col = col.child(
div()
.pt(px(4.))
.pb(px(6.))
.px(px(6.))
.text_size(px(14.))
.font_weight(FontWeight(700.))
.text_color(s.primary)
.child(title),
);
}
for (i, row) in self.rows.into_iter().enumerate() {
let top = if i == 0 { 22. } else { 6. };
let bottom = if i + 1 == n { 22. } else { 6. };
col = col.child(
div()
.flex()
.items_center()
.min_h(px(52.))
.py(px(12.))
.px(px(18.))
.corners(corners([top, top, bottom, bottom]))
.bg(bg)
.child(div().flex_1().min_w_0().child(row)),
);
}
col
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BubbleKind {
User,
Notice,
Error,
}
#[derive(IntoElement)]
pub struct Bubble {
kind: BubbleKind,
max_width: Length,
padding: (Pixels, Pixels),
entrance: Option<(ElementId, f32)>,
children: Vec<AnyElement>,
}
impl Bubble {
pub fn new(kind: BubbleKind, text: impl Into<SharedString>) -> Self {
Self::custom(kind).child(text.into())
}
pub fn custom(kind: BubbleKind) -> Self {
Self {
kind,
max_width: px(420.).into(),
padding: (px(14.), px(10.)),
entrance: None,
children: Vec::new(),
}
}
pub fn max_w(mut self, max_width: Pixels) -> Self {
self.max_width = max_width.into();
self
}
pub fn max_w_fraction(mut self, fraction: f32) -> Self {
self.max_width = relative(fraction).into();
self
}
pub fn padding(mut self, x: Pixels, y: Pixels) -> Self {
self.padding = (x, y);
self
}
pub fn entrance(mut self, id: impl Into<ElementId>, delay: f32) -> Self {
self.entrance = Some((id.into(), delay));
self
}
}
impl ParentElement for Bubble {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Bubble {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let (bg, fg, r) = match self.kind {
BubbleKind::User => (
s.primary_container,
s.on_primary_container,
[20., 20., 4., 20.],
),
BubbleKind::Notice => (
s.tertiary_container,
s.on_tertiary_container,
[4., 20., 20., 20.],
),
BubbleKind::Error => (s.tertiary_container, s.error, [4., 20., 20., 20.]),
};
let p = match &self.entrance {
Some((id, delay)) => {
entrance(key(id, "bubble"), Spring::SPATIAL_FAST, *delay, window, cx)
}
None => 1.,
};
let r = r.map(|corner| corner + (20. - corner) * (1. - p).clamp(0., 1.));
let slide = (1. - p) * 8.;
div()
.relative()
.when(p < 1., |d| match self.kind {
BubbleKind::User => d.left(px(slide)).top(px(slide)).opacity(p.clamp(0., 1.)),
_ => d.left(px(-slide)).top(px(-slide)).opacity(p.clamp(0., 1.)),
})
.max_w(self.max_width)
.px(self.padding.0)
.py(self.padding.1)
.corners(corners(r))
.bg(bg)
.text_color(fg)
.font_family(font_family())
.text_size(px(13.5))
.children(self.children)
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::size;
fn card() -> Bounds<Pixels> {
Bounds::new(point(px(100.), px(200.)), size(px(200.), px(300.)))
}
#[test]
fn an_upward_reveal_grows_from_the_bottom_edge() {
let half = reveal_mask(card(), PopDirection::Up.into(), 0.5);
assert_eq!(half.bottom(), px(500. + REVEAL_SHADOW));
assert_eq!(half.top(), px(500. - 0.5 * (300. + REVEAL_SHADOW)));
assert_eq!(half.left(), px(100. - REVEAL_SHADOW));
assert_eq!(half.right(), px(300. + REVEAL_SHADOW));
let closed = reveal_mask(card(), PopDirection::Up.into(), 0.);
assert_eq!(closed.size.height, px(REVEAL_SHADOW));
assert!(closed.top() >= card().bottom());
}
#[test]
fn a_corner_reveal_grows_away_from_its_corner() {
let start = reveal_mask(card(), Corner::TopRight.into(), 0.);
assert_eq!(start.top(), px(200. - REVEAL_SHADOW));
assert_eq!(start.right(), px(300. + REVEAL_SHADOW));
assert!(start.left() > card().left());
let open = reveal_mask(card(), Corner::TopRight.into(), 1.);
assert!(open.left() <= card().left() - px(REVEAL_SHADOW) + px(0.01));
assert!(open.bottom() >= card().bottom() + px(REVEAL_SHADOW) - px(0.01));
}
#[test]
fn the_bounce_past_the_end_is_not_drawn() {
let open = reveal_mask(card(), Corner::BottomLeft.into(), 1.);
assert_eq!(reveal_mask(card(), Corner::BottomLeft.into(), 1.1), open);
}
}