use super::parts::{self, IconButton, rem};
use crate::{
components::form,
density,
icons::CarbonIcon,
theme::{LayerLevel, color},
tokens::{ColorRole, FontFamilyKey},
};
use gpui_kit::{
App, ClickEvent, ClipboardItem, ElementId, Hsla, InteractiveElement, IntoElement,
ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window,
component::tooltip::Tooltip, div, prelude::FluentBuilder as _,
};
use std::rc::Rc;
pub const COPIED_MS: u64 = 1500;
pub fn masked(value: &str) -> String {
let chars: Vec<char> = value.chars().collect();
let head: String = chars.iter().take(4).collect();
let tail: String = chars[chars.len().saturating_sub(4)..].iter().collect();
format!("{head}…{tail}")
}
pub fn box_look(level: LayerLevel) -> (Hsla, Hsla) {
(color(ColorRole::Layer02), color(level.border_strong()))
}
type ClickHandler = Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct CopyField {
id: ElementId,
label: Option<SharedString>,
value: SharedString,
mono: bool,
masked: bool,
dashed: bool,
level: LayerLevel,
on_open: Option<ClickHandler>,
open_label: SharedString,
copy_label: Option<SharedString>,
copied_label: SharedString,
}
impl CopyField {
pub fn new(id: impl Into<ElementId>, value: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: None,
value: value.into(),
mono: true,
masked: false,
dashed: false,
level: LayerLevel::One,
on_open: None,
open_label: "Open".into(),
copy_label: None,
copied_label: "Copied".into(),
}
}
pub fn level(mut self, level: LayerLevel) -> Self {
self.level = level;
self
}
pub fn dashed(mut self, dashed: bool) -> Self {
self.dashed = dashed;
self
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn mono(mut self, mono: bool) -> Self {
self.mono = mono;
self
}
pub fn masked(mut self, masked: bool) -> Self {
self.masked = masked;
self
}
pub fn on_open(mut self, f: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static) -> Self {
self.on_open = Some(Rc::new(f));
self
}
pub fn button_labels(
mut self,
open: impl Into<SharedString>,
copy: impl Into<SharedString>,
copied: impl Into<SharedString>,
) -> Self {
self.open_label = open.into();
self.copy_label = Some(copy.into());
self.copied_label = copied.into();
self
}
}
impl RenderOnce for CopyField {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let flash = parts::flash_state((id.clone(), "flash"), window, cx);
let done = parts::flash_on(&flash, COPIED_MS, cx);
let field_h = density::current().field().px_value();
let shown: SharedString = if self.masked {
masked(&self.value).into()
} else {
self.value.clone()
};
let copy_label = if done {
self.copied_label.clone()
} else {
self.copy_label.clone().unwrap_or_else(|| {
format!("Copy {}", self.label.as_deref().unwrap_or("value")).into()
})
};
let title = (!self.masked).then(|| self.value.clone());
let value = div()
.id((id.clone(), "value"))
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.text_color(color(ColorRole::TextPrimary))
.map(|d| {
if self.mono {
d.font(crate::typography::font(FontFamilyKey::Mono, 400))
.text_size(rem(12.))
.line_height(rem(16.))
} else {
d.text_size(rem(14.)).line_height(rem(18.))
}
})
.when_some(title, |d, t| {
d.tooltip(move |window, cx| Tooltip::new(t.clone()).build(window, cx))
})
.child(shown);
let text = self.value.clone();
let copy = IconButton::new(
(id.clone(), "copy"),
if done {
CarbonIcon::Checkmark
} else {
CarbonIcon::Copy
},
copy_label,
)
.on_click(move |_, _, cx| {
cx.write_to_clipboard(ClipboardItem::new_string(text.to_string()));
parts::flash(&flash, COPIED_MS, cx);
});
let open = self.on_open.clone().map(|f| {
IconButton::new(
(id.clone(), "open"),
CarbonIcon::Launch,
self.open_label.clone(),
)
.on_click(move |e, w, cx| f(e, w, cx))
});
let (fill, border) = box_look(self.level);
let body = div()
.relative()
.flex()
.items_center()
.min_h(rem(field_h))
.pl(rem(12.))
.bg(fill)
.when(self.dashed, |d| {
d.border_1().border_dashed().border_color(border)
})
.child(value)
.children(open)
.child(copy)
.when(!self.dashed, |d| d.child(form::bottom_rule(border)));
parts::field()
.when_some(self.label.clone(), |f, l| f.child(parts::label(l)))
.child(body)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn masking_keeps_four_each_side() {
assert_eq!(masked("sk-live-123456"), "sk-l…3456");
assert_eq!(masked("abc"), "abc…abc");
}
#[test]
fn layer_levels_select_contextual_tokens() {
crate::theme::tests_set(CarbonTheme::White);
let c = |r| color_in(CarbonTheme::White, r);
assert_eq!(
box_look(LayerLevel::One),
(c(ColorRole::Layer02), c(ColorRole::BorderStrong01))
);
assert_eq!(
box_look(LayerLevel::Two),
(c(ColorRole::Layer02), c(ColorRole::BorderStrong02))
);
assert_eq!(c(ColorRole::Layer02), gpui_kit::rgb(0xffffff).into());
assert_eq!(
color_in(CarbonTheme::G100, ColorRole::Layer02),
gpui_kit::rgb(0x393939).into()
);
}
}