From 3a836aaa922dae723908803854b09605db115df1 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 12 Oct 2017 12:30:00 -0700 Subject: [PATCH] Fixes drag scroll position --- src/div.rs | 29 +++++++++++++++-------------- src/view/scroll.rs | 11 ++++++----- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/div.rs b/src/div.rs index faddc8b..f8d581b 100644 --- a/src/div.rs +++ b/src/div.rs @@ -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(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 + } +} diff --git a/src/view/scroll.rs b/src/view/scroll.rs index 7799f79..70430e0 100644 --- a/src/view/scroll.rs +++ b/src/view/scroll.rs @@ -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,11 +182,9 @@ impl ScrollBase { } else { // Just jump a bit... self.thumb_grab = height / 2; + self.drag(position); } - 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);