Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a paste Event to TextInput #1350

Merged
merged 3 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion native/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ where
padding: Padding,
size: Option<u16>,
on_change: Box<dyn Fn(String) -> Message + 'a>,
on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
on_submit: Option<Message>,
style: <Renderer::Theme as StyleSheet>::Style,
}
Expand Down Expand Up @@ -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(),
}
Expand All @@ -113,6 +115,16 @@ where
self
}

/// 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
Expand Down Expand Up @@ -225,6 +237,7 @@ pub fn update<'a, Message, Renderer>(
font: &Renderer::Font,
is_secure: bool,
on_change: &dyn Fn(String) -> Message,
on_paste: Option<&dyn Fn(String) -> Message>,
on_submit: &Option<Message>,
state: impl FnOnce() -> &'a mut State,
) -> event::Status
Expand Down Expand Up @@ -512,7 +525,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);
Expand Down Expand Up @@ -804,6 +821,7 @@ where
&self.font,
self.is_secure,
self.on_change.as_ref(),
self.on_paste.as_deref(),
&self.on_submit,
|| &mut self.state,
)
Expand Down
13 changes: 13 additions & 0 deletions pure/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ where
padding: Padding,
size: Option<u16>,
on_change: Box<dyn Fn(String) -> Message + 'a>,
on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
on_submit: Option<Message>,
style: <Renderer::Theme as StyleSheet>::Style,
}
Expand Down Expand Up @@ -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(),
}
Expand All @@ -86,6 +88,16 @@ where
self
}

/// 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
Expand Down Expand Up @@ -215,6 +227,7 @@ where
&self.font,
self.is_secure,
self.on_change.as_ref(),
self.on_paste.as_deref(),
&self.on_submit,
|| tree.state.downcast_mut::<text_input::State>(),
)
Expand Down