use super::row::{self, Handler, ghost_button, icon_button};
use crate::{icons::CarbonIcon, t3::press::icon, theme::color, tokens::ColorRole};
use gpui_kit::{
AnyElement, App, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce,
SharedString, StatefulInteractiveElement as _, Styled as _, Window, div,
prelude::FluentBuilder as _,
};
use std::rc::Rc;
const LEADING_ICON: f32 = 20.;
#[derive(Clone)]
pub struct SettingsAction {
pub label: SharedString,
pub icon: Option<CarbonIcon>,
on_click: Option<Handler>,
}
impl SettingsAction {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
icon: None,
on_click: None,
}
}
pub fn icon(label: impl Into<SharedString>, glyph: CarbonIcon) -> Self {
Self {
icon: Some(glyph),
..Self::new(label)
}
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
pub fn kind(&self) -> ActionKind {
if self.icon.is_some() {
ActionKind::IconButton
} else {
ActionKind::TextButton
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ActionKind {
IconButton,
TextButton,
}
#[derive(IntoElement)]
pub struct SettingsListRow {
id: ElementId,
icon: Option<CarbonIcon>,
leading: Option<AnyElement>,
title: SharedString,
subtitle: Option<SharedString>,
status: Option<AnyElement>,
actions: Vec<SettingsAction>,
on_click: Option<Handler>,
}
impl SettingsListRow {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
icon: None,
leading: None,
title: title.into(),
subtitle: None,
status: None,
actions: Vec::new(),
on_click: None,
}
}
pub fn icon(mut self, glyph: CarbonIcon) -> Self {
self.icon = Some(glyph);
self
}
pub fn leading(mut self, el: impl IntoElement) -> Self {
self.leading = Some(el.into_any_element());
self
}
pub fn subtitle(mut self, s: impl Into<SharedString>) -> Self {
self.subtitle = Some(s.into());
self
}
pub fn status(mut self, el: impl IntoElement) -> Self {
self.status = Some(el.into_any_element());
self
}
pub fn action(mut self, action: SettingsAction) -> Self {
self.actions.push(action);
self
}
pub fn actions(mut self, actions: impl IntoIterator<Item = SettingsAction>) -> Self {
self.actions.extend(actions);
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl RenderOnce for SettingsListRow {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id;
let leading = self.leading.or_else(|| {
self.icon.map(|g| {
div()
.flex_none()
.child(icon(g, LEADING_ICON, color(ColorRole::IconSecondary)))
.into_any_element()
})
});
let actions = (!self.actions.is_empty()).then(|| {
div().flex().flex_none().items_center().children(
self.actions.into_iter().enumerate().map(|(i, a)| {
let aid: ElementId =
(id.clone(), SharedString::from(format!("act-{i}"))).into();
match a.icon {
Some(g) => icon_button(aid, a.label, g, a.on_click).into_any_element(),
None => ghost_button(aid, a.label, None, a.on_click).into_any_element(),
}
}),
)
});
let on_click = self.on_click;
row::frame()
.id(id.clone())
.when_some(on_click, |this, f| {
this.cursor_pointer().on_click(move |_, w, cx| f(w, cx))
})
.when_some(leading, |this, l| this.child(l))
.child(row::main(self.title, self.subtitle.map(row::subtitle)))
.when_some(self.status, |this, s| this.child(s))
.when_some(actions, |this, a| this.child(a))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn actions_with_an_icon_are_icon_buttons() {
assert_eq!(
SettingsAction::icon("Open", CarbonIcon::Launch).kind(),
ActionKind::IconButton
);
assert_eq!(SettingsAction::new("Open").kind(), ActionKind::TextButton);
}
#[test]
fn builders_keep_order() {
let r = SettingsListRow::new("r", "carbon-ui")
.subtitle("~/code/carbon-ui")
.action(SettingsAction::icon("Open", CarbonIcon::Launch))
.actions([SettingsAction::icon("Remove", CarbonIcon::TrashCan)]);
let labels: Vec<_> = r.actions.iter().map(|a| a.label.as_ref()).collect();
assert_eq!(labels, ["Open", "Remove"]);
assert_eq!(r.subtitle.as_deref(), Some("~/code/carbon-ui"));
}
}