use crate::t3::press::{self, Handler, rem};
use crate::{icons::CarbonIcon, theme::color, tokens::ColorRole};
use gpui_kit::{
AnyElement, App, CursorStyle, ElementId, Hsla, 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)]
#[allow(missing_docs)]
pub enum EffortLevel {
Minimal,
Low,
#[default]
Medium,
High,
Max,
}
impl EffortLevel {
pub const ALL: [EffortLevel; 5] = [
Self::Minimal,
Self::Low,
Self::Medium,
Self::High,
Self::Max,
];
pub const fn name(self) -> &'static str {
match self {
Self::Minimal => "minimal",
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
Self::Max => "max",
}
}
pub const fn bars(self) -> usize {
self as usize + 1
}
}
pub const fn effort_bar_height(i: usize) -> f32 {
4. + i as f32 * 2.
}
pub fn effort_bar_color(on: bool) -> Hsla {
let ink = color(ColorRole::IconPrimary);
if on { ink } else { ink.opacity(0.26) }
}
#[derive(IntoElement)]
pub struct EffortGlyph {
level: EffortLevel,
fast: bool,
}
impl EffortGlyph {
pub fn new(level: EffortLevel) -> Self {
Self { level, fast: false }
}
pub fn fast(mut self, fast: bool) -> Self {
self.fast = fast;
self
}
}
impl RenderOnce for EffortGlyph {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let lit = self.level.bars();
div()
.flex()
.flex_row()
.flex_none()
.items_end()
.gap(px(1.))
.h(rem(14.))
.children((1..=5).map(|i| {
div()
.w(px(3.))
.h(rem(effort_bar_height(i)))
.bg(effort_bar_color(i <= lit))
}))
.when(self.fast, |this| {
this.child(div().ml(px(2.)).self_center().child(press::icon(
CarbonIcon::FlashFilled,
12.,
color(ColorRole::SupportWarning),
)))
})
}
}
pub fn composer_control_colors(hot: bool) -> (Hsla, Hsla) {
if hot {
(
color(ColorRole::BackgroundHover),
color(ColorRole::TextPrimary),
)
} else {
(
gpui_kit::transparent_black(),
color(ColorRole::TextSecondary),
)
}
}
#[derive(IntoElement)]
pub struct ComposerControl {
id: ElementId,
icon: Option<CarbonIcon>,
label: SharedString,
children: Vec<AnyElement>,
active: bool,
on_click: Option<Handler>,
}
impl ComposerControl {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
icon: None,
label: label.into(),
children: Vec::new(),
active: false,
on_click: None,
}
}
pub fn icon(mut self, icon: CarbonIcon) -> Self {
self.icon = Some(icon);
self
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl gpui_kit::base::Selectable for ComposerControl {
fn selected(self, selected: bool) -> Self {
self.active(selected)
}
fn is_selected(&self) -> bool {
self.active
}
}
impl ParentElement for ComposerControl {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for ComposerControl {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let (hovered, hover) = press::hover((id.clone(), "hover"), window, cx);
let (focus, ring) = press::focus((id.clone(), "focus"), window, cx);
let (bg, ink) = composer_control_colors(hovered || self.active);
let on_click = self.on_click.clone();
let content: Vec<AnyElement> = if self.children.is_empty() {
vec![self.label.clone().into_any_element()]
} else {
self.children
};
gpui_kit::base::Button::new(id)
.track_focus(&focus)
.accessibility_label(self.label.clone())
.relative()
.flex()
.flex_row()
.flex_none()
.items_center()
.gap(rem(4.))
.h(rem(32.))
.px(rem(8.))
.bg(bg)
.text_color(ink)
.text_size(rem(12.))
.line_height(rem(16.))
.whitespace_nowrap()
.cursor(CursorStyle::PointingHand)
.on_hover(press::track_hover(hover))
.when_some(on_click, |this, f| {
this.on_click(move |_, window, cx| f(window, cx))
})
.when_some(self.icon, |this, icon| {
this.child(press::icon(icon, 16., ink))
})
.children(content)
.child(press::icon(CarbonIcon::ChevronDown, 12., ink.opacity(0.7)))
.when(ring, |this| this.child(press::ring()))
}
}
#[derive(IntoElement)]
pub struct ReadOnlyComposerBar {
lead: SharedString,
status: Option<SharedString>,
action: Option<AnyElement>,
}
impl ReadOnlyComposerBar {
pub fn new(lead: impl Into<SharedString>) -> Self {
Self {
lead: lead.into(),
status: None,
action: None,
}
}
pub fn status(mut self, status: impl Into<SharedString>) -> Self {
self.status = Some(status.into());
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.action = Some(action.into_any_element());
self
}
}
impl RenderOnce for ReadOnlyComposerBar {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
div()
.flex()
.flex_row()
.items_center()
.gap(rem(8.))
.h(rem(48.))
.px(rem(16.))
.w_full()
.min_w_0()
.bg(color(ColorRole::Layer01))
.text_color(color(ColorRole::TextSecondary))
.text_size(rem(14.))
.line_height(rem(18.))
.child(press::icon(
CarbonIcon::Locked,
16.,
color(ColorRole::IconSecondary),
))
.child(
div()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.text_color(color(ColorRole::TextPrimary))
.child(self.lead),
)
.when_some(self.status, |this, status| {
this.child(
div()
.flex_none()
.text_size(rem(12.))
.line_height(rem(16.))
.child(status),
)
})
.child(div().flex_1())
.children(self.action)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
#[test]
fn effort_bars_rise_and_dim() {
crate::theme::tests_set(CarbonTheme::White);
assert_eq!(
(1..=5).map(effort_bar_height).collect::<Vec<_>>(),
[6., 8., 10., 12., 14.]
);
assert_eq!(EffortLevel::Minimal.bars(), 1);
assert_eq!(EffortLevel::Medium.bars(), 3);
assert_eq!(EffortLevel::Max.bars(), 5);
assert_eq!(hex(effort_bar_color(true)), "#161616");
assert!((effort_bar_color(false).a - 0.26).abs() < 1e-3);
}
#[test]
fn composer_control_is_selected_while_its_popover_is_open() {
use gpui_kit::base::Selectable as _;
let control = ComposerControl::new("model", "Model");
assert!(!control.is_selected());
let open = control.open(true);
assert!(open.is_selected() && open.is_open() && open.active);
assert!(!open.selected(false).is_selected());
}
#[test]
fn composer_control_hover_and_on_share_a_look() {
crate::theme::tests_set(CarbonTheme::White);
let (bg, ink) = composer_control_colors(true);
assert_eq!(bg, color(ColorRole::BackgroundHover));
assert_eq!(hex(ink), "#161616");
let (rest, ink) = composer_control_colors(false);
assert_eq!(rest.a, 0.);
assert_eq!(hex(ink), "#525252");
}
}