use super::form::{self, Validation, rem};
use super::input::{FIELD_MOTION, FieldLook, FieldState};
use crate::{motion, theme::LayerLevel, tokens::TypeStyle, typography::TypeStyled as _};
use gpui_kit::{
App, CursorStyle, ElementId, Entity, Focusable as _, Hsla, InteractiveElement, IntoElement,
MouseButton, ParentElement, RenderOnce, SharedString, Styled, Window,
component::{
Sizable as _, Size,
input::{IndentInline, OutdentInline, Textarea as KitTextarea},
},
div,
prelude::FluentBuilder as _,
};
pub use gpui_kit::component::input::TextareaState;
pub const PADDING_TOP: f32 = 11.;
pub const PADDING_BOTTOM: f32 = 8.;
pub const LINE: f32 = 20.;
const KIT_INSET: (f32, f32) = (2., 4.);
pub fn move_focus(forward: bool, window: &mut Window, cx: &mut App) {
let name = if forward {
"root::Tab"
} else {
"root::TabPrev"
};
match cx.build_action(name, None) {
Ok(action) => window.dispatch_action(action, cx),
Err(_) if forward => window.focus_next(cx),
Err(_) => window.focus_prev(cx),
}
}
pub fn tab_moves_focus<E: InteractiveElement>(el: E) -> E {
el.capture_action(|_: &IndentInline, window, cx| {
cx.stop_propagation();
move_focus(true, window, cx);
})
.capture_action(|_: &OutdentInline, window, cx| {
cx.stop_propagation();
move_focus(false, window, cx);
})
}
pub const MIN_HEIGHT: f32 = 40.;
pub fn field_height(rows: usize) -> f32 {
(rows as f32 * LINE + PADDING_TOP + PADDING_BOTTOM + 1.).max(MIN_HEIGHT)
}
pub fn text_height(height: f32) -> f32 {
(height - PADDING_TOP - PADDING_BOTTOM - 1.).max(0.)
}
pub fn counter(enabled: bool, count: usize, max: Option<usize>) -> Option<String> {
match (enabled, max) {
(true, Some(max)) => Some(format!("{count}/{max}")),
_ => None,
}
}
#[derive(IntoElement)]
pub struct Textarea {
id: ElementId,
state: Entity<TextareaState>,
label: Option<SharedString>,
hide_label: bool,
helper_text: Option<SharedString>,
placeholder: Option<SharedString>,
invalid: bool,
invalid_text: Option<SharedString>,
warn: bool,
warn_text: Option<SharedString>,
rows: usize,
enable_counter: bool,
max_count: Option<usize>,
disabled: bool,
readonly: bool,
level: LayerLevel,
}
impl Textarea {
pub fn new(id: impl Into<ElementId>, state: &Entity<TextareaState>) -> Self {
Self {
id: id.into(),
state: state.clone(),
label: None,
hide_label: false,
helper_text: None,
placeholder: None,
invalid: false,
invalid_text: None,
warn: false,
warn_text: None,
rows: 4,
enable_counter: false,
max_count: None,
disabled: false,
readonly: false,
level: LayerLevel::One,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn hide_label(mut self, hide: bool) -> Self {
self.hide_label = hide;
self
}
pub fn helper_text(mut self, text: impl Into<SharedString>) -> Self {
self.helper_text = Some(text.into());
self
}
pub fn placeholder(mut self, text: impl Into<SharedString>) -> Self {
self.placeholder = Some(text.into());
self
}
pub fn invalid(mut self, invalid: bool) -> Self {
self.invalid = invalid;
self
}
pub fn invalid_text(mut self, text: impl Into<SharedString>) -> Self {
self.invalid_text = Some(text.into());
self
}
pub fn warn(mut self, warn: bool) -> Self {
self.warn = warn;
self
}
pub fn warn_text(mut self, text: impl Into<SharedString>) -> Self {
self.warn_text = Some(text.into());
self
}
pub fn rows(mut self, rows: usize) -> Self {
self.rows = rows.max(1);
self
}
pub fn enable_counter(mut self, enable: bool) -> Self {
self.enable_counter = enable;
self
}
pub fn max_count(mut self, max: usize) -> Self {
self.max_count = Some(max);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn readonly(mut self, readonly: bool) -> Self {
self.readonly = readonly;
self
}
pub fn layer(mut self, level: LayerLevel) -> Self {
self.level = level;
self
}
}
impl RenderOnce for Textarea {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let state = self.state.clone();
let focus = state.read(cx).focus_handle(cx);
let focused = focus.is_focused(window);
let value = state.read(cx).value();
let count = value.chars().count();
let validation = if self.disabled || self.readonly {
Validation::None
} else {
Validation::from_flags(
self.invalid,
self.invalid_text.clone(),
self.warn,
self.warn_text.clone(),
)
};
let mut look = FieldLook::of(
self.level,
FieldState {
focused,
disabled: self.disabled,
readonly: self.readonly,
},
);
if self.disabled {
look.border_bottom = Hsla::transparent_black();
}
let outline_target = form::field_outline(focused && !self.disabled, &validation)
.unwrap_or(Hsla::transparent_black());
let background = motion::animate_color(
(id.clone(), "bg"),
look.background,
FIELD_MOTION,
window,
cx,
);
let outline = motion::animate_color(
(id.clone(), "outline"),
outline_target,
FIELD_MOTION,
window,
cx,
);
let pad_end = if validation.has_icon() { 40. } else { 16. };
let height = field_height(self.rows);
let editor_height = text_height(height);
let editor = KitTextarea::new(&state)
.appearance(false)
.with_size(Size::XSmall)
.disabled(self.disabled)
.readonly(self.readonly)
.when_some(self.label.clone(), |this, label| this.aria_label(label))
.h(rem(editor_height + 2. * KIT_INSET.0))
.w_full()
.text_size(rem(14.))
.line_height(rem(LINE))
.text_color(look.text);
let placeholder = self
.placeholder
.clone()
.filter(|_| value.is_empty())
.map(|text| {
div()
.absolute()
.top(rem(PADDING_TOP))
.left(rem(16.))
.right(rem(pad_end))
.type_style(TypeStyle::Body01)
.text_color(look.placeholder)
.child(text)
});
let icon = form::status_icon(&validation)
.map(|icon| div().absolute().top(rem(12.)).right(rem(16.)).child(icon));
let field = tab_moves_focus(div().id((id.clone(), "field")))
.relative()
.w_full()
.h(rem(height))
.min_h(rem(MIN_HEIGHT))
.overflow_hidden()
.pt(rem(PADDING_TOP - KIT_INSET.0))
.pb(rem(PADDING_BOTTOM - KIT_INSET.0 + form::FIELD_RULE))
.pl(rem(16. - KIT_INSET.1))
.pr(rem(pad_end - KIT_INSET.1))
.bg(background)
.type_style(TypeStyle::Body01)
.text_color(look.text)
.cursor(if self.disabled {
CursorStyle::OperationNotAllowed
} else {
CursorStyle::IBeam
})
.when(!self.disabled, |this| {
let focus = focus.clone();
this.on_mouse_down(MouseButton::Left, move |_, window, cx| {
focus.focus(window, cx);
})
})
.child(editor)
.children(placeholder)
.children(icon)
.child(form::bottom_rule(look.border_bottom))
.child(form::inset_outline(2., outline));
let counter_text = counter(self.enable_counter, count, self.max_count);
let label = self
.label
.clone()
.filter(|l| !self.hide_label && !l.is_empty());
let label_row = (label.is_some() || counter_text.is_some()).then(|| {
div()
.flex()
.flex_row()
.justify_between()
.w_full()
.child(form::label(
label.clone().unwrap_or_default(),
self.disabled,
))
.children(counter_text.map(|text| {
form::label(text, self.disabled).text_color(form::label_color(self.disabled))
}))
});
form::form_item()
.children(label_row)
.child(field)
.children(form::message(
&validation,
self.helper_text.as_ref(),
self.disabled,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn height_follows_rows_and_min_block_size() {
assert_eq!(field_height(4), 100.);
assert_eq!(field_height(1), 40.);
assert_eq!(text_height(field_height(4)), 80.);
assert_eq!(text_height(MIN_HEIGHT), 20.);
}
#[test]
fn counter_needs_both_props() {
assert_eq!(counter(true, 3, Some(200)).as_deref(), Some("3/200"));
assert_eq!(counter(true, 3, None), None);
assert_eq!(counter(false, 3, Some(200)), None);
}
}