From d5f446a35115fa09adc4bb9f30c0afcd8187bab5 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 26 Jun 2024 17:47:42 +0800 Subject: [PATCH] Add to support no appearance for TextField and fix invalid key handle. --- crates/ui/src/dropdown.rs | 0 crates/ui/src/picker.rs | 4 +- crates/ui/src/text_field/mod.rs | 67 ++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 crates/ui/src/dropdown.rs diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs new file mode 100644 index 00000000..e69de29b diff --git a/crates/ui/src/picker.rs b/crates/ui/src/picker.rs index 975419cd..fe92a74f 100644 --- a/crates/ui/src/picker.rs +++ b/crates/ui/src/picker.rs @@ -158,7 +158,7 @@ impl Picker { Self { delegate, - query_input: None, + query_input, head: cx.new_view(Empty::new), width: None, is_modal: false, @@ -174,7 +174,7 @@ impl Picker { cx: &mut ViewContext, ) -> View { cx.new_view(|cx| { - let mut input = TextField::new(cx); + let mut input = TextField::new(cx).appearance(false); input.set_placeholder(placehoder, cx); input }) diff --git a/crates/ui/src/text_field/mod.rs b/crates/ui/src/text_field/mod.rs index 6a1d8eba..9db2b230 100644 --- a/crates/ui/src/text_field/mod.rs +++ b/crates/ui/src/text_field/mod.rs @@ -2,20 +2,21 @@ mod blink_manager; mod cursor_layout; mod text_view; -use crate::theme::ActiveTheme; +use crate::{h_flex, theme::ActiveTheme}; use gpui::{ - div, prelude::FluentBuilder as _, AppContext, ClipboardItem, EventEmitter, FocusHandle, - FocusableView, InteractiveElement, IntoElement, KeyDownEvent, MouseButton, ParentElement, - Render, RenderOnce, SharedString, Styled, View, ViewContext, WindowContext, + div, prelude::FluentBuilder as _, AppContext, ClipboardItem, Div, EventEmitter, FocusHandle, + FocusableView, InteractiveElement, Interactivity, IntoElement, KeyDownEvent, MouseButton, + ParentElement, Render, RenderOnce, SharedString, Style, StyleRefinement, Styled, View, + ViewContext, WindowContext, }; -use std::time::Duration; +use std::{sync::Arc, time::Duration}; use text_view::TextView; const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500); -#[derive(Clone)] pub struct TextField { focus_handle: FocusHandle, + appearance: bool, pub view: View, } @@ -24,13 +25,25 @@ impl TextField { let focus_handle = cx.focus_handle(); let view = TextView::init(cx, &focus_handle); - Self { focus_handle, view } + Self { + focus_handle, + view, + appearance: true, + } } pub fn focus(&mut self, cx: &mut WindowContext) { cx.focus(&self.focus_handle); } + /// Set the appearance of the text field. + /// + /// If false, the text field will not have a border, background and focus ring. + pub fn appearance(mut self, appearance: bool) -> Self { + self.appearance = appearance; + self + } + pub fn set_placeholder( &mut self, placeholder: impl Into, @@ -78,7 +91,8 @@ impl Render for TextField { let focused = self.focus_handle.is_focused(cx); let disabled = text_view.disabled; - div() + h_flex() + .w_full() .track_focus(&focus_handle) .when(!text_view.disabled, |this| { this.on_mouse_down( @@ -213,16 +227,7 @@ impl Render for TextField { text_view.selection = i..i; } } - _ => { - if let Some(c) = keystroke.chars().next() { - text_view.text.replace_range( - text_view.char_range_to_text_range(&text_view.text), - c.to_string().as_str(), - ); - let i = text_view.selection.start + 1; - text_view.selection = i..i; - } - } + _ => {} }; } @@ -235,19 +240,21 @@ impl Render for TextField { }); })) }) - .border_color(if focused { theme.ring } else { theme.input }) - .border_1() - .rounded_sm() - .py_1() - .px_3() - .h_9() - .shadow_sm() - .min_w_20() - .bg(if disabled { - theme.muted - } else { - theme.background + .when(self.appearance, |this| { + this.border_color(if focused { theme.ring } else { theme.input }) + .border_1() + .rounded_sm() + .py_1() + .px_3() + .h_9() + .shadow_sm() + .bg(if disabled { + theme.muted + } else { + theme.background + }) }) + .min_w_20() .child(view) } }