use crate::components::kbd::Kbd;
use crate::t3::base::{
REDUCED_PULSE_PERIOD, Spin, endless_phase, fmt_elapsed, loop_phase, reduced_pulse_opacity,
use_ticker,
};
use crate::t3::press::{self, rem};
use crate::{
icons::CarbonIcon,
motion::{self, Curve, Motion, Speed},
theme::color,
tokens::{ColorRole, FontFamilyKey},
typography::font,
};
use gpui_kit::{
Animation, AnimationExt as _, AnyElement, App, ElementId, Hsla, IntoElement, ParentElement,
RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, StyledText, TextRun, Window,
div, prelude::FluentBuilder as _,
};
use std::time::Duration;
pub const SHIMMER_PERIOD: Duration = Duration::from_millis(2400);
pub fn shimmer_intensity(x: f32, t: f32) -> f32 {
let center = -0.5 + 3. * t.rem_euclid(1.);
(1. - (x - center).abs() / 0.3).max(0.)
}
fn mix(a: Hsla, b: Hsla, k: f32) -> Hsla {
let (a, b) = (a.to_rgb(), b.to_rgb());
gpui_kit::Rgba {
r: a.r + (b.r - a.r) * k,
g: a.g + (b.g - a.g) * k,
b: a.b + (b.b - a.b) * k,
a: a.a + (b.a - a.a) * k,
}
.into()
}
pub fn reduced_shimmer_opacity(elapsed: Duration) -> f32 {
reduced_pulse_opacity(loop_phase(elapsed, REDUCED_PULSE_PERIOD))
}
pub(crate) fn shimmer(
id: impl Into<ElementId>,
text: SharedString,
weight: u16,
window: &mut Window,
cx: &App,
) -> AnyElement {
let secondary = color(ColorRole::TextSecondary);
if motion::reduced_motion(cx) {
let opacity = reduced_pulse_opacity(endless_phase(REDUCED_PULSE_PERIOD, window));
return div()
.text_color(secondary)
.opacity(opacity)
.child(text)
.into_any_element();
}
let primary = color(ColorRole::TextPrimary);
let chars: Vec<(usize, char)> = text.char_indices().collect();
let n = chars.len().max(1) as f32;
div()
.with_animation(
id,
Animation::new(SHIMMER_PERIOD).repeat(),
move |this, t| {
let runs = chars
.iter()
.enumerate()
.map(|(i, (_, c))| TextRun {
len: c.len_utf8(),
font: font(FontFamilyKey::Sans, weight),
color: mix(
secondary,
primary,
shimmer_intensity((i as f32 + 0.5) / n, t),
),
background_color: None,
underline: None,
strikethrough: None,
})
.collect::<Vec<_>>();
this.child(StyledText::new(text.clone()).with_runs(runs))
},
)
.into_any_element()
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[allow(missing_docs)]
pub enum ToolKind {
#[default]
Read,
Edit,
Write,
Command,
Search,
Web,
Browser,
Mcp,
Image,
Todo,
Think,
Agent,
Other,
}
impl ToolKind {
pub const ALL: [ToolKind; 13] = [
Self::Read,
Self::Edit,
Self::Write,
Self::Command,
Self::Search,
Self::Web,
Self::Browser,
Self::Mcp,
Self::Image,
Self::Todo,
Self::Think,
Self::Agent,
Self::Other,
];
pub const fn name(self) -> &'static str {
match self {
Self::Read => "read",
Self::Edit => "edit",
Self::Write => "write",
Self::Command => "command",
Self::Search => "search",
Self::Web => "web",
Self::Browser => "browser",
Self::Mcp => "mcp",
Self::Image => "image",
Self::Todo => "todo",
Self::Think => "think",
Self::Agent => "agent",
Self::Other => "other",
}
}
pub const fn icon(self) -> CarbonIcon {
match self {
Self::Read => CarbonIcon::Document,
Self::Edit => CarbonIcon::Edit,
Self::Write => CarbonIcon::DocumentAdd,
Self::Command => CarbonIcon::Terminal,
Self::Search => CarbonIcon::Search,
Self::Web | Self::Browser => CarbonIcon::Earth,
Self::Mcp => CarbonIcon::Plug,
Self::Image => CarbonIcon::Image,
Self::Todo => CarbonIcon::ListChecked,
Self::Think => CarbonIcon::Idea,
Self::Agent => CarbonIcon::Bot,
Self::Other => CarbonIcon::Function,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ToolStatus {
#[default]
Done,
Running,
Failed,
}
#[derive(IntoElement)]
pub struct ToolActivityIcon {
id: ElementId,
kind: ToolKind,
status: ToolStatus,
running_label: SharedString,
}
impl ToolActivityIcon {
pub fn new(id: impl Into<ElementId>, kind: ToolKind, status: ToolStatus) -> Self {
Self {
id: id.into(),
kind,
status,
running_label: "Running".into(),
}
}
pub fn running_label(mut self, label: impl Into<SharedString>) -> Self {
self.running_label = label.into();
self
}
}
impl RenderOnce for ToolActivityIcon {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div().flex().flex_none().w(rem(16.)).justify_center().child(
if self.status == ToolStatus::Running {
Spin::new(self.id)
.label(self.running_label)
.into_any_element()
} else {
press::icon(self.kind.icon(), 16., color(ColorRole::IconSecondary))
.into_any_element()
},
)
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct WorkEntry {
pub kind: ToolKind,
pub verb: SharedString,
pub target: SharedString,
pub stat: Option<SharedString>,
pub status: ToolStatus,
pub detail: Option<SharedString>,
}
impl WorkEntry {
pub fn new(
kind: ToolKind,
verb: impl Into<SharedString>,
target: impl Into<SharedString>,
) -> Self {
Self {
kind,
verb: verb.into(),
target: target.into(),
..Default::default()
}
}
pub fn stat(mut self, stat: impl Into<SharedString>) -> Self {
self.stat = Some(stat.into());
self
}
pub fn status(mut self, status: ToolStatus) -> Self {
self.status = status;
self
}
pub fn detail(mut self, detail: impl Into<SharedString>) -> Self {
self.detail = Some(detail.into());
self
}
}
#[derive(Clone)]
pub struct WorkLogTexts {
pub worked: SharedString,
pub working: SharedString,
pub steps: std::rc::Rc<dyn Fn(usize) -> SharedString>,
pub failed: SharedString,
}
impl Default for WorkLogTexts {
fn default() -> Self {
Self {
worked: "Worked for ".into(),
working: "Working for ".into(),
steps: std::rc::Rc::new(|n| format!("{n} steps").into()),
failed: "Failed".into(),
}
}
}
#[derive(IntoElement)]
pub struct WorkLog {
id: ElementId,
entries: Vec<WorkEntry>,
elapsed: u64,
default_open: bool,
live: bool,
interactive: bool,
texts: WorkLogTexts,
}
impl WorkLog {
pub fn new(id: impl Into<ElementId>, entries: Vec<WorkEntry>, elapsed: u64) -> Self {
Self {
id: id.into(),
entries,
elapsed,
default_open: false,
live: false,
interactive: true,
texts: WorkLogTexts::default(),
}
}
pub fn interactive(mut self, interactive: bool) -> Self {
self.interactive = interactive;
self
}
pub fn default_open(mut self, open: bool) -> Self {
self.default_open = open;
self
}
pub fn live(mut self, live: bool) -> Self {
self.live = live;
self
}
pub fn texts(mut self, texts: WorkLogTexts) -> Self {
self.texts = texts;
self
}
}
pub const CHEVRON_MOTION: Motion =
Motion::new(Curve::ProductiveStandard, Speed::Fast02).spatial(true);
impl RenderOnce for WorkLog {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let open_state = window.use_keyed_state((id.clone(), "open"), cx, {
let open = self.default_open;
move |_, _| open
});
let open = *open_state.read(cx);
let detail_state = window.use_keyed_state((id.clone(), "detail"), cx, |_, _| None::<usize>);
let detail = *detail_state.read(cx);
let interactive = self.interactive;
let (focus, ring) = press::focus((id.clone(), "toggle-focus"), window, cx);
let (raw_hovered, hover) = press::hover((id.clone(), "toggle-hover"), window, cx);
let hovered = interactive && raw_hovered;
let angle = motion::animate(
(id.clone(), "chev"),
if open {
std::f32::consts::FRAC_PI_2
} else {
0.
},
CHEVRON_MOTION,
window,
cx,
);
let texts = self.texts.clone();
let ink = color(if hovered {
ColorRole::TextPrimary
} else {
ColorRole::TextSecondary
});
let toggle_state = open_state.clone();
let toggle = gpui_kit::base::Button::new((id.clone(), "toggle"))
.when(interactive, |b| b.track_focus(&focus))
.relative()
.flex()
.flex_row()
.items_center()
.justify_start()
.self_start()
.gap(rem(4.))
.pt(rem(4.))
.pb(rem(4.))
.pr(rem(4.))
.text_color(ink)
.when(interactive, |b| {
b.cursor_pointer()
.on_hover(press::track_hover(hover))
.on_click(move |_, _, cx| {
toggle_state.update(cx, |o, cx| {
*o = !*o;
cx.notify();
})
})
.child(
press::icon(
CarbonIcon::ChevronRight,
16.,
color(ColorRole::IconSecondary),
)
.rotate(gpui_kit::radians(angle)),
)
})
.child(SharedString::from(format!(
"{}{}",
if self.live {
&texts.working
} else {
&texts.worked
},
fmt_elapsed(self.elapsed)
)))
.child(
div()
.ml(rem(8.))
.text_size(rem(12.))
.text_color(color(ColorRole::TextHelper))
.child((texts.steps)(self.entries.len())),
)
.when(interactive && ring, |this| this.child(press::ring()));
let list = open.then(|| {
let rows: Vec<AnyElement> = self
.entries
.into_iter()
.enumerate()
.map(|(i, entry)| {
let key = |p: &str| -> ElementId {
(id.clone(), SharedString::from(format!("{p}-{i}"))).into()
};
let (row_hovered, row_hover) = press::hover(key("hover"), window, cx);
let (row_focus, row_ring) = press::focus(key("focus"), window, cx);
let failed = entry.status == ToolStatus::Failed;
let row_ink = color(if row_hovered {
ColorRole::TextPrimary
} else {
ColorRole::TextSecondary
});
let detail_state = detail_state.clone();
let open_detail = detail == Some(i) && entry.detail.is_some();
let row = gpui_kit::base::Button::new(key("row"))
.track_focus(&row_focus)
.relative()
.flex()
.flex_row()
.items_center()
.justify_start()
.w_full()
.gap(rem(8.))
.min_h(rem(32.))
.px(rem(8.))
.text_color(row_ink)
.cursor_pointer()
.when(row_hovered, |this| this.bg(color(ColorRole::LayerHover01)))
.on_hover(press::track_hover(row_hover))
.on_click(move |_, _, cx| {
detail_state.update(cx, |d, cx| {
*d = if *d == Some(i) { None } else { Some(i) };
cx.notify();
})
})
.child(ToolActivityIcon::new(key("tool"), entry.kind, entry.status))
.child(
div()
.flex_none()
.whitespace_nowrap()
.text_color(color(if failed {
ColorRole::TextError
} else {
ColorRole::TextPrimary
}))
.child(entry.verb.clone()),
)
.child(
div()
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.font(font(FontFamilyKey::Mono, 400))
.text_size(rem(12.))
.child(entry.target.clone()),
)
.when_some(entry.stat.clone(), |this, s| {
this.child(
div()
.flex_none()
.whitespace_nowrap()
.text_size(rem(12.))
.child(s),
)
})
.when(failed, |this| {
this.child(press::icon(
CarbonIcon::ErrorFilled,
16.,
color(ColorRole::SupportError),
))
})
.when(row_ring, |this| this.child(press::ring()));
div()
.flex()
.flex_col()
.child(row)
.when(open_detail, |this| {
this.child(
div()
.mb(rem(8.))
.ml(rem(40.))
.py(rem(8.))
.px(rem(12.))
.bg(color(ColorRole::Layer01))
.font(font(FontFamilyKey::Mono, 400))
.text_size(rem(12.))
.line_height(rem(16.))
.text_color(color(ColorRole::TextSecondary))
.children(entry.detail.clone()),
)
})
.into_any_element()
})
.collect();
div()
.flex()
.flex_col()
.mt(rem(4.))
.ml(rem(8.))
.pl(rem(12.))
.border_l_1()
.border_color(color(ColorRole::BorderSubtle01))
.children(rows)
});
div()
.flex()
.flex_col()
.w_full()
.min_w_0()
.text_size(rem(14.))
.line_height(rem(18.))
.child(toggle)
.children(list)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum WorkingState {
#[default]
Working,
Thinking,
Compacting,
Setup,
Background,
}
impl WorkingState {
pub const ALL: [WorkingState; 5] = [
Self::Working,
Self::Thinking,
Self::Compacting,
Self::Setup,
Self::Background,
];
pub const fn name(self) -> &'static str {
match self {
Self::Working => "working",
Self::Thinking => "thinking",
Self::Compacting => "compacting",
Self::Setup => "setup",
Self::Background => "background",
}
}
pub const fn default_label(self) -> &'static str {
match self {
Self::Working => "Working",
Self::Thinking => "Thinking",
Self::Compacting => "Compacting",
Self::Setup => "Setting up worktree…",
Self::Background => "Background setup",
}
}
}
#[derive(IntoElement)]
pub struct WorkingIndicator {
id: ElementId,
state: WorkingState,
seconds: u64,
label: Option<SharedString>,
key: SharedString,
hint: SharedString,
}
impl WorkingIndicator {
pub fn new(id: impl Into<ElementId>, state: WorkingState, seconds: u64) -> Self {
Self {
id: id.into(),
state,
seconds,
label: None,
key: "Esc".into(),
hint: "to interrupt".into(),
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn hint(mut self, key: impl Into<SharedString>, hint: impl Into<SharedString>) -> Self {
self.key = key.into();
self.hint = hint.into();
self
}
}
impl RenderOnce for WorkingIndicator {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let t = use_ticker((id.clone(), "ticker"), self.seconds, true, window, cx);
let text = self
.label
.clone()
.unwrap_or_else(|| self.state.default_label().into());
let helper = color(ColorRole::TextHelper);
div()
.flex()
.flex_row()
.items_center()
.gap(rem(8.))
.text_size(rem(14.))
.line_height(rem(18.))
.child(shimmer((id.clone(), "shimmer"), text, 400, window, cx))
.child(
div()
.text_size(rem(12.))
.text_color(helper)
.child(SharedString::from(fmt_elapsed(t))),
)
.child(Kbd::new(self.key))
.child(
div()
.text_size(rem(12.))
.text_color(helper)
.child(self.hint),
)
.min_w_0()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_shimmer_pulses_under_reduced_motion() {
assert!((reduced_shimmer_opacity(Duration::ZERO) - 1.).abs() < 1e-4);
assert!((reduced_shimmer_opacity(Duration::from_millis(700)) - 0.35).abs() < 1e-4);
assert!((reduced_shimmer_opacity(Duration::from_millis(1400)) - 1.).abs() < 1e-4);
let quarter = reduced_shimmer_opacity(Duration::from_millis(350));
assert!(quarter > 0.35 && quarter < 1.);
}
#[test]
fn the_shimmer_peak_sweeps_left_to_right() {
assert_eq!(shimmer_intensity(0., 0.), 0.);
assert!((shimmer_intensity(0., 1. / 6.) - 1.).abs() < 1e-4);
assert!((shimmer_intensity(1., 0.5) - 1.).abs() < 1e-4);
assert_eq!(shimmer_intensity(0., 0.5), 0.);
}
#[test]
fn tool_kinds_map_to_reference_icons() {
assert_eq!(ToolKind::Command.icon(), CarbonIcon::Terminal);
assert_eq!(ToolKind::Browser.icon(), CarbonIcon::Earth);
assert_eq!(ToolKind::Other.icon(), CarbonIcon::Function);
assert_eq!(WorkingState::Setup.default_label(), "Setting up worktree…");
}
}