Rename on_change to on_input for TextInput
This commit is contained in:
parent
f10e936f00
commit
e6a93e960c
12 changed files with 205 additions and 212 deletions
|
|
@ -142,7 +142,7 @@ mod numeric_input {
|
|||
.as_deref()
|
||||
.unwrap_or(""),
|
||||
)
|
||||
.on_change(Event::InputChanged)
|
||||
.on_input(Event::InputChanged)
|
||||
.padding(10),
|
||||
button("+", Event::IncrementPressed),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ impl Program for Controls {
|
|||
)
|
||||
.push(
|
||||
text_input("Placeholder", text)
|
||||
.on_change(Message::TextChanged),
|
||||
.on_input(Message::TextChanged),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -215,7 +215,7 @@ impl Sandbox for App {
|
|||
scrollable(options).height(Length::Fill),
|
||||
row![
|
||||
text_input("Add a new option", &self.input)
|
||||
.on_change(Message::InputChanged)
|
||||
.on_input(Message::InputChanged)
|
||||
.on_submit(Message::AddItem(self.input.clone())),
|
||||
button(text(format!("Toggle Order ({})", self.order)))
|
||||
.on_press(Message::ToggleOrder)
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ impl Application for App {
|
|||
column![
|
||||
text("Email").size(12),
|
||||
text_input("abc@123.com", &self.email,)
|
||||
.on_change(Message::Email)
|
||||
.on_input(Message::Email)
|
||||
.on_submit(Message::Submit)
|
||||
.padding(5),
|
||||
]
|
||||
|
|
@ -142,7 +142,7 @@ impl Application for App {
|
|||
column
|
||||
|
|
@ -65,7 +65,7 @@ where
|
|||
width: Length,
|
||||
padding: Padding,
|
||||
size: Option<f32>,
|
||||
on_change: Option<Box<dyn Fn(String) -> Message + 'a>>,
|
||||
on_input: Option<Box<dyn Fn(String) -> Message + 'a>>,
|
||||
on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
|
||||
on_submit: Option<Message>,
|
||||
icon: Option<Icon<Renderer::Font>>,
|
||||
|
|
@ -93,7 +93,7 @@ where
|
|||
width: Length::Fill,
|
||||
padding: Padding::new(5.0),
|
||||
size: None,
|
||||
on_change: None,
|
||||
on_input: None,
|
||||
on_paste: None,
|
||||
on_submit: None,
|
||||
icon: None,
|
||||
|
|
@ -113,6 +113,23 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets the callback which is called when the text gets changed
|
||||
/// If not specified, the widget 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));
|
||||
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 some text is pasted into
|
||||
/// the [`TextInput`].
|
||||
pub fn on_paste(
|
||||
|
|
@ -155,13 +172,6 @@ where
|
|||
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 style of the [`TextInput`].
|
||||
pub fn style(
|
||||
mut self,
|
||||
|
|
@ -171,16 +181,6 @@ where
|
|||
self
|
||||
}
|
||||
|
||||
/// Sets the callback which is called when the text gets changed
|
||||
/// If not specified, the widget will be disabled
|
||||
pub fn on_change<F>(mut self, callback: F) -> Self
|
||||
where
|
||||
F: 'a + Fn(String) -> Message,
|
||||
{
|
||||
self.on_change = Some(Box::new(callback));
|
||||
self
|
||||
}
|
||||
|
||||
/// Draws the [`TextInput`] with the given [`Renderer`], overriding its
|
||||
/// [`Value`] if provided.
|
||||
///
|
||||
|
|
@ -204,10 +204,10 @@ where
|
|||
&self.placeholder,
|
||||
self.size,
|
||||
&self.font,
|
||||
self.on_input.is_none(),
|
||||
self.is_secure,
|
||||
self.icon.as_ref(),
|
||||
&self.style,
|
||||
self.on_change.is_none(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -284,7 +284,7 @@ where
|
|||
self.size,
|
||||
&self.font,
|
||||
self.is_secure,
|
||||
self.on_change.as_deref(),
|
||||
self.on_input.as_deref(),
|
||||
self.on_paste.as_deref(),
|
||||
&self.on_submit,
|
||||
|| tree.state.downcast_mut::<State>(),
|
||||
|
|
@ -311,10 +311,10 @@ where
|
|||
&self.placeholder,
|
||||
self.size,
|
||||
&self.font,
|
||||
self.on_input.is_none(),
|
||||
self.is_secure,
|
||||
self.icon.as_ref(),
|
||||
&self.style,
|
||||
self.on_change.is_none(),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
@ -500,7 +500,7 @@ pub fn update<'a, Message, Renderer>(
|
|||
size: Option<f32>,
|
||||
font: &Renderer::Font,
|
||||
is_secure: bool,
|
||||
on_change: Option<&dyn Fn(String) -> Message>,
|
||||
on_input: Option<&dyn Fn(String) -> Message>,
|
||||
on_paste: Option<&dyn Fn(String) -> Message>,
|
||||
on_submit: &Option<Message>,
|
||||
state: impl FnOnce() -> &'a mut State,
|
||||
|
|
@ -509,14 +509,12 @@ where
|
|||
Message: Clone,
|
||||
Renderer: text::Renderer,
|
||||
{
|
||||
let is_disabled = on_change.is_none();
|
||||
|
||||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
let state = state();
|
||||
let is_clicked = layout.bounds().contains(cursor_position);
|
||||
let is_clicked = is_clicked && !is_disabled;
|
||||
let is_clicked =
|
||||
layout.bounds().contains(cursor_position) && on_input.is_some();
|
||||
|
||||
state.is_focused = if is_clicked {
|
||||
state.is_focused.or_else(|| {
|
||||
|
|
@ -646,7 +644,8 @@ where
|
|||
let state = state();
|
||||
|
||||
if let Some(focus) = &mut state.is_focused {
|
||||
if let Some(on_change) = on_change {
|
||||
let Some(on_input) = on_input else { return event::Status::Ignored };
|
||||
|
||||
if state.is_pasting.is_none()
|
||||
&& !state.keyboard_modifiers.command()
|
||||
&& !c.is_control()
|
||||
|
|
@ -655,7 +654,7 @@ where
|
|||
|
||||
editor.insert(c);
|
||||
|
||||
let message = (on_change)(editor.contents());
|
||||
let message = (on_input)(editor.contents());
|
||||
shell.publish(message);
|
||||
|
||||
focus.updated_at = Instant::now();
|
||||
|
|
@ -664,12 +663,12 @@ where
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => {
|
||||
let state = state();
|
||||
|
||||
if let Some(focus) = &mut state.is_focused {
|
||||
if let Some(on_change) = on_change {
|
||||
let Some(on_input) = on_input else { return event::Status::Ignored };
|
||||
|
||||
let modifiers = state.keyboard_modifiers;
|
||||
focus.updated_at = Instant::now();
|
||||
|
||||
|
|
@ -692,11 +691,10 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
let mut editor =
|
||||
Editor::new(value, &mut state.cursor);
|
||||
let mut editor = Editor::new(value, &mut state.cursor);
|
||||
editor.backspace();
|
||||
|
||||
let message = (on_change)(editor.contents());
|
||||
let message = (on_input)(editor.contents());
|
||||
shell.publish(message);
|
||||
}
|
||||
keyboard::KeyCode::Delete => {
|
||||
|
|
@ -713,11 +711,10 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
let mut editor =
|
||||
Editor::new(value, &mut state.cursor);
|
||||
let mut editor = Editor::new(value, &mut state.cursor);
|
||||
editor.delete();
|
||||
|
||||
let message = (on_change)(editor.contents());
|
||||
let message = (on_input)(editor.contents());
|
||||
shell.publish(message);
|
||||
}
|
||||
keyboard::KeyCode::Left => {
|
||||
|
|
@ -775,9 +772,8 @@ where
|
|||
if let Some((start, end)) =
|
||||
state.cursor.selection(value)
|
||||
{
|
||||
clipboard.write(
|
||||
value.select(start, end).to_string(),
|
||||
);
|
||||
clipboard
|
||||
.write(value.select(start, end).to_string());
|
||||
}
|
||||
}
|
||||
keyboard::KeyCode::X
|
||||
|
|
@ -786,16 +782,14 @@ where
|
|||
if let Some((start, end)) =
|
||||
state.cursor.selection(value)
|
||||
{
|
||||
clipboard.write(
|
||||
value.select(start, end).to_string(),
|
||||
);
|
||||
clipboard
|
||||
.write(value.select(start, end).to_string());
|
||||
}
|
||||
|
||||
let mut editor =
|
||||
Editor::new(value, &mut state.cursor);
|
||||
let mut editor = Editor::new(value, &mut state.cursor);
|
||||
editor.delete();
|
||||
|
||||
let message = (on_change)(editor.contents());
|
||||
let message = (on_input)(editor.contents());
|
||||
shell.publish(message);
|
||||
}
|
||||
keyboard::KeyCode::V => {
|
||||
|
|
@ -822,7 +816,7 @@ where
|
|||
let message = if let Some(paste) = &on_paste {
|
||||
(paste)(editor.contents())
|
||||
} else {
|
||||
(on_change)(editor.contents())
|
||||
(on_input)(editor.contents())
|
||||
};
|
||||
shell.publish(message);
|
||||
|
||||
|
|
@ -851,7 +845,6 @@ where
|
|||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
return event::Status::Captured;
|
||||
}
|
||||
|
|
@ -917,10 +910,10 @@ pub fn draw<Renderer>(
|
|||
placeholder: &str,
|
||||
size: Option<f32>,
|
||||
font: &Renderer::Font,
|
||||
is_disabled: bool,
|
||||
is_secure: bool,
|
||||
icon: Option<&Icon<Renderer::Font>>,
|
||||
style: &<Renderer::Theme as StyleSheet>::Style,
|
||||
is_disabled: bool,
|
||||
) where
|
||||
Renderer: text::Renderer,
|
||||
Renderer::Theme: StyleSheet,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue