diff --git a/crates/story/src/input_story.rs b/crates/story/src/input_story.rs index c62057fc..e75f5a6a 100644 --- a/crates/story/src/input_story.rs +++ b/crates/story/src/input_story.rs @@ -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.cycle_focus(false, cx); } + + fn on_input_event( + &mut self, + _: View, + event: &TextEvent, + _cx: &mut ViewContext, + ) { + match event { + TextEvent::Input { text } => println!("Input: {}", text), + TextEvent::PressEnter => println!("PressEnter"), + TextEvent::Focus => println!("Focus"), + TextEvent::Blur => println!("Blur"), + }; + } } impl FocusableCycle for InputStory { diff --git a/crates/ui/src/input/input.rs b/crates/ui/src/input/input.rs index f083d330..657c85b7 100644 --- a/crates/ui/src/input/input.rs +++ b/crates/ui/src/input/input.rs @@ -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) { @@ -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) { diff --git a/crates/ui/src/list/list.rs b/crates/ui/src/list/list.rs index 77b79d9e..33ffc498 100644 --- a/crates/ui/src/list/list.rs +++ b/crates/ui/src/list/list.rs @@ -160,13 +160,13 @@ where event: &TextEvent, cx: &mut ViewContext, ) { - #[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), + _ => {} } }