use gpui::{div, prelude::*, AnyElement, App, SharedString, StyleRefinement, Styled, Window};
use super::direction::{is_rtl, DirectionalStyled};
use crate::sx::SxStyled;
use crate::{styles, styles::ApplyStyleOverrides};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ButtonGroupOrientation {
#[default]
Horizontal,
Vertical,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum GroupPosition {
Only,
First,
Middle,
Last,
}
type GroupItem = Box<dyn FnOnce(GroupPosition, ButtonGroupOrientation) -> AnyElement>;
#[derive(IntoElement)]
pub struct ButtonGroup {
items: Vec<GroupItem>,
orientation: ButtonGroupOrientation,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(ButtonGroup);
impl ButtonGroup {
#[must_use]
pub fn new() -> Self {
Self {
items: Vec::new(),
orientation: ButtonGroupOrientation::Horizontal,
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
#[must_use]
pub fn orientation(mut self, orientation: ButtonGroupOrientation) -> Self {
self.orientation = orientation;
self
}
#[must_use]
pub fn vertical(self) -> Self {
self.orientation(ButtonGroupOrientation::Vertical)
}
#[must_use]
pub fn item<Control: IntoElement + Styled + 'static>(mut self, control: Control) -> Self {
self.items.push(Box::new(move |position, orientation| {
let control = match (orientation, position) {
(_, GroupPosition::Only) => control,
(_, GroupPosition::Middle) => control.rounded_none(),
(ButtonGroupOrientation::Horizontal, GroupPosition::First) => {
control.rounded_e_none()
}
(ButtonGroupOrientation::Horizontal, GroupPosition::Last) => {
control.rounded_s_none()
}
(ButtonGroupOrientation::Vertical, GroupPosition::First) => {
control.rounded_b_none()
}
(ButtonGroupOrientation::Vertical, GroupPosition::Last) => control.rounded_t_none(),
};
let control = match (orientation, position) {
(_, GroupPosition::Only | GroupPosition::First) => control,
(ButtonGroupOrientation::Horizontal, _) if is_rtl() => control.border_r_0(),
(ButtonGroupOrientation::Horizontal, _) => control.border_l_0(),
(ButtonGroupOrientation::Vertical, _) => control.border_t_0(),
};
control.into_any_element()
}));
self
}
#[must_use]
pub fn text(self, text: impl Into<SharedString>) -> Self {
self.item(ButtonGroupText::new(text))
}
}
impl Default for ButtonGroup {
fn default() -> Self {
Self::new()
}
}
styles! {
BUTTON_GROUP = {
group: { display: flex, align: stretch },
orientation(ButtonGroupOrientation): {
Horizontal: {},
Vertical: { direction: column },
},
text: {
display: flex,
align: center,
padding_x: 4,
radius: md,
border: 1,
border_color: input,
background: muted,
text: sm,
font: medium,
},
}
}
impl RenderOnce for ButtonGroup {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
let count = self.items.len();
let orientation = self.orientation;
let children = self.items.into_iter().enumerate().map(|(index, item)| {
let position = match (index, count) {
(_, 1) => GroupPosition::Only,
(0, _) => GroupPosition::First,
(index, count) if index + 1 == count => GroupPosition::Last,
_ => GroupPosition::Middle,
};
item(position, orientation)
});
div()
.sx((
&BUTTON_GROUP.group,
BUTTON_GROUP.orientation(orientation),
&self.sx,
))
.children(children)
.apply_style_overrides(&self.style_overrides)
}
}
#[derive(IntoElement)]
pub struct ButtonGroupText {
text: SharedString,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(ButtonGroupText);
impl ButtonGroupText {
pub fn new(text: impl Into<SharedString>) -> Self {
Self {
text: text.into(),
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
}
impl RenderOnce for ButtonGroupText {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
div()
.sx((&BUTTON_GROUP.text, &self.sx))
.child(crate::components::bidi_text::text(self.text))
.apply_style_overrides(&self.style_overrides)
}
}