use crate::{
components::{Button, ButtonSize, ButtonVariant, Switch, SwitchSize},
motion::{self, Curve, Motion, Speed},
t3::{
IdentitySize, ProviderIcon,
craft::{rem, sans},
},
theme::color,
tokens::{self, ColorRole},
u0::{rows as row, rows::StateTone, tasks::EnabledState},
};
use gpui_kit::{
AnyElement, App, ElementId, Hsla, InteractiveElement as _, IntoElement, ParentElement,
RenderOnce, SharedString, Styled, TestSupportExt as _, Window, div,
prelude::FluentBuilder as _, px, relative,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AgentState {
#[default]
Installed,
Installing,
Updating,
Missing,
}
impl AgentState {
pub const ALL: [Self; 4] = [
Self::Installed,
Self::Installing,
Self::Updating,
Self::Missing,
];
pub const fn name(self) -> &'static str {
match self {
Self::Installed => "installed",
Self::Installing => "installing",
Self::Updating => "updating",
Self::Missing => "missing",
}
}
pub const fn in_progress(self) -> bool {
matches!(self, Self::Installing | Self::Updating)
}
}
#[derive(Clone)]
pub struct AgentRowTexts {
pub installing: SharedString,
pub updating: SharedString,
pub not_installed: SharedString,
pub signed_in: SharedString,
pub accounts: Rc<dyn Fn(usize) -> SharedString>,
pub not_signed_in: SharedString,
pub available: Rc<dyn Fn(&str) -> SharedString>,
pub install: SharedString,
pub update: SharedString,
pub sign_in: SharedString,
pub cancel: SharedString,
pub enabled: Rc<dyn Fn(&str) -> SharedString>,
}
impl Default for AgentRowTexts {
fn default() -> Self {
Self {
installing: "Installing".into(),
updating: "Updating".into(),
not_installed: "Not installed".into(),
signed_in: "Signed in".into(),
accounts: Rc::new(|n| format!(" · {n} account{}", if n > 1 { "s" } else { "" }).into()),
not_signed_in: "Not signed in".into(),
available: Rc::new(|v| format!("· {v} available").into()),
install: "Install".into(),
update: "Update".into(),
sign_in: "Sign in".into(),
cancel: "Cancel".into(),
enabled: Rc::new(|name| format!("{name} enabled").into()),
}
}
}
pub const BAR_WIDTH: f32 = 128.;
pub const MIN_TEXT_WIDTH: f32 = 56.;
pub const BAR_HEIGHT: f32 = 4.;
pub const BAR_MOTION: Motion = Motion::new(Curve::ProductiveStandard, Speed::Moderate02);
pub fn progress_bar(
id: impl Into<ElementId>,
progress: f32,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let target = (progress / 100.).clamp(0., 1.);
let fraction = motion::animate(
(id.into(), "g-bar"),
target,
BAR_MOTION.spatial(true),
window,
cx,
);
div()
.flex_shrink(1.)
.min_w_0()
.w(rem(BAR_WIDTH))
.h(px(BAR_HEIGHT))
.bg(color(ColorRole::LayerAccent01))
.child(
div()
.h_full()
.w(relative(fraction))
.bg(color(ColorRole::Interactive)),
)
.into_any_element()
}
pub fn name_ink(on: bool) -> Hsla {
color(if on {
ColorRole::TextPrimary
} else {
ColorRole::TextSecondary
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AgentButton {
Install,
Update,
SignIn,
Cancel,
}
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
type ToggleHandler = Rc<dyn Fn(bool, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct AgentRow {
id: ElementId,
name: SharedString,
version: Option<SharedString>,
state: AgentState,
progress: f32,
enabled: bool,
signed_in: bool,
accounts: Vec<SharedString>,
latest: Option<SharedString>,
accent: Option<u8>,
mark: Option<AnyElement>,
texts: AgentRowTexts,
on_install: Option<Handler>,
on_update: Option<Handler>,
on_sign_in: Option<Handler>,
on_cancel: Option<Handler>,
on_toggle: Option<ToggleHandler>,
}
impl AgentRow {
pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
name: name.into(),
version: None,
state: AgentState::Installed,
progress: 0.,
enabled: true,
signed_in: false,
accounts: Vec::new(),
latest: None,
accent: None,
mark: None,
texts: AgentRowTexts::default(),
on_install: None,
on_update: None,
on_sign_in: None,
on_cancel: None,
on_toggle: None,
}
}
pub fn mark(mut self, mark: impl IntoElement) -> Self {
self.mark = Some(mark.into_any_element());
self
}
pub fn version(mut self, version: impl Into<SharedString>) -> Self {
self.version = Some(version.into());
self
}
pub fn state(mut self, state: AgentState) -> Self {
self.state = state;
self
}
pub fn progress(mut self, progress: f32) -> Self {
self.progress = progress;
self
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn signed_in(mut self, signed_in: bool) -> Self {
self.signed_in = signed_in;
self
}
pub fn accounts(mut self, accounts: impl IntoIterator<Item = impl Into<SharedString>>) -> Self {
self.accounts = accounts.into_iter().map(Into::into).collect();
self
}
pub fn latest(mut self, latest: impl Into<SharedString>) -> Self {
self.latest = Some(latest.into());
self
}
pub fn accent(mut self, accent: u8) -> Self {
self.accent = Some(accent);
self
}
pub fn texts(mut self, texts: AgentRowTexts) -> Self {
self.texts = texts;
self
}
pub fn on_install(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_install = Some(Rc::new(f));
self
}
pub fn on_update(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_update = Some(Rc::new(f));
self
}
pub fn on_sign_in(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_sign_in = Some(Rc::new(f));
self
}
pub fn on_cancel(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_cancel = Some(Rc::new(f));
self
}
fn cancels(&self) -> bool {
self.state.in_progress() && self.on_cancel.is_some()
}
pub fn on_toggle(mut self, f: impl Fn(bool, &mut Window, &mut App) + 'static) -> Self {
self.on_toggle = Some(Rc::new(f));
self
}
pub fn has_update(&self) -> bool {
self.latest
.as_ref()
.is_some_and(|l| Some(l) != self.version.as_ref())
}
pub fn buttons(&self) -> Vec<AgentButton> {
let mut out = Vec::new();
if self.state == AgentState::Missing {
out.push(AgentButton::Install);
} else if self.has_update() && self.state == AgentState::Installed {
out.push(AgentButton::Update);
}
if !self.signed_in && self.state == AgentState::Installed {
out.push(AgentButton::SignIn);
}
if self.cancels() {
out.push(AgentButton::Cancel);
}
out
}
fn sub_line(&self) -> Vec<AnyElement> {
let mut runs = Vec::new();
if self.signed_in {
let mut t = self.texts.signed_in.to_string();
if !self.accounts.is_empty() {
t.push_str(&(self.texts.accounts)(self.accounts.len()));
}
runs.push(row::state_text_wrapping(StateTone::Ok, t).into_any_element());
} else {
runs.push(
row::state_text_wrapping(StateTone::Warn, self.texts.not_signed_in.clone())
.into_any_element(),
);
}
if self.has_update()
&& let Some(latest) = &self.latest
{
runs.push(
row::dim((self.texts.available)(latest), 12.)
.min_w_0()
.whitespace_normal()
.into_any_element(),
);
}
runs
}
}
pub fn status_min_width(status: &str, window: &Window) -> gpui_kit::Pixels {
crate::typography::longest_word_width(status, 12., tokens::FontFamilyKey::Sans, 400, window)
}
impl RenderOnce for AgentRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let enabled = self.enabled;
let store = window.use_keyed_state((id.clone(), "enabled"), cx, |_, _| {
EnabledState::new(enabled)
});
let on = store.update(cx, |s, _| {
s.sync(enabled);
s.on()
});
let mut title = row::title(name_ink(on)).child(
row::title_text(self.name.clone())
.flex_shrink_0()
.max_w_full(),
);
if let Some(v) = &self.version {
title = title.child(
row::mono_text(v.clone())
.min_w_0()
.overflow_hidden()
.text_color(color(ColorRole::TextSecondary)),
);
}
let sub = sans(div(), 12., 400)
.flex()
.flex_wrap()
.items_center()
.gap(tokens::SPACING_02)
.min_w_0()
.text_color(color(ColorRole::TextSecondary));
let mut text_min = rem(MIN_TEXT_WIDTH).to_pixels(window.rem_size());
let sub = if self.state.in_progress() {
let word = if self.state == AgentState::Installing {
self.texts.installing.clone()
} else {
self.texts.updating.clone()
};
let status = format!("{word} {}%", self.progress.round() as i64);
let status_min = status_min_width(&status, window);
text_min = text_min
.max(status_min + rem(tokens::SPACING_03.px_value()).to_pixels(window.rem_size()));
sub.child(
div()
.flex()
.items_center()
.gap(tokens::SPACING_03)
.min_w_0()
.child(progress_bar(id.clone(), self.progress, window, cx))
.child(
div()
.id((id.clone(), "status"))
.test_support()
.flex_shrink(1.)
.min_w(status_min)
.child(status),
),
)
} else if self.state == AgentState::Missing {
sub.child(self.texts.not_installed.clone())
} else {
sub.children(self.sub_line())
};
let buttons = self.buttons();
let texts = self.texts.clone();
let (on_install, on_update, on_sign_in, on_cancel) = (
self.on_install.clone(),
self.on_update.clone(),
self.on_sign_in.clone(),
self.on_cancel.clone(),
);
let on_toggle = self.on_toggle.clone();
let switch_label = (self.texts.enabled)(&self.name);
let switched = self.state != AgentState::Missing && !self.cancels();
row::frame()
.flex_wrap()
.justify_end()
.child(match self.mark {
Some(mark) => div()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(IdentitySize::Md.px()))
.child(mark)
.into_any_element(),
None => ProviderIcon::new(self.name.clone())
.id((id.clone(), "mark"))
.size(IdentitySize::Md)
.accent(self.accent)
.into_any_element(),
})
.child(
div()
.flex()
.flex_col()
.flex_grow(1.)
.flex_shrink(1.)
.flex_basis(px(0.))
.min_w(text_min)
.overflow_hidden()
.gap(px(2.))
.child(title)
.child(sub),
)
.children(buttons.into_iter().map(|b| {
let (label, variant, handler, key) = match b {
AgentButton::Install => (
texts.install.clone(),
ButtonVariant::Tertiary,
on_install.clone(),
"install",
),
AgentButton::Update => (
texts.update.clone(),
ButtonVariant::Ghost,
on_update.clone(),
"update",
),
AgentButton::SignIn => (
texts.sign_in.clone(),
ButtonVariant::Ghost,
on_sign_in.clone(),
"sign-in",
),
AgentButton::Cancel => (
texts.cancel.clone(),
ButtonVariant::Ghost,
on_cancel.clone(),
"cancel",
),
};
row::tail(
Button::new((id.clone(), key), label)
.variant(variant)
.size(ButtonSize::Sm)
.when_some(handler, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
)
}))
.when(switched, |this| {
this.child(row::tail(
Switch::new((id.clone(), "toggle"))
.size(SwitchSize::Sm)
.label(switch_label)
.hide_label(true)
.checked(on)
.on_checked_change(move |v, w, cx| {
store.update(cx, |s, cx| {
s.set(v);
cx.notify();
});
if let Some(f) = &on_toggle {
f(v, w, cx);
}
}),
))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::CarbonTheme;
#[test]
fn buttons_follow_the_state() {
let r = AgentRow::new("a", "Cursor").state(AgentState::Missing);
assert_eq!(r.buttons(), vec![AgentButton::Install]);
let r = AgentRow::new("a", "Codex")
.version("0.9.2")
.latest("0.10.0")
.signed_in(true);
assert_eq!(r.buttons(), vec![AgentButton::Update]);
let r = AgentRow::new("a", "Aider").version("0.62");
assert_eq!(r.buttons(), vec![AgentButton::SignIn]);
let r = AgentRow::new("a", "Gemini")
.state(AgentState::Installing)
.progress(64.);
assert!(r.buttons().is_empty());
let r = AgentRow::new("a", "Gemini")
.state(AgentState::Installing)
.on_cancel(|_, _| {});
assert_eq!(r.buttons(), vec![AgentButton::Cancel]);
let r = AgentRow::new("a", "Gemini")
.signed_in(true)
.on_cancel(|_, _| {});
assert!(r.buttons().is_empty());
let r = AgentRow::new("a", "Claude")
.version("2.1.0")
.latest("2.1.0")
.signed_in(true);
assert!(!r.has_update());
assert!(r.buttons().is_empty());
}
#[test]
fn account_count_is_pluralised() {
let t = AgentRowTexts::default();
assert_eq!((t.accounts)(1), SharedString::from(" · 1 account"));
assert_eq!((t.accounts)(2), SharedString::from(" · 2 accounts"));
assert_eq!(
(t.available)("0.10.0"),
SharedString::from("· 0.10.0 available")
);
}
#[test]
fn bar_geometry_and_motion_follow_g_bar() {
assert_eq!(BAR_WIDTH, 128.);
assert_eq!(BAR_HEIGHT, 4.);
assert_eq!(BAR_MOTION.speed, Speed::Moderate02);
assert_eq!(BAR_MOTION.curve, Curve::ProductiveStandard);
}
#[test]
fn switched_off_greys_the_name() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(name_ink(false), color(ColorRole::TextSecondary));
assert_eq!(name_ink(true), color(ColorRole::TextPrimary));
}
}