Skip to content

Commit

Permalink
fix(ad): correcting behaviour of middle/right click inside of a range…
Browse files Browse the repository at this point in the history
… dot
  • Loading branch information
sminez committed Sep 14, 2024
1 parent 37537fe commit 13aa951
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
35 changes: 24 additions & 11 deletions src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,27 +597,40 @@ impl Buffer {
}
}

pub(crate) fn set_dot_from_screen_coords(&mut self, x: usize, y: usize, screen_rows: usize) {
self.clear_render_cache_between_indices(self.dot.first_cur().idx, self.dot.last_cur().idx);
pub(crate) fn set_dot_from_screen_coords_if_outside_current_range(
&mut self,
x: usize,
y: usize,
screen_rows: usize,
) {
let mouse_cur = self.cur_from_screen_coords(x, y, screen_rows);
if !self.dot.contains(&mouse_cur) {
self.set_dot_from_screen_coords(x, y, screen_rows);
}
}

fn cur_from_screen_coords(&mut self, x: usize, y: usize, screen_rows: usize) -> Cur {
let (_, w_sgncol) = self.sign_col_dims(screen_rows);
self.rx = x - 1 - w_sgncol;
let y = min(y - 1 + self.row_off, self.len_lines() - 1);
let mut cur = Cur::from_yx(y, self.x_from_rx(y), self);

cur.clamp_idx(self.txt.len_chars());

let mut dot = Dot::Cur {
c: Cur::from_yx(y, self.x_from_rx(y), self),
cur
}

pub(crate) fn set_dot_from_screen_coords(&mut self, x: usize, y: usize, screen_rows: usize) {
self.clear_render_cache_between_indices(self.dot.first_cur().idx, self.dot.last_cur().idx);
self.dot = Dot::Cur {
c: self.cur_from_screen_coords(x, y, screen_rows),
};
dot.clamp_idx(self.txt.len_chars());
self.dot = dot;
}

pub(crate) fn extend_dot_to_screen_coords(&mut self, x: usize, y: usize, screen_rows: usize) {
self.clear_render_cache_between_indices(self.dot.first_cur().idx, self.dot.last_cur().idx);
let (_, w_sgncol) = self.sign_col_dims(screen_rows);
self.rx = x - 1 - w_sgncol;
let y = min(y - 1 + self.row_off, self.len_lines() - 1);

let mut r = self.dot.as_range();
let c = Cur::from_yx(y, self.x_from_rx(y), self);
let c = self.cur_from_screen_coords(x, y, screen_rows);
r.set_active_cursor(c);

let mut dot = Dot::Range { r };
Expand Down
2 changes: 1 addition & 1 deletion src/dot/cur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl Cur {
cur
}

pub(super) fn clamp_idx(&mut self, max_idx: usize) {
pub(crate) fn clamp_idx(&mut self, max_idx: usize) {
self.idx = min(self.idx, max_idx);
}

Expand Down
18 changes: 18 additions & 0 deletions src/dot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ impl Dot {
}
}

/// Whether or not this dot is a [Cur].
pub fn is_cur(&self) -> bool {
matches!(self, Dot::Cur { .. })
}

/// Whether or not this dot is a [Range].
pub fn is_range(&self) -> bool {
matches!(self, Dot::Range { .. })
}

/// Whether or not this dot contains `cur` within it.
pub fn contains(&self, cur: &Cur) -> bool {
match self {
Dot::Cur { c } => cur == c,
Dot::Range { r } => r.contains(cur),
}
}

/// The address representation of this dot in the form that is enterable by the user.
/// Indices are 1-based rather than their internal 0-based representation.
pub fn addr(&self, b: &Buffer) -> String {
Expand Down
4 changes: 4 additions & 0 deletions src/dot/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl Range {
)
}

pub(crate) fn contains(&self, cur: &Cur) -> bool {
cur.idx >= self.start.idx && cur.idx <= self.end.idx
}

/// Extends the STARTING cursor to its line start
#[must_use]
pub(super) fn extend_to_line_start(mut self, b: &Buffer) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,14 @@ impl Editor {
MouseEvent::Press { b: Right, x, y } => {
self.buffers
.active_mut()
.set_dot_from_screen_coords(x, y, self.screen_rows);
.set_dot_from_screen_coords_if_outside_current_range(x, y, self.screen_rows);
self.load_dot();
}

MouseEvent::Press { b: Middle, x, y } => {
self.buffers
.active_mut()
.set_dot_from_screen_coords(x, y, self.screen_rows);
.set_dot_from_screen_coords_if_outside_current_range(x, y, self.screen_rows);
self.execute_dot();
}

Expand Down

0 comments on commit 13aa951

Please sign in to comment.