summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-14 14:18:49 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2023-09-14 14:18:49 +0200
commitb24b94d82778733ddae1b824d0d7690afcec3056 (patch)
tree727f2197fee64e377758cfa9e5d5b16b692b8858 /graphics
parente6c2db8a9312e3fe37f30f049d1fa497892f1a86 (diff)
downloadiced-b24b94d82778733ddae1b824d0d7690afcec3056.tar.gz
iced-b24b94d82778733ddae1b824d0d7690afcec3056.tar.bz2
iced-b24b94d82778733ddae1b824d0d7690afcec3056.zip
Handle motions when a selection is present in `text::Editor`
Diffstat (limited to 'graphics')
-rw-r--r--graphics/src/text/editor.rs70
1 files changed, 47 insertions, 23 deletions
diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs
index 83d41c85..d88bcd1d 100644
--- a/graphics/src/text/editor.rs
+++ b/graphics/src/text/editor.rs
@@ -1,4 +1,4 @@
-use crate::core::text::editor::{self, Action, Cursor, Motion};
+use crate::core::text::editor::{self, Action, Cursor, Direction, Motion};
use crate::core::text::LineHeight;
use crate::core::{Font, Pixels, Point, Rectangle, Size};
use crate::text;
@@ -219,30 +219,37 @@ impl editor::Editor for Editor {
match action {
// Motion events
Action::Move(motion) => {
- if let Some(_selection) = editor.select_opt() {
+ if let Some(selection) = editor.select_opt() {
+ let cursor = editor.cursor();
+
+ let (left, right) = if cursor < selection {
+ (cursor, selection)
+ } else {
+ (selection, cursor)
+ };
+
editor.set_select_opt(None);
+
+ match motion {
+ // These motions are performed as-is even when a selection
+ // is present
+ Motion::Home
+ | Motion::End
+ | Motion::DocumentStart
+ | Motion::DocumentEnd => {
+ editor.action(
+ font_system.raw(),
+ motion_to_action(motion),
+ );
+ }
+ // Other motions simply move the cursor to one end of the selection
+ _ => editor.set_cursor(match motion.direction() {
+ Direction::Left => left,
+ Direction::Right => right,
+ }),
+ }
} else {
- editor.action(
- font_system.raw(),
- match motion {
- Motion::Left => cosmic_text::Action::Left,
- Motion::Right => cosmic_text::Action::Right,
- Motion::Up => cosmic_text::Action::Up,
- Motion::Down => cosmic_text::Action::Down,
- Motion::WordLeft => cosmic_text::Action::LeftWord,
- Motion::WordRight => cosmic_text::Action::RightWord,
- Motion::Home => cosmic_text::Action::Home,
- Motion::End => cosmic_text::Action::End,
- Motion::PageUp => cosmic_text::Action::PageUp,
- Motion::PageDown => cosmic_text::Action::PageDown,
- Motion::DocumentStart => {
- cosmic_text::Action::BufferStart
- }
- Motion::DocumentEnd => {
- cosmic_text::Action::BufferEnd
- }
- },
- );
+ editor.action(font_system.raw(), motion_to_action(motion));
}
}
@@ -509,3 +516,20 @@ fn visual_lines_offset(line: usize, buffer: &cosmic_text::Buffer) -> i32 {
visual_lines_before_start as i32 - buffer.scroll()
}
+
+fn motion_to_action(motion: Motion) -> cosmic_text::Action {
+ match motion {
+ Motion::Left => cosmic_text::Action::Left,
+ Motion::Right => cosmic_text::Action::Right,
+ Motion::Up => cosmic_text::Action::Up,
+ Motion::Down => cosmic_text::Action::Down,
+ Motion::WordLeft => cosmic_text::Action::LeftWord,
+ Motion::WordRight => cosmic_text::Action::RightWord,
+ Motion::Home => cosmic_text::Action::Home,
+ Motion::End => cosmic_text::Action::End,
+ Motion::PageUp => cosmic_text::Action::PageUp,
+ Motion::PageDown => cosmic_text::Action::PageDown,
+ Motion::DocumentStart => cosmic_text::Action::BufferStart,
+ Motion::DocumentEnd => cosmic_text::Action::BufferEnd,
+ }
+}