From 2ee0414b668e8bb0eaceab877d9a0942ea2345c3 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Feb 2018 18:09:14 -0800 Subject: [PATCH] avoid panic when dragging onto partial rows/columns --- term/src/terminalstate.rs | 15 ++++++++++++++- term/src/test/selection.rs | 5 ++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 2b06a288b..0afbd8aae 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -191,7 +191,20 @@ impl TerminalState { self.invalidate_hyperlinks(); } - pub fn mouse_event(&mut self, event: MouseEvent, host: &mut TerminalHost) -> Result<(), Error> { + pub fn mouse_event( + &mut self, + mut event: MouseEvent, + host: &mut TerminalHost, + ) -> Result<(), Error> { + // Clamp the mouse coordinates to the size of the model. + // This situation can trigger for example when the + // window is resized and leaves a partial row at the bottom of the + // terminal. The mouse can move over that portion and the gui layer + // can thus send us out-of-bounds row or column numbers. We want to + // make sure that we clamp this and handle it nicely at the model layer. + event.y = event.y.min(self.screen().physical_rows as i64 - 1); + event.x = event.x.min(self.screen().physical_cols - 1); + // Remember the last reported mouse position so that we can use it // for highlighting clickable things elsewhere. let new_position = CursorPosition { diff --git a/term/src/test/selection.rs b/term/src/test/selection.rs index 2d21f0a4e..6d43d0da3 100644 --- a/term/src/test/selection.rs +++ b/term/src/test/selection.rs @@ -28,7 +28,10 @@ fn drag_selection() { term.drag_select(1, 0, 6, 1); assert_eq!(term.get_clipboard().unwrap(), "ello world\n\u{1F480}skull"); - term.drag_select(0, 0, 0, 2); + // This next one drags off the bottom; this is technically out of bounds + // but we want to make sure we handle this without panicking. See the + // comment in TerminalState::mouse_event for more info. + term.drag_select(0, 0, 15, 3); assert_eq!( term.get_clipboard().unwrap(), "hello world\n\u{1F480}skull\n"