From 4b63bddd55853e878ec0f85b4613dd02749a3811 Mon Sep 17 00:00:00 2001 From: Joe Wilm Date: Sun, 11 Dec 2016 22:02:03 -0800 Subject: Track terminal cells on mouse movement The cell under the cursor is now tracked in the input processor at `self.mouse.line` and `self.mouse.column`. This could probably be optimized to only compute the cell when in certain states, but the calculation is cheap. --- src/term/mod.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/term') diff --git a/src/term/mod.rs b/src/term/mod.rs index da5683e0..523fcb62 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -259,6 +259,17 @@ impl SizeInfo { pub fn cols(&self) -> Column { Column((self.width / self.cell_width) as usize) } + + pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option<(Line, Column)> { + if x > self.width as usize || y > self.height as usize { + return None; + } + + let col = Column(x / (self.cell_width as usize)); + let line = Line(y / (self.cell_height as usize)); + + Some((line, col)) + } } impl Term { @@ -306,15 +317,7 @@ impl Term { /// /// Returns None if the coordinates are outside the screen pub fn pixels_to_coords(&self, x: usize, y: usize) -> Option<(Line, Column)> { - let size = self.size_info(); - if x > size.width as usize || y > size.height as usize { - return None; - } - - let col = Column(x / (size.cell_width as usize)); - let line = Line(y / (size.cell_height as usize)); - - Some((line, col)) + self.size_info().pixels_to_coords(x, y) } /// Access to the raw grid data structure -- cgit