From 70e9c7cb16c6593166adfc3267ff0ca929344d33 Mon Sep 17 00:00:00 2001 From: Jun Wu Date: Mon, 20 Jan 2020 13:55:18 -0800 Subject: [PATCH] Make Shift+LeftClick extend existing selection This matches cmd.exe and other programs (notepad, office, etc) behavior: First click selects "start", then Shift+click selects "end". They form a range. This is particularly useful to select a large range of text, since the user can release the left button, then operate on the scroll bar without worrying about messing up text selection. It gets a bit more complicated with the POSIX "mouse grabbed" situation. When the mouse is grabbed, it's usually a full-screen ncurses-like application. Selection mostly likely only makes sense within a single screen. So Shift + LeftClick just works as starting a selection in this case (otherwise it'll be hard to clear a selection). --- src/frontend/gui/selection.rs | 4 ++++ src/frontend/gui/termwindow.rs | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/frontend/gui/selection.rs b/src/frontend/gui/selection.rs index 4b8108b2d..3de273455 100644 --- a/src/frontend/gui/selection.rs +++ b/src/frontend/gui/selection.rs @@ -26,6 +26,10 @@ impl Selection { self.range = None; self.start = Some(start); } + + pub fn is_empty(&self) -> bool { + self.range.is_none() + } } /// The x,y coordinates of either the start or end of a selection region diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index d8830d146..6017b23cb 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -2512,6 +2512,7 @@ impl TermWindow { } // Left single click to initiate a selection drag + // Shift+Left click to extend a selection ( WMEK::Press(MousePress::Left), Some(LastMouseClick { @@ -2520,9 +2521,24 @@ impl TermWindow { .. }), ) => { - self.selection(tab.tab_id()) - .begin(SelectionCoordinate { x, y: stable_row }); - self.window.as_ref().unwrap().set_clipboard(String::new()); + // If the mouse is grabbed, do not use Shfit+Left to + // extend a selection, since otherwise it's hard to + // clear a selection. + if tab.is_mouse_grabbed() + || !event.modifiers.contains(Modifiers::SHIFT) + || self.selection(tab.tab_id()).is_empty() + { + // Initiate a selection + self.selection(tab.tab_id()) + .begin(SelectionCoordinate { x, y: stable_row }); + self.window.as_ref().unwrap().set_clipboard(String::new()); + } else { + // Extend selection + let end = SelectionCoordinate { x, y: stable_row }; + let mut selection = self.selection(tab.tab_id()); + selection.range = selection.range.map(|sel| sel.extend(end)); + context.invalidate(); + } } // Release button to finish a selection