From 827ba5b16c4acb1b63535898d4ef7df5ea2b8703 Mon Sep 17 00:00:00 2001 From: JL710 <76447362+JL710@users.noreply.github.com> Date: Wed, 17 Apr 2024 13:42:35 +0200 Subject: Add `*_maybe` helper methods for `TextInput` --- widget/src/text_input.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'widget/src') diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 92047381..e129826c 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -137,6 +137,21 @@ where self } + /// Sets the message that should be produced when some text is typed into + /// the [`TextInput`], if `Some`. + /// + /// If `None`, the [`TextInput`] will be disabled. + pub fn on_input_maybe(mut self, callback: Option) -> Self + where + F: 'a + Fn(String) -> Message, + { + self.on_input = match callback { + Some(c) => Some(Box::new(c)), + None => None, + }; + self + } + /// Sets the message that should be produced when the [`TextInput`] is /// focused and the enter key is pressed. pub fn on_submit(mut self, message: Message) -> Self { @@ -144,6 +159,15 @@ where self } + /// Sets the message that should be produced when the [`TextInput`] is + /// focused and the enter key is pressed, if `Some`. + /// + /// If `None` the [`TextInput`] nothing will happen. + pub fn on_submit_maybe(mut self, on_submit: Option) -> Self { + self.on_submit = on_submit; + self + } + /// Sets the message that should be produced when some text is pasted into /// the [`TextInput`]. pub fn on_paste( @@ -154,6 +178,21 @@ where self } + /// Sets the message that should be produced when some text is pasted into + /// the [`TextInput`], if `Some`. + /// + /// If `None` nothing will happen. + pub fn on_paste_maybe( + mut self, + on_paste: Option Message + 'a>, + ) -> Self { + self.on_paste = match on_paste { + Some(func) => Some(Box::new(func)), + None => None, + }; + self + } + /// Sets the [`Font`] of the [`TextInput`]. /// /// [`Font`]: text::Renderer::Font -- cgit From 09174d5a25aaea3dcdf177689ac23576ef81b377 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Sep 2024 23:00:48 +0200 Subject: Simplify type signature of `TextInput` methods --- widget/src/text_input.rs | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'widget/src') diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index e129826c..0a8e6690 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -129,11 +129,11 @@ where /// the [`TextInput`]. /// /// If this method is not called, the [`TextInput`] will be disabled. - pub fn on_input(mut self, callback: F) -> Self - where - F: 'a + Fn(String) -> Message, - { - self.on_input = Some(Box::new(callback)); + pub fn on_input( + mut self, + on_input: impl Fn(String) -> Message + 'a, + ) -> Self { + self.on_input = Some(Box::new(on_input)); self } @@ -141,14 +141,11 @@ where /// the [`TextInput`], if `Some`. /// /// If `None`, the [`TextInput`] will be disabled. - pub fn on_input_maybe(mut self, callback: Option) -> Self - where - F: 'a + Fn(String) -> Message, - { - self.on_input = match callback { - Some(c) => Some(Box::new(c)), - None => None, - }; + pub fn on_input_maybe( + mut self, + on_input: Option Message + 'a>, + ) -> Self { + self.on_input = on_input.map(|f| Box::new(f) as _); self } @@ -161,8 +158,6 @@ where /// Sets the message that should be produced when the [`TextInput`] is /// focused and the enter key is pressed, if `Some`. - /// - /// If `None` the [`TextInput`] nothing will happen. pub fn on_submit_maybe(mut self, on_submit: Option) -> Self { self.on_submit = on_submit; self @@ -180,16 +175,11 @@ where /// Sets the message that should be produced when some text is pasted into /// the [`TextInput`], if `Some`. - /// - /// If `None` nothing will happen. pub fn on_paste_maybe( mut self, on_paste: Option Message + 'a>, ) -> Self { - self.on_paste = match on_paste { - Some(func) => Some(Box::new(func)), - None => None, - }; + self.on_paste = on_paste.map(|f| Box::new(f) as _); self } -- cgit