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::{
button::Button,
h_flex,
input::{InputOtp, TextInput},
input::{InputOtp, TextEvent, TextInput},
prelude::FluentBuilder as _,
v_flex, Clickable, FocusableCycle, IconName, Size,
};
@ -53,6 +53,12 @@ impl InputStory {
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 mut input = TextInput::new(cx).cleanable(true);
input.set_masked(true, cx);
@ -93,7 +99,7 @@ impl InputStory {
Self {
input1,
input2: cx.new_view(|cx| TextInput::new(cx).placeholder("Enter text here...")),
input2,
mash_input: mask_input,
disabled_input: cx.new_view(|cx| {
let mut input = TextInput::new(cx);
@ -132,6 +138,20 @@ impl InputStory {
fn tab_prev(&mut self, _: &TabPrev, cx: &mut ViewContext<Self>) {
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 {

View file

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

View file

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