use std::rc::Rc;
use gpui::{
AnyElement, App, Bounds, Edges, ElementId, Entity, Hsla, IntoElement, Pixels, SharedString,
Window,
};
use gpui_base::input::{InputBaseState, InputModeKind};
use gpui_material_foundation::InteractionSource;
use gpui_material_theme::{CornerShape, TypeStyle};
use super::{TextAreaState, TextFieldState};
mod render;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum TextFieldVariant {
#[default]
Filled,
Outlined,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TextFieldLabelPosition {
Inside,
Cutout,
Above,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum TextFieldLabelAlignment {
#[default]
Start,
Center,
End,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct TextFieldColorOverrides {
pub container: Option<Hsla>,
pub text: Option<Hsla>,
pub label: Option<Hsla>,
pub placeholder: Option<Hsla>,
pub leading: Option<Hsla>,
pub trailing: Option<Hsla>,
pub prefix: Option<Hsla>,
pub suffix: Option<Hsla>,
pub supporting: Option<Hsla>,
pub indicator: Option<Hsla>,
pub caret: Option<Hsla>,
pub selection: Option<Hsla>,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct TextFieldColors {
pub unfocused: TextFieldColorOverrides,
pub focused: TextFieldColorOverrides,
pub disabled: TextFieldColorOverrides,
pub invalid: TextFieldColorOverrides,
pub hovered_indicator: Option<Hsla>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TextFieldLayout {
pub editor_bounds: Bounds<Pixels>,
pub text_bounds: Option<Bounds<Pixels>>,
pub line_count: usize,
}
type OnTextLayout = Box<dyn FnOnce(TextFieldLayout, &mut Window, &mut App)>;
type DropdownRequest = Rc<dyn Fn(bool, &mut Window, &mut App)>;
struct FieldDescription {
text: SharedString,
content: Option<AnyElement>,
}
#[derive(IntoElement)]
pub struct TextField {
id: ElementId,
label: SharedString,
editor: Editor,
variant: TextFieldVariant,
label_content: Option<AnyElement>,
label_position: Option<TextFieldLabelPosition>,
label_minimized: bool,
expanded_alignment: TextFieldLabelAlignment,
minimized_alignment: TextFieldLabelAlignment,
placeholder: Option<(SharedString, AnyElement)>,
supporting: Option<FieldDescription>,
error: Option<SharedString>,
invalid: bool,
required: bool,
character_count: Option<usize>,
leading: Option<AnyElement>,
trailing: Option<AnyElement>,
prefix: Option<AnyElement>,
suffix: Option<AnyElement>,
dropdown_editor: bool,
expanded: Option<bool>,
dropdown_request: Option<DropdownRequest>,
font_scale: f32,
text_style: Option<TypeStyle>,
shape: Option<CornerShape>,
content_padding: Option<Edges<Pixels>>,
colors: TextFieldColors,
on_text_layout: Option<OnTextLayout>,
interaction_source: Option<Entity<InteractionSource>>,
}
#[derive(Clone)]
enum Editor {
Single(Entity<TextFieldState>),
Multiple(Entity<TextAreaState>),
Secure(Entity<TextFieldState>),
}
impl Editor {
fn layout(&self, cx: &App) -> TextFieldLayout {
fn snapshot(state: &InputBaseState<impl InputModeKind>) -> TextFieldLayout {
TextFieldLayout {
editor_bounds: state.input_bounds(),
text_bounds: state.text_bounds(),
line_count: state.display_map().wrap_row_count(),
}
}
match self {
Self::Single(state) | Self::Secure(state) => snapshot(state.read(cx)),
Self::Multiple(state) => snapshot(state.read(cx)),
}
}
}
impl TextField {
pub fn new(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
state: &Entity<TextFieldState>,
) -> Self {
Self::with_editor(id, label, Editor::Single(state.clone()))
}
pub fn multiline(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
state: &Entity<TextAreaState>,
) -> Self {
Self::with_editor(id, label, Editor::Multiple(state.clone()))
}
pub fn secure(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
state: &Entity<TextFieldState>,
) -> Self {
Self::with_editor(id, label, Editor::Secure(state.clone()))
}
fn with_editor(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
editor: Editor,
) -> Self {
Self {
id: id.into(),
label: label.into(),
editor,
variant: TextFieldVariant::Filled,
label_content: None,
label_position: None,
label_minimized: false,
expanded_alignment: TextFieldLabelAlignment::Start,
minimized_alignment: TextFieldLabelAlignment::Start,
placeholder: None,
supporting: None,
error: None,
invalid: false,
required: false,
character_count: None,
leading: None,
trailing: None,
prefix: None,
suffix: None,
dropdown_editor: false,
expanded: None,
dropdown_request: None,
font_scale: 1.,
text_style: None,
shape: None,
content_padding: None,
colors: TextFieldColors::default(),
on_text_layout: None,
interaction_source: None,
}
}
pub fn variant(mut self, variant: TextFieldVariant) -> Self {
self.variant = variant;
self
}
pub fn font_scale(mut self, scale: f32) -> Self {
self.font_scale = scale;
self
}
pub fn text_style(mut self, style: TypeStyle) -> Self {
self.text_style = Some(style);
self
}
pub fn shape(mut self, shape: CornerShape) -> Self {
self.shape = Some(shape);
self
}
pub fn content_padding(mut self, padding: Edges<Pixels>) -> Self {
self.content_padding = Some(padding);
self
}
pub fn colors(mut self, colors: TextFieldColors) -> Self {
self.colors = colors;
self
}
pub fn on_text_layout(
mut self,
listener: impl FnOnce(TextFieldLayout, &mut Window, &mut App) + 'static,
) -> Self {
self.on_text_layout = Some(Box::new(listener));
self
}
pub fn interaction_source(mut self, source: &Entity<InteractionSource>) -> Self {
self.interaction_source = Some(source.clone());
self
}
pub fn supporting_text(mut self, text: impl Into<SharedString>) -> Self {
self.supporting = Some(FieldDescription {
text: text.into(),
content: None,
});
self
}
pub fn label_content(mut self, element: impl IntoElement) -> Self {
self.label_content = Some(element.into_any_element());
self
}
pub fn label_position(mut self, position: TextFieldLabelPosition) -> Self {
self.label_position = Some(position);
self
}
pub fn label_always_minimized(mut self, minimized: bool) -> Self {
self.label_minimized = minimized;
self
}
pub fn label_alignment(
mut self,
expanded: TextFieldLabelAlignment,
minimized: TextFieldLabelAlignment,
) -> Self {
self.expanded_alignment = expanded;
self.minimized_alignment = minimized;
self
}
pub fn placeholder_content(
mut self,
hint: impl Into<SharedString>,
element: impl IntoElement,
) -> Self {
self.placeholder = Some((hint.into(), element.into_any_element()));
self
}
pub fn supporting_content(
mut self,
description: impl Into<SharedString>,
element: impl IntoElement,
) -> Self {
self.supporting = Some(FieldDescription {
text: description.into(),
content: Some(element.into_any_element()),
});
self
}
pub fn error(mut self, message: impl Into<SharedString>) -> Self {
self.error = Some(message.into());
self.invalid = true;
self
}
pub fn invalid(mut self, invalid: bool) -> Self {
self.invalid = invalid;
self
}
pub fn required(mut self, required: bool) -> Self {
self.required = required;
self
}
pub fn character_count(mut self, limit: usize) -> Self {
self.character_count = Some(limit);
self
}
pub fn leading(mut self, element: impl IntoElement) -> Self {
self.leading = Some(element.into_any_element());
self
}
pub fn trailing(mut self, element: impl IntoElement) -> Self {
self.trailing = Some(element.into_any_element());
self
}
pub fn prefix(mut self, element: impl IntoElement) -> Self {
self.prefix = Some(element.into_any_element());
self
}
pub fn suffix(mut self, element: impl IntoElement) -> Self {
self.suffix = Some(element.into_any_element());
self
}
pub(super) fn dropdown(mut self, open: bool, request: Option<DropdownRequest>) -> Self {
self.dropdown_editor = true;
self.expanded = request.is_some().then_some(open);
self.dropdown_request = request;
self
}
}