use std::rc::Rc;
use gpui::{
AnyElement, App, ElementId, Entity, FocusHandle, InteractiveElement, IntoElement,
ParentElement, Pixels, RenderOnce, Role, SharedString, StatefulInteractiveElement, Styled,
Window, div, prelude::FluentBuilder, px,
};
use gpui_material_components::{
ButtonBorder, ButtonColors, ButtonElevations, ButtonPadding, ButtonShapes, ButtonVariant,
ControlSize, Icon, IconName, Menu, MenuItem, SemanticTone,
button_composition::{ButtonPart, Segment, label_width, split_trailing_size},
};
use gpui_material_foundation::{InteractionSource, current_material_theme};
use gpui_material_theme::Direction;
type Command = Rc<dyn Fn(&mut Window, &mut App)>;
type Change = Rc<dyn Fn(bool, &mut Window, &mut App)>;
enum Leading {
Label,
IconAndLabel(AnyElement),
Icon(AnyElement),
Content(AnyElement),
}
enum Trailing {
Arrow,
Icon(AnyElement),
Content(AnyElement),
}
enum TrailingAction {
Menu(Vec<MenuItem>),
Invoke(Command),
Toggle { selected: bool, change: Change },
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SplitButtonSegmentStyle {
pub shapes: ButtonShapes,
pub colors: ButtonColors,
pub border: Option<ButtonBorder>,
pub elevations: ButtonElevations,
pub padding: Option<ButtonPadding>,
}
#[derive(Default)]
struct Part {
style: SplitButtonSegmentStyle,
source: Option<Entity<InteractionSource>>,
}
impl Part {
fn apply(self, button: ButtonPart) -> ButtonPart {
button
.shapes(self.style.shapes)
.colors(self.style.colors)
.elevations(self.style.elevations)
.when_some(self.style.border, ButtonPart::border)
.when_some(self.style.padding, ButtonPart::content_padding)
.when_some(self.source.as_ref(), ButtonPart::interaction_source)
}
}
#[derive(IntoElement)]
pub struct SplitButton {
id: ElementId,
leading_name: SharedString,
trailing_name: SharedString,
leading_content: Leading,
trailing_content: Trailing,
leading_style: Part,
trailing_style: Part,
trailing_action: TrailingAction,
size: ControlSize,
variant: ButtonVariant,
leading_tone: SemanticTone,
trailing_tone: SemanticTone,
spacing: Pixels,
disabled: bool,
trailing_disabled: bool,
on_activate: Option<Command>,
focus: Option<(FocusHandle, FocusHandle)>,
}
impl SplitButton {
pub fn new(
id: impl Into<ElementId>,
leading_name: impl Into<SharedString>,
trailing_name: impl Into<SharedString>,
menu: impl IntoIterator<Item = MenuItem>,
) -> Self {
Self {
id: id.into(),
leading_name: leading_name.into(),
trailing_name: trailing_name.into(),
leading_content: Leading::Label,
trailing_content: Trailing::Arrow,
leading_style: Part::default(),
trailing_style: Part::default(),
trailing_action: TrailingAction::Menu(menu.into_iter().collect()),
size: ControlSize::default(),
variant: ButtonVariant::default(),
leading_tone: SemanticTone::Default,
trailing_tone: SemanticTone::Default,
spacing: px(2.),
disabled: false,
trailing_disabled: false,
on_activate: None,
focus: None,
}
}
pub fn size(mut self, size: ControlSize) -> Self {
self.size = size;
self
}
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
self
}
pub fn tone(mut self, tone: SemanticTone) -> Self {
self.leading_tone = tone;
self.trailing_tone = tone;
self
}
pub fn leading_tone(mut self, tone: SemanticTone) -> Self {
self.leading_tone = tone;
self
}
pub fn trailing_tone(mut self, tone: SemanticTone) -> Self {
self.trailing_tone = tone;
self
}
pub fn spacing(mut self, spacing: Pixels) -> Self {
self.spacing = if f32::from(spacing).is_finite() {
spacing.max(px(0.))
} else {
px(2.)
};
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn menu_disabled(self, disabled: bool) -> Self {
self.trailing_disabled(disabled)
}
pub fn trailing_disabled(mut self, disabled: bool) -> Self {
self.trailing_disabled = disabled;
self
}
pub fn on_activate(mut self, callback: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_activate = Some(Rc::new(callback));
self
}
pub fn on_trailing_activate(
mut self,
callback: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
self.trailing_action = TrailingAction::Invoke(Rc::new(callback));
self
}
pub fn trailing_toggle(
mut self,
selected: bool,
callback: impl Fn(bool, &mut Window, &mut App) + 'static,
) -> Self {
self.trailing_action = TrailingAction::Toggle {
selected,
change: Rc::new(callback),
};
self
}
pub fn leading_icon(mut self, icon: impl IntoElement) -> Self {
self.leading_content = Leading::IconAndLabel(icon.into_any_element());
self
}
pub fn leading_icon_only(mut self, icon: impl IntoElement) -> Self {
self.leading_content = Leading::Icon(icon.into_any_element());
self
}
pub fn leading_content(mut self, content: impl IntoElement) -> Self {
self.leading_content = Leading::Content(content.into_any_element());
self
}
pub fn trailing_icon(mut self, icon: impl IntoElement) -> Self {
self.trailing_content = Trailing::Icon(icon.into_any_element());
self
}
pub fn trailing_content(mut self, content: impl IntoElement) -> Self {
self.trailing_content = Trailing::Content(content.into_any_element());
self
}
pub fn leading_style(mut self, style: SplitButtonSegmentStyle) -> Self {
self.leading_style.style = style;
self
}
pub fn trailing_style(mut self, style: SplitButtonSegmentStyle) -> Self {
self.trailing_style.style = style;
self
}
pub fn leading_interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
self.leading_style.source = Some(source.clone());
self
}
pub fn trailing_interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
self.trailing_style.source = Some(source.clone());
self
}
pub fn track_focus(mut self, primary: &FocusHandle, trailing: &FocusHandle) -> Self {
self.focus = Some((primary.clone(), trailing.clone()));
self
}
}
impl RenderOnce for SplitButton {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let direction = current_material_theme(window, cx).layout.direction;
let (primary_focus, trailing_focus) = self.focus.unwrap_or_else(|| {
window
.use_keyed_state((self.id.clone(), "split-focus"), cx, |_, cx| {
(cx.focus_handle(), cx.focus_handle())
})
.read(cx)
.clone()
});
let primary_id = ElementId::from((self.id.clone(), "primary"));
let primary = if matches!(self.leading_content, Leading::Label)
&& self.leading_style.style.padding.is_none()
{
let width = label_width(self.size, self.leading_name.clone(), window, cx);
ButtonPart::new(primary_id, self.leading_name, width)
} else {
ButtonPart::intrinsic(primary_id, self.leading_name)
};
let primary = match self.leading_content {
Leading::Label => primary,
Leading::IconAndLabel(icon) => primary.leading_icon(icon),
Leading::Icon(icon) => primary.icon(icon),
Leading::Content(content) => primary.content(content),
};
let primary = self
.leading_style
.apply(primary)
.size(self.size)
.variant(self.variant)
.tone(self.leading_tone)
.segment(Some(Segment::SplitLeading))
.track_focus(&primary_focus)
.disabled(self.disabled)
.when_some(self.on_activate, |button, action| {
button.on_activate(move |window, cx| action(window, cx))
});
let trailing_id = ElementId::from((self.id.clone(), "secondary"));
let restore = trailing_focus.clone();
let trailing = move |active| {
let part = if matches!(self.trailing_content, Trailing::Content(_))
|| self.trailing_style.style.padding.is_some()
{
ButtonPart::intrinsic(trailing_id, self.trailing_name)
} else {
ButtonPart::new(
trailing_id,
self.trailing_name,
px(split_trailing_size(self.size).1),
)
};
let part = match self.trailing_content {
Trailing::Arrow => part.icon(Icon::new(IconName::ArrowDropDown).rotate(
gpui::Radians(if active { std::f32::consts::PI } else { 0. }),
)),
Trailing::Icon(icon) => part.icon(icon),
Trailing::Content(content) => part.content(content),
};
self.trailing_style
.apply(part)
.size(self.size)
.variant(self.variant)
.tone(self.trailing_tone)
.segment(Some(Segment::SplitTrailing))
.track_focus(&trailing_focus)
.disabled(self.trailing_disabled)
};
let trailing = match self.trailing_action {
TrailingAction::Menu(items) if self.trailing_disabled || items.is_empty() => {
trailing(false).disabled(true).into_any_element()
}
TrailingAction::Menu(items) => {
Menu::composed((self.id.clone(), "menu"), items, move |open, toggle| {
trailing(open)
.presentation_selected(open)
.on_accessible_activate(move |window, cx| {
restore.focus(window, cx);
toggle(window, cx);
})
.into_any_element()
})
.into_any_element()
}
TrailingAction::Invoke(command) => trailing(false)
.on_activate(move |window, cx| command(window, cx))
.into_any_element(),
TrailingAction::Toggle { selected, change } => trailing(selected)
.selected(Some(selected))
.on_change(move |next, window, cx| change(next, window, cx))
.into_any_element(),
};
div()
.id(self.id)
.role(Role::Group)
.flex()
.items_center()
.min_w_0()
.max_w_full()
.gap(self.spacing)
.when(direction == Direction::Rtl, Styled::flex_row_reverse)
.child(primary)
.child(trailing)
}
}