use gpui_kit::component::input::{InputEvent, TextareaState};
use gpui_kit::{
App, AppContext, Context, Entity, EntityInputHandler as _, EventEmitter, FocusHandle,
KeyBinding, SharedString, Subscription, Window, actions,
};
actions!(subspace_composer, [ComposerSubmit, ComposerEscape]);
pub const COMPOSER_CONTEXT: &str = "SubspaceComposer";
const INPUT_MIN_ROWS: usize = 4;
const INPUT_MAX_ROWS: usize = 20;
const TAB_PADDING_Y_PX: f32 = 2.;
const PREVIEW_MIN_HEIGHT_PX: f32 = 96.;
const MAX_IMAGES: usize = 8;
const MAX_IMAGE_DRAFT_BYTES: usize = 32 * 1024 * 1024;
pub(crate) fn bind_keys(cx: &mut App) {
cx.bind_keys([
KeyBinding::new(
crate::secondary!("enter"),
ComposerSubmit,
Some("SubspaceComposer > Input"),
),
KeyBinding::new("escape", ComposerEscape, Some("SubspaceComposer > Input")),
KeyBinding::new(
crate::secondary!("enter"),
ComposerSubmit,
Some(COMPOSER_CONTEXT),
),
KeyBinding::new("escape", ComposerEscape, Some(COMPOSER_CONTEXT)),
]);
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComposerEvent {
Submit(String),
Cancel,
}
pub fn escape_closes(text: &str, initial: &str, asked: bool) -> bool {
text.trim().is_empty() || text == initial || asked
}
pub fn submit_error(text: &str, optional: bool) -> Option<String> {
(!optional && text.trim().is_empty()).then(|| "Text is required.".into())
}
pub fn note_text(busy: bool, asked: bool, optional: bool, submit_label: &str) -> String {
if busy {
return "Sending…".to_owned();
}
if asked {
return "Press Esc again to discard the changes.".to_owned();
}
let keys = format!(
"Ctrl+Enter: {} · Esc to cancel",
submit_label.to_lowercase()
);
if optional {
format!("Text is optional · {keys}")
} else {
keys
}
}
pub struct Composer {
images: ComposerImages,
state: ComposerState,
hint: Option<SharedString>,
label: SharedString,
submit_label: SharedString,
input: Entity<TextareaState>,
focus: FocusHandle,
initial: String,
preview: bool,
error: Option<SharedString>,
_subscription: Subscription,
}
struct ComposerState {
retained: bool,
readonly: bool,
submit_enabled: bool,
optional: bool,
asked: bool,
busy: bool,
}
type ComposerAccessory = dyn Fn(&mut Window, &mut App) -> gpui_kit::AnyElement;
#[derive(Default)]
struct ComposerImages {
enabled: bool,
byte_limit: Option<usize>,
accessory: Option<std::sync::Arc<ComposerAccessory>>,
}
impl EventEmitter<ComposerEvent> for Composer {}
pub struct ComposerImagePaste(pub crate::chat_input::attachments::IngestionBatch);
impl EventEmitter<ComposerImagePaste> for Composer {}
impl Composer {
pub fn new(
label: impl Into<SharedString>,
placeholder: impl Into<SharedString>,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let placeholder = placeholder.into();
let input = cx.new(|cx| {
let mut state = TextareaState::new(window, cx).placeholder(placeholder);
state.set_auto_grow(INPUT_MIN_ROWS, INPUT_MAX_ROWS, cx);
state
});
let subscription = cx.subscribe(&input, |composer: &mut Self, _, event, cx| {
if matches!(event, InputEvent::Change)
&& (composer.state.asked || composer.error.is_some())
{
composer.state.asked = false;
composer.error = None;
cx.notify();
}
});
Self {
images: ComposerImages::default(),
state: ComposerState {
retained: false,
readonly: false,
submit_enabled: true,
optional: false,
asked: false,
busy: false,
},
hint: None,
label: label.into(),
submit_label: "Submit".into(),
input,
focus: cx.focus_handle(),
initial: String::new(),
preview: false,
error: None,
_subscription: subscription,
}
}
pub fn set_text(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) {
self.initial = text.to_owned();
let text = text.to_owned();
self.input
.update(cx, |input, cx| input.set_value(text, window, cx));
cx.notify();
}
pub fn set_label(&mut self, label: impl Into<SharedString>, cx: &mut Context<Self>) {
self.label = label.into();
cx.notify();
}
pub fn set_placeholder(
&mut self,
placeholder: impl Into<SharedString>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let placeholder = placeholder.into();
self.input.update(cx, |input, cx| {
input.set_placeholder(placeholder, window, cx)
});
}
pub fn set_submit_label(&mut self, label: impl Into<SharedString>, cx: &mut Context<Self>) {
self.submit_label = label.into();
cx.notify();
}
pub fn set_optional(&mut self, optional: bool, cx: &mut Context<Self>) {
self.state.optional = optional;
self.error = None;
cx.notify();
}
pub fn set_images(&mut self, enabled: bool, cx: &mut Context<Self>) {
self.images.enabled = enabled;
cx.notify();
}
pub fn set_image_byte_limit(&mut self, bytes: usize, cx: &mut Context<Self>) {
self.images.byte_limit = Some(bytes);
cx.notify();
}
pub fn set_accessory(
&mut self,
render: impl Fn(&mut Window, &mut App) -> gpui_kit::AnyElement + 'static,
cx: &mut Context<Self>,
) {
self.images.accessory = Some(std::sync::Arc::new(render));
cx.notify();
}
pub fn focus(&self, window: &mut Window, cx: &mut App) {
self.input.update(cx, |input, cx| input.focus(window, cx));
}
pub fn text(&self, cx: &App) -> String {
self.input.read(cx).value().to_string()
}
pub fn set_retained(&mut self, retained: bool, cx: &mut Context<Self>) {
self.state.retained = retained;
cx.notify();
}
pub fn input(&self) -> Entity<TextareaState> {
self.input.clone()
}
pub fn set_readonly(&mut self, readonly: bool, cx: &mut Context<Self>) {
self.state.readonly = readonly;
cx.notify();
}
pub fn set_submit_enabled(&mut self, enabled: bool, cx: &mut Context<Self>) {
self.state.submit_enabled = enabled;
cx.notify();
}
pub fn set_hint(&mut self, hint: Option<SharedString>, cx: &mut Context<Self>) {
self.hint = hint;
cx.notify();
}
pub fn set_error(&mut self, error: impl Into<SharedString>, cx: &mut Context<Self>) {
self.error = Some(error.into());
self.state.busy = false;
cx.notify();
}
pub fn set_busy(&mut self, busy: bool, cx: &mut Context<Self>) {
self.state.busy = busy;
cx.notify();
}
fn submit(&mut self, _: &ComposerSubmit, window: &mut Window, cx: &mut Context<Self>) {
if self.state.busy
|| self.state.readonly
|| !self.state.submit_enabled
|| self.input.update(cx, |input, cx| {
input.marked_text_range(window, cx).is_some()
})
{
return;
}
let text = self.text(cx);
if let Some(error) = submit_error(&text, self.state.optional) {
self.error = Some(error.into());
cx.notify();
return;
}
cx.emit(ComposerEvent::Submit(text));
}
fn escape(&mut self, _: &ComposerEscape, _: &mut Window, cx: &mut Context<Self>) {
if self.state.retained || escape_closes(&self.text(cx), &self.initial, self.state.asked) {
cx.emit(ComposerEvent::Cancel);
} else {
self.state.asked = true;
cx.notify();
}
}
fn set_preview(&mut self, preview: bool, window: &mut Window, cx: &mut Context<Self>) {
self.preview = preview;
if preview {
self.focus.focus(window, cx);
} else {
self.focus(window, cx);
}
cx.notify();
}
}
mod view;
#[cfg(test)]
#[path = "composer/tests.rs"]
mod retained_tests;
#[cfg(test)]
mod tests {
use super::{escape_closes, note_text, submit_error};
#[test]
fn escape_asks_once_before_discarding_changes() {
assert!(escape_closes("", "", false));
assert!(escape_closes(" \n", "", false));
assert!(!escape_closes("draft", "", false));
assert!(escape_closes("draft", "", true));
assert!(escape_closes("stored", "stored", false), "unchanged text");
assert!(!escape_closes("stored!", "stored", false));
}
#[test]
fn submit_rules() {
assert_eq!(
submit_error(" ", false).as_deref(),
Some("Text is required.")
);
assert_eq!(submit_error("text", false), None);
assert_eq!(submit_error("", true), None, "an optional composer");
}
#[test]
fn note_reports_the_composer_state() {
assert_eq!(note_text(true, true, true, "Save"), "Sending…");
assert_eq!(
note_text(false, true, false, "Save"),
"Press Esc again to discard the changes."
);
assert_eq!(
note_text(false, false, false, "Save"),
"Ctrl+Enter: save · Esc to cancel"
);
assert_eq!(
note_text(false, false, true, "Approve"),
"Text is optional · Ctrl+Enter: approve · Esc to cancel"
);
}
}