iced_core/widget/operation/text_input.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
//! Operate on widgets that have text input.
use crate::widget::operation::Operation;
use crate::widget::Id;
use crate::Rectangle;
/// 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,
id: Option<&Id>,
_bounds: Rectangle,
state: &mut dyn TextInput,
) {
match id {
Some(id) if id == &self.target => {
state.move_cursor_to_front();
}
_ => {}
}
}
fn container(
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
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,
id: Option<&Id>,
_bounds: Rectangle,
state: &mut dyn TextInput,
) {
match id {
Some(id) if id == &self.target => {
state.move_cursor_to_end();
}
_ => {}
}
}
fn container(
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
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,
id: Option<&Id>,
_bounds: Rectangle,
state: &mut dyn TextInput,
) {
match id {
Some(id) if id == &self.target => {
state.move_cursor_to(self.position);
}
_ => {}
}
}
fn container(
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
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,
id: Option<&Id>,
_bounds: Rectangle,
state: &mut dyn TextInput,
) {
match id {
Some(id) if id == &self.target => {
state.select_all();
}
_ => {}
}
}
fn container(
&mut self,
_id: Option<&Id>,
_bounds: Rectangle,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
) {
operate_on_children(self);
}
}
MoveCursor { target }
}