From 55a71a5b72238a5a94136a2495304c48ff9f4af5 Mon Sep 17 00:00:00 2001 From: tsuza Date: Sat, 19 Oct 2024 01:35:10 +0200 Subject: [PATCH 1/3] feat: add on_key_press to the text input widget --- widget/src/text_input.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index ff41377946..7f375fdc0c 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -118,6 +118,8 @@ pub struct TextInput< on_input: Option Message + 'a>>, on_paste: Option Message + 'a>>, on_submit: Option, + on_key_press: + Option Message + 'a>>, icon: Option>, class: Theme::Class<'a>, } @@ -148,6 +150,7 @@ where on_input: None, on_paste: None, on_submit: None, + on_key_press: None, icon: None, class: Theme::default(), } @@ -223,6 +226,16 @@ where self } + /// Sets the message that should be produced when a key is pressed + /// when the [`TextInput`] is focused. + pub fn on_key_press( + mut self, + on_key_press: impl Fn(keyboard::Key, keyboard::Modifiers) -> Message + 'a, + ) -> Self { + self.on_key_press = Some(Box::new(on_key_press)); + self + } + /// Sets the [`Font`] of the [`TextInput`]. /// /// [`Font`]: text::Renderer::Font @@ -817,6 +830,13 @@ where let modifiers = state.keyboard_modifiers; focus.updated_at = Instant::now(); + if let Some(on_key_press) = &self.on_key_press { + let message: Message = + (on_key_press)(key.clone(), modifiers.clone()); + + shell.publish(message); + } + match key.as_ref() { keyboard::Key::Character("c") if state.keyboard_modifiers.command() From c0a0aaadc1ea8bd267b1e25831293d2457b01a56 Mon Sep 17 00:00:00 2001 From: tsuza Date: Sat, 19 Oct 2024 01:42:59 +0200 Subject: [PATCH 2/3] docs: improve the function's documentation --- widget/src/text_input.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 7f375fdc0c..395048d9dd 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -226,8 +226,8 @@ where self } - /// Sets the message that should be produced when a key is pressed - /// when the [`TextInput`] is focused. + /// Sets the message that should be produced when the [`TextInput`] is + /// focused and a key is pressed. pub fn on_key_press( mut self, on_key_press: impl Fn(keyboard::Key, keyboard::Modifiers) -> Message + 'a, From be95f1be5a96fd047aba410ed50c2d8c239a991d Mon Sep 17 00:00:00 2001 From: tsuza Date: Sat, 19 Oct 2024 02:02:48 +0200 Subject: [PATCH 3/3] style: remove an unnecessary type annotation I accidentally right clicked it with my mouse --- widget/src/text_input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 395048d9dd..c4265f376e 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -831,7 +831,7 @@ where focus.updated_at = Instant::now(); if let Some(on_key_press) = &self.on_key_press { - let message: Message = + let message = (on_key_press)(key.clone(), modifiers.clone()); shell.publish(message);