use super::parts::{card_sub, child_id, icon_button};
use crate::{
components::{Badge, BadgeSize, BadgeVariant, Button, ButtonSize, ButtonVariant},
icons::CarbonIcon,
t3::{
craft::{BODY_LINE, mono, sans, sized},
press::{self, Handler, rem},
shell::{
ConnectionStatus, ConnectionStatusDot, EnvironmentMachineIcon, IdentitySize,
MachineKind, ProviderIcon,
},
},
theme::{color, color_in},
tokens::{CarbonTheme, ColorRole},
};
use gpui_kit::{
AnyElement, App, Bounds, ElementId, Hsla, InteractiveElement as _, IntoElement, ParentElement,
Pixels, RenderOnce, SharedString, Styled, Window, canvas, div, prelude::FluentBuilder as _, px,
relative,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ProviderStatus {
#[default]
Ready,
Signin,
Error,
Updating,
}
impl ProviderStatus {
pub const ALL: [ProviderStatus; 4] = [Self::Ready, Self::Signin, Self::Error, Self::Updating];
pub const fn name(self) -> &'static str {
match self {
Self::Ready => "ready",
Self::Signin => "signin",
Self::Error => "error",
Self::Updating => "updating",
}
}
pub const fn label(self) -> &'static str {
match self {
Self::Ready => "Ready",
Self::Signin => "Sign in required",
Self::Error => "Not found",
Self::Updating => "Update available",
}
}
pub const fn variant(self) -> BadgeVariant {
match self {
Self::Ready => BadgeVariant::Green,
Self::Signin => BadgeVariant::WarmGray,
Self::Error => BadgeVariant::Red,
Self::Updating => BadgeVariant::Blue,
}
}
pub fn colors(self) -> Option<(Hsla, Hsla)> {
(self == Self::Signin).then(|| {
(
color_in(CarbonTheme::White, ColorRole::NotificationBackgroundWarning),
crate::t3::base::ink(),
)
})
}
}
pub fn provider_subtitle(name: &str, provider: &str, version: Option<&str>) -> String {
let mut s = String::new();
if provider != name {
s.push_str(provider);
if version.is_some() {
s.push_str(" \u{b7} ");
}
}
if let Some(v) = version {
s.push('v');
s.push_str(v);
}
s
}
#[derive(Clone)]
pub struct ProviderCardTexts {
pub status: Option<SharedString>,
pub more: SharedString,
pub account: SharedString,
pub models: SharedString,
pub available: Rc<dyn Fn(usize) -> SharedString>,
pub endpoint: SharedString,
pub sign_in_run: (SharedString, SharedString),
pub sign_in: SharedString,
pub run: SharedString,
pub signed_in: SharedString,
pub not_found: SharedString,
pub set_path: SharedString,
}
impl Default for ProviderCardTexts {
fn default() -> Self {
Self {
status: None,
more: "More".into(),
account: "Authenticated as".into(),
models: "Models".into(),
available: Rc::new(|n| format!("{n} available").into()),
endpoint: "Endpoint".into(),
sign_in_run: ("Sign in by running ".into(), ".".into()),
sign_in: "Sign in with the agent\u{2019}s login command.".into(),
run: "Run in terminal".into(),
signed_in: "I\u{2019}ve signed in".into(),
not_found: "The CLI was not found on PATH.".into(),
set_path: "Set path\u{2026}".into(),
}
}
}
#[derive(IntoElement)]
pub struct ProviderInstanceCard {
id: ElementId,
name: SharedString,
provider: SharedString,
status: ProviderStatus,
account: Option<SharedString>,
version: Option<SharedString>,
models: usize,
env: Option<SharedString>,
login_command: Option<SharedString>,
primary: bool,
texts: ProviderCardTexts,
on_more: Option<Handler>,
on_run: Option<Handler>,
on_signed_in: Option<Handler>,
on_set_path: Option<Handler>,
}
impl ProviderInstanceCard {
pub fn new(
id: impl Into<ElementId>,
name: impl Into<SharedString>,
provider: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
name: name.into(),
provider: provider.into(),
status: ProviderStatus::Ready,
account: None,
version: None,
models: 0,
env: None,
login_command: None,
primary: false,
texts: ProviderCardTexts::default(),
on_more: None,
on_run: None,
on_signed_in: None,
on_set_path: None,
}
}
pub fn status(mut self, status: ProviderStatus) -> Self {
self.status = status;
self
}
pub fn account(mut self, account: impl Into<SharedString>) -> Self {
self.account = Some(account.into());
self
}
pub fn version(mut self, version: impl Into<SharedString>) -> Self {
self.version = Some(version.into());
self
}
pub fn models(mut self, models: usize) -> Self {
self.models = models;
self
}
pub fn env(mut self, env: impl Into<SharedString>) -> Self {
self.env = Some(env.into());
self
}
pub fn login_command(mut self, command: impl Into<SharedString>) -> Self {
self.login_command = Some(command.into());
self
}
pub fn primary(mut self, primary: bool) -> Self {
self.primary = primary;
self
}
pub fn texts(mut self, texts: ProviderCardTexts) -> Self {
self.texts = texts;
self
}
pub fn on_more(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_more = Some(Rc::new(f));
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_signed_in(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_signed_in = Some(Rc::new(f));
self
}
pub fn on_set_path(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_set_path = Some(Rc::new(f));
self
}
}
fn click(b: Button, f: Option<Handler>) -> Button {
match f {
Some(f) => b.on_click(move |_, w, cx| f(w, cx)),
None => b,
}
}
fn key_values(pairs: Vec<(SharedString, SharedString)>) -> impl IntoElement {
let column = || div().flex().flex_col().gap(rem(4.));
sans(div(), 14., 400)
.flex()
.flex_row()
.gap(rem(16.))
.min_w_0()
.child(column().flex_none().children(pairs.iter().map(|(k, _)| {
div()
.text_color(color(ColorRole::TextSecondary))
.child(k.clone())
})))
.child(
column()
.flex_1()
.min_w_0()
.children(pairs.iter().map(|(_, v)| {
div()
.min_w_0()
.overflow_hidden()
.text_ellipsis()
.whitespace_nowrap()
.text_color(color(ColorRole::TextPrimary))
.child(v.clone())
})),
)
}
impl RenderOnce for ProviderInstanceCard {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id.clone();
let texts = self.texts.clone();
let status = self.status;
let subtitle = provider_subtitle(&self.name, &self.provider, self.version.as_deref());
let tag = {
let label = texts
.status
.clone()
.unwrap_or_else(|| status.label().into());
let badge = Badge::new(label)
.variant(status.variant())
.size(BadgeSize::Sm);
match status.colors() {
Some((bg, fg)) => badge.colors(bg, fg),
None => badge,
}
};
let head = div()
.flex()
.flex_row()
.items_center()
.gap(rem(12.))
.child(ProviderIcon::new(self.provider.clone()).size(IdentitySize::Lg))
.child(
div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.child(
sans(div(), 16., 600)
.text_color(color(ColorRole::TextPrimary))
.child(self.name.clone()),
)
.when(!subtitle.is_empty(), |this| this.child(card_sub(subtitle))),
)
.child(tag)
.child(click(
icon_button(
(id.clone(), "more"),
texts.more.clone(),
CarbonIcon::OverflowMenuVertical,
),
self.on_more.clone(),
));
let mut pairs = Vec::new();
if let Some(a) = self.account.clone() {
pairs.push((texts.account.clone(), a));
}
pairs.push((texts.models.clone(), (texts.available)(self.models)));
if let Some(e) = self.env.clone() {
pairs.push((texts.endpoint.clone(), e));
}
let strip = |bg: ColorRole| {
sans(div(), 14., 400)
.flex()
.flex_row()
.flex_wrap()
.items_center()
.gap(rem(8.))
.p(rem(12.))
.bg(color(bg))
.text_color(color(ColorRole::TextPrimary))
};
let diagnostic = match status {
ProviderStatus::Signin => {
let message = match self.login_command.clone() {
Some(cmd) => div()
.flex()
.flex_row()
.flex_wrap()
.items_baseline()
.child(texts.sign_in_run.0.clone())
.child(mono(div(), 14., 400).whitespace_nowrap().child(cmd))
.child(texts.sign_in_run.1.clone()),
None => div().child(texts.sign_in.clone()),
};
Some(
strip(ColorRole::NotificationBackgroundWarning)
.child(crate::t3::base::warning_glyph(16.))
.child(message.flex_1().min_w_0())
.child(
div()
.flex()
.flex_row()
.flex_none()
.ml_auto()
.gap(rem(8.))
.child(click(
Button::new((id.clone(), "run"), texts.run.clone())
.size(ButtonSize::Sm)
.variant(if self.primary {
ButtonVariant::Primary
} else {
ButtonVariant::Tertiary
}),
self.on_run.clone(),
))
.child(click(
Button::new((id.clone(), "signed-in"), texts.signed_in.clone())
.size(ButtonSize::Sm)
.variant(ButtonVariant::Ghost),
self.on_signed_in.clone(),
)),
),
)
}
ProviderStatus::Error => Some(
strip(ColorRole::NotificationBackgroundError)
.child(press::icon(
CarbonIcon::ErrorFilled,
16.,
color(ColorRole::SupportError),
))
.child(div().flex_1().min_w_0().child(texts.not_found.clone()))
.child(click(
Button::new((id.clone(), "set-path"), texts.set_path.clone())
.size(ButtonSize::Sm)
.variant(ButtonVariant::Ghost),
self.on_set_path.clone(),
)),
),
_ => None,
};
div()
.id(id)
.flex()
.flex_col()
.gap(rem(12.))
.p(rem(16.))
.bg(color(ColorRole::Layer01))
.child(head)
.child(key_values(pairs))
.children(diagnostic)
}
}
#[derive(Clone, Debug)]
pub struct EnvironmentRowTexts {
pub current: SharedString,
pub options: SharedString,
}
impl Default for EnvironmentRowTexts {
fn default() -> Self {
Self {
current: "This computer".into(),
options: "Options".into(),
}
}
}
#[derive(IntoElement)]
pub struct EnvironmentRow {
id: ElementId,
name: SharedString,
kind: MachineKind,
transport: SharedString,
status: ConnectionStatus,
detail: Option<SharedString>,
current: bool,
actions: Option<AnyElement>,
texts: EnvironmentRowTexts,
on_options: Option<Handler>,
}
impl EnvironmentRow {
pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
name: name.into(),
kind: MachineKind::Laptop,
transport: "Local".into(),
status: ConnectionStatus::Connected,
detail: None,
current: false,
actions: None,
texts: EnvironmentRowTexts::default(),
on_options: None,
}
}
pub fn kind(mut self, kind: MachineKind) -> Self {
self.kind = kind;
self
}
pub fn transport(mut self, transport: impl Into<SharedString>) -> Self {
self.transport = transport.into();
self
}
pub fn status(mut self, status: ConnectionStatus) -> Self {
self.status = status;
self
}
pub fn detail(mut self, detail: impl Into<SharedString>) -> Self {
self.detail = Some(detail.into());
self
}
pub fn current(mut self, current: bool) -> Self {
self.current = current;
self
}
pub fn actions(mut self, actions: impl IntoElement) -> Self {
self.actions = Some(actions.into_any_element());
self
}
pub fn texts(mut self, texts: EnvironmentRowTexts) -> Self {
self.texts = texts;
self
}
pub fn on_options(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_options = Some(Rc::new(f));
self
}
}
impl RenderOnce for EnvironmentRow {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id.clone();
let sub = match &self.detail {
Some(d) => format!("{} \u{b7} {d}", self.transport),
None => self.transport.to_string(),
};
let actions = self.actions.unwrap_or_else(|| {
click(
icon_button(
(id.clone(), "options"),
self.texts.options.clone(),
CarbonIcon::OverflowMenuVertical,
),
self.on_options.clone(),
)
.into_any_element()
});
let surface = color(ColorRole::Layer01);
div()
.id(id.clone())
.flex()
.flex_row()
.items_center()
.gap(rem(12.))
.min_h(rem(64.))
.py(rem(8.))
.pl(rem(16.))
.pr(rem(4.))
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.bg(surface)
.child(
div()
.relative()
.flex()
.flex_none()
.child(EnvironmentMachineIcon::new(self.kind).size(20.))
.child(
div().absolute().right(px(-3.)).bottom(px(-2.)).child(
ConnectionStatusDot::new(self.status)
.id((id.clone(), "dot"))
.cutout(surface),
),
),
)
.child(
div()
.flex()
.flex_col()
.flex_1()
.min_w_0()
.gap(px(2.))
.child(
sans(div(), 14., 600)
.flex()
.flex_row()
.items_center()
.gap(rem(8.))
.text_color(color(ColorRole::TextPrimary))
.child(self.name.clone())
.when(self.current, |this| {
this.child(
Badge::new(self.texts.current.clone())
.variant(BadgeVariant::Blue)
.size(BadgeSize::Sm),
)
}),
)
.child(card_sub(sub)),
)
.child(actions)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StepState {
Done,
Current,
Upcoming,
}
impl StepState {
pub fn of(i: usize, current: usize) -> Self {
match i.cmp(¤t) {
std::cmp::Ordering::Less => Self::Done,
std::cmp::Ordering::Equal => Self::Current,
std::cmp::Ordering::Greater => Self::Upcoming,
}
}
pub fn bullet(self) -> (Hsla, Hsla) {
match self {
Self::Done => (
color(ColorRole::SupportSuccess),
color(ColorRole::TextOnColor),
),
Self::Current => (color(ColorRole::Interactive), color(ColorRole::TextOnColor)),
Self::Upcoming => (
color(ColorRole::LayerAccent01),
color(ColorRole::TextPrimary),
),
}
}
}
pub(crate) fn bullet(fill: Hsla, ink: Hsla, content: AnyElement) -> gpui_kit::Div {
sized(div(), 11.)
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(20.))
.rounded_full()
.bg(fill)
.text_color(ink)
.child(content)
}
#[derive(IntoElement, Clone, Debug)]
pub struct WizardSteps {
steps: Vec<SharedString>,
current: usize,
}
impl WizardSteps {
pub fn new<S: Into<SharedString>>(steps: impl IntoIterator<Item = S>, current: usize) -> Self {
Self {
steps: steps.into_iter().map(Into::into).collect(),
current,
}
}
}
impl RenderOnce for WizardSteps {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
sans(div(), 12., 400)
.flex()
.flex_row()
.flex_wrap()
.gap(rem(16.))
.text_color(color(ColorRole::TextSecondary))
.children(self.steps.iter().enumerate().map(|(i, s)| {
let state = StepState::of(i, self.current);
let (fill, ink) = state.bullet();
let content = if state == StepState::Done {
press::icon(CarbonIcon::Checkmark, 12., ink).into_any_element()
} else {
SharedString::from((i + 1).to_string()).into_any_element()
};
sans(
div(),
12.,
if state == StepState::Current {
600
} else {
400
},
)
.flex()
.flex_row()
.items_center()
.gap(rem(4.))
.when(state == StepState::Current, |this| {
this.text_color(color(ColorRole::TextPrimary))
})
.child(bullet(fill, ink, content))
.child(s.clone())
}))
}
}
#[derive(Clone, Debug)]
pub struct WizardTexts {
pub back: SharedString,
pub next: SharedString,
}
impl Default for WizardTexts {
fn default() -> Self {
Self {
back: "Back".into(),
next: "Next".into(),
}
}
}
#[derive(IntoElement)]
pub struct Wizard {
id: ElementId,
title: SharedString,
steps: Vec<SharedString>,
current: usize,
children: Vec<AnyElement>,
texts: WizardTexts,
on_back: Option<Handler>,
on_next: Option<Handler>,
}
impl Wizard {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
title: title.into(),
steps: Vec::new(),
current: 0,
children: Vec::new(),
texts: WizardTexts::default(),
on_back: None,
on_next: None,
}
}
pub fn steps<S: Into<SharedString>>(mut self, steps: impl IntoIterator<Item = S>) -> Self {
self.steps = steps.into_iter().map(Into::into).collect();
self
}
pub fn current(mut self, current: usize) -> Self {
self.current = current;
self
}
pub fn next_label(mut self, label: impl Into<SharedString>) -> Self {
self.texts.next = label.into();
self
}
pub fn texts(mut self, texts: WizardTexts) -> Self {
self.texts = texts;
self
}
pub fn on_back(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_back = Some(Rc::new(f));
self
}
pub fn on_next(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_next = Some(Rc::new(f));
self
}
}
impl ParentElement for Wizard {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Wizard {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id.clone();
let half = |b: Button| div().flex_1().min_w_0().child(b.full_width(true));
div()
.id(id.clone())
.flex()
.flex_col()
.w_full()
.max_w(rem(576.))
.bg(color(ColorRole::Layer01))
.shadow(crate::components::overlay::floating_shadow())
.child(
div()
.flex()
.flex_col()
.gap(rem(12.))
.p(rem(16.))
.child(
sans(div(), 20., 400)
.text_color(color(ColorRole::TextPrimary))
.child(self.title.clone()),
)
.child(WizardSteps::new(self.steps.clone(), self.current)),
)
.child(div().px(rem(16.)).pb(rem(32.)).children(self.children))
.child(
div()
.flex()
.flex_row()
.child(half(click(
Button::new((id.clone(), "back"), self.texts.back.clone())
.variant(ButtonVariant::Secondary)
.size(ButtonSize::Xl)
.disabled(self.current == 0),
self.on_back.clone(),
)))
.child(half(click(
Button::new((id.clone(), "next"), self.texts.next.clone())
.size(ButtonSize::Xl),
self.on_next.clone(),
))),
)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum StatTone {
Error,
Success,
}
impl StatTone {
pub fn color(self) -> Hsla {
color(match self {
Self::Error => ColorRole::TextError,
Self::Success => ColorRole::SupportSuccess,
})
}
}
#[derive(IntoElement, Clone, Debug)]
pub struct StatBlock {
label: SharedString,
value: SharedString,
sub: Option<SharedString>,
tone: Option<StatTone>,
}
impl StatBlock {
pub fn new(label: impl Into<SharedString>, value: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
value: value.into(),
sub: None,
tone: None,
}
}
#[allow(clippy::should_implement_trait)]
pub fn sub(mut self, sub: impl Into<SharedString>) -> Self {
self.sub = Some(sub.into());
self
}
pub fn tone(mut self, tone: StatTone) -> Self {
self.tone = Some(tone);
self
}
}
impl RenderOnce for StatBlock {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_col()
.gap(rem(4.))
.py(rem(12.))
.px(rem(16.))
.size_full()
.bg(color(ColorRole::Layer01))
.child(
sans(div(), 12., 400)
.text_color(color(ColorRole::TextSecondary))
.child(self.label),
)
.child(
sans(div(), 28., 300)
.line_height(relative(BODY_LINE))
.text_color(
self.tone
.map(StatTone::color)
.unwrap_or(color(ColorRole::TextPrimary)),
)
.child(self.value),
)
.when_some(self.sub, |this, sub| {
this.child(
sans(div(), 12., 400)
.text_color(color(ColorRole::TextHelper))
.child(sub),
)
})
}
}
pub const STAT_MIN_WIDTH: f32 = 144.;
pub fn stat_columns(width: f32, count: usize) -> usize {
let fit = ((width + 1.) / (STAT_MIN_WIDTH + 1.)).floor().max(1.) as usize;
fit.min(count.max(1))
}
#[derive(IntoElement)]
pub struct StatsGrid {
id: ElementId,
children: Vec<AnyElement>,
}
impl StatsGrid {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
children: Vec::new(),
}
}
}
impl ParentElement for StatsGrid {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for StatsGrid {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let bounds = window.use_keyed_state((id.clone(), "bounds"), cx, |_, _| {
Bounds::<Pixels>::default()
});
let width = f32::from(bounds.read(cx).size.width)
/ (f32::from(window.rem_size()) / crate::tokens::REM_PX);
let count = self.children.len();
let columns = if width > 0. {
stat_columns(width, count)
} else {
count.max(1)
};
let measure = canvas(
move |b, window, cx| {
crate::measure::store(
&bounds,
Bounds::new(gpui_kit::Point::default(), b.size),
window,
cx,
);
},
|_, _, _, _| {},
)
.absolute()
.top_0()
.left_0()
.size_full();
let mut rows = Vec::new();
let mut cells = self.children.into_iter().peekable();
let mut r = 0;
while cells.peek().is_some() {
let mut row = div().flex().flex_row().gap(px(1.));
for _ in 0..columns {
row = row.child(div().flex_1().min_w_0().children(cells.next()));
}
rows.push(row.id(child_id(&id, "row", r)));
r += 1;
}
div()
.id(id)
.relative()
.flex()
.flex_col()
.gap(px(1.))
.w_full()
.bg(color(ColorRole::BorderSubtle01))
.children(rows)
.child(measure)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn subtitle_joins_provider_and_version() {
assert_eq!(
provider_subtitle("Claude (work)", "Claude", Some("2.1.4")),
"Claude \u{b7} v2.1.4"
);
assert_eq!(provider_subtitle("Codex", "Codex", None), "");
assert_eq!(provider_subtitle("Codex", "Codex", Some("1.0")), "v1.0");
assert_eq!(provider_subtitle("Work", "Kiro", None), "Kiro");
}
#[test]
fn provider_tags_follow_the_status_map() {
assert_eq!(ProviderStatus::Ready.variant(), BadgeVariant::Green);
assert_eq!(ProviderStatus::Signin.variant(), BadgeVariant::WarmGray);
assert_eq!(ProviderStatus::Error.label(), "Not found");
assert!(ProviderStatus::Signin.colors().is_some());
assert!(ProviderStatus::Ready.colors().is_none());
}
#[test]
fn steps_before_current_are_done() {
assert_eq!(StepState::of(0, 1), StepState::Done);
assert_eq!(StepState::of(1, 1), StepState::Current);
assert_eq!(StepState::of(2, 1), StepState::Upcoming);
}
#[test]
fn stat_grid_fits_9rem_tracks() {
assert_eq!(stat_columns(720., 5), 4);
assert_eq!(stat_columns(1000., 5), 5);
assert_eq!(stat_columns(100., 5), 1);
assert_eq!(stat_columns(289., 3), 2);
assert_eq!(stat_columns(288., 3), 1);
}
}