diff options
author | 2024-09-07 23:09:19 +0200 | |
---|---|---|
committer | 2024-09-07 23:09:19 +0200 | |
commit | dff14bd440732e4aef4994492242ffc83c54a6a3 (patch) | |
tree | cc12d38a2c7538f164ab7c4e4bb63982ab0fc2ac /widget | |
parent | b3b76ed42f7735f201ad1571793ab224b8674f7c (diff) | |
parent | 09174d5a25aaea3dcdf177689ac23576ef81b377 (diff) | |
download | iced-dff14bd440732e4aef4994492242ffc83c54a6a3.tar.gz iced-dff14bd440732e4aef4994492242ffc83c54a6a3.tar.bz2 iced-dff14bd440732e4aef4994492242ffc83c54a6a3.zip |
Merge pull request #2390 from JL710/TextInput-Maybe
Text input maybe
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/text_input.rs | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 92047381..0a8e6690 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -129,11 +129,23 @@ where /// the [`TextInput`]. /// /// If this method is not called, the [`TextInput`] will be disabled. - pub fn on_input<F>(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 + } + + /// 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, + on_input: Option<impl Fn(String) -> Message + 'a>, + ) -> Self { + self.on_input = on_input.map(|f| Box::new(f) as _); self } @@ -144,6 +156,13 @@ where self } + /// Sets the message that should be produced when the [`TextInput`] is + /// focused and the enter key is pressed, if `Some`. + pub fn on_submit_maybe(mut self, on_submit: Option<Message>) -> 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 +173,16 @@ where self } + /// Sets the message that should be produced when some text is pasted into + /// the [`TextInput`], if `Some`. + pub fn on_paste_maybe( + mut self, + on_paste: Option<impl Fn(String) -> Message + 'a>, + ) -> Self { + self.on_paste = on_paste.map(|f| Box::new(f) as _); + self + } + /// Sets the [`Font`] of the [`TextInput`]. /// /// [`Font`]: text::Renderer::Font |