Add text input operations
This commit is contained in:
parent
23299a555f
commit
c4bca3f2af
8 changed files with 215 additions and 1 deletions
|
|
@ -260,6 +260,14 @@ where
|
||||||
) {
|
) {
|
||||||
self.operation.focusable(state, id);
|
self.operation.focusable(state, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn text_input(
|
||||||
|
&mut self,
|
||||||
|
state: &mut dyn widget::operation::TextInput,
|
||||||
|
id: Option<&widget::Id>,
|
||||||
|
) {
|
||||||
|
self.operation.text_input(state, id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.with_element(|element| {
|
self.with_element(|element| {
|
||||||
|
|
|
||||||
|
|
@ -324,6 +324,14 @@ where
|
||||||
) {
|
) {
|
||||||
self.operation.scrollable(state, id);
|
self.operation.scrollable(state, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn text_input(
|
||||||
|
&mut self,
|
||||||
|
state: &mut dyn widget::operation::TextInput,
|
||||||
|
id: Option<&widget::Id>,
|
||||||
|
) {
|
||||||
|
self.operation.text_input(state, id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.widget
|
self.widget
|
||||||
|
|
|
||||||
|
|
@ -178,6 +178,14 @@ where
|
||||||
) {
|
) {
|
||||||
self.operation.scrollable(state, id);
|
self.operation.scrollable(state, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn text_input(
|
||||||
|
&mut self,
|
||||||
|
state: &mut dyn widget::operation::TextInput,
|
||||||
|
id: Option<&widget::Id>,
|
||||||
|
) {
|
||||||
|
self.operation.text_input(state, id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.content
|
self.content
|
||||||
|
|
|
||||||
|
|
@ -93,4 +93,12 @@ where
|
||||||
) {
|
) {
|
||||||
self.operation.scrollable(state, id);
|
self.operation.scrollable(state, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn text_input(
|
||||||
|
&mut self,
|
||||||
|
state: &mut dyn operation::TextInput,
|
||||||
|
id: Option<&Id>,
|
||||||
|
) {
|
||||||
|
self.operation.text_input(state, id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
//! Query or update internal widget state.
|
//! Query or update internal widget state.
|
||||||
pub mod focusable;
|
pub mod focusable;
|
||||||
pub mod scrollable;
|
pub mod scrollable;
|
||||||
|
pub mod text_input;
|
||||||
|
|
||||||
pub use focusable::Focusable;
|
pub use focusable::Focusable;
|
||||||
pub use scrollable::Scrollable;
|
pub use scrollable::Scrollable;
|
||||||
|
pub use text_input::TextInput;
|
||||||
|
|
||||||
use crate::widget::Id;
|
use crate::widget::Id;
|
||||||
|
|
||||||
|
|
@ -28,6 +30,9 @@ pub trait Operation<T> {
|
||||||
/// Operates on a widget that can be scrolled.
|
/// Operates on a widget that can be scrolled.
|
||||||
fn scrollable(&mut self, _state: &mut dyn Scrollable, _id: Option<&Id>) {}
|
fn scrollable(&mut self, _state: &mut dyn Scrollable, _id: Option<&Id>) {}
|
||||||
|
|
||||||
|
/// Operates on a widget that has text input.
|
||||||
|
fn text_input(&mut self, _state: &mut dyn TextInput, _id: Option<&Id>) {}
|
||||||
|
|
||||||
/// Finishes the [`Operation`] and returns its [`Outcome`].
|
/// Finishes the [`Operation`] and returns its [`Outcome`].
|
||||||
fn finish(&self) -> Outcome<T> {
|
fn finish(&self) -> Outcome<T> {
|
||||||
Outcome::None
|
Outcome::None
|
||||||
|
|
|
||||||
131
native/src/widget/operation/text_input.rs
Normal file
131
native/src/widget/operation/text_input.rs
Normal file
|
|
@ -0,0 +1,131 @@
|
||||||
|
//! Operate on widgets that have text input.
|
||||||
|
use crate::widget::operation::Operation;
|
||||||
|
use crate::widget::Id;
|
||||||
|
|
||||||
|
/// The internal state of a widget that has text input.
|
||||||
|
pub trait TextInput {
|
||||||
|
/// Moves the cursor of the text input to the front of the input text.
|
||||||
|
fn move_cursor_to_front(&mut self);
|
||||||
|
/// Moves the cursor of the text input to the end of the input text.
|
||||||
|
fn move_cursor_to_end(&mut self);
|
||||||
|
/// Moves the cursor of the text input to an arbitrary location.
|
||||||
|
fn move_cursor_to(&mut self, position: usize);
|
||||||
|
/// Selects all the content of the text input.
|
||||||
|
fn select_all(&mut self);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produces an [`Operation`] that moves the cursor of the widget with the given [`Id`] to the
|
||||||
|
/// front.
|
||||||
|
pub fn move_cursor_to_front<T>(target: Id) -> impl Operation<T> {
|
||||||
|
struct MoveCursor {
|
||||||
|
target: Id,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Operation<T> for MoveCursor {
|
||||||
|
fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
|
||||||
|
match id {
|
||||||
|
Some(id) if id == &self.target => {
|
||||||
|
state.move_cursor_to_front();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn container(
|
||||||
|
&mut self,
|
||||||
|
_id: Option<&Id>,
|
||||||
|
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
|
||||||
|
) {
|
||||||
|
operate_on_children(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveCursor { target }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produces an [`Operation`] that moves the cursor of the widget with the given [`Id`] to the
|
||||||
|
/// end.
|
||||||
|
pub fn move_cursor_to_end<T>(target: Id) -> impl Operation<T> {
|
||||||
|
struct MoveCursor {
|
||||||
|
target: Id,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Operation<T> for MoveCursor {
|
||||||
|
fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
|
||||||
|
match id {
|
||||||
|
Some(id) if id == &self.target => {
|
||||||
|
state.move_cursor_to_end();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn container(
|
||||||
|
&mut self,
|
||||||
|
_id: Option<&Id>,
|
||||||
|
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
|
||||||
|
) {
|
||||||
|
operate_on_children(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveCursor { target }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produces an [`Operation`] that moves the cursor of the widget with the given [`Id`] to the
|
||||||
|
/// provided position.
|
||||||
|
pub fn move_cursor_to<T>(target: Id, position: usize) -> impl Operation<T> {
|
||||||
|
struct MoveCursor {
|
||||||
|
target: Id,
|
||||||
|
position: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Operation<T> for MoveCursor {
|
||||||
|
fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
|
||||||
|
match id {
|
||||||
|
Some(id) if id == &self.target => {
|
||||||
|
state.move_cursor_to(self.position);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn container(
|
||||||
|
&mut self,
|
||||||
|
_id: Option<&Id>,
|
||||||
|
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
|
||||||
|
) {
|
||||||
|
operate_on_children(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveCursor { target, position }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produces an [`Operation`] that selects all the content of the widget with the given [`Id`].
|
||||||
|
pub fn select_all<T>(target: Id) -> impl Operation<T> {
|
||||||
|
struct MoveCursor {
|
||||||
|
target: Id,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Operation<T> for MoveCursor {
|
||||||
|
fn text_input(&mut self, state: &mut dyn TextInput, id: Option<&Id>) {
|
||||||
|
match id {
|
||||||
|
Some(id) if id == &self.target => {
|
||||||
|
state.select_all();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn container(
|
||||||
|
&mut self,
|
||||||
|
_id: Option<&Id>,
|
||||||
|
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
|
||||||
|
) {
|
||||||
|
operate_on_children(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MoveCursor { target }
|
||||||
|
}
|
||||||
|
|
@ -233,6 +233,7 @@ where
|
||||||
let state = tree.state.downcast_mut::<State>();
|
let state = tree.state.downcast_mut::<State>();
|
||||||
|
|
||||||
operation.focusable(state, self.id.as_ref().map(|id| &id.0));
|
operation.focusable(state, self.id.as_ref().map(|id| &id.0));
|
||||||
|
operation.text_input(state, self.id.as_ref().map(|id| &id.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(
|
fn on_event(
|
||||||
|
|
@ -337,6 +338,32 @@ pub fn focus<Message: 'static>(id: Id) -> Command<Message> {
|
||||||
Command::widget(operation::focusable::focus(id.0))
|
Command::widget(operation::focusable::focus(id.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Produces a [`Command`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the
|
||||||
|
/// end.
|
||||||
|
pub fn move_cursor_to_end<Message: 'static>(id: Id) -> Command<Message> {
|
||||||
|
Command::widget(operation::text_input::move_cursor_to_end(id.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produces a [`Command`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the
|
||||||
|
/// front.
|
||||||
|
pub fn move_cursor_to_front<Message: 'static>(id: Id) -> Command<Message> {
|
||||||
|
Command::widget(operation::text_input::move_cursor_to_front(id.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produces a [`Command`] that moves the cursor of the [`TextInput`] with the given [`Id`] to the
|
||||||
|
/// provided position.
|
||||||
|
pub fn move_cursor_to<Message: 'static>(
|
||||||
|
id: Id,
|
||||||
|
position: usize,
|
||||||
|
) -> Command<Message> {
|
||||||
|
Command::widget(operation::text_input::move_cursor_to(id.0, position))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Produces a [`Command`] that selects all the content of the [`TextInput`] with the given [`Id`].
|
||||||
|
pub fn select_all<Message: 'static>(id: Id) -> Command<Message> {
|
||||||
|
Command::widget(operation::text_input::select_all(id.0))
|
||||||
|
}
|
||||||
|
|
||||||
/// Computes the layout of a [`TextInput`].
|
/// Computes the layout of a [`TextInput`].
|
||||||
pub fn layout<Renderer>(
|
pub fn layout<Renderer>(
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
|
|
@ -1001,6 +1028,24 @@ impl operation::Focusable for State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl operation::TextInput for State {
|
||||||
|
fn move_cursor_to_front(&mut self) {
|
||||||
|
State::move_cursor_to_front(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_cursor_to_end(&mut self) {
|
||||||
|
State::move_cursor_to_end(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_cursor_to(&mut self, position: usize) {
|
||||||
|
State::move_cursor_to(self, position)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn select_all(&mut self) {
|
||||||
|
State::select_all(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod platform {
|
mod platform {
|
||||||
use crate::keyboard;
|
use crate::keyboard;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,8 @@ pub mod toggler {
|
||||||
pub mod text_input {
|
pub mod text_input {
|
||||||
//! Display fields that can be filled with text.
|
//! Display fields that can be filled with text.
|
||||||
pub use iced_native::widget::text_input::{
|
pub use iced_native::widget::text_input::{
|
||||||
focus, Appearance, Id, StyleSheet,
|
focus, move_cursor_to, move_cursor_to_end, move_cursor_to_front,
|
||||||
|
select_all, Appearance, Id, StyleSheet,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A field that can be filled with text.
|
/// A field that can be filled with text.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue