use crate::components::{
Button, ButtonSize, ButtonVariant,
menu::{self, MenuItem, MenuSize},
overlay::{self, OpenChange},
};
use crate::{
icons::{CarbonIcon, Icon},
motion,
tokens::{REM_PX, TypeStyle},
};
use gpui_kit::{
AnyElement, App, Bounds, ClickEvent, ElementId, IntoElement, ParentElement, Pixels, RenderOnce,
SharedString, Styled, TestSupportExt as _, Window, canvas, div, px, rems,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ComboButtonSize {
Sm,
Md,
#[default]
Lg,
}
impl ComboButtonSize {
pub const ALL: [Self; 3] = [Self::Sm, Self::Md, Self::Lg];
pub const fn name(self) -> &'static str {
match self {
Self::Sm => "sm",
Self::Md => "md",
Self::Lg => "lg",
}
}
pub const fn button(self) -> ButtonSize {
match self {
Self::Sm => ButtonSize::Sm,
Self::Md => ButtonSize::Md,
Self::Lg => ButtonSize::Lg,
}
}
pub const fn menu(self) -> MenuSize {
match self {
Self::Sm => MenuSize::Sm,
Self::Md => MenuSize::Md,
Self::Lg => MenuSize::Lg,
}
}
}
pub const fn primary_min_width(size: ComboButtonSize) -> f32 {
match size {
ComboButtonSize::Sm => 7.9375,
ComboButtonSize::Md => 7.4375,
ComboButtonSize::Lg => 6.9375,
}
}
pub const PRIMARY_MAX_WIDTH: f32 = 14.9375;
pub const GAP: f32 = 1.;
type ClickHandler = Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct ComboButton {
id: ElementId,
label: SharedString,
menu_label: SharedString,
variant: ButtonVariant,
size: ComboButtonSize,
compact: bool,
label_style: Option<TypeStyle>,
leading: Vec<AnyElement>,
disabled: bool,
open: Option<bool>,
on_open_change: Option<OpenChange>,
on_click: Option<ClickHandler>,
items: Vec<MenuItem>,
}
impl ComboButton {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
menu_label: "Additional actions".into(),
variant: ButtonVariant::Primary,
size: ComboButtonSize::Lg,
compact: false,
label_style: None,
leading: Vec::new(),
disabled: false,
open: None,
on_open_change: None,
on_click: None,
items: Vec::new(),
}
}
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn compact(mut self, compact: bool) -> Self {
self.compact = compact;
self
}
pub fn label_style(mut self, style: TypeStyle) -> Self {
self.label_style = Some(style);
self
}
pub fn leading(mut self, element: impl IntoElement) -> Self {
self.leading.push(element.into_any_element());
self
}
pub fn menu_label(mut self, label: impl Into<SharedString>) -> Self {
self.menu_label = label.into();
self
}
pub fn size(mut self, size: ComboButtonSize) -> Self {
self.size = size;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn open(mut self, open: bool) -> Self {
self.open = Some(open);
self
}
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
}
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 item(mut self, item: MenuItem) -> Self {
self.items.push(item);
self
}
}
impl RenderOnce for ComboButton {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let openness = overlay::openness(&id, self.open, false, self.on_open_change, window, cx);
let open = openness.open && !self.disabled;
let set_open = openness.setter();
let size = self.size;
let bounds = window.use_keyed_state((id.clone(), "bounds"), cx, |_, _| {
Bounds::<Pixels>::default()
});
let width_rem =
f32::from(bounds.read(cx).size.width) / (f32::from(window.rem_size()) / REM_PX);
let rotation = motion::chevron_rotation((id.clone(), "chevron"), open, window, cx);
let mut action = Button::new((id.clone(), "action"), self.label)
.variant(self.variant)
.size(size.button())
.disabled(self.disabled)
.full_width(!self.compact);
if let Some(style) = self.label_style {
action = action.label_style(style);
}
for el in self.leading {
action = action.leading(el);
}
if let Some(f) = self.on_click {
action = action.on_click(move |e, window, cx| f(e, window, cx));
}
let chevron = Button::new((id.clone(), "trigger"), self.menu_label.clone())
.variant(self.variant)
.size(size.button())
.icon(Icon::carbon(CarbonIcon::ChevronDown).rotate(gpui_kit::radians(rotation)))
.icon_only(true)
.selected(open && self.variant == ButtonVariant::Ghost)
.tooltip(!open)
.disabled(self.disabled);
let trigger = if self.disabled {
div().child(chevron).into_any_element()
} else {
let set_open = set_open.clone();
overlay::trigger_wrapper(
(id.clone(), "trigger-wrap"),
chevron.into_any_element(),
Rc::new(move |window, cx| set_open(!open, window, cx)),
)
.test_support()
.into_any_element()
};
let measure = canvas(
move |b, window, cx| {
let size_only = Bounds::new(gpui_kit::Point::default(), b.size);
crate::measure::store(&bounds, size_only, window, cx);
},
|_, _, _, _| {},
)
.absolute()
.top_0()
.left_0()
.size_full();
let gap = if self.variant == ButtonVariant::Ghost {
0.
} else {
GAP
};
let mut action_wrap = div()
.max_w(rems(PRIMARY_MAX_WIDTH))
.overflow_hidden()
.child(action);
if !self.compact {
action_wrap = action_wrap.min_w(rems(primary_min_width(size)));
}
let container = div()
.relative()
.flex()
.flex_row()
.items_center()
.gap(px(gap))
.child(action_wrap)
.child(trigger)
.child(measure)
.into_any_element();
let menu = menu::anchored_button_menu(
&id,
container,
menu::MenuSpec {
items: self.items,
size: size.menu(),
label: self.menu_label,
open,
set_open,
min_width: None,
on_unhandled_key: None,
},
width_rem,
window,
cx,
);
div().flex().flex_row().child(menu)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn widths_match_the_css() {
assert_eq!(primary_min_width(ComboButtonSize::Sm) * REM_PX, 127.);
assert_eq!(primary_min_width(ComboButtonSize::Md) * REM_PX, 119.);
assert_eq!(primary_min_width(ComboButtonSize::Lg) * REM_PX, 111.);
assert_eq!(PRIMARY_MAX_WIDTH * REM_PX, 239.);
assert_eq!(GAP, 1.);
}
#[test]
fn sizes_map_to_button_and_menu() {
assert_eq!(ComboButtonSize::default(), ComboButtonSize::Lg);
assert_eq!(ComboButtonSize::Sm.button(), ButtonSize::Sm);
assert_eq!(ComboButtonSize::Md.menu(), MenuSize::Md);
assert_eq!(
ComboButtonSize::ALL.map(ComboButtonSize::name),
["sm", "md", "lg"]
);
}
}