use std::f32::consts::TAU;
use std::rc::Rc;
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, ClickEvent, ElementId, FontWeight, InteractiveElement, IntoElement,
MouseButton, ParentElement, Pixels, RenderOnce, SharedString, StatefulInteractiveElement,
Styled, Window, div, px,
};
use super::{ClickHandler, Icon, RoundedExt, all};
use crate::interaction::{interactive, state_layer, use_interaction};
use crate::motion::{Spring, animate_color, animate_f32, continuous_at, entrance, key};
use crate::shapes::Tone;
use crate::theme::{MONO_FONT_FAMILY, Theme, alpha, font_family};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum ChipKind {
#[default]
Assist,
Filled,
Filter,
Input,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum ChipSize {
Small,
#[default]
Medium,
}
#[derive(IntoElement)]
pub struct Chip {
id: ElementId,
label: SharedString,
sublabel: Option<SharedString>,
icon: Option<SharedString>,
leading: Option<AnyElement>,
trailing: Option<AnyElement>,
kind: ChipKind,
tone: Option<Tone>,
size: ChipSize,
selected: bool,
show_check: bool,
open: Option<bool>,
muted: bool,
mono: bool,
inert: bool,
pulse_icon: bool,
max_width: Option<Pixels>,
on_click: Option<ClickHandler>,
on_remove: Option<ClickHandler>,
}
impl Chip {
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,
kind: ChipKind::Assist,
tone: None,
size: ChipSize::Medium,
selected: false,
show_check: true,
open: None,
muted: false,
mono: false,
inert: false,
pulse_icon: false,
max_width: None,
on_click: None,
on_remove: None,
}
}
pub fn input(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self::new(id, label).kind(ChipKind::Input)
}
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 trailing(mut self, el: impl IntoElement) -> Self {
self.trailing = Some(el.into_any_element());
self
}
pub fn open(mut self, open: bool) -> Self {
self.open = Some(open);
self
}
pub fn sublabel(mut self, sublabel: impl Into<SharedString>) -> Self {
self.sublabel = Some(sublabel.into());
self
}
pub fn kind(mut self, kind: ChipKind) -> Self {
self.kind = kind;
self
}
pub fn filled(self) -> Self {
self.kind(ChipKind::Filled)
}
pub fn filter(mut self, selected: bool) -> Self {
self.kind = ChipKind::Filter;
self.selected = selected;
self
}
pub fn tone(mut self, tone: Tone) -> Self {
self.tone = Some(tone);
self
}
pub fn size(mut self, size: ChipSize) -> Self {
self.size = size;
self
}
pub fn show_check(mut self, show: bool) -> Self {
self.show_check = show;
self
}
pub fn muted(mut self, muted: bool) -> Self {
self.muted = muted;
self
}
pub fn mono(mut self, mono: bool) -> Self {
self.mono = mono;
self
}
pub fn inert(mut self, inert: bool) -> Self {
self.inert = inert;
self
}
pub fn pulse_icon(mut self, pulse: bool) -> Self {
self.pulse_icon = pulse;
self
}
pub fn max_w(mut self, max_width: Pixels) -> Self {
self.max_width = Some(max_width);
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
}
pub fn on_remove(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_remove = Some(Rc::new(f));
self
}
}
impl RenderOnce for Chip {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let reactive = !self.inert;
let state = use_interaction(&self.id, window, cx);
let pressed = state.read(cx).pressed && reactive;
let on = self.kind == ChipKind::Filter && self.selected;
let (height, text_size, icon_size, gap, pad) = match self.size {
ChipSize::Small => (24., 12., 13., 5., 8.),
ChipSize::Medium => (32., 12.5, 16., 7., 12.),
};
let radius = match (pressed, on) {
(true, _) | (false, true) => height / 2.,
(false, false) => 8.,
};
let radius = px(animate_f32(
key(&self.id, "radius"),
Spring::SPATIAL_FAST,
radius,
window,
cx,
));
let shrink = animate_f32(
key(&self.id, "shrink"),
Spring::SPATIAL_FAST,
if pressed { 1. } else { 0. },
window,
cx,
);
let (bg, fg, border, icon_color, weight) = match (self.kind, self.tone) {
(ChipKind::Filter, _) if on => (
s.primary_container,
s.on_primary_container,
alpha(s.outline_variant, 0.),
s.on_primary_container,
600.,
),
(_, Some(tone)) => {
let (bg, fg) = tone.colors(&s);
(bg, fg, alpha(bg, 0.), fg, 500.)
}
(ChipKind::Assist | ChipKind::Input, None) => (
alpha(s.surface, 0.),
s.on_surface,
s.outline_variant,
s.primary,
500.,
),
(ChipKind::Filled, None) => (
s.primary_container,
s.on_primary_container,
alpha(s.primary_container, 0.),
s.on_primary_container,
600.,
),
(ChipKind::Filter, None) => (
alpha(s.surface, 0.),
s.on_surface_variant,
s.outline_variant,
s.on_surface_variant,
500.,
),
};
let (fg, icon_color) = if self.muted && !on {
(s.on_surface_variant, s.on_surface_variant)
} else {
(fg, icon_color)
};
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 border = animate_color(key(&self.id, "border"), Spring::EFFECTS, border, window, cx);
let icon_color = animate_color(
key(&self.id, "icon"),
Spring::EFFECTS,
icon_color,
window,
cx,
);
let icon_color = if self.pulse_icon {
let t = continuous_at(30., window, cx);
alpha(icon_color, 0.725 + 0.275 * (t / 1.4 * TAU).cos())
} else {
icon_color
};
let layer = state_layer(&self.id, &state, all(radius), fg, window, cx);
let has_leading = self.icon.is_some() || self.leading.is_some() || on;
let mut face = div()
.relative()
.flex()
.items_center()
.gap(px(gap))
.h(px(height - shrink * 2.))
.pl(px(if has_leading { pad - 4. } else { pad }))
.pr(px(if self.kind == ChipKind::Input {
4.
} else {
pad
}))
.corners(all(radius))
.bg(bg)
.border_1()
.border_color(border)
.text_color(fg)
.font_family(font_family())
.text_size(px(text_size))
.font_weight(FontWeight(weight))
.when_some(self.max_width, |d, w| d.max_w(w))
.child(layer);
if on && self.show_check {
face = face.child(Icon::new("check").size(px(icon_size)).color(fg));
} else if let Some(leading) = self.leading {
face = face.child(leading);
} else if let Some(icon) = self.icon {
let color = if on { fg } else { icon_color };
face = face.child(Icon::new(icon).size(px(icon_size)).color(color));
}
let truncate = self.max_width.is_some();
let label = div()
.flex()
.items_baseline()
.whitespace_nowrap()
.when(truncate, |d| d.min_w_0().overflow_hidden())
.when(!truncate, |d| d.flex_none())
.when(self.mono, |d| {
d.font_family(MONO_FONT_FAMILY)
.font_weight(FontWeight(400.))
})
.child(
div()
.when(truncate, |d| d.min_w_0().overflow_hidden().text_ellipsis())
.child(self.label),
)
.children(
self.sublabel
.map(|sub| div().flex_none().text_color(s.outline).child(sub)),
);
face = face.child(label);
if let Some(trailing) = self.trailing {
face = face.child(div().flex_none().child(trailing));
}
if let Some(open) = self.open {
let rotation = animate_f32(
key(&self.id, "chevron"),
Spring::SPATIAL,
if open { std::f32::consts::PI } else { 0. },
window,
cx,
);
face = face.child(
Icon::new("chevron-down")
.size(px(12.))
.color(fg)
.rotate(rotation),
);
}
if self.kind == ChipKind::Input {
let remove_id = super::child_id(&self.id, "remove");
let remove_state = use_interaction(&remove_id, window, cx);
let remove_layer = state_layer(&remove_id, &remove_state, all(px(9.)), fg, window, cx);
let remove = div()
.id(remove_id)
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(px(18.))
.mr(px(2.))
.rounded(px(9.))
.child(remove_layer)
.child(Icon::new("x").size(px(12.)).color(s.on_surface_variant))
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation());
let mut remove = interactive(remove, &remove_state, true, true);
if let Some(f) = self.on_remove {
remove = remove.on_click(move |e, w, cx| {
cx.stop_propagation();
f(e, w, cx)
});
}
face = face.child(remove);
}
let el = div()
.id(self.id.clone())
.flex()
.flex_none()
.items_center()
.h(px(height))
.when_some(self.max_width, |d, w| d.max_w(w))
.child(face);
let mut el = interactive(el, &state, reactive, reactive);
if let (Some(f), true) = (self.on_click, reactive) {
el = el.on_click(move |e, w, cx| f(e, w, cx));
}
el
}
}
#[derive(IntoElement)]
pub struct SplitButton {
id: ElementId,
label: SharedString,
sublabel: Option<SharedString>,
open: bool,
on_click: Option<ClickHandler>,
on_toggle: Option<ClickHandler>,
}
impl SplitButton {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
sublabel: None,
open: false,
on_click: None,
on_toggle: None,
}
}
pub fn sublabel(mut self, sublabel: impl Into<SharedString>) -> Self {
self.sublabel = Some(sublabel.into());
self
}
pub fn open(mut self, open: bool) -> Self {
self.open = open;
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
}
pub fn on_toggle(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_toggle = Some(Rc::new(f));
self
}
}
impl RenderOnce for SplitButton {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let main_id = super::child_id(&self.id, "main");
let trail_id = super::child_id(&self.id, "trail");
let main_state = use_interaction(&main_id, window, cx);
let trail_state = use_interaction(&trail_id, window, cx);
let main_pressed = main_state.read(cx).pressed;
let trail_pressed = trail_state.read(cx).pressed;
let inner_main = if main_pressed { 8. } else { 4. };
let main_r = crate::motion::animate(
key(&main_id, "radius"),
Spring::SPATIAL_FAST,
[16., inner_main, inner_main, 16.],
window,
cx,
);
let inner_trail = if self.open {
16.
} else if trail_pressed {
8.
} else {
4.
};
let trail_r = crate::motion::animate(
key(&trail_id, "radius"),
Spring::SPATIAL_FAST,
[inner_trail, 16., 16., inner_trail],
window,
cx,
);
let rotation = animate_f32(
key(&trail_id, "rot"),
Spring::SPATIAL,
if self.open { std::f32::consts::PI } else { 0. },
window,
cx,
);
let (trail_bg, trail_fg) = if self.open {
(s.primary, s.on_primary)
} else {
(s.secondary_container, s.on_secondary_container)
};
let trail_bg = animate_color(key(&trail_id, "bg"), Spring::EFFECTS, trail_bg, window, cx);
let trail_fg = animate_color(key(&trail_id, "fg"), Spring::EFFECTS, trail_fg, window, cx);
let main_layer = state_layer(
&main_id,
&main_state,
super::corners(main_r),
s.on_secondary_container,
window,
cx,
);
let trail_layer = state_layer(
&trail_id,
&trail_state,
super::corners(trail_r),
trail_fg,
window,
cx,
);
let mut main = div()
.id(main_id.clone())
.relative()
.flex()
.items_center()
.gap(px(6.))
.h(px(32.))
.pl(px(14.))
.pr(px(10.))
.corners(super::corners(main_r))
.bg(s.secondary_container)
.text_color(s.on_secondary_container)
.font_weight(FontWeight(600.))
.child(main_layer)
.child(self.label);
if let Some(sub) = self.sublabel {
main = main.child(div().font_weight(FontWeight(500.)).opacity(0.75).child(sub));
}
let mut main = interactive(main, &main_state, true, true);
if let Some(f) = self.on_click {
main = main.on_click(move |e, w, cx| f(e, w, cx));
}
let trail = div()
.id(trail_id.clone())
.relative()
.flex()
.items_center()
.justify_center()
.size(px(32.))
.corners(super::corners(trail_r))
.bg(trail_bg)
.child(trail_layer)
.child(
Icon::new("chevron-down")
.size(px(16.))
.color(trail_fg)
.rotate(rotation),
);
let mut trail = interactive(trail, &trail_state, true, true);
if let Some(f) = self.on_toggle {
trail = trail.on_click(move |e, w, cx| f(e, w, cx));
}
div()
.flex()
.flex_none()
.items_center()
.gap(px(2.))
.font_family(font_family())
.text_size(px(12.5))
.child(main)
.child(trail)
}
}
#[derive(IntoElement)]
pub struct Badge {
label: Option<SharedString>,
tone: Option<Tone>,
strong: bool,
mono: bool,
height: Pixels,
dot_size: Pixels,
pop: Option<ElementId>,
}
impl Badge {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: Some(label.into()),
tone: None,
strong: false,
mono: false,
height: px(18.),
dot_size: px(6.),
pop: None,
}
}
pub fn strong(mut self, tone: Tone) -> Self {
self.tone = Some(tone);
self.strong = true;
self
}
pub fn size(mut self, size: Pixels) -> Self {
self.dot_size = size;
self
}
pub fn pop(mut self, id: impl Into<ElementId>) -> Self {
self.pop = Some(id.into());
self
}
pub fn dot() -> Self {
Self {
label: None,
..Self::new("")
}
}
pub fn tonal(tone: Tone, label: impl Into<SharedString>) -> Self {
Self {
tone: Some(tone),
..Self::new(label)
}
}
pub fn mono(mut self, mono: bool) -> Self {
self.mono = mono;
self
}
pub fn height(mut self, height: Pixels) -> Self {
self.height = height;
self
}
}
impl RenderOnce for Badge {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let strong = |tone: Tone| match tone {
Tone::Primary => s.primary,
Tone::Secondary => s.on_secondary_container,
Tone::Tertiary => s.tertiary,
};
let Some(label) = self.label else {
let size = f32::from(self.dot_size);
let grow = match &self.pop {
Some(id) => entrance(key(id, "pop"), Spring::SPATIAL_FAST, 0., window, cx),
None => 1.,
};
let color = self.tone.map_or(s.error, strong);
let shown = (size * grow).max(0.);
return div()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(self.dot_size)
.child(div().size(px(shown)).rounded(px(shown / 2.)).bg(color));
};
let (bg, fg, weight, pad, min_w) = match self.tone {
Some(tone) if self.strong => (strong(tone), s.surface, 700., 5., 18.),
Some(tone) => {
let (bg, fg) = tone.colors(&s);
(bg, fg, 500., 6., 0.)
}
None => (s.tertiary_container, s.on_tertiary_container, 700., 5., 18.),
};
let height = f32::from(self.height);
div()
.flex()
.flex_none()
.items_center()
.justify_center()
.min_w(px(min_w))
.h(self.height)
.px(px(pad))
.rounded(px(height / 2.))
.bg(bg)
.text_color(fg)
.font_family(if self.mono {
MONO_FONT_FAMILY
} else {
font_family()
})
.text_size(px(11.))
.font_weight(FontWeight(if self.mono { 400. } else { weight }))
.whitespace_nowrap()
.child(label)
}
}