use std::rc::Rc;
use std::time::{Duration, Instant};
use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, ClickEvent, ElementId, FontWeight, Hsla, InteractiveElement, IntoElement,
ParentElement, Pixels, RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window,
div, px, relative,
};
use super::{ChangeHandler, ClickHandler, Icon, RoundedExt, all};
use crate::interaction::{interactive, keyboard_focus, state_layer, use_interaction};
use crate::motion::{
Spring, animate_color, animate_f32, animate_on_activate, cubic_bezier, key, replay,
};
use crate::theme::type_scale::{
HEADLINE_LARGE, HEADLINE_SMALL, LABEL_LARGE, TITLE_LARGE, TITLE_MEDIUM, TypeStyle,
};
use crate::theme::{Theme, alpha, corner, font_family};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum ButtonVariant {
Filled,
#[default]
Tonal,
Outlined,
Text,
Ghost,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum ButtonSize {
Tiny,
Small,
Compact,
#[default]
Medium,
Comfortable,
Large,
}
impl ButtonSize {
fn metrics(self) -> (f32, f32, f32, f32, f32) {
match self {
ButtonSize::Tiny => (28., 9., 11.5, 8., 8.),
ButtonSize::Small => (30., 12., 12.5, 10., 8.),
ButtonSize::Compact => (32., 14., 12.5, 12., 8.),
ButtonSize::Medium => (36., 18., 13.5, 12., 8.),
ButtonSize::Comfortable => (40., 20., 14., 14., 10.),
ButtonSize::Large => (48., 22., 14., 16., 12.),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum M3ButtonSize {
Xs,
#[default]
S,
M,
L,
Xl,
}
pub(super) struct M3Metrics {
pub(super) height: f32,
pub(super) padding: f32,
pub(super) icon: f32,
pub(super) gap: f32,
pub(super) square: f32,
pub(super) pressed: f32,
pub(super) outline: f32,
pub(super) text: TypeStyle,
}
impl M3ButtonSize {
pub(super) fn metrics(self) -> M3Metrics {
let (height, padding, icon, gap, square, pressed, outline, text) = match self {
M3ButtonSize::Xs => (32., 12., 20., 4., corner::M, corner::S, 1., LABEL_LARGE),
M3ButtonSize::S => (40., 16., 20., 8., corner::M, corner::S, 1., LABEL_LARGE),
M3ButtonSize::M => (56., 24., 24., 8., corner::L, corner::M, 1., TITLE_MEDIUM),
M3ButtonSize::L => (
96.,
48.,
32.,
12.,
corner::XL,
corner::L,
2.,
HEADLINE_SMALL,
),
M3ButtonSize::Xl => (
136.,
64.,
40.,
16.,
corner::XL,
corner::L,
3.,
HEADLINE_LARGE,
),
};
M3Metrics {
height,
padding,
icon,
gap,
square: f32::from(square),
pressed: f32::from(pressed),
outline,
text,
}
}
pub(super) fn icon_button(self, width: IconButtonWidth) -> (f32, f32) {
let (icon, [narrow, default, wide]) = match self {
M3ButtonSize::Xs => (20., [4., 6., 10.]),
M3ButtonSize::S => (24., [4., 8., 14.]),
M3ButtonSize::M => (24., [12., 16., 24.]),
M3ButtonSize::L => (32., [16., 32., 48.]),
M3ButtonSize::Xl => (40., [32., 48., 72.]),
};
let padding = match width {
IconButtonWidth::Narrow => narrow,
IconButtonWidth::Default => default,
IconButtonWidth::Wide => wide,
};
(icon, padding)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum IconButtonWidth {
Narrow,
#[default]
Default,
Wide,
}
fn palette(
cx: &App,
variant: ButtonVariant,
selected: Option<bool>,
enabled: bool,
) -> (Hsla, Hsla, Option<Hsla>) {
let s = Theme::of(cx);
if !enabled {
let bg = if variant == ButtonVariant::Text {
alpha(s.on_surface, 0.)
} else {
alpha(s.on_surface, 0.10)
};
return (bg, alpha(s.on_surface, 0.38), None);
}
match (variant, selected) {
(ButtonVariant::Filled, Some(true)) | (ButtonVariant::Filled, None) => {
(s.primary, s.on_primary, None)
}
(ButtonVariant::Filled, Some(false)) => {
(s.surface_container_high, s.on_surface_variant, None)
}
(ButtonVariant::Outlined, Some(true)) => (
alpha(s.on_surface, 0.),
s.on_secondary_container,
Some(alpha(s.outline_variant, 0.)),
),
(ButtonVariant::Outlined, Some(false)) => (
alpha(s.on_surface, 0.),
s.on_surface_variant,
Some(s.outline_variant),
),
(_, Some(true)) => (
alpha(s.secondary_container, 0.),
s.on_secondary_container,
None,
),
(_, Some(false)) => (alpha(s.on_surface, 0.), s.on_surface_variant, None),
(ButtonVariant::Tonal, _) => (s.secondary_container, s.on_secondary_container, None),
(ButtonVariant::Outlined, _) => (
alpha(s.on_surface, 0.),
s.on_surface_variant,
Some(s.outline_variant),
),
(ButtonVariant::Text, _) => (alpha(s.on_surface, 0.), s.primary, None),
(ButtonVariant::Ghost, _) => (alpha(s.secondary_container, 0.), s.outline, None),
}
}
#[derive(IntoElement)]
pub struct Button {
id: ElementId,
label: Option<SharedString>,
sublabel: Option<SharedString>,
icon: Option<SharedString>,
variant: ButtonVariant,
size: ButtonSize,
m3_size: Option<M3ButtonSize>,
square: bool,
selected: Option<bool>,
open: bool,
disabled: bool,
trailing: Option<AnyElement>,
confirm: Option<(SharedString, Duration)>,
label_on_hover: bool,
icon_only: bool,
truncate: bool,
autofocus: bool,
tooltip: Option<SharedString>,
on_click: Option<ClickHandler>,
}
impl Button {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: Some(label.into()),
sublabel: None,
icon: None,
variant: ButtonVariant::default(),
size: ButtonSize::default(),
m3_size: None,
square: false,
selected: None,
open: false,
disabled: false,
trailing: None,
confirm: None,
label_on_hover: false,
icon_only: false,
truncate: false,
autofocus: false,
tooltip: None,
on_click: None,
}
}
pub fn icon_only(mut self, icon_only: bool) -> Self {
self.icon_only = icon_only;
self
}
pub fn truncate(mut self, truncate: bool) -> Self {
self.truncate = truncate;
self
}
pub fn autofocus(mut self, autofocus: bool) -> Self {
self.autofocus = autofocus;
self
}
pub fn tooltip(mut self, text: impl Into<SharedString>) -> Self {
self.tooltip = Some(text.into());
self
}
pub fn confirm(mut self, label: impl Into<SharedString>, duration: Duration) -> Self {
self.confirm = Some((label.into(), duration));
self
}
pub fn label_on_hover(mut self, on_hover: bool) -> Self {
self.label_on_hover = on_hover;
self
}
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 trailing(mut self, el: impl IntoElement) -> Self {
self.trailing = Some(el.into_any_element());
self
}
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn filled(self) -> Self {
self.variant(ButtonVariant::Filled)
}
pub fn tonal(self) -> Self {
self.variant(ButtonVariant::Tonal)
}
pub fn outlined(self) -> Self {
self.variant(ButtonVariant::Outlined)
}
pub fn text(self) -> Self {
self.variant(ButtonVariant::Text)
}
pub fn size(mut self, size: ButtonSize) -> Self {
self.size = size;
self
}
pub fn m3_size(mut self, size: M3ButtonSize) -> Self {
self.m3_size = Some(size);
self
}
pub fn square(mut self, square: bool) -> Self {
self.square = square;
self
}
pub fn icon(mut self, icon: impl Into<SharedString>) -> Self {
self.icon = Some(icon.into());
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = Some(selected);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
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 Button {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let m3 = self.m3_size.map(M3ButtonSize::metrics);
let (height, pad, font, selected_radius, pressed_radius) = match &m3 {
Some(m) => (
m.height,
m.padding,
f32::from(m.text.size),
m.square,
m.pressed,
),
None => self.size.metrics(),
};
let enabled = !self.disabled;
let state = use_interaction(&self.id, window, cx);
let (hovered, pressed) = {
let st = state.read(cx);
(st.hovered && enabled, st.pressed && enabled)
};
let confirmed_at = self
.confirm
.as_ref()
.map(|_| window.use_keyed_state(key(&self.id, "confirm"), cx, |_, _| None::<Instant>));
let confirming = match (&confirmed_at, &self.confirm) {
(Some(at), Some((_, duration))) => at.read(cx).is_some_and(|t| t.elapsed() < *duration),
_ => false,
};
let ghost_on = self.variant == ButtonVariant::Ghost && (hovered || confirming);
let squared = (self.selected == Some(true)) != self.square || self.open;
let radius_target = if pressed || ghost_on {
pressed_radius
} else if squared {
selected_radius
} else {
height / 2.
};
let radius = px(animate_f32(
key(&self.id, "radius"),
Spring::SPATIAL_FAST,
radius_target,
window,
cx,
));
let (bg, fg, border) = if confirming {
let s = Theme::of(cx);
(s.primary_container, s.on_primary_container, None)
} else if ghost_on {
let s = Theme::of(cx);
(s.secondary_container, s.on_secondary_container, None)
} else {
palette(cx, self.variant, self.selected, enabled)
};
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 weight = if self.selected == Some(true) {
700.
} else {
600.
};
let indicator = (self.selected.is_some()
&& self.variant != ButtonVariant::Filled
&& enabled)
.then(|| {
let [grow, opacity] = animate_on_activate(
key(&self.id, "indicator"),
Spring::SPATIAL_FAST,
self.selected == Some(true),
[0.35, 0.],
[1., 1.],
window,
cx,
);
div()
.absolute()
.top_0()
.h_full()
.left(relative((1. - grow) / 2.))
.w(relative(grow.max(0.)))
.corners(all(radius))
.bg(alpha(
Theme::of(cx).secondary_container,
opacity.clamp(0., 1.),
))
});
let icon_only = self.icon_only && self.icon.is_some() && !self.label_on_hover;
let tooltip = self.tooltip.clone().or_else(|| {
icon_only.then(|| match (&self.label, &self.sublabel) {
(Some(label), Some(sub)) => SharedString::from(format!("{label} {sub}")),
(Some(label), None) => label.clone(),
_ => SharedString::default(),
})
});
let mut el = div()
.id(self.id.clone())
.relative()
.flex()
.items_center()
.when(!self.truncate || icon_only, |d| d.flex_none())
.when(self.truncate && !icon_only, |d| {
d.flex_shrink().min_w_0().overflow_hidden()
})
.when(icon_only, |d| d.w(px(height)))
.justify_center()
.gap(px(if self.label_on_hover {
0.
} else if let Some(m) = &m3 {
m.gap
} else if self.size == ButtonSize::Tiny {
5.
} else {
8.
}))
.h(px(height))
.when(!icon_only, |d| {
d.px(px(if self.icon.is_some() && m3.is_none() {
pad - 2.
} else {
pad
}))
})
.corners(all(radius))
.bg(bg)
.text_color(fg)
.font_family(font_family())
.text_size(px(font))
.font_weight(FontWeight(weight))
.when(self.selected == Some(true), |d| d.children(indicator))
.child(layer);
if let Some(border) = border {
el = match &m3 {
Some(m) => el.border(px(m.outline)),
None => el.border_1(),
}
.border_color(border);
}
let icon = if confirming {
Some(SharedString::from("check"))
} else {
self.icon
};
if let Some(icon) = icon {
let size = if let Some(m) = &m3 {
m.icon
} else if self.size == ButtonSize::Tiny {
14.
} else {
font + 4.
};
el = el.child(Icon::new(icon).size(px(size)).color(fg));
}
let label = match (&self.confirm, confirming) {
(Some((label, _)), true) => Some(label.clone()),
_ if icon_only => None,
_ => self.label,
};
if let Some(label) = label {
if self.label_on_hover {
let shown = animate_f32(
key(&self.id, "label"),
Spring::EFFECTS,
if hovered || confirming { 1. } else { 0. },
window,
cx,
)
.clamp(0., 1.);
el = el.child(
div()
.overflow_hidden()
.whitespace_nowrap()
.max_w(px(80. * shown))
.pl(px(5. * shown))
.opacity(shown)
.child(label),
);
} else if self.truncate {
el = el.child(
div()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.child(label),
);
} else {
el = el.child(label);
}
}
if let Some(sub) = self.sublabel.filter(|_| !icon_only) {
el = el.child(
div()
.ml(px(-2.))
.font_weight(FontWeight(500.))
.opacity(0.75)
.when(self.truncate, |d| d.flex_none())
.child(sub),
);
}
if let Some(trailing) = self.trailing.filter(|_| !icon_only) {
el = el.child(trailing);
}
if let Some(tooltip) = tooltip {
el = el.tooltip(super::Tooltip::text(tooltip));
}
let mut el = interactive(el, &state, enabled, true);
if enabled {
el = keyboard_focus(el, &self.id, all(radius), self.autofocus, window, cx);
}
if enabled && (self.on_click.is_some() || confirmed_at.is_some()) {
let f = self.on_click;
let duration = self.confirm.as_ref().map(|(_, d)| *d);
el = el.on_click(move |e, w, cx| {
if let (Some(at), Some(duration)) = (&confirmed_at, duration) {
at.update(cx, |at, cx| {
*at = Some(Instant::now());
cx.notify();
});
let at = at.clone();
let timer = cx.background_executor().timer(duration);
cx.spawn(async move |cx| {
timer.await;
at.update(cx, |_, cx| cx.notify()).ok();
})
.detach();
}
if let Some(f) = &f {
f(e, w, cx);
}
});
}
el
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum IconButtonVariant {
#[default]
Standard,
Filled,
Tonal,
Outlined,
}
#[derive(IntoElement)]
pub struct IconButton {
id: ElementId,
icon: SharedString,
variant: IconButtonVariant,
size: Pixels,
m3_size: Option<M3ButtonSize>,
width: IconButtonWidth,
square: bool,
selected: Option<bool>,
hover_morph: bool,
disabled: bool,
tooltip: Option<SharedString>,
launch: Option<u64>,
on_click: Option<ClickHandler>,
}
impl IconButton {
pub fn new(id: impl Into<ElementId>, icon: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
icon: icon.into(),
variant: IconButtonVariant::default(),
size: px(32.),
m3_size: None,
width: IconButtonWidth::default(),
square: false,
selected: None,
hover_morph: false,
disabled: false,
tooltip: None,
launch: None,
on_click: None,
}
}
pub fn launch(mut self, generation: u64) -> Self {
self.launch = Some(generation);
self
}
pub fn variant(mut self, variant: IconButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn filled(self) -> Self {
self.variant(IconButtonVariant::Filled)
}
pub fn tonal(self) -> Self {
self.variant(IconButtonVariant::Tonal)
}
pub fn outlined(self) -> Self {
self.variant(IconButtonVariant::Outlined)
}
pub fn size(mut self, size: Pixels) -> Self {
self.size = size;
self
}
pub fn m3_size(mut self, size: M3ButtonSize) -> Self {
self.m3_size = Some(size);
self
}
pub fn width(mut self, width: IconButtonWidth) -> Self {
self.width = width;
self
}
pub fn square(mut self, square: bool) -> Self {
self.square = square;
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = Some(selected);
self
}
pub fn tooltip(mut self, text: impl Into<SharedString>) -> Self {
self.tooltip = Some(text.into());
self
}
pub fn hover_morph(mut self, morph: bool) -> Self {
self.hover_morph = morph;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
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 IconButton {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let enabled = !self.disabled;
let state = use_interaction(&self.id, window, cx);
let (hovered, pressed) = {
let st = state.read(cx);
(st.hovered && enabled, st.pressed && enabled)
};
let (width, height, icon_size, square_radius, pressed_radius, outline) = match self.m3_size
{
Some(m3) => {
let m = m3.metrics();
let (icon, padding) = m3.icon_button(self.width);
(
icon + 2. * padding,
m.height,
icon,
m.square,
m.pressed,
m.outline,
)
}
None => {
let size = f32::from(self.size);
let icon = (size * 0.56).round().min(22.);
(size, size, icon, (size * 0.33).round(), 8., 1.)
}
};
let squared = (self.selected == Some(true)) != self.square || (self.hover_morph && hovered);
let radius_target = if pressed {
pressed_radius
} else if squared {
square_radius
} else {
height / 2.
};
let radius = px(animate_f32(
key(&self.id, "radius"),
Spring::SPATIAL_FAST,
radius_target,
window,
cx,
));
let scale = animate_f32(
key(&self.id, "scale"),
Spring::SPATIAL_FAST,
if pressed { 0.94 } else { 1. },
window,
cx,
);
let launch = self
.launch
.and_then(|generation| replay(key(&self.id, "launch"), generation, 0.6, window, cx))
.map(launch_frame);
let (radius, scale) = match launch {
Some(frame) => (
radius + (px(height / 2.) - radius) * frame.round,
scale * frame.scale,
),
None => (radius, scale),
};
let selected = self.selected == Some(true);
let (bg, fg, border) = if !enabled {
let bg = if self.variant == IconButtonVariant::Standard {
alpha(s.on_surface, 0.)
} else {
alpha(s.on_surface, 0.10)
};
(bg, alpha(s.on_surface, 0.38), None)
} else {
match self.variant {
IconButtonVariant::Standard if selected => (alpha(s.primary, 0.), s.primary, None),
IconButtonVariant::Standard => {
(alpha(s.on_surface, 0.), s.on_surface_variant, None)
}
IconButtonVariant::Filled if self.selected == Some(false) => {
(s.surface_container_high, s.primary, None)
}
IconButtonVariant::Filled => (s.primary, s.on_primary, None),
IconButtonVariant::Tonal if selected => {
(s.secondary_container, s.on_secondary_container, None)
}
IconButtonVariant::Tonal if self.selected == Some(false) => {
(s.surface_container_high, s.on_surface_variant, None)
}
IconButtonVariant::Tonal => (s.secondary_container, s.on_secondary_container, None),
IconButtonVariant::Outlined if selected => (s.on_surface, s.surface, None),
IconButtonVariant::Outlined => (
alpha(s.on_surface, 0.),
s.on_surface_variant,
Some(s.outline_variant),
),
}
};
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 (inner_w, inner_h) = (width * scale, height * scale);
let layer = state_layer(&self.id, &state, all(radius), fg, window, cx);
let mut face = div()
.absolute()
.top(px((height - inner_h) / 2.))
.left(px((width - inner_w) / 2.))
.w(px(inner_w))
.h(px(inner_h))
.flex()
.items_center()
.justify_center()
.corners(all(radius))
.bg(bg)
.child(layer)
.child(
div()
.relative()
.top(px(launch.map_or(0., |frame| frame.icon_y)))
.opacity(launch.map_or(1., |frame| frame.icon_opacity))
.child(Icon::new(self.icon).size(px(icon_size * scale)).color(fg)),
);
if let Some(border) = border {
face = if outline == 1. {
face.border_1()
} else {
face.border(px(outline))
}
.border_color(border);
}
let mut el = div()
.id(self.id.clone())
.relative()
.flex_none()
.w(px(width))
.h(px(height))
.child(face);
if let Some(text) = self.tooltip {
el = el.tooltip(super::Tooltip::text(text));
}
let mut el = interactive(el, &state, enabled, true);
if let (Some(f), true) = (self.on_click, enabled) {
el = el.on_click(move |e, w, cx| f(e, w, cx));
}
el
}
}
#[derive(Clone, Copy)]
struct LaunchFrame {
scale: f32,
round: f32,
icon_y: f32,
icon_opacity: f32,
}
fn launch_frame(t: f32) -> LaunchFrame {
let squeeze_end = 0.18;
let (scale, round) = if t < squeeze_end {
let k = smoothstep(t / squeeze_end);
(1. - 0.18 * k, k)
} else {
let k = Spring::SPATIAL_FAST.step(t - squeeze_end);
(0.82 + 0.18 * k, 1. - k)
};
let fly_end = 0.27;
let (icon_y, icon_opacity) = if t < fly_end {
let k = smoothstep(t / fly_end);
(-22. * k, 1. - k)
} else {
let k = Spring::SPATIAL.step(t - fly_end);
(18. * (1. - k), k.clamp(0., 1.))
};
LaunchFrame {
scale,
round: round.clamp(0., 1.),
icon_y,
icon_opacity,
}
}
fn smoothstep(x: f32) -> f32 {
let x = x.clamp(0., 1.);
x * x * (3. - 2. * x)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum FabSize {
Small,
#[default]
Baseline,
Medium,
Large,
}
impl FabSize {
fn metrics(self) -> (f32, f32, f32) {
match self {
FabSize::Small => (40., f32::from(corner::M), 24.),
FabSize::Baseline => (56., f32::from(corner::L), 24.),
FabSize::Medium => (80., f32::from(corner::LI), 28.),
FabSize::Large => (96., f32::from(corner::XL), 36.),
}
}
}
#[derive(IntoElement)]
pub struct Fab {
id: ElementId,
icon: SharedString,
label: Option<SharedString>,
size: FabSize,
hover_radius: Option<f32>,
in_toolbar: Option<(Hsla, Hsla)>,
menu_look: Option<(Hsla, Hsla, bool, f32)>,
on_click: Option<ClickHandler>,
}
impl Fab {
pub fn new(id: impl Into<ElementId>, icon: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
icon: icon.into(),
label: None,
size: FabSize::default(),
hover_radius: None,
in_toolbar: None,
menu_look: None,
on_click: None,
}
}
pub fn size(mut self, size: FabSize) -> Self {
self.size = size;
self
}
pub fn small(self) -> Self {
self.size(FabSize::Small)
}
pub fn medium(self) -> Self {
self.size(FabSize::Medium)
}
pub fn large(self) -> Self {
self.size(FabSize::Large)
}
pub fn hover_radius(mut self, radius: f32) -> Self {
self.hover_radius = Some(radius);
self
}
pub(super) fn in_toolbar(mut self, container: Hsla, content: Hsla) -> Self {
self.in_toolbar = Some((container, content));
self
}
pub(super) fn menu_look(
mut self,
container: Hsla,
content: Hsla,
circle: bool,
turn: f32,
) -> Self {
self.menu_look = Some((container, content, circle, turn));
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.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 RenderOnce for Fab {
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 (hovered, pressed) = {
let st = state.read(cx);
(st.hovered, st.pressed)
};
let extended = self.label.is_some();
let (size, mut rest_radius, icon_size) = self.size.metrics();
if let Some((_, _, true, _)) = self.menu_look {
rest_radius = size / 2.;
}
let compact = extended && self.size == FabSize::Baseline;
let height = match (extended, self.size) {
_ if compact => 42.,
(true, FabSize::Small) => 56.,
_ => size,
};
let target = match (pressed, hovered, extended) {
(true, _, _) => rest_radius * 0.75,
(false, true, _) => {
self.hover_radius
.unwrap_or(if extended { rest_radius } else { size / 2. })
}
_ => rest_radius,
};
let radius = px(animate_f32(
key(&self.id, "radius"),
Spring::SPATIAL_FAST,
target,
window,
cx,
));
let scale = animate_f32(
key(&self.id, "scale"),
Spring::SPATIAL_FAST,
if pressed { 0.95 } else { 1. },
window,
cx,
);
let (container, content) = match self.menu_look {
Some((container, content, _, _)) => (container, content),
None => self
.in_toolbar
.unwrap_or((s.primary_container, s.on_primary_container)),
};
let icon_turn = self.menu_look.map_or(0., |look| look.3);
let layer = state_layer(&self.id, &state, all(radius), content, window, cx);
let shadow = gpui::BoxShadow {
color: s.shadow,
offset: gpui::point(px(0.), px(2.)),
blur_radius: px(6.),
spread_radius: px(0.),
};
let shadow_far = gpui::BoxShadow {
color: alpha(s.shadow, 0.8),
offset: gpui::point(px(0.), px(8.)),
blur_radius: px(24.),
spread_radius: px(0.),
};
let mut face = div()
.relative()
.flex()
.items_center()
.justify_center()
.gap(px(match self.size {
FabSize::Small | FabSize::Baseline => 10.,
FabSize::Medium => 12.,
FabSize::Large => 16.,
}))
.h(px(height * scale))
.corners(all(radius))
.bg(container)
.text_color(content)
.when(self.in_toolbar.is_none(), |d| {
d.shadow(vec![shadow, shadow_far])
})
.child(layer)
.child(
Icon::new(self.icon)
.size(px(if compact { 19. } else { icon_size }))
.color(content)
.rotate(icon_turn),
);
face = if let Some(label) = self.label {
let (pad, text) = match self.size {
FabSize::Baseline => (0., px(13.5)),
FabSize::Small => (16., TITLE_MEDIUM.size),
FabSize::Medium => (26., TITLE_LARGE.size),
FabSize::Large => (28., HEADLINE_SMALL.size),
};
let face = if compact {
face.pl(px(13.)).pr(px(16.))
} else {
face.px(px(pad))
};
face.font_family(font_family())
.text_size(text)
.font_weight(FontWeight(600.))
.child(label)
} else {
face.w(px(height * scale))
};
let el = div()
.id(self.id.clone())
.flex()
.flex_none()
.items_center()
.justify_center()
.h(px(height))
.when(!extended, |d| d.w(px(height)))
.child(face);
let mut el = interactive(el, &state, true, true);
if let Some(f) = self.on_click {
el = el.on_click(move |e, w, cx| f(e, w, cx));
}
el
}
}
#[derive(IntoElement)]
pub struct FavStar {
id: ElementId,
on: bool,
hover_group: Option<SharedString>,
on_toggle: Option<ChangeHandler<bool>>,
}
impl FavStar {
pub fn new(id: impl Into<ElementId>, on: bool) -> Self {
Self {
id: id.into(),
on,
hover_group: None,
on_toggle: None,
}
}
pub fn hover_group(mut self, name: impl Into<SharedString>) -> Self {
self.hover_group = Some(name.into());
self
}
pub fn on_toggle(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_toggle = Some(Rc::new(f));
self
}
}
const STAR_BURST: [(f32, f32); 5] = [(0., -13.), (12., -4.), (8., 11.), (-8., 11.), (-12., -4.)];
impl RenderOnce for FavStar {
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 hovered = state.read(cx).hovered;
let radius = px(animate_f32(
key(&self.id, "radius"),
Spring::SPATIAL_FAST,
if hovered { 8. } else { 11. },
window,
cx,
));
let flip = replay(key(&self.id, "flip"), self.on as u64, 0.62, window, cx);
let (scale, turn) = match (flip, self.on) {
(Some(t), true) => {
let p = Spring::SPATIAL_SLOW.step(t);
(0.4 + 0.6 * p, -144f32.to_radians() * (1. - p))
}
(Some(t), false) if t < 0.3 => {
let phase = t / 0.3;
let w = if phase < 0.4 {
phase / 0.4
} else {
(1. - phase) / 0.6
};
(1. - 0.25 * w, 36f32.to_radians() * w)
}
_ => (1., 0.),
};
let color = if self.on {
s.primary
} else if hovered {
s.on_surface
} else {
s.outline
};
let color = animate_color(key(&self.id, "color"), Spring::EFFECTS, color, window, cx);
let burst = flip
.filter(|t| self.on && *t < 0.52)
.map(|t| cubic_bezier(0.2, 0., 0., 1.)(t / 0.52));
let mut el = div()
.id(self.id.clone())
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(px(22.))
.corners(all(radius))
.when(hovered, |d| d.bg(s.surface_container_high))
.child(
Icon::new(if self.on { "star-fill" } else { "star" })
.size(px(15.))
.color(color)
.scale(scale)
.rotate(turn),
);
if let Some(p) = burst {
for (x, y) in STAR_BURST {
el = el.child(
div()
.absolute()
.left(px(11. - 1.5 + x * p))
.top(px(11. - 1.5 + y * p))
.size(px(3.))
.rounded(px(1.5))
.bg(alpha(s.primary, 1. - p)),
);
}
}
if let (Some(group), false) = (self.hover_group, self.on) {
el = el.invisible().group_hover(group, |style| style.visible());
}
let mut el = interactive(el, &state, true, false);
if let Some(f) = self.on_toggle {
let on = self.on;
el = el.on_click(move |_, w, cx| {
cx.stop_propagation();
f(!on, w, cx)
});
}
el
}
}