use super::parts::{self, rem};
use crate::{
components::form,
density,
icons::{CarbonIcon, Icon},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
App, AppContext as _, Context, CursorStyle, ElementId, Entity, Focusable as _, Hsla,
InteractiveElement, IntoElement, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, Subscription, Window,
component::input::{Escape, InputEvent, InputState},
div,
prelude::FluentBuilder as _,
px,
};
use std::rc::Rc;
pub type ValidateFn = dyn Fn(&str) -> Option<SharedString>;
type Validate = Rc<ValidateFn>;
type Handler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Commit {
Set(String),
Keep,
Error(SharedString),
}
pub fn commit(draft: &str, validate: Option<&ValidateFn>) -> Commit {
let t = draft.trim();
if let Some(err) = validate.and_then(|v| v(t)).filter(|e| !e.is_empty()) {
return Commit::Error(err);
}
if t.is_empty() {
Commit::Keep
} else {
Commit::Set(t.to_string())
}
}
pub fn input_outline(error: bool) -> Hsla {
color(if error {
ColorRole::SupportError
} else {
ColorRole::Focus
})
}
struct Model {
value: SharedString,
editing: bool,
error: Option<SharedString>,
input: Entity<InputState>,
validate: Option<Validate>,
on_change: Option<Handler>,
controlled: bool,
_subscription: Subscription,
}
impl Model {
fn start(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.editing = true;
self.error = None;
let v = self.value.clone();
self.input.update(cx, |s, cx| {
s.set_value(v, window, cx);
s.select_all(window, cx);
});
let focus = self.input.read(cx).focus_handle(cx);
focus.focus(window, cx);
cx.notify();
}
fn commit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
if !self.editing {
return;
}
let draft = self.input.read(cx).value().to_string();
match commit(&draft, self.validate.as_deref()) {
Commit::Error(e) => self.error = Some(e),
outcome => {
if let Commit::Set(t) = outcome {
let t: SharedString = t.into();
if !self.controlled {
self.value = t.clone();
}
if let Some(f) = self.on_change.clone() {
f(t, window, cx);
}
}
self.editing = false;
self.error = None;
}
}
cx.notify();
}
fn cancel(&mut self, cx: &mut Context<Self>) {
self.editing = false;
self.error = None;
cx.notify();
}
}
#[derive(IntoElement)]
pub struct EditableText {
id: ElementId,
value: Option<SharedString>,
default_value: SharedString,
placeholder: SharedString,
validate: Option<Validate>,
on_value_change: Option<Handler>,
input_label: SharedString,
rename_label: Option<SharedString>,
}
impl EditableText {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
value: None,
default_value: SharedString::default(),
placeholder: "Untitled".into(),
validate: None,
on_value_change: None,
input_label: "Rename".into(),
rename_label: None,
}
}
pub fn value(mut self, value: impl Into<SharedString>) -> Self {
self.value = Some(value.into());
self
}
pub fn default_value(mut self, value: impl Into<SharedString>) -> Self {
self.default_value = value.into();
self
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.placeholder = placeholder.into();
self
}
pub fn validate(mut self, f: impl Fn(&str) -> Option<SharedString> + 'static) -> Self {
self.validate = Some(Rc::new(f));
self
}
pub fn labels(
mut self,
input: impl Into<SharedString>,
button: impl Into<SharedString>,
) -> Self {
self.input_label = input.into();
self.rename_label = Some(button.into());
self
}
pub fn on_value_change(
mut self,
f: impl Fn(SharedString, &mut Window, &mut App) + 'static,
) -> Self {
self.on_value_change = Some(Rc::new(f));
self
}
}
impl RenderOnce for EditableText {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let initial = self.value.clone().unwrap_or(self.default_value.clone());
let model = window.use_keyed_state((id.clone(), "edit"), cx, |window, cx| {
let input = cx.new(|cx| InputState::new(window, cx));
let subscription = cx.subscribe_in(
&input,
window,
|this: &mut Model, _, e, window, cx| match e {
InputEvent::Blur | InputEvent::PressEnter { .. } => this.commit(window, cx),
InputEvent::Change if this.error.is_some() => {}
_ => {}
},
);
Model {
value: initial,
editing: false,
error: None,
input,
validate: None,
on_change: None,
controlled: false,
_subscription: subscription,
}
});
model.update(cx, |m, _| {
m.validate = self.validate.clone();
m.on_change = self.on_value_change.clone();
m.controlled = self.value.is_some();
if let Some(v) = &self.value {
m.value = v.clone();
}
});
let (value, editing, error, input) = {
let m = model.read(cx);
(m.value.clone(), m.editing, m.error.clone(), m.input.clone())
};
if editing {
let field_sm = density::current().field_sm().px_value();
let cancel = model.clone();
return div()
.id(id.clone())
.flex()
.flex_col()
.gap(px(2.))
.capture_action(move |_: &Escape, _, cx| {
cancel.update(cx, |m, cx| m.cancel(cx));
cx.stop_propagation();
})
.child(
div()
.relative()
.flex()
.items_center()
.h(rem(field_sm))
.px(rem(8.))
.bg(color(ColorRole::Field01))
.child(
gpui_kit::component::input::Input::new(&input)
.appearance(false)
.p_0()
.h_full()
.flex_1()
.min_w_0()
.aria_label(self.input_label.clone())
.text_color(color(ColorRole::TextPrimary)),
)
.child(form::inset_outline(2., input_outline(error.is_some()))),
)
.children(error.map(parts::error))
.into_any_element();
}
let button_focus = parts::keyed_focus((id.clone(), "btn-focus"), window, cx);
let ring = parts::focus_visible(&button_focus, window);
let row_hover = window.use_keyed_state((id.clone(), "row-hover"), cx, |_, _| false);
let btn_hover = window.use_keyed_state((id.clone(), "btn-hover"), cx, |_, _| false);
let visible = *row_hover.read(cx) || ring;
let rename_label = self
.rename_label
.clone()
.unwrap_or_else(|| format!("Rename {value}").into());
let start = model.clone();
let text = if value.is_empty() {
div()
.text_color(color(ColorRole::TextPlaceholder))
.child(self.placeholder.clone())
} else {
div().child(value.clone())
};
let bh = btn_hover.clone();
div()
.id(id.clone())
.flex()
.items_center()
.gap(rem(4.))
.on_hover(move |h, _, cx| {
row_hover.update(cx, |v, cx| {
*v = *h;
cx.notify();
})
})
.child(text)
.child(
gpui_kit::base::Button::new((id.clone(), "rename"))
.track_focus(&button_focus)
.accessibility_label(rename_label)
.relative()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(rem(24.))
.opacity(if visible { 1. } else { 0. })
.when(*btn_hover.read(cx), |b| {
b.bg(color(ColorRole::LayerHover01))
})
.cursor(CursorStyle::PointingHand)
.on_hover(move |h, _, cx| {
bh.update(cx, |v, cx| {
*v = *h;
cx.notify();
})
})
.child(
Icon::carbon(CarbonIcon::Edit)
.size_length(px(14.))
.color(color(ColorRole::IconSecondary)),
)
.when(ring, |b| b.child(parts::focus_ring()))
.on_click(move |_, window, cx| start.update(cx, |m, cx| m.start(window, cx))),
)
.into_any_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn commit_trims_validates_and_keeps_empty() {
assert_eq!(commit(" carbon ", None), Commit::Set("carbon".into()));
assert_eq!(commit(" ", None), Commit::Keep);
let required = |t: &str| t.is_empty().then(|| SharedString::from("Name is required"));
assert_eq!(
commit(" ", Some(&required)),
Commit::Error("Name is required".into())
);
assert_eq!(commit("x", Some(&required)), Commit::Set("x".into()));
}
}