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 select_all method to TextInput. #776

Merged
merged 3 commits into from
Jul 22, 2021
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
5 changes: 4 additions & 1 deletion examples/todos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,11 @@ impl Task {
self.completed = completed;
}
TaskMessage::Edit => {
let mut text_input = text_input::State::focused();
text_input.select_all();

self.state = TaskState::Editing {
text_input: text_input::State::focused(),
text_input,
delete_button: button::State::new(),
};
}
Expand Down
5 changes: 5 additions & 0 deletions native/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,11 @@ impl State {
pub fn move_cursor_to(&mut self, position: usize) {
self.cursor.move_to(position);
}

/// Selects all the content of the [`TextInput`].
pub fn select_all(&mut self) {
self.cursor.select_range(0, usize::MAX);
}
Comment on lines +797 to +799
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to expose a more general select function, accepting both the start and the end, then in case you needed to select all you would use it just like you currently call cursor.select_range.

I'm not sure if there's a way to clip the range to the actual text size, since the Value is not available for the State, but right now this is done anyway on the Renderer: https://github.com/hecrj/iced/blob/f076649fbb575ee036ab0b3f4511690e3379c115/graphics/src/widget/text_input.rs#L157-L158

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think select_all satisfies a clear use case.

We can expose a select_range or similar and focus on those use cases in a different unit of work.

}

// TODO: Reduce allocations
Expand Down
5 changes: 5 additions & 0 deletions web/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,9 @@ impl State {
// TODO
Self::default()
}

/// Selects all the content of the [`TextInput`].
pub fn select_all(&mut self) {
// TODO
}
}