Skip to content

Commit

Permalink
Add *_maybe helper methods for TextInput
Browse files Browse the repository at this point in the history
  • Loading branch information
JL710 authored and hecrj committed Sep 7, 2024
1 parent 9426418 commit 827ba5b
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,37 @@ 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<F>(mut self, callback: Option<F>) -> 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 {
self.on_submit = Some(message);
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<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(
Expand All @@ -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<impl Fn(String) -> 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
Expand Down

0 comments on commit 827ba5b

Please sign in to comment.