Implement event capturing for `TextInput`

This commit is contained in:
Héctor Ramón Jiménez 2020-11-12 01:11:09 +01:00
parent c361fe48c7
commit bf6c65b5ad
1 changed files with 179 additions and 141 deletions

View File

@ -225,6 +225,9 @@ where
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
let is_clicked = layout.bounds().contains(cursor_position); let is_clicked = layout.bounds().contains(cursor_position);
self.state.is_dragging = is_clicked;
self.state.is_focused = is_clicked;
if is_clicked { if is_clicked {
let text_layout = layout.children().next().unwrap(); let text_layout = layout.children().next().unwrap();
let target = cursor_position.x - text_layout.bounds().x; let target = cursor_position.x - text_layout.bounds().x;
@ -282,10 +285,9 @@ where
} }
self.state.last_click = Some(click); self.state.last_click = Some(click);
}
self.state.is_dragging = is_clicked; return event::Status::Captured;
self.state.is_focused = is_clicked; }
} }
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => { Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
self.state.is_dragging = false; self.state.is_dragging = false;
@ -316,6 +318,8 @@ where
position, position,
); );
} }
return event::Status::Captured;
} }
} }
Event::Keyboard(keyboard::Event::CharacterReceived(c)) Event::Keyboard(keyboard::Event::CharacterReceived(c))
@ -330,11 +334,14 @@ where
let message = (self.on_change)(editor.contents()); let message = (self.on_change)(editor.contents());
messages.push(message); messages.push(message);
return event::Status::Captured;
} }
Event::Keyboard(keyboard::Event::KeyPressed { Event::Keyboard(keyboard::Event::KeyPressed {
key_code, key_code,
modifiers, modifiers,
}) if self.state.is_focused => match key_code { }) if self.state.is_focused => {
match key_code {
keyboard::KeyCode::Enter => { keyboard::KeyCode::Enter => {
if let Some(on_submit) = self.on_submit.clone() { if let Some(on_submit) = self.on_submit.clone() {
messages.push(on_submit); messages.push(on_submit);
@ -342,18 +349,27 @@ where
} }
keyboard::KeyCode::Backspace => { keyboard::KeyCode::Backspace => {
if platform::is_jump_modifier_pressed(modifiers) if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection(&self.value).is_none() && self
.state
.cursor
.selection(&self.value)
.is_none()
{ {
if self.is_secure { if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value); let cursor_pos =
self.state.cursor.end(&self.value);
self.state.cursor.select_range(0, cursor_pos); self.state.cursor.select_range(0, cursor_pos);
} else { } else {
self.state.cursor.select_left_by_words(&self.value); self.state
.cursor
.select_left_by_words(&self.value);
} }
} }
let mut editor = let mut editor = Editor::new(
Editor::new(&mut self.value, &mut self.state.cursor); &mut self.value,
&mut self.state.cursor,
);
editor.backspace(); editor.backspace();
@ -362,10 +378,15 @@ where
} }
keyboard::KeyCode::Delete => { keyboard::KeyCode::Delete => {
if platform::is_jump_modifier_pressed(modifiers) if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection(&self.value).is_none() && self
.state
.cursor
.selection(&self.value)
.is_none()
{ {
if self.is_secure { if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value); let cursor_pos =
self.state.cursor.end(&self.value);
self.state self.state
.cursor .cursor
.select_range(cursor_pos, self.value.len()); .select_range(cursor_pos, self.value.len());
@ -376,8 +397,10 @@ where
} }
} }
let mut editor = let mut editor = Editor::new(
Editor::new(&mut self.value, &mut self.state.cursor); &mut self.value,
&mut self.state.cursor,
);
editor.delete(); editor.delete();
@ -389,9 +412,13 @@ where
&& !self.is_secure && !self.is_secure
{ {
if modifiers.shift { if modifiers.shift {
self.state.cursor.select_left_by_words(&self.value); self.state
.cursor
.select_left_by_words(&self.value);
} else { } else {
self.state.cursor.move_left_by_words(&self.value); self.state
.cursor
.move_left_by_words(&self.value);
} }
} else if modifiers.shift { } else if modifiers.shift {
self.state.cursor.select_left(&self.value) self.state.cursor.select_left(&self.value)
@ -408,7 +435,9 @@ where
.cursor .cursor
.select_right_by_words(&self.value); .select_right_by_words(&self.value);
} else { } else {
self.state.cursor.move_right_by_words(&self.value); self.state
.cursor
.move_right_by_words(&self.value);
} }
} else if modifiers.shift { } else if modifiers.shift {
self.state.cursor.select_right(&self.value) self.state.cursor.select_right(&self.value)
@ -439,7 +468,8 @@ where
keyboard::KeyCode::V => { keyboard::KeyCode::V => {
if platform::is_copy_paste_modifier_pressed(modifiers) { if platform::is_copy_paste_modifier_pressed(modifiers) {
if let Some(clipboard) = clipboard { if let Some(clipboard) = clipboard {
let content = match self.state.is_pasting.take() { let content = match self.state.is_pasting.take()
{
Some(content) => content, Some(content) => content,
None => { None => {
let content: String = clipboard let content: String = clipboard
@ -460,7 +490,8 @@ where
editor.paste(content.clone()); editor.paste(content.clone());
let message = (self.on_change)(editor.contents()); let message =
(self.on_change)(editor.contents());
messages.push(message); messages.push(message);
self.state.is_pasting = Some(content); self.state.is_pasting = Some(content);
@ -480,15 +511,22 @@ where
self.state.is_pasting = None; self.state.is_pasting = None;
} }
_ => {} _ => {}
}, }
return event::Status::Captured;
}
Event::Keyboard(keyboard::Event::KeyReleased { Event::Keyboard(keyboard::Event::KeyReleased {
key_code, .. key_code, ..
}) => match key_code { }) if self.state.is_focused => {
match key_code {
keyboard::KeyCode::V => { keyboard::KeyCode::V => {
self.state.is_pasting = None; self.state.is_pasting = None;
} }
_ => {} _ => {}
}, }
return event::Status::Captured;
}
_ => {} _ => {}
} }