use crate::{
icons::{CarbonIcon, Icon},
t3::craft::{rem, sans},
theme::color,
tokens::{self, ColorRole},
u0::primitives::parts::{focus_ring, focus_visible, keyed_focus},
};
use gpui_kit::{
AnyElement, App, CursorStyle, ElementId, Hsla, InteractiveElement as _, IntoElement,
ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, Window, div,
prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum NoteKind {
Stopped,
#[default]
Note,
Limit,
Error,
}
impl NoteKind {
pub const ALL: [Self; 4] = [Self::Stopped, Self::Note, Self::Limit, Self::Error];
pub const fn name(self) -> &'static str {
match self {
Self::Stopped => "stopped",
Self::Note => "note",
Self::Limit => "limit",
Self::Error => "error",
}
}
pub const fn glyph(self) -> CarbonIcon {
match self {
Self::Stopped => CarbonIcon::StopFilled,
Self::Note => CarbonIcon::InformationFilled,
Self::Limit => CarbonIcon::Hourglass,
Self::Error => CarbonIcon::ErrorFilled,
}
}
pub fn colors(self) -> (Hsla, Hsla) {
match self {
Self::Error => (color(ColorRole::TextError), color(ColorRole::SupportError)),
Self::Limit => (
color(ColorRole::TextSecondary),
color(ColorRole::SupportWarning),
),
Self::Stopped | Self::Note => (
color(ColorRole::TextSecondary),
color(ColorRole::IconSecondary),
),
}
}
}
#[derive(Clone)]
pub struct NoteLineTexts {
pub resumes_at: Rc<dyn Fn(&str) -> SharedString>,
}
impl Default for NoteLineTexts {
fn default() -> Self {
Self {
resumes_at: Rc::new(|t| format!("· resumes at {t}").into()),
}
}
}
#[derive(IntoElement)]
pub struct NoteLine {
kind: NoteKind,
text: SharedString,
resumes_at: Option<SharedString>,
action: Option<AnyElement>,
texts: NoteLineTexts,
}
impl NoteLine {
pub fn new(kind: NoteKind, text: impl Into<SharedString>) -> Self {
Self {
kind,
text: text.into(),
resumes_at: None,
action: None,
texts: NoteLineTexts::default(),
}
}
pub fn resumes_at(mut self, time: impl Into<SharedString>) -> Self {
self.resumes_at = Some(time.into());
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.action = Some(action.into_any_element());
self
}
pub fn texts(mut self, texts: NoteLineTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for NoteLine {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let (ink, glyph) = self.kind.colors();
sans(div(), 12., 400)
.flex()
.items_center()
.justify_center()
.gap(tokens::SPACING_03)
.py(tokens::SPACING_03)
.text_color(ink)
.child(
Icon::carbon(self.kind.glyph())
.size_length(rem(14.))
.color(glyph)
.flex_none(),
)
.child(self.text)
.when_some(self.resumes_at, |d, t| {
d.child(
div()
.text_color(color(ColorRole::TextSecondary))
.child((self.texts.resumes_at)(&t)),
)
})
.children(self.action)
}
}
pub fn restore_visible(marker_hovered: bool, focus_visible: bool) -> bool {
marker_hovered || focus_visible
}
pub fn restore_fill(hovered: bool) -> Hsla {
color(if hovered {
ColorRole::LayerHover01
} else {
ColorRole::Background
})
}
#[derive(Clone)]
pub struct CheckpointMarkerTexts {
pub restore: SharedString,
pub files: Rc<dyn Fn(usize) -> SharedString>,
}
impl Default for CheckpointMarkerTexts {
fn default() -> Self {
Self {
restore: "Restore to here".into(),
files: Rc::new(|n| format!(" · {n} files").into()),
}
}
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct CheckpointMarker {
id: ElementId,
label: SharedString,
time: Option<SharedString>,
files: Option<usize>,
on_restore: Option<Handler>,
texts: CheckpointMarkerTexts,
}
impl CheckpointMarker {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
time: None,
files: None,
on_restore: None,
texts: CheckpointMarkerTexts::default(),
}
}
pub fn time(mut self, time: impl Into<SharedString>) -> Self {
self.time = Some(time.into());
self
}
pub fn files(mut self, files: usize) -> Self {
self.files = Some(files);
self
}
pub fn on_restore(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_restore = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: CheckpointMarkerTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for CheckpointMarker {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let hover = |key: &'static str, window: &mut Window, cx: &mut App| {
window.use_keyed_state((id.clone(), key), cx, |_, _| false)
};
let (marker, button) = (hover("hover", window, cx), hover("btn-hover", window, cx));
let focus = keyed_focus((id.clone(), "focus"), window, cx);
let ring = focus_visible(&focus, window);
let shown = restore_visible(*marker.read(cx), ring);
let link = color(ColorRole::LinkPrimary);
let dim = color(ColorRole::TextSecondary);
let track = |store: gpui_kit::Entity<bool>| {
move |h: &bool, _: &mut Window, cx: &mut App| {
store.update(cx, |v, cx| {
if *v != *h {
*v = *h;
cx.notify();
}
})
}
};
let line = || {
div()
.flex_1()
.h(px(1.))
.bg(color(ColorRole::BorderSubtle01))
};
let on_restore = self.on_restore.clone();
let over = *button.read(cx);
let restore = sans(
gpui_kit::base::Button::new((id.clone(), "restore")),
12.,
400,
)
.track_focus(&focus)
.absolute()
.right_0()
.top(gpui_kit::relative(0.5))
.mt(rem(-12.))
.flex()
.items_center()
.gap(tokens::SPACING_02)
.h(rem(24.))
.px(tokens::SPACING_03)
.bg(restore_fill(over))
.text_color(link)
.opacity(if shown { 1. } else { 0. })
.cursor(CursorStyle::PointingHand)
.on_hover(track(button))
.on_click(move |_, w, cx| {
if let Some(f) = &on_restore {
f(w, cx);
}
})
.child(
Icon::carbon(CarbonIcon::Undo)
.size_length(rem(12.))
.color(link),
)
.child(self.texts.restore.clone())
.when(ring, |b| b.child(focus_ring()));
sans(div(), 12., 400)
.id(id)
.relative()
.flex()
.items_center()
.w_full()
.min_h(rem(24.))
.gap(tokens::SPACING_03)
.text_color(dim)
.on_hover(track(marker))
.child(line())
.child(
div()
.flex()
.flex_none()
.items_center()
.gap(tokens::SPACING_02)
.child(
Icon::carbon(CarbonIcon::Bookmark)
.size_length(rem(12.))
.color(dim),
)
.child(
div()
.flex()
.child(self.label.clone())
.when_some(self.time.clone(), |d, t| d.child(format!(" · {t}")))
.when_some(self.files, |d, n| d.child((self.texts.files)(n))),
),
)
.child(line())
.child(restore)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn notes_follow_the_css() {
crate::theme::tests_set(CarbonTheme::White);
let c = |r| color_in(CarbonTheme::White, r);
assert_eq!(
NoteKind::Error.colors(),
(c(ColorRole::TextError), c(ColorRole::SupportError))
);
assert_eq!(NoteKind::Limit.colors().1, c(ColorRole::SupportWarning));
assert_eq!(NoteKind::Note.colors().1, c(ColorRole::IconSecondary));
assert_eq!(NoteKind::Limit.glyph(), CarbonIcon::Hourglass);
assert_eq!(
(NoteLineTexts::default().resumes_at)("14:00"),
"· resumes at 14:00"
);
}
#[test]
fn restore_floats_on_hover_or_focus() {
assert!(!restore_visible(false, false));
assert!(restore_visible(true, false));
assert!(restore_visible(false, true));
crate::theme::tests_set(CarbonTheme::G100);
let c = |r| color_in(CarbonTheme::G100, r);
assert_eq!(restore_fill(false), c(ColorRole::Background));
assert_eq!(restore_fill(true), c(ColorRole::LayerHover01));
}
}