From 837b7111b3ce02723c96e70835507642fd69788f Mon Sep 17 00:00:00 2001 From: Nate Butler Date: Thu, 11 Apr 2024 10:03:36 -0400 Subject: [PATCH] Update TextField (#10415) This PR makes some simple updates to the TextField api and update it's styling. Release Notes: - N/A --- crates/ui_text_field/src/ui_text_field.rs | 110 +++++++++------------- 1 file changed, 47 insertions(+), 63 deletions(-) diff --git a/crates/ui_text_field/src/ui_text_field.rs b/crates/ui_text_field/src/ui_text_field.rs index cdbc95f0c9..3e5a347a6d 100644 --- a/crates/ui_text_field/src/ui_text_field.rs +++ b/crates/ui_text_field/src/ui_text_field.rs @@ -13,6 +13,7 @@ use ui::*; #[derive(Debug, Clone, Copy, PartialEq)] pub enum FieldLabelLayout { + Hidden, Inline, Stacked, } @@ -30,10 +31,8 @@ pub struct TextField { /// An optional label for the text field. /// /// Its position is determined by the [`FieldLabelLayout`]. - label: Option, + label: SharedString, /// The placeholder text for the text field. - /// - /// All text fields must have placeholder text that is displayed when the field is empty. placeholder: SharedString, /// Exposes the underlying [`View`] to allow for customizing the editor beyond the provided API. /// @@ -44,7 +43,7 @@ pub struct TextField { /// For example, a magnifying glass icon in a search field. start_icon: Option, /// The layout of the label relative to the text field. - label_layout: FieldLabelLayout, + with_label: FieldLabelLayout, } impl FocusableView for TextField { @@ -54,7 +53,11 @@ impl FocusableView for TextField { } impl TextField { - pub fn new(placeholder: impl Into, cx: &mut WindowContext) -> Self { + pub fn new( + cx: &mut WindowContext, + label: impl Into, + placeholder: impl Into, + ) -> Self { let placeholder_text = placeholder.into(); let editor = cx.new_view(|cx| { @@ -64,31 +67,21 @@ impl TextField { }); Self { - label: None, + label: label.into(), placeholder: placeholder_text, editor, start_icon: None, - label_layout: FieldLabelLayout::Stacked, + with_label: FieldLabelLayout::Hidden, } } - pub fn label(mut self, label: impl Into) -> Self { - self.label = Some(label.into()); - self - } - - pub fn placeholder(mut self, placeholder: impl Into) -> Self { - self.placeholder = placeholder.into(); - self - } - pub fn start_icon(mut self, icon: IconName) -> Self { self.start_icon = Some(icon); self } - pub fn label_layout(mut self, layout: FieldLabelLayout) -> Self { - self.label_layout = layout; + pub fn with_label(mut self, layout: FieldLabelLayout) -> Self { + self.with_label = layout; self } } @@ -133,52 +126,43 @@ impl Render for TextField { ..Default::default() }; - let stacked_label: Option