avoid panic when dragging onto partial rows/columns

This commit is contained in:
Wez Furlong committed 2018-02-11 18:09:48 -08:00
1 parent d0de936f62
commit 2ee0414b66
2 files changed
+18 -2

No files matched your search

+14 -1
View File
@@ -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 {
+4 -1
View File
@@ -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"