diff options
| author | Polesznyák Márk <contact@pml68.dev> | 2025-10-21 18:09:06 +0200 |
|---|---|---|
| committer | Polesznyák Márk <contact@pml68.dev> | 2025-10-21 18:09:06 +0200 |
| commit | 8700f85ff826dd8fc428e2acd3c7c5ae18a4945d (patch) | |
| tree | 0456dccecaa4641ac9d0037592e584c5fdc7f3e2 /src/text.rs | |
| parent | refactor: clean up text imports, cursor position code (diff) | |
| download | iced_selection-8700f85ff826dd8fc428e2acd3c7c5ae18a4945d.tar.gz | |
feat: implement triple-click + drag by-line selection
Diffstat (limited to '')
| -rw-r--r-- | src/text.rs | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/src/text.rs b/src/text.rs index b407cd5..484456a 100644 --- a/src/text.rs +++ b/src/text.rs @@ -159,11 +159,20 @@ pub struct State { content: String, is_hovered: bool, selection: Selection, - is_dragging: bool, + dragging: Option<Dragging>, last_click: Option<mouse::Click>, keyboard_modifiers: keyboard::Modifiers, } +/// The type of dragging selection. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[allow(missing_docs)] +pub enum Dragging { + Grapheme, + Word, + Line, +} + impl State { fn grapheme_line_and_index(&self, point: Point) -> Option<(usize, usize)> { let cursor = self.paragraph.buffer().hit(point.x, point.y)?; @@ -266,7 +275,7 @@ where if viewport.intersection(&bounds).is_none() && state.selection == Selection::default() - && !state.is_dragging + && state.dragging.is_none() { return; } @@ -299,7 +308,7 @@ where state.selection.select_range(new_end, new_end); } - state.is_dragging = true; + state.dragging = Some(Dragging::Grapheme); } click::Kind::Double => { let (line, index) = state @@ -311,7 +320,8 @@ where index, &state.paragraph, ); - state.is_dragging = false; + + state.dragging = None; } click::Kind::Triple => { let (line, _) = state @@ -319,7 +329,7 @@ where .unwrap_or((0, 0)); state.selection.select_line(line, &state.paragraph); - state.is_dragging = false; + state.dragging = Some(Dragging::Line); } } @@ -333,20 +343,31 @@ where Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) | Event::Touch(touch::Event::FingerLifted { .. }) | Event::Touch(touch::Event::FingerLost { .. }) => { - state.is_dragging = false; + state.dragging = None; } Event::Mouse(mouse::Event::CursorMoved { .. }) | Event::Touch(touch::Event::FingerMoved { .. }) => { if let Some(position) = click_position - && state.is_dragging + && let Some(dragging) = state.dragging { let (line, index) = state .grapheme_line_and_index(position) .unwrap_or((0, 0)); - let new_end = SelectionEnd { line, index }; + match dragging { + Dragging::Grapheme => { + let new_end = SelectionEnd { line, index }; - state.selection.change_selection(new_end); + state.selection.change_selection(new_end); + } + Dragging::Word => {} + Dragging::Line => { + state.selection.change_selection_by_line( + line, + &state.paragraph, + ); + } + }; } } Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => { @@ -455,7 +476,7 @@ where shell.capture_event(); } keyboard::Key::Named(key::Named::Escape) => { - state.is_dragging = false; + state.dragging = None; state.selection = Selection::default(); state.keyboard_modifiers = |
