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,165 +334,199 @@ 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 => {
keyboard::KeyCode::Enter => { match key_code {
if let Some(on_submit) = self.on_submit.clone() { keyboard::KeyCode::Enter => {
messages.push(on_submit); if let Some(on_submit) = self.on_submit.clone() {
} messages.push(on_submit);
}
keyboard::KeyCode::Backspace => {
if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection(&self.value).is_none()
{
if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value);
self.state.cursor.select_range(0, cursor_pos);
} else {
self.state.cursor.select_left_by_words(&self.value);
} }
} }
keyboard::KeyCode::Backspace => {
let mut editor = if platform::is_jump_modifier_pressed(modifiers)
Editor::new(&mut self.value, &mut self.state.cursor); && self
.state
editor.backspace();
let message = (self.on_change)(editor.contents());
messages.push(message);
}
keyboard::KeyCode::Delete => {
if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection(&self.value).is_none()
{
if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value);
self.state
.cursor .cursor
.select_range(cursor_pos, self.value.len()); .selection(&self.value)
} else { .is_none()
self.state {
.cursor if self.is_secure {
.select_right_by_words(&self.value); let cursor_pos =
self.state.cursor.end(&self.value);
self.state.cursor.select_range(0, cursor_pos);
} else {
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.delete();
let message = (self.on_change)(editor.contents());
messages.push(message);
}
keyboard::KeyCode::Left => {
if platform::is_jump_modifier_pressed(modifiers)
&& !self.is_secure
{
if modifiers.shift {
self.state.cursor.select_left_by_words(&self.value);
} else {
self.state.cursor.move_left_by_words(&self.value);
}
} else if modifiers.shift {
self.state.cursor.select_left(&self.value)
} else {
self.state.cursor.move_left(&self.value);
}
}
keyboard::KeyCode::Right => {
if platform::is_jump_modifier_pressed(modifiers)
&& !self.is_secure
{
if modifiers.shift {
self.state
.cursor
.select_right_by_words(&self.value);
} else {
self.state.cursor.move_right_by_words(&self.value);
}
} else if modifiers.shift {
self.state.cursor.select_right(&self.value)
} else {
self.state.cursor.move_right(&self.value);
}
}
keyboard::KeyCode::Home => {
if modifiers.shift {
self.state.cursor.select_range(
self.state.cursor.start(&self.value),
0,
); );
} else {
self.state.cursor.move_to(0); editor.backspace();
let message = (self.on_change)(editor.contents());
messages.push(message);
} }
} keyboard::KeyCode::Delete => {
keyboard::KeyCode::End => { if platform::is_jump_modifier_pressed(modifiers)
if modifiers.shift { && self
self.state.cursor.select_range( .state
self.state.cursor.start(&self.value), .cursor
self.value.len(), .selection(&self.value)
.is_none()
{
if self.is_secure {
let cursor_pos =
self.state.cursor.end(&self.value);
self.state
.cursor
.select_range(cursor_pos, self.value.len());
} else {
self.state
.cursor
.select_right_by_words(&self.value);
}
}
let mut editor = Editor::new(
&mut self.value,
&mut self.state.cursor,
); );
} else {
self.state.cursor.move_to(self.value.len()); editor.delete();
let message = (self.on_change)(editor.contents());
messages.push(message);
} }
} keyboard::KeyCode::Left => {
keyboard::KeyCode::V => { if platform::is_jump_modifier_pressed(modifiers)
if platform::is_copy_paste_modifier_pressed(modifiers) { && !self.is_secure
if let Some(clipboard) = clipboard { {
let content = match self.state.is_pasting.take() { if modifiers.shift {
Some(content) => content, self.state
None => { .cursor
let content: String = clipboard .select_left_by_words(&self.value);
.content() } else {
.unwrap_or(String::new()) self.state
.chars() .cursor
.filter(|c| !c.is_control()) .move_left_by_words(&self.value);
.collect(); }
} else if modifiers.shift {
Value::new(&content) self.state.cursor.select_left(&self.value)
} } else {
}; self.state.cursor.move_left(&self.value);
}
let mut editor = Editor::new( }
&mut self.value, keyboard::KeyCode::Right => {
&mut self.state.cursor, if platform::is_jump_modifier_pressed(modifiers)
&& !self.is_secure
{
if modifiers.shift {
self.state
.cursor
.select_right_by_words(&self.value);
} else {
self.state
.cursor
.move_right_by_words(&self.value);
}
} else if modifiers.shift {
self.state.cursor.select_right(&self.value)
} else {
self.state.cursor.move_right(&self.value);
}
}
keyboard::KeyCode::Home => {
if modifiers.shift {
self.state.cursor.select_range(
self.state.cursor.start(&self.value),
0,
); );
} else {
editor.paste(content.clone()); self.state.cursor.move_to(0);
let message = (self.on_change)(editor.contents());
messages.push(message);
self.state.is_pasting = Some(content);
} }
} else { }
keyboard::KeyCode::End => {
if modifiers.shift {
self.state.cursor.select_range(
self.state.cursor.start(&self.value),
self.value.len(),
);
} else {
self.state.cursor.move_to(self.value.len());
}
}
keyboard::KeyCode::V => {
if platform::is_copy_paste_modifier_pressed(modifiers) {
if let Some(clipboard) = clipboard {
let content = match self.state.is_pasting.take()
{
Some(content) => content,
None => {
let content: String = clipboard
.content()
.unwrap_or(String::new())
.chars()
.filter(|c| !c.is_control())
.collect();
Value::new(&content)
}
};
let mut editor = Editor::new(
&mut self.value,
&mut self.state.cursor,
);
editor.paste(content.clone());
let message =
(self.on_change)(editor.contents());
messages.push(message);
self.state.is_pasting = Some(content);
}
} else {
self.state.is_pasting = None;
}
}
keyboard::KeyCode::A => {
if platform::is_copy_paste_modifier_pressed(modifiers) {
self.state.cursor.select_all(&self.value);
}
}
keyboard::KeyCode::Escape => {
self.state.is_focused = false;
self.state.is_dragging = false;
self.state.is_pasting = None; self.state.is_pasting = None;
} }
_ => {}
} }
keyboard::KeyCode::A => {
if platform::is_copy_paste_modifier_pressed(modifiers) { return event::Status::Captured;
self.state.cursor.select_all(&self.value); }
}
}
keyboard::KeyCode::Escape => {
self.state.is_focused = false;
self.state.is_dragging = false;
self.state.is_pasting = None;
}
_ => {}
},
Event::Keyboard(keyboard::Event::KeyReleased { Event::Keyboard(keyboard::Event::KeyReleased {
key_code, .. key_code, ..
}) => match key_code { }) if self.state.is_focused => {
keyboard::KeyCode::V => { match key_code {
self.state.is_pasting = None; keyboard::KeyCode::V => {
self.state.is_pasting = None;
}
_ => {}
} }
_ => {}
}, return event::Status::Captured;
}
_ => {} _ => {}
} }