Better drag/click positioning
This commit is contained in:
parent
908937fb34
commit
a672490f97
2 changed files with 15 additions and 13 deletions
|
|
@ -43,16 +43,20 @@ impl Area {
|
|||
self.size.map(|e| e as usize)
|
||||
}
|
||||
|
||||
pub fn translate(&self, pos: [isize; 2]) -> [isize; 2] {
|
||||
[
|
||||
pos[0] - self.origin[0] as isize,
|
||||
pos[1] - self.origin[1] as isize,
|
||||
]
|
||||
}
|
||||
|
||||
pub fn contains(&self, pos: [isize; 2]) -> Option<[isize; 2]> {
|
||||
if (self.origin[0] as isize..self.origin[0] as isize + self.size[0] as isize)
|
||||
.contains(&pos[0])
|
||||
&& (self.origin[1] as isize..self.origin[1] as isize + self.size[1] as isize)
|
||||
.contains(&pos[1])
|
||||
{
|
||||
Some([
|
||||
pos[0] - self.origin[0] as isize,
|
||||
pos[1] - self.origin[1] as isize,
|
||||
])
|
||||
Some(self.translate(pos))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
|
|||
|
|
@ -171,10 +171,9 @@ impl Input {
|
|||
if self.scroll_grab.map_or(false, |(di, _)| di == drag_id) =>
|
||||
{
|
||||
if let Some((_, offset)) = self.scroll_grab
|
||||
&& let Some(pos) = self.frame_area.contains(pos)
|
||||
&& let Some((_, scroll_sz, frame_sz)) = self.last_scroll_pos
|
||||
{
|
||||
self.focus[1] = ((pos[1] - offset).max(0) as usize
|
||||
self.focus[1] = ((self.frame_area.translate(pos)[1] - offset).max(0) as usize
|
||||
* buffer.text.lines().count()
|
||||
/ frame_sz) as isize;
|
||||
}
|
||||
|
|
@ -184,13 +183,12 @@ impl Input {
|
|||
Action::Mouse(MouseAction::Drag, pos, false, _)
|
||||
| Action::Mouse(MouseAction::Click, pos, true, _),
|
||||
) => {
|
||||
if let Some(pos) = self.last_area.contains(pos) {
|
||||
buffer.goto_cursor(
|
||||
cursor_id,
|
||||
[self.focus[0] + pos[0], self.focus[1] + pos[1]],
|
||||
false,
|
||||
);
|
||||
}
|
||||
let pos = self.last_area.translate(pos);
|
||||
buffer.goto_cursor(
|
||||
cursor_id,
|
||||
[self.focus[0] + pos[0], self.focus[1] + pos[1]],
|
||||
false,
|
||||
);
|
||||
Ok(Resp::handled(None))
|
||||
}
|
||||
Some(Action::Undo) => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue