From e8cfa644e726a84c676154f7d0efb369da2a809f Mon Sep 17 00:00:00 2001 From: Wyatt Jacob Herkamp Date: Sat, 28 May 2022 16:07:02 -0400 Subject: Added `on_paste` handler to `TextInput` --- native/src/widget/text_input.rs | 19 +++++++++++++++++-- pure/src/widget/text_input.rs | 12 +++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 98265ef2..c131cc77 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -66,6 +66,7 @@ where padding: Padding, size: Option, on_change: Box Message + 'a>, + on_paste: Option Message + 'a>>, on_submit: Option, style: ::Style, } @@ -102,6 +103,7 @@ where padding: Padding::ZERO, size: None, on_change: Box::new(on_change), + on_paste: None, on_submit: None, style: Default::default(), } @@ -112,7 +114,14 @@ where self.is_secure = true; self } - + /// Set's the message that should be produced when a message is pasted into the [`TextInput`]. + pub fn on_paste(mut self, on_paste: OnPaste) -> Self + where + OnPaste: 'a + Fn(String) -> Message, + { + self.on_paste = Some(Box::new(on_paste)); + self + } /// Sets the [`Font`] of the [`TextInput`]. /// /// [`Font`]: crate::text::Renderer::Font @@ -225,6 +234,7 @@ pub fn update<'a, Message, Renderer>( font: &Renderer::Font, is_secure: bool, on_change: &dyn Fn(String) -> Message, + on_paste: &Option Message + 'a>>, on_submit: &Option, state: impl FnOnce() -> &'a mut State, ) -> event::Status @@ -512,7 +522,11 @@ where editor.paste(content.clone()); - let message = (on_change)(editor.contents()); + let message = if let Some(paste) = &on_paste { + (paste)(editor.contents()) + } else { + (on_change)(editor.contents()) + }; shell.publish(message); state.is_pasting = Some(content); @@ -804,6 +818,7 @@ where &self.font, self.is_secure, self.on_change.as_ref(), + &self.on_paste, &self.on_submit, || &mut self.state, ) diff --git a/pure/src/widget/text_input.rs b/pure/src/widget/text_input.rs index 9b0a466a..88726a52 100644 --- a/pure/src/widget/text_input.rs +++ b/pure/src/widget/text_input.rs @@ -46,6 +46,7 @@ where padding: Padding, size: Option, on_change: Box Message + 'a>, + on_paste: Option Message + 'a>>, on_submit: Option, style: ::Style, } @@ -75,6 +76,7 @@ where padding: Padding::ZERO, size: None, on_change: Box::new(on_change), + on_paste: None, on_submit: None, style: Default::default(), } @@ -85,7 +87,14 @@ where self.is_secure = true; self } - + /// Set's the message that should be produced when a message is pasted into the [`TextInput`]. + pub fn on_paste(mut self, on_paste: OnPaste) -> Self + where + OnPaste: 'a + Fn(String) -> Message, + { + self.on_paste = Some(Box::new(on_paste)); + self + } /// Sets the [`Font`] of the [`TextInput`]. /// /// [`Font`]: text::Renderer::Font @@ -215,6 +224,7 @@ where &self.font, self.is_secure, self.on_change.as_ref(), + &self.on_paste, &self.on_submit, || tree.state.downcast_mut::(), ) -- cgit From 891b9175769cb0ded3164841393ff599581f332f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 13 Jul 2022 18:35:41 +0200 Subject: Simplify `on_paste` signature ... and fix spacing. --- native/src/widget/text_input.rs | 13 ++++++++----- pure/src/widget/text_input.rs | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index c131cc77..9fc09b5d 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -114,14 +114,17 @@ where self.is_secure = true; self } - /// Set's the message that should be produced when a message is pasted into the [`TextInput`]. - pub fn on_paste(mut self, on_paste: OnPaste) -> Self - where - OnPaste: 'a + Fn(String) -> Message, - { + + /// Sets the message that should be produced when some text is pasted into + /// the [`TextInput`]. + pub fn on_paste( + mut self, + on_paste: impl Fn(String) -> Message + 'a, + ) -> Self { self.on_paste = Some(Box::new(on_paste)); self } + /// Sets the [`Font`] of the [`TextInput`]. /// /// [`Font`]: crate::text::Renderer::Font diff --git a/pure/src/widget/text_input.rs b/pure/src/widget/text_input.rs index 88726a52..34edef28 100644 --- a/pure/src/widget/text_input.rs +++ b/pure/src/widget/text_input.rs @@ -87,14 +87,17 @@ where self.is_secure = true; self } - /// Set's the message that should be produced when a message is pasted into the [`TextInput`]. - pub fn on_paste(mut self, on_paste: OnPaste) -> Self - where - OnPaste: 'a + Fn(String) -> Message, - { + + /// Sets the message that should be produced when some text is pasted into + /// the [`TextInput`]. + pub fn on_paste( + mut self, + on_paste: impl Fn(String) -> Message + 'a, + ) -> Self { self.on_paste = Some(Box::new(on_paste)); self } + /// Sets the [`Font`] of the [`TextInput`]. /// /// [`Font`]: text::Renderer::Font -- cgit From 54c9815b7b94e1863ebde272c53cc59c7e8be69d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 13 Jul 2022 18:37:27 +0200 Subject: Simplify `update` signature in `text_input` --- native/src/widget/text_input.rs | 4 ++-- pure/src/widget/text_input.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 9fc09b5d..fd360cd7 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -237,7 +237,7 @@ pub fn update<'a, Message, Renderer>( font: &Renderer::Font, is_secure: bool, on_change: &dyn Fn(String) -> Message, - on_paste: &Option Message + 'a>>, + on_paste: Option<&dyn Fn(String) -> Message>, on_submit: &Option, state: impl FnOnce() -> &'a mut State, ) -> event::Status @@ -821,7 +821,7 @@ where &self.font, self.is_secure, self.on_change.as_ref(), - &self.on_paste, + self.on_paste.as_deref(), &self.on_submit, || &mut self.state, ) diff --git a/pure/src/widget/text_input.rs b/pure/src/widget/text_input.rs index 34edef28..514a6795 100644 --- a/pure/src/widget/text_input.rs +++ b/pure/src/widget/text_input.rs @@ -227,7 +227,7 @@ where &self.font, self.is_secure, self.on_change.as_ref(), - &self.on_paste, + self.on_paste.as_deref(), &self.on_submit, || tree.state.downcast_mut::(), ) -- cgit