Rename on_change to on_input for TextInput

This commit is contained in:
Héctor Ramón Jiménez 2023-04-12 04:13:36 +02:00
parent f10e936f00
commit e6a93e960c
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
12 changed files with 205 additions and 212 deletions

View file

@ -142,7 +142,7 @@ mod numeric_input {
.as_deref() .as_deref()
.unwrap_or(""), .unwrap_or(""),
) )
.on_change(Event::InputChanged) .on_input(Event::InputChanged)
.padding(10), .padding(10),
button("+", Event::IncrementPressed), button("+", Event::IncrementPressed),
] ]

View file

@ -102,7 +102,7 @@ impl Program for Controls {
) )
.push( .push(
text_input("Placeholder", text) text_input("Placeholder", text)
.on_change(Message::TextChanged), .on_input(Message::TextChanged),
), ),
), ),
) )

View file

@ -215,7 +215,7 @@ impl Sandbox for App {
scrollable(options).height(Length::Fill), scrollable(options).height(Length::Fill),
row![ row![
text_input("Add a new option", &self.input) text_input("Add a new option", &self.input)
.on_change(Message::InputChanged) .on_input(Message::InputChanged)
.on_submit(Message::AddItem(self.input.clone())), .on_submit(Message::AddItem(self.input.clone())),
button(text(format!("Toggle Order ({})", self.order))) button(text(format!("Toggle Order ({})", self.order)))
.on_press(Message::ToggleOrder) .on_press(Message::ToggleOrder)

View file

@ -134,7 +134,7 @@ impl Application for App {
column![ column![
text("Email").size(12), text("Email").size(12),
text_input("abc@123.com", &self.email,) text_input("abc@123.com", &self.email,)
.on_change(Message::Email) .on_input(Message::Email)
.on_submit(Message::Submit) .on_submit(Message::Submit)
.padding(5), .padding(5),
] ]
@ -142,7 +142,7 @@ impl Application for App {
column![ column![
text("Password").size(12), text("Password").size(12),
text_input("", &self.password) text_input("", &self.password)
.on_change(Message::Password) .on_input(Message::Password)
.on_submit(Message::Submit) .on_submit(Message::Submit)
.password() .password()
.padding(5), .padding(5),

View file

@ -51,7 +51,7 @@ impl Sandbox for QRGenerator {
let input = let input =
text_input("Type the data of your QR code here...", &self.data) text_input("Type the data of your QR code here...", &self.data)
.on_change(Message::DataChanged) .on_input(Message::DataChanged)
.size(30) .size(30)
.padding(15); .padding(15);

View file

@ -91,7 +91,7 @@ impl Sandbox for Styling {
); );
let text_input = text_input("Type something...", &self.input_value) let text_input = text_input("Type something...", &self.input_value)
.on_change(Message::InputChanged) .on_input(Message::InputChanged)
.padding(10) .padding(10)
.size(20); .size(20);

View file

@ -74,7 +74,7 @@ impl Application for Example {
let mut txt_input = text_input(placeholder, &self.data); let mut txt_input = text_input(placeholder, &self.data);
if self.text_edit_enabled { if self.text_edit_enabled {
txt_input = txt_input.on_change(Message::TextInputChanged); txt_input = txt_input.on_input(Message::TextInputChanged);
} }
let btn = button("Enable/Disable").on_press(StartTimer); let btn = button("Enable/Disable").on_press(StartTimer);

View file

@ -120,14 +120,14 @@ impl Application for App {
subtitle( subtitle(
"Title", "Title",
text_input("", &self.editing.title) text_input("", &self.editing.title)
.on_change(Message::Title) .on_input(Message::Title)
.on_submit(Message::Add) .on_submit(Message::Add)
.into() .into()
), ),
subtitle( subtitle(
"Message", "Message",
text_input("", &self.editing.body) text_input("", &self.editing.body)
.on_change(Message::Body) .on_input(Message::Body)
.on_submit(Message::Add) .on_submit(Message::Add)
.into() .into()
), ),

View file

@ -205,11 +205,11 @@ impl Application for Todos {
.horizontal_alignment(alignment::Horizontal::Center); .horizontal_alignment(alignment::Horizontal::Center);
let input = text_input("What needs to be done?", input_value) let input = text_input("What needs to be done?", input_value)
.on_change(Message::InputChanged)
.id(INPUT_ID.clone()) .id(INPUT_ID.clone())
.on_input(Message::InputChanged)
.on_submit(Message::CreateTask)
.padding(15) .padding(15)
.size(30) .size(30);
.on_submit(Message::CreateTask);
let controls = view_controls(tasks, *filter); let controls = view_controls(tasks, *filter);
let filtered_tasks = let filtered_tasks =
@ -375,7 +375,7 @@ impl Task {
let text_input = let text_input =
text_input("Describe your task...", &self.description) text_input("Describe your task...", &self.description)
.id(Self::text_input_id(i)) .id(Self::text_input_id(i))
.on_change(TaskMessage::DescriptionEdited) .on_input(TaskMessage::DescriptionEdited)
.on_submit(TaskMessage::FinishEdition) .on_submit(TaskMessage::FinishEdition)
.padding(10); .padding(10);

View file

@ -571,7 +571,7 @@ impl<'a> Step {
}; };
let mut text_input = text_input("Type something to continue...", value) let mut text_input = text_input("Type something to continue...", value)
.on_change(StepMessage::InputChanged) .on_input(StepMessage::InputChanged)
.padding(10) .padding(10)
.size(30); .size(30);

View file

@ -126,7 +126,7 @@ impl Application for WebSocket {
let new_message_input = { let new_message_input = {
let mut input = text_input("Type a message...", &self.new_message) let mut input = text_input("Type a message...", &self.new_message)
.on_change(Message::NewMessageChanged) .on_input(Message::NewMessageChanged)
.padding(10); .padding(10);
let mut button = button( let mut button = button(

View file

@ -47,7 +47,7 @@ pub use iced_style::text_input::{Appearance, StyleSheet};
/// "This is the placeholder...", /// "This is the placeholder...",
/// value, /// value,
/// ) /// )
/// .on_change(|_| Message::TextInputChanged) /// .on_input(Message::TextInputChanged)
/// .padding(10); /// .padding(10);
/// ``` /// ```
/// ![Text input drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text_input.png?raw=true) /// ![Text input drawn by `iced_wgpu`](https://github.com/iced-rs/iced/blob/7760618fb112074bc40b148944521f312152012a/docs/images/text_input.png?raw=true)
@ -65,7 +65,7 @@ where
width: Length, width: Length,
padding: Padding, padding: Padding,
size: Option<f32>, 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_paste: Option<Box<dyn Fn(String) -> Message + 'a>>,
on_submit: Option<Message>, on_submit: Option<Message>,
icon: Option<Icon<Renderer::Font>>, icon: Option<Icon<Renderer::Font>>,
@ -93,7 +93,7 @@ where
width: Length::Fill, width: Length::Fill,
padding: Padding::new(5.0), padding: Padding::new(5.0),
size: None, size: None,
on_change: None, on_input: None,
on_paste: None, on_paste: None,
on_submit: None, on_submit: None,
icon: None, icon: None,
@ -113,6 +113,23 @@ where
self 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 /// Sets the message that should be produced when some text is pasted into
/// the [`TextInput`]. /// the [`TextInput`].
pub fn on_paste( pub fn on_paste(
@ -155,13 +172,6 @@ where
self 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`]. /// Sets the style of the [`TextInput`].
pub fn style( pub fn style(
mut self, mut self,
@ -171,16 +181,6 @@ where
self 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 /// Draws the [`TextInput`] with the given [`Renderer`], overriding its
/// [`Value`] if provided. /// [`Value`] if provided.
/// ///
@ -204,10 +204,10 @@ where
&self.placeholder, &self.placeholder,
self.size, self.size,
&self.font, &self.font,
self.on_input.is_none(),
self.is_secure, self.is_secure,
self.icon.as_ref(), self.icon.as_ref(),
&self.style, &self.style,
self.on_change.is_none(),
) )
} }
} }
@ -284,7 +284,7 @@ where
self.size, self.size,
&self.font, &self.font,
self.is_secure, self.is_secure,
self.on_change.as_deref(), self.on_input.as_deref(),
self.on_paste.as_deref(), self.on_paste.as_deref(),
&self.on_submit, &self.on_submit,
|| tree.state.downcast_mut::<State>(), || tree.state.downcast_mut::<State>(),
@ -311,10 +311,10 @@ where
&self.placeholder, &self.placeholder,
self.size, self.size,
&self.font, &self.font,
self.on_input.is_none(),
self.is_secure, self.is_secure,
self.icon.as_ref(), self.icon.as_ref(),
&self.style, &self.style,
self.on_change.is_none(),
) )
} }
@ -500,7 +500,7 @@ pub fn update<'a, Message, Renderer>(
size: Option<f32>, size: Option<f32>,
font: &Renderer::Font, font: &Renderer::Font,
is_secure: bool, 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_paste: Option<&dyn Fn(String) -> Message>,
on_submit: &Option<Message>, on_submit: &Option<Message>,
state: impl FnOnce() -> &'a mut State, state: impl FnOnce() -> &'a mut State,
@ -509,14 +509,12 @@ where
Message: Clone, Message: Clone,
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
let is_disabled = on_change.is_none();
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
let state = state(); let state = state();
let is_clicked = layout.bounds().contains(cursor_position); let is_clicked =
let is_clicked = is_clicked && !is_disabled; layout.bounds().contains(cursor_position) && on_input.is_some();
state.is_focused = if is_clicked { state.is_focused = if is_clicked {
state.is_focused.or_else(|| { state.is_focused.or_else(|| {
@ -646,22 +644,22 @@ where
let state = state(); let state = state();
if let Some(focus) = &mut state.is_focused { 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()
{
let mut editor = Editor::new(value, &mut state.cursor);
editor.insert(c); if state.is_pasting.is_none()
&& !state.keyboard_modifiers.command()
&& !c.is_control()
{
let mut editor = Editor::new(value, &mut state.cursor);
let message = (on_change)(editor.contents()); editor.insert(c);
shell.publish(message);
focus.updated_at = Instant::now(); let message = (on_input)(editor.contents());
shell.publish(message);
return event::Status::Captured; focus.updated_at = Instant::now();
}
return event::Status::Captured;
} }
} }
} }
@ -669,188 +667,183 @@ where
let state = state(); let state = state();
if let Some(focus) = &mut state.is_focused { 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();
match key_code { let modifiers = state.keyboard_modifiers;
keyboard::KeyCode::Enter focus.updated_at = Instant::now();
| keyboard::KeyCode::NumpadEnter => {
if let Some(on_submit) = on_submit.clone() { match key_code {
shell.publish(on_submit); keyboard::KeyCode::Enter
} | keyboard::KeyCode::NumpadEnter => {
if let Some(on_submit) = on_submit.clone() {
shell.publish(on_submit);
} }
keyboard::KeyCode::Backspace => { }
if platform::is_jump_modifier_pressed(modifiers) keyboard::KeyCode::Backspace => {
&& state.cursor.selection(value).is_none() if platform::is_jump_modifier_pressed(modifiers)
{ && state.cursor.selection(value).is_none()
if is_secure { {
let cursor_pos = state.cursor.end(value); if is_secure {
state.cursor.select_range(0, cursor_pos); let cursor_pos = state.cursor.end(value);
} else { state.cursor.select_range(0, cursor_pos);
state.cursor.select_left_by_words(value);
}
}
let mut editor =
Editor::new(value, &mut state.cursor);
editor.backspace();
let message = (on_change)(editor.contents());
shell.publish(message);
}
keyboard::KeyCode::Delete => {
if platform::is_jump_modifier_pressed(modifiers)
&& state.cursor.selection(value).is_none()
{
if is_secure {
let cursor_pos = state.cursor.end(value);
state
.cursor
.select_range(cursor_pos, value.len());
} else {
state.cursor.select_right_by_words(value);
}
}
let mut editor =
Editor::new(value, &mut state.cursor);
editor.delete();
let message = (on_change)(editor.contents());
shell.publish(message);
}
keyboard::KeyCode::Left => {
if platform::is_jump_modifier_pressed(modifiers)
&& !is_secure
{
if modifiers.shift() {
state.cursor.select_left_by_words(value);
} else {
state.cursor.move_left_by_words(value);
}
} else if modifiers.shift() {
state.cursor.select_left(value)
} else { } else {
state.cursor.move_left(value); state.cursor.select_left_by_words(value);
} }
} }
keyboard::KeyCode::Right => {
if platform::is_jump_modifier_pressed(modifiers) let mut editor = Editor::new(value, &mut state.cursor);
&& !is_secure editor.backspace();
{
if modifiers.shift() { let message = (on_input)(editor.contents());
state.cursor.select_right_by_words(value); shell.publish(message);
} else { }
state.cursor.move_right_by_words(value); keyboard::KeyCode::Delete => {
} if platform::is_jump_modifier_pressed(modifiers)
} else if modifiers.shift() { && state.cursor.selection(value).is_none()
state.cursor.select_right(value) {
} else { if is_secure {
state.cursor.move_right(value); let cursor_pos = state.cursor.end(value);
}
}
keyboard::KeyCode::Home => {
if modifiers.shift() {
state state
.cursor .cursor
.select_range(state.cursor.start(value), 0); .select_range(cursor_pos, value.len());
} else { } else {
state.cursor.move_to(0); state.cursor.select_right_by_words(value);
} }
} }
keyboard::KeyCode::End => {
let mut editor = Editor::new(value, &mut state.cursor);
editor.delete();
let message = (on_input)(editor.contents());
shell.publish(message);
}
keyboard::KeyCode::Left => {
if platform::is_jump_modifier_pressed(modifiers)
&& !is_secure
{
if modifiers.shift() { if modifiers.shift() {
state.cursor.select_range( state.cursor.select_left_by_words(value);
state.cursor.start(value),
value.len(),
);
} else { } else {
state.cursor.move_to(value.len()); state.cursor.move_left_by_words(value);
} }
} else if modifiers.shift() {
state.cursor.select_left(value)
} else {
state.cursor.move_left(value);
} }
keyboard::KeyCode::C }
if state.keyboard_modifiers.command() => keyboard::KeyCode::Right => {
if platform::is_jump_modifier_pressed(modifiers)
&& !is_secure
{ {
if let Some((start, end)) = if modifiers.shift() {
state.cursor.selection(value) state.cursor.select_right_by_words(value);
{ } else {
clipboard.write( state.cursor.move_right_by_words(value);
value.select(start, end).to_string(),
);
} }
} else if modifiers.shift() {
state.cursor.select_right(value)
} else {
state.cursor.move_right(value);
} }
keyboard::KeyCode::X }
if state.keyboard_modifiers.command() => keyboard::KeyCode::Home => {
if modifiers.shift() {
state
.cursor
.select_range(state.cursor.start(value), 0);
} else {
state.cursor.move_to(0);
}
}
keyboard::KeyCode::End => {
if modifiers.shift() {
state.cursor.select_range(
state.cursor.start(value),
value.len(),
);
} else {
state.cursor.move_to(value.len());
}
}
keyboard::KeyCode::C
if state.keyboard_modifiers.command() =>
{
if let Some((start, end)) =
state.cursor.selection(value)
{ {
if let Some((start, end)) = clipboard
state.cursor.selection(value) .write(value.select(start, end).to_string());
{ }
clipboard.write( }
value.select(start, end).to_string(), keyboard::KeyCode::X
); if state.keyboard_modifiers.command() =>
} {
if let Some((start, end)) =
state.cursor.selection(value)
{
clipboard
.write(value.select(start, end).to_string());
}
let mut editor = Editor::new(value, &mut state.cursor);
editor.delete();
let message = (on_input)(editor.contents());
shell.publish(message);
}
keyboard::KeyCode::V => {
if state.keyboard_modifiers.command() {
let content = match state.is_pasting.take() {
Some(content) => content,
None => {
let content: String = clipboard
.read()
.unwrap_or_default()
.chars()
.filter(|c| !c.is_control())
.collect();
Value::new(&content)
}
};
let mut editor = let mut editor =
Editor::new(value, &mut state.cursor); Editor::new(value, &mut state.cursor);
editor.delete();
let message = (on_change)(editor.contents()); editor.paste(content.clone());
shell.publish(message);
}
keyboard::KeyCode::V => {
if state.keyboard_modifiers.command() {
let content = match state.is_pasting.take() {
Some(content) => content,
None => {
let content: String = clipboard
.read()
.unwrap_or_default()
.chars()
.filter(|c| !c.is_control())
.collect();
Value::new(&content) let message = if let Some(paste) = &on_paste {
} (paste)(editor.contents())
};
let mut editor =
Editor::new(value, &mut state.cursor);
editor.paste(content.clone());
let message = if let Some(paste) = &on_paste {
(paste)(editor.contents())
} else {
(on_change)(editor.contents())
};
shell.publish(message);
state.is_pasting = Some(content);
} else { } else {
state.is_pasting = None; (on_input)(editor.contents())
} };
} shell.publish(message);
keyboard::KeyCode::A
if state.keyboard_modifiers.command() =>
{
state.cursor.select_all(value);
}
keyboard::KeyCode::Escape => {
state.is_focused = None;
state.is_dragging = false;
state.is_pasting = None;
state.keyboard_modifiers = state.is_pasting = Some(content);
keyboard::Modifiers::default(); } else {
state.is_pasting = None;
} }
keyboard::KeyCode::Tab
| keyboard::KeyCode::Up
| keyboard::KeyCode::Down => {
return event::Status::Ignored;
}
_ => {}
} }
keyboard::KeyCode::A
if state.keyboard_modifiers.command() =>
{
state.cursor.select_all(value);
}
keyboard::KeyCode::Escape => {
state.is_focused = None;
state.is_dragging = false;
state.is_pasting = None;
state.keyboard_modifiers =
keyboard::Modifiers::default();
}
keyboard::KeyCode::Tab
| keyboard::KeyCode::Up
| keyboard::KeyCode::Down => {
return event::Status::Ignored;
}
_ => {}
} }
return event::Status::Captured; return event::Status::Captured;
@ -917,10 +910,10 @@ pub fn draw<Renderer>(
placeholder: &str, placeholder: &str,
size: Option<f32>, size: Option<f32>,
font: &Renderer::Font, font: &Renderer::Font,
is_disabled: bool,
is_secure: bool, is_secure: bool,
icon: Option<&Icon<Renderer::Font>>, icon: Option<&Icon<Renderer::Font>>,
style: &<Renderer::Theme as StyleSheet>::Style, style: &<Renderer::Theme as StyleSheet>::Style,
is_disabled: bool,
) where ) where
Renderer: text::Renderer, Renderer: text::Renderer,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,