From e8692c1336f83acfff39400de7614ff5ac67212e Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Thu, 21 Jun 2018 23:44:24 +0200 Subject: Remove 'selection_started' method from ActionContext By switching the start position/side of the selection to an `Option` it was made possible to remove the `selection_started` method in favor of checking if the two variables are `Some`. This also renames `selection_start_pos` to `selection_start_point` to unify the naming of variables. --- src/event.rs | 15 ++++----------- src/input.rs | 45 +++++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/event.rs b/src/event.rs index 12e4ff6e..2a4d82ec 100644 --- a/src/event.rs +++ b/src/event.rs @@ -75,10 +75,6 @@ impl<'a, N: Notify + 'a> input::ActionContext for ActionContext<'a, N> { self.selection_modified = true; } - fn selection_started(&self) -> bool { - self.selection.is_some() - } - fn update_selection(&mut self, point: Point, side: Side) { self.selection_modified = true; // Update selection if one exists @@ -165,8 +161,8 @@ pub struct Mouse { pub column: Column, pub cell_side: Side, pub lines_scrolled: f32, - pub selection_start_pos: Point, - pub selection_start_side: Side, + pub selection_start_point: Option, + pub selection_start_side: Option, } impl Default for Mouse { @@ -184,11 +180,8 @@ impl Default for Mouse { column: Column(0), cell_side: Side::Left, lines_scrolled: 0.0, - selection_start_pos: Point { - line: Line(0), - col: Column(0), - }, - selection_start_side: Side::Left, + selection_start_point: None, + selection_start_side: None, } } } diff --git a/src/input.rs b/src/input.rs index 3996ecfb..579b1df3 100644 --- a/src/input.rs +++ b/src/input.rs @@ -55,7 +55,6 @@ pub trait ActionContext { fn size_info(&self) -> SizeInfo; fn copy_selection(&self, Buffer); fn clear_selection(&mut self); - fn selection_started(&self) -> bool; fn update_selection(&mut self, point: Point, side: Side); fn simple_selection(&mut self, point: Point, side: Side); fn semantic_selection(&mut self, point: Point); @@ -284,15 +283,19 @@ impl<'a, A: ActionContext + 'a> Processor<'a, A> { || !self.ctx.terminal_mode().intersects(TermMode::MOUSE_REPORT_CLICK | motion_mode) ) { - if self.ctx.selection_started() { + let start_point = self.ctx.mouse().selection_start_point; + let start_side = self.ctx.mouse().selection_start_side; + if let (Some(point), Some(side)) = (start_point, start_side) { + // Reset beginning of selection once selection is started + self.ctx.mouse_mut().selection_start_point = None; + self.ctx.mouse_mut().selection_start_side = None; + + self.ctx.update_selection(point, side); + } else { self.ctx.update_selection(Point { line: point.line, col: point.col }, cell_side); - } else { - let pos = self.ctx.mouse().selection_start_pos; - let side = self.ctx.mouse().selection_start_side; - self.ctx.update_selection(pos, side); } } else if self.ctx.terminal_mode().intersects(motion_mode) // Only report motion when changing cells @@ -366,24 +369,30 @@ impl<'a, A: ActionContext + 'a> Processor<'a, A> { } pub fn on_mouse_double_click(&mut self) { - if self.ctx.selection_started() { + if let Some(point) = self.ctx.mouse().selection_start_point { + // Reset beginning of selection once selection is started + self.ctx.mouse_mut().selection_start_point = None; + self.ctx.mouse_mut().selection_start_side = None; + + self.ctx.semantic_selection(point); + } else { if let Some(point) = self.ctx.mouse_coords() { self.ctx.semantic_selection(point); } - } else { - let point = self.ctx.mouse().selection_start_pos; - self.ctx.semantic_selection(point); } } pub fn on_mouse_triple_click(&mut self) { - if self.ctx.selection_started() { + if let Some(point) = self.ctx.mouse().selection_start_point { + // Reset beginning of selection once selection is started + self.ctx.mouse_mut().selection_start_point = None; + self.ctx.mouse_mut().selection_start_side = None; + + self.ctx.line_selection(point); + } else { if let Some(point) = self.ctx.mouse_coords() { self.ctx.line_selection(point); } - } else { - let point = self.ctx.mouse().selection_start_pos; - self.ctx.line_selection(point); } } @@ -404,8 +413,8 @@ impl<'a, A: ActionContext + 'a> Processor<'a, A> { _ => { // Store click position for accurate selection if let Some((point, side)) = self.get_mouse_pos() { - self.ctx.mouse_mut().selection_start_pos = point; - self.ctx.mouse_mut().selection_start_side = side; + self.ctx.mouse_mut().selection_start_point = Some(point); + self.ctx.mouse_mut().selection_start_side = Some(side); } self.ctx.clear_selection(); @@ -706,10 +715,6 @@ mod tests { // STUBBED } - fn selection_started(&self) -> bool { - false - } - fn clear_selection(&mut self) {} fn update_selection(&mut self, _point: Point, _side: Side) {} fn simple_selection(&mut self, _point: Point, _side: Side) {} -- cgit v1.2.3-54-g00ecf