use crate::{
density,
icons::{CarbonIcon, Icon},
t3::{
craft::{rem, sans},
shell::{IdentitySize, ProviderIcon},
},
theme::color,
tokens::{self, ColorRole},
u0::primitives::{
IconButton,
parts::{editor, focus_ring, focus_visible, keyed_focus},
},
};
use gpui_kit::{
AnyElement, App, AppContext as _, Context, CursorStyle, ElementId, Entity, Focusable as _,
Hsla, InteractiveElement as _, IntoElement, ParentElement, Pixels, RenderOnce, SharedString,
Styled, Subscription, Window,
component::input::{InputEvent, InputState},
div,
prelude::FluentBuilder as _,
px,
};
use std::rc::Rc;
const INPUT_PAD_X: Pixels = px(2.);
pub mod stacked {
pub const MIN_HEIGHT: f32 = 88.;
pub const PADDING: [f32; 4] = [12., 8., 8., 12.];
pub const ROW_GAP: f32 = 8.;
pub const COLUMN_GAP: f32 = 4.;
pub const INPUT_HEIGHT: f32 = 22.;
}
pub fn send_colors(enabled: bool) -> (Hsla, Hsla) {
if enabled {
(
color(ColorRole::ButtonPrimary),
color(ColorRole::TextOnColor),
)
} else {
(
color(ColorRole::ButtonDisabled),
color(ColorRole::TextOnColorDisabled),
)
}
}
#[derive(Clone, Debug)]
pub struct ComposerCompactTexts {
pub placeholder: SharedString,
pub message: SharedString,
pub attach: SharedString,
pub send: SharedString,
}
impl Default for ComposerCompactTexts {
fn default() -> Self {
Self {
placeholder: "Ask on the side…".into(),
message: "Side chat message".into(),
attach: "Attach".into(),
send: "Send".into(),
}
}
}
type SendHandler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
struct Model {
input: Entity<InputState>,
on_send: Option<SendHandler>,
_subscription: Subscription,
}
impl Model {
fn send(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let text = self.input.read(cx).value().to_string();
let Some(f) = self.on_send.clone().filter(|_| !text.trim().is_empty()) else {
return;
};
f(text.into(), window, cx);
self.input.update(cx, |s, cx| s.set_value("", window, cx));
cx.notify();
}
}
#[derive(IntoElement)]
pub struct ComposerCompact {
id: ElementId,
agent: SharedString,
lead: Option<AnyElement>,
disabled: bool,
stacked: bool,
on_send: Option<SendHandler>,
on_attach: Option<Handler>,
texts: ComposerCompactTexts,
}
impl ComposerCompact {
pub fn new(id: impl Into<ElementId>, agent: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
agent: agent.into(),
lead: None,
disabled: false,
stacked: false,
on_send: None,
on_attach: None,
texts: ComposerCompactTexts::default(),
}
}
pub fn lead(mut self, lead: impl IntoElement) -> Self {
self.lead = Some(lead.into_any_element());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn stacked(mut self, stacked: bool) -> Self {
self.stacked = stacked;
self
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.texts.placeholder = placeholder.into();
self
}
pub fn on_send(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_send = Some(Rc::new(f));
self
}
pub fn on_attach(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_attach = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: ComposerCompactTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for ComposerCompact {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let model = window.use_keyed_state((id.clone(), "model"), cx, |window, cx| {
let input = cx.new(|cx| InputState::new(window, cx));
let subscription =
cx.subscribe_in(&input, window, |m: &mut Model, _, e, window, cx| match e {
InputEvent::PressEnter { .. } => m.send(window, cx),
InputEvent::Change => cx.notify(),
_ => {}
});
Model {
input,
on_send: None,
_subscription: subscription,
}
});
let disabled = self.disabled;
model.update(cx, |m, _| {
m.on_send = (!disabled).then(|| self.on_send.clone()).flatten();
});
let input = model.read(cx).input.clone();
let text = input.read(cx).value().to_string();
let enabled = !disabled && !text.trim().is_empty();
let focused = input.read(cx).focus_handle(cx).is_focused(window);
let send_focus = keyed_focus((id.clone(), "send-focus"), window, cx);
let send_ring = focus_visible(&send_focus, window);
let (fill, ink) = send_colors(enabled);
let placeholder = text.is_empty().then(|| {
sans(div(), 14., 400)
.absolute()
.left(INPUT_PAD_X)
.top_0()
.bottom_0()
.flex()
.items_center()
.text_color(color(ColorRole::TextPlaceholder))
.child(self.texts.placeholder.clone())
});
let send_model = model.clone();
let lead = self.lead.unwrap_or_else(|| {
ProviderIcon::new(self.agent.clone())
.id((id.clone(), "agent"))
.size(IdentitySize::Xs)
.into_any_element()
});
let field = div()
.relative()
.flex()
.items_center()
.min_w_0()
.px(INPUT_PAD_X)
.children(placeholder)
.child(editor(&input).disabled(disabled));
let attach = self.on_attach.clone().map(|f| {
IconButton::new(
(id.clone(), "attach"),
CarbonIcon::Add,
self.texts.attach.clone(),
)
.size_px(24.)
.on_click(move |_, w, cx| f(w, cx))
});
let send = gpui_kit::base::Button::new((id.clone(), "send"))
.track_focus(&send_focus)
.disabled(!enabled)
.accessibility_label(self.texts.send.clone())
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(24.))
.bg(fill)
.cursor(if enabled {
CursorStyle::PointingHand
} else {
CursorStyle::OperationNotAllowed
})
.on_click(move |_, w, cx| send_model.update(cx, |m, cx| m.send(w, cx)))
.child(
Icon::carbon(CarbonIcon::SendFilled)
.size_length(rem(14.))
.color(ink),
)
.when(send_ring, |b| b.child(focus_ring()));
let frame = div()
.id(id.clone())
.relative()
.flex()
.w_full()
.bg(color(ColorRole::Field01))
.border_b_1()
.border_color(color(ColorRole::BorderStrong01));
let frame = if self.stacked {
let [top, right, bottom, left] = stacked::PADDING;
frame
.flex_col()
.min_h(rem(stacked::MIN_HEIGHT))
.pt(rem(top))
.pr(rem(right))
.pb(rem(bottom))
.pl(rem(left))
.gap(rem(stacked::ROW_GAP))
.child(
div()
.flex()
.flex_col()
.flex_1()
.w_full()
.child(field.w_full().h(rem(stacked::INPUT_HEIGHT))),
)
.child(
div()
.flex()
.items_center()
.w_full()
.gap(rem(stacked::COLUMN_GAP))
.child(div().flex().flex_none().items_center().child(lead))
.child(div().flex_1())
.children(attach)
.child(send),
)
} else {
frame
.items_center()
.gap(tokens::SPACING_02)
.h(density::current().field())
.pl(tokens::SPACING_03)
.pr(tokens::SPACING_02)
.child(lead)
.child(field.flex_1().h_full())
.children(attach)
.child(send)
};
frame.when(focused, |d| d.child(focus_ring()))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CarbonTheme, theme::color_in};
#[test]
fn send_square_follows_the_css() {
crate::theme::tests_set(CarbonTheme::White);
let c = |r| color_in(CarbonTheme::White, r);
assert_eq!(
send_colors(true),
(c(ColorRole::ButtonPrimary), c(ColorRole::TextOnColor))
);
assert_eq!(
send_colors(false),
(
c(ColorRole::ButtonDisabled),
c(ColorRole::TextOnColorDisabled)
)
);
}
}