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
157
158
159
160
161
162
|
use crate::{
input::{keyboard, mouse, ButtonState},
layout, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size,
Widget,
};
pub use iced_core::{text_input::State, TextInput};
impl<'a, Message, Renderer> Widget<Message, Renderer> for TextInput<'a, Message>
where
Renderer: self::Renderer,
Message: Clone + std::fmt::Debug,
{
fn width(&self) -> Length {
self.width
}
fn layout(
&self,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
let padding = self.padding as f32;
let text_size = self.size.unwrap_or(renderer.default_size());
let limits = limits
.pad(padding)
.width(self.width)
.height(Length::Units(text_size));
let mut text = layout::Node::new(limits.resolve(Size::ZERO));
text.bounds.x = padding;
text.bounds.y = padding;
layout::Node::with_children(text.size().pad(padding), vec![text])
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
_renderer: &Renderer,
) {
match event {
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state: ButtonState::Pressed,
}) => {
self.state.is_focused =
layout.bounds().contains(cursor_position);
if self.state.cursor_position(&self.value) == 0 {
self.state.move_cursor_to_end(&self.value);
}
}
Event::Keyboard(keyboard::Event::CharacterReceived(c))
if self.state.is_focused && !c.is_control() =>
{
let cursor_position = self.state.cursor_position(&self.value);
self.value.insert(cursor_position, c);
self.state.move_cursor_right(&self.value);
let message = (self.on_change)(self.value.to_string());
messages.push(message);
}
Event::Keyboard(keyboard::Event::Input {
key_code,
state: ButtonState::Pressed,
}) if self.state.is_focused => match key_code {
keyboard::KeyCode::Enter => {
if let Some(on_submit) = self.on_submit.clone() {
messages.push(on_submit);
}
}
keyboard::KeyCode::Backspace => {
let cursor_position =
self.state.cursor_position(&self.value);
if cursor_position > 0 {
self.state.move_cursor_left(&self.value);
let _ = self.value.remove(cursor_position - 1);
let message = (self.on_change)(self.value.to_string());
messages.push(message);
}
}
keyboard::KeyCode::Delete => {
let cursor_position =
self.state.cursor_position(&self.value);
if cursor_position < self.value.len() {
let _ = self.value.remove(cursor_position);
let message = (self.on_change)(self.value.to_string());
messages.push(message);
}
}
keyboard::KeyCode::Left => {
self.state.move_cursor_left(&self.value);
}
keyboard::KeyCode::Right => {
self.state.move_cursor_right(&self.value);
}
_ => {}
},
_ => {}
}
}
fn draw(
&self,
renderer: &mut Renderer,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
let bounds = layout.bounds();
let text_bounds = layout.children().next().unwrap().bounds();
renderer.draw(&self, bounds, text_bounds, cursor_position)
}
fn hash_layout(&self, state: &mut Hasher) {
use std::any::TypeId;
use std::hash::Hash;
TypeId::of::<TextInput<'static, ()>>().hash(state);
self.width.hash(state);
self.max_width.hash(state);
self.padding.hash(state);
self.size.hash(state);
}
}
pub trait Renderer: crate::Renderer + Sized {
fn default_size(&self) -> u16;
fn draw<Message>(
&mut self,
text_input: &TextInput<'_, Message>,
bounds: Rectangle,
text_bounds: Rectangle,
cursor_position: Point,
) -> Self::Output;
}
impl<'a, Message, Renderer> From<TextInput<'a, Message>>
for Element<'a, Message, Renderer>
where
Renderer: 'static + self::Renderer,
Message: 'static + Clone + std::fmt::Debug,
{
fn from(
text_input: TextInput<'a, Message>,
) -> Element<'a, Message, Renderer> {
Element::new(text_input)
}
}
|