Fixes drag scroll position

This commit is contained in:
Alexandre Bury 2017-10-12 12:30:00 -07:00
parent 5931ab17c8
commit 3a836aaa92
2 changed files with 21 additions and 19 deletions

View File

@ -1,15 +1,16 @@
use num::Num;
// Integer division that rounds up.
// pub fn div_up_usize(p: usize, q: usize) -> usize {
// div_up(p as u32, q as u32) as usize
// }
//
// Integer division that rounds up.
// pub fn div_up(p: u32, q: u32) -> u32 {
// if p % q == 0 {
// p / q
// } else {
// 1 + p / q
// }
// }
//
/// Integer division that rounds up.
#[allow(dead_code)]
pub fn div_up<T>(p: T, q: T) -> T
where
T: Num + Clone,
{
let d = p.clone() / q.clone();
if p % q == T::zero() {
d
} else {
T::one() + d
}
}

View File

@ -2,6 +2,7 @@ use Printer;
use std::cmp::{max, min};
use theme::ColorStyle;
use vec::Vec2;
use div::div_up;
/// Provide scrolling functionalities to a view.
///
@ -146,9 +147,10 @@ impl ScrollBase {
pub fn scroll_to_thumb(&mut self, thumb_y: usize, thumb_height: usize) {
// The min() is there to stop at the bottom of the content.
// The saturating_sub is there to stop at the bottom of the content.
eprintln!("Scrolling to {}", thumb_y);
self.start_line = min(
(1 + self.content_height - self.view_height) * thumb_y
/ (self.view_height - thumb_height + 1),
div_up((1 + self.content_height - self.view_height) * thumb_y
, (self.view_height - thumb_height + 1)),
self.content_height - self.view_height,
);
}
@ -180,10 +182,8 @@ impl ScrollBase {
} else {
// Just jump a bit...
self.thumb_grab = height / 2;
}
self.drag(position);
}
true
}
@ -192,6 +192,7 @@ impl ScrollBase {
pub fn drag(&mut self, position: Vec2) {
// Our goal is self.scrollbar_thumb_y()+thumb_grab == position.y
// Which means that position.y is the middle of the scrollbar.
eprintln!("Dragged: {:?}", position);
let height = self.scrollbar_thumb_height();
let grab = self.thumb_grab;
self.scroll_to_thumb(position.y.saturating_sub(grab), height);