summaryrefslogtreecommitdiffstats
path: root/widget/src/text_editor.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-13 16:11:43 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-13 16:11:43 +0200
commitf4c51a96d50953d5fb6e9eb62194f226e2cbfd3c (patch)
tree8f32e9741b23d7461604cfaaacb552f7ac97c9f4 /widget/src/text_editor.rs
parent52b36a9574f45138363a4bfc6394c6da03baa433 (diff)
downloadiced-f4c51a96d50953d5fb6e9eb62194f226e2cbfd3c.tar.gz
iced-f4c51a96d50953d5fb6e9eb62194f226e2cbfd3c.tar.bz2
iced-f4c51a96d50953d5fb6e9eb62194f226e2cbfd3c.zip
Introduce `Motion` concept in `core::text::editor`
Diffstat (limited to 'widget/src/text_editor.rs')
-rw-r--r--widget/src/text_editor.rs77
1 files changed, 44 insertions, 33 deletions
diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs
index a8069069..38c243bd 100644
--- a/widget/src/text_editor.rs
+++ b/widget/src/text_editor.rs
@@ -14,7 +14,7 @@ use crate::core::{
use std::cell::RefCell;
pub use crate::style::text_editor::{Appearance, StyleSheet};
-pub use text::editor::Action;
+pub use text::editor::{Action, Motion};
pub struct TextEditor<'a, Message, Renderer = crate::Renderer>
where
@@ -189,16 +189,16 @@ where
};
match update {
- Update::Unfocus => {
- state.is_focused = false;
- state.is_dragging = false;
- }
Update::Click { click, action } => {
state.is_focused = true;
- state.last_click = Some(click);
state.is_dragging = true;
+ state.last_click = Some(click);
shell.publish(on_edit(action));
}
+ Update::Unfocus => {
+ state.is_focused = false;
+ state.is_dragging = false;
+ }
Update::StopDragging => {
state.is_dragging = false;
}
@@ -352,6 +352,9 @@ impl Update {
padding: Padding,
cursor: mouse::Cursor,
) -> Option<Self> {
+ let edit = |action| Some(Update::Edit(action));
+ let move_ = |motion| Some(Update::Edit(Action::Move(motion)));
+
match event {
Event::Mouse(event) => match event {
mouse::Event::ButtonPressed(mouse::Button::Left) => {
@@ -386,7 +389,7 @@ impl Update {
let cursor_position = cursor.position_in(bounds)?
- Vector::new(padding.top, padding.left);
- Some(Self::Edit(Action::Drag(cursor_position)))
+ edit(Action::Drag(cursor_position))
}
_ => None,
},
@@ -394,37 +397,31 @@ impl Update {
keyboard::Event::KeyPressed {
key_code,
modifiers,
- } if state.is_focused => match key_code {
- keyboard::KeyCode::Left => {
- if platform::is_jump_modifier_pressed(modifiers) {
- Some(Self::Edit(Action::MoveLeftWord))
+ } if state.is_focused => {
+ if let Some(motion) = motion(key_code) {
+ let motion = if modifiers.control() {
+ motion.widen()
} else {
- Some(Self::Edit(Action::MoveLeft))
- }
- }
- keyboard::KeyCode::Right => {
- if platform::is_jump_modifier_pressed(modifiers) {
- Some(Self::Edit(Action::MoveRightWord))
+ motion
+ };
+
+ return edit(if modifiers.shift() {
+ Action::Select(motion)
} else {
- Some(Self::Edit(Action::MoveRight))
- }
- }
- keyboard::KeyCode::Up => Some(Self::Edit(Action::MoveUp)),
- keyboard::KeyCode::Down => {
- Some(Self::Edit(Action::MoveDown))
- }
- keyboard::KeyCode::Enter => Some(Self::Edit(Action::Enter)),
- keyboard::KeyCode::Backspace => {
- Some(Self::Edit(Action::Backspace))
+ Action::Move(motion)
+ });
}
- keyboard::KeyCode::Delete => {
- Some(Self::Edit(Action::Delete))
+
+ match key_code {
+ keyboard::KeyCode::Enter => edit(Action::Enter),
+ keyboard::KeyCode::Backspace => edit(Action::Backspace),
+ keyboard::KeyCode::Delete => edit(Action::Delete),
+ keyboard::KeyCode::Escape => Some(Self::Unfocus),
+ _ => None,
}
- keyboard::KeyCode::Escape => Some(Self::Unfocus),
- _ => None,
- },
+ }
keyboard::Event::CharacterReceived(c) if state.is_focused => {
- Some(Self::Edit(Action::Insert(c)))
+ edit(Action::Insert(c))
}
_ => None,
},
@@ -433,6 +430,20 @@ impl Update {
}
}
+fn motion(key_code: keyboard::KeyCode) -> Option<Motion> {
+ match key_code {
+ keyboard::KeyCode::Left => Some(Motion::Left),
+ keyboard::KeyCode::Right => Some(Motion::Right),
+ keyboard::KeyCode::Up => Some(Motion::Up),
+ keyboard::KeyCode::Down => Some(Motion::Down),
+ keyboard::KeyCode::Home => Some(Motion::Home),
+ keyboard::KeyCode::End => Some(Motion::End),
+ keyboard::KeyCode::PageUp => Some(Motion::PageUp),
+ keyboard::KeyCode::PageDown => Some(Motion::PageDown),
+ _ => None,
+ }
+}
+
mod platform {
use crate::core::keyboard;