use super::row::{self, Handler, ghost_button, icon_button};
use crate::{
components::kbd::Kbd,
icons::CarbonIcon,
t3::craft::{mono, rem, sans},
theme::color,
tokens::{self, ColorRole},
};
use gpui_kit::{
App, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window, div,
prelude::FluentBuilder as _,
};
use std::rc::Rc;
const KIND_WIDTH: f32 = 56.;
const KIND_LINE: f32 = 20.;
const KBD_GAP: f32 = 2.;
pub fn kind_label(kind: &str) -> String {
kind.to_uppercase()
}
pub fn shortcut_keys(shortcut: &str) -> Vec<&str> {
shortcut.split(' ').filter(|k| !k.is_empty()).collect()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RunSlot {
Run,
Stop,
}
impl RunSlot {
pub fn of(running: bool) -> Self {
if running { Self::Stop } else { Self::Run }
}
}
#[derive(Clone)]
pub struct ScriptRowTexts {
pub run: Rc<dyn Fn(&str) -> SharedString>,
pub stop: SharedString,
pub edit: Rc<dyn Fn(&str) -> SharedString>,
}
impl Default for ScriptRowTexts {
fn default() -> Self {
Self {
run: Rc::new(|name| format!("Run {name}").into()),
stop: "Stop".into(),
edit: Rc::new(|name| format!("Edit {name}").into()),
}
}
}
#[derive(IntoElement)]
pub struct ScriptRow {
id: ElementId,
name: SharedString,
command: SharedString,
kind: SharedString,
shortcut: Option<SharedString>,
running: bool,
texts: ScriptRowTexts,
on_run: Option<Handler>,
on_stop: Option<Handler>,
on_edit: Option<Handler>,
}
impl ScriptRow {
pub fn new(
id: impl Into<ElementId>,
name: impl Into<SharedString>,
command: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
name: name.into(),
command: command.into(),
kind: "run".into(),
shortcut: None,
running: false,
texts: ScriptRowTexts::default(),
on_run: None,
on_stop: None,
on_edit: None,
}
}
pub fn kind(mut self, kind: impl Into<SharedString>) -> Self {
self.kind = kind.into();
self
}
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
pub fn running(mut self, running: bool) -> Self {
self.running = running;
self
}
pub fn texts(mut self, texts: ScriptRowTexts) -> Self {
self.texts = texts;
self
}
pub fn on_run(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_run = Some(Rc::new(f));
self
}
pub fn on_stop(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_stop = Some(Rc::new(f));
self
}
pub fn on_edit(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_edit = Some(Rc::new(f));
self
}
}
impl RenderOnce for ScriptRow {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id;
let kind = sans(div(), 11., 400)
.flex()
.flex_none()
.justify_center()
.items_center()
.w(rem(KIND_WIDTH))
.h(rem(KIND_LINE))
.px(tokens::SPACING_02)
.overflow_hidden()
.whitespace_nowrap()
.bg(color(ColorRole::LayerAccent01))
.text_color(color(ColorRole::TextSecondary))
.child(kind_label(&self.kind));
let command = mono(div(), 12., 400)
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.text_color(color(ColorRole::TextSecondary))
.child(self.command);
let keys = self.shortcut.as_ref().map(|s| {
div().flex().flex_none().gap(rem(KBD_GAP)).children(
shortcut_keys(s)
.into_iter()
.map(|k| Kbd::new(k.to_string())),
)
});
let run = match RunSlot::of(self.running) {
RunSlot::Stop => ghost_button(
(id.clone(), "stop"),
self.texts.stop.clone(),
Some(CarbonIcon::StopFilled),
self.on_stop,
)
.into_any_element(),
RunSlot::Run => icon_button(
(id.clone(), "run"),
(self.texts.run)(&self.name),
CarbonIcon::Play,
self.on_run,
)
.into_any_element(),
};
let edit = icon_button(
(id.clone(), "edit"),
(self.texts.edit)(&self.name),
CarbonIcon::Edit,
self.on_edit,
);
row::frame()
.child(kind)
.child(row::main(self.name, Some(command)))
.when_some(keys, |this, k| this.child(k))
.child(run)
.child(edit)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn kind_is_upper_cased() {
assert_eq!(kind_label("run"), "RUN");
assert_eq!(kind_label("setup"), "SETUP");
}
#[test]
fn shortcut_splits_on_spaces() {
assert_eq!(shortcut_keys("⌘ R"), ["⌘", "R"]);
assert_eq!(shortcut_keys("⌘ ⇧ T"), ["⌘", "⇧", "T"]);
assert!(shortcut_keys("").is_empty());
}
#[test]
fn running_swaps_run_for_stop() {
assert_eq!(RunSlot::of(true), RunSlot::Stop);
assert_eq!(RunSlot::of(false), RunSlot::Run);
let t = ScriptRowTexts::default();
assert_eq!((t.run)("Test").as_ref(), "Run Test");
assert_eq!((t.edit)("Test").as_ref(), "Edit Test");
}
#[test]
fn chip_matches_g_script_kind() {
assert_eq!(KIND_WIDTH, 3.5 * tokens::REM_PX);
assert_eq!(KIND_LINE, 1.25 * tokens::REM_PX);
assert_eq!(tokens::SPACING_02.px_value(), 4.);
}
}