From 290be82696f7053379d12e05f240f25491161f1e Mon Sep 17 00:00:00 2001 From: Frederic Laing Date: Mon, 22 Dec 2025 18:14:15 +0100 Subject: [PATCH] implement shift press plus mouse click text selection --- src/text_box.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/text_box.rs b/src/text_box.rs index e97e285..4a8246f 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -27,8 +27,8 @@ use cosmic::{ theme::Theme, }; use cosmic_text::{ - Action, BorrowedWithFontSystem, Edit, Metrics, Motion, Renderer as _, Scroll, Selection, - ViEditor, + Action, BorrowedWithFontSystem, Cursor, Edit, Metrics, Motion, Renderer as _, Scroll, + Selection, ViEditor, }; use std::{ cell::Cell, @@ -1072,6 +1072,12 @@ where } } Event::Keyboard(KeyEvent::ModifiersChanged(modifiers)) => { + if modifiers.shift() && !state.modifiers.shift() { + let anchor = editor.cursor(); + *state.shift_anchor.lock().unwrap() = Some(anchor); + } else if !modifiers.shift() && state.modifiers.shift() { + *state.shift_anchor.lock().unwrap() = None; + } state.modifiers = modifiers; } Event::Mouse(MouseEvent::ButtonPressed(button)) => { @@ -1122,6 +1128,16 @@ where } else { ClickKind::Single }; + let maybe_anchor = if state.modifiers.shift() { + state.shift_anchor.lock().unwrap().clone() + } else { + None + }; + + if let Some(anchor) = maybe_anchor { + editor.set_selection(Selection::Normal(anchor)); + } + match click_kind { ClickKind::Single => editor.action(Action::Click { x: x as i32, @@ -1136,6 +1152,10 @@ where y: y as i32, }), } + + if let Some(anchor) = maybe_anchor { + editor.set_selection(Selection::Normal(anchor)); + } state.click = Some((click_kind, Instant::now())); state.dragging = Some(Dragging::Buffer); } else if scrollbar_v_rect.contains(Point::new(x_logical, y_logical)) { @@ -1341,6 +1361,7 @@ pub struct State { scrollbar_v_rect: Cell>, scrollbar_h_rect: Cell>>, handle_opt: Mutex>, + shift_anchor: Mutex>, } impl State { @@ -1357,6 +1378,7 @@ impl State { scrollbar_v_rect: Cell::new(Rectangle::default()), scrollbar_h_rect: Cell::new(None), handle_opt: Mutex::new(None), + shift_anchor: Mutex::new(None), } } }