mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Fixes drag scroll position
This commit is contained in:
parent
5931ab17c8
commit
3a836aaa92
29
src/div.rs
29
src/div.rs
@ -1,15 +1,16 @@
|
|||||||
|
use num::Num;
|
||||||
|
|
||||||
// Integer division that rounds up.
|
/// Integer division that rounds up.
|
||||||
// pub fn div_up_usize(p: usize, q: usize) -> usize {
|
#[allow(dead_code)]
|
||||||
// div_up(p as u32, q as u32) as usize
|
pub fn div_up<T>(p: T, q: T) -> T
|
||||||
// }
|
where
|
||||||
//
|
T: Num + Clone,
|
||||||
// Integer division that rounds up.
|
{
|
||||||
// pub fn div_up(p: u32, q: u32) -> u32 {
|
let d = p.clone() / q.clone();
|
||||||
// if p % q == 0 {
|
|
||||||
// p / q
|
if p % q == T::zero() {
|
||||||
// } else {
|
d
|
||||||
// 1 + p / q
|
} else {
|
||||||
// }
|
T::one() + d
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
|
@ -2,6 +2,7 @@ use Printer;
|
|||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
use theme::ColorStyle;
|
use theme::ColorStyle;
|
||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
|
use div::div_up;
|
||||||
|
|
||||||
/// Provide scrolling functionalities to a view.
|
/// 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) {
|
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 min() is there to stop at the bottom of the content.
|
||||||
// The saturating_sub 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(
|
self.start_line = min(
|
||||||
(1 + self.content_height - self.view_height) * thumb_y
|
div_up((1 + self.content_height - self.view_height) * thumb_y
|
||||||
/ (self.view_height - thumb_height + 1),
|
, (self.view_height - thumb_height + 1)),
|
||||||
self.content_height - self.view_height,
|
self.content_height - self.view_height,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -180,10 +182,8 @@ impl ScrollBase {
|
|||||||
} else {
|
} else {
|
||||||
// Just jump a bit...
|
// Just jump a bit...
|
||||||
self.thumb_grab = height / 2;
|
self.thumb_grab = height / 2;
|
||||||
}
|
|
||||||
|
|
||||||
self.drag(position);
|
self.drag(position);
|
||||||
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
@ -192,6 +192,7 @@ impl ScrollBase {
|
|||||||
pub fn drag(&mut self, position: Vec2) {
|
pub fn drag(&mut self, position: Vec2) {
|
||||||
// Our goal is self.scrollbar_thumb_y()+thumb_grab == position.y
|
// Our goal is self.scrollbar_thumb_y()+thumb_grab == position.y
|
||||||
// Which means that position.y is the middle of the scrollbar.
|
// Which means that position.y is the middle of the scrollbar.
|
||||||
|
eprintln!("Dragged: {:?}", position);
|
||||||
let height = self.scrollbar_thumb_height();
|
let height = self.scrollbar_thumb_height();
|
||||||
let grab = self.thumb_grab;
|
let grab = self.thumb_grab;
|
||||||
self.scroll_to_thumb(position.y.saturating_sub(grab), height);
|
self.scroll_to_thumb(position.y.saturating_sub(grab), height);
|
||||||
|
Loading…
Reference in New Issue
Block a user