input: TextEvent Add Focus & Blur events

This commit is contained in:
Floyd Wang 2024-07-18 17:48:57 +08:00
parent 07a1f3099e
commit 91e123e942
3 changed files with 27 additions and 3 deletions

View file

@ -7,7 +7,7 @@ use gpui::{
use ui::{ use ui::{
button::Button, button::Button,
h_flex, h_flex,
input::{InputOtp, TextInput}, input::{InputOtp, TextEvent, TextInput},
prelude::FluentBuilder as _, prelude::FluentBuilder as _,
v_flex, Clickable, FocusableCycle, IconName, Size, v_flex, Clickable, FocusableCycle, IconName, Size,
}; };
@ -53,6 +53,12 @@ impl InputStory {
input input
}); });
cx.subscribe(&input1, Self::on_input_event).detach();
let input2 = cx.new_view(|cx| TextInput::new(cx).placeholder("Enter text here..."));
cx.subscribe(&input2, Self::on_input_event).detach();
let mask_input = cx.new_view(|cx| { let mask_input = cx.new_view(|cx| {
let mut input = TextInput::new(cx).cleanable(true); let mut input = TextInput::new(cx).cleanable(true);
input.set_masked(true, cx); input.set_masked(true, cx);
@ -93,7 +99,7 @@ impl InputStory {
Self { Self {
input1, input1,
input2: cx.new_view(|cx| TextInput::new(cx).placeholder("Enter text here...")), input2,
mash_input: mask_input, mash_input: mask_input,
disabled_input: cx.new_view(|cx| { disabled_input: cx.new_view(|cx| {
let mut input = TextInput::new(cx); let mut input = TextInput::new(cx);
@ -132,6 +138,20 @@ impl InputStory {
fn tab_prev(&mut self, _: &TabPrev, cx: &mut ViewContext<Self>) { fn tab_prev(&mut self, _: &TabPrev, cx: &mut ViewContext<Self>) {
self.cycle_focus(false, cx); self.cycle_focus(false, cx);
} }
fn on_input_event(
&mut self,
_: View<TextInput>,
event: &TextEvent,
_cx: &mut ViewContext<Self>,
) {
match event {
TextEvent::Input { text } => println!("Input: {}", text),
TextEvent::PressEnter => println!("PressEnter"),
TextEvent::Focus => println!("Focus"),
TextEvent::Blur => println!("Blur"),
};
}
} }
impl FocusableCycle for InputStory { impl FocusableCycle for InputStory {

View file

@ -50,6 +50,8 @@ actions!(
pub enum TextEvent { pub enum TextEvent {
Input { text: SharedString }, Input { text: SharedString },
PressEnter, PressEnter,
Focus,
Blur,
} }
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
@ -503,6 +505,7 @@ impl TextInput {
self.blink_cursor.update(cx, |cursor, cx| { self.blink_cursor.update(cx, |cursor, cx| {
cursor.start(cx); cursor.start(cx);
}); });
cx.emit(TextEvent::Focus);
} }
fn on_blur(&mut self, cx: &mut ViewContext<Self>) { fn on_blur(&mut self, cx: &mut ViewContext<Self>) {
@ -510,6 +513,7 @@ impl TextInput {
self.blink_cursor.update(cx, |cursor, cx| { self.blink_cursor.update(cx, |cursor, cx| {
cursor.stop(cx); cursor.stop(cx);
}); });
cx.emit(TextEvent::Blur);
} }
fn pause_blink_cursor(&mut self, cx: &mut ViewContext<Self>) { fn pause_blink_cursor(&mut self, cx: &mut ViewContext<Self>) {

View file

@ -160,13 +160,13 @@ where
event: &TextEvent, event: &TextEvent,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
#[allow(clippy::single_match)]
match event { match event {
TextEvent::Input { text } => { TextEvent::Input { text } => {
self.delegate.perform_search(&text.trim(), cx); self.delegate.perform_search(&text.trim(), cx);
cx.notify() cx.notify()
} }
TextEvent::PressEnter => self.action_confirm(&Confirm, cx), TextEvent::PressEnter => self.action_confirm(&Confirm, cx),
_ => {}
} }
} }