Fix some mouse/scroll issues with SelectView and TextView

This commit is contained in:
Alexandre Bury 2017-10-13 17:53:39 -07:00
parent 9026d87aab
commit a5952d0741
4 changed files with 30 additions and 20 deletions

View File

@ -3,7 +3,7 @@ extern crate cursive;
use cursive::Cursive;
use cursive::align::HAlign;
use cursive::view::Boxable;
use cursive::views::{Dialog, TextView};
use cursive::views::{Dialog, TextView, Panel};
fn main() {
// Read some long text from a file.
@ -17,7 +17,7 @@ fn main() {
// The text is too long to fit on a line, so the view will wrap lines,
// and will adapt to the terminal size.
siv.add_fullscreen_layer(
Dialog::around(TextView::new(content))
Dialog::around(Panel::new(TextView::new(content)))
.h_align(HAlign::Center)
.button("Quit", |s| s.quit())
.full_screen(),

View File

@ -87,6 +87,8 @@ impl ScrollBase {
self.view_height = view_height;
self.content_height = content_height;
// eprintln!("Setting heights: {} in {}", content_height, view_height);
if self.scrollable() {
self.start_line =
min(self.start_line, self.content_height - self.view_height);
@ -198,6 +200,7 @@ impl ScrollBase {
// 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);
// eprintln!("thumb: {:?}", self.thumb_grab);
if let Some(grab) = self.thumb_grab {
let height = self.scrollbar_thumb_height();
self.scroll_to_thumb(position.y.saturating_sub(grab), height);

View File

@ -398,7 +398,13 @@ impl<T: 'static> SelectView<T> {
self.scrollbase.release_grab();
if self.on_submit.is_some() {
if let Some(position) = position.checked_sub(offset) {
if position < self.last_size
let scrollbar_size = if self.scrollbase.scrollable() {
(2, 0)
} else {
(0, 0)
};
let clickable_size = self.last_size.saturating_sub(scrollbar_size);
if position < clickable_size
&& (position.y + self.scrollbase.start_line)
== self.focus()
{

View File

@ -22,7 +22,8 @@ pub struct TextView {
// ScrollBase make many scrolling-related things easier
scrollbase: ScrollBase,
scroll_strategy: ScrollStrategy,
last_size: Option<XY<SizeCache>>,
size_cache: Option<XY<SizeCache>>,
last_size: Vec2,
width: Option<usize>,
}
@ -46,7 +47,8 @@ impl TextView {
scrollbase: ScrollBase::new(),
scroll_strategy: ScrollStrategy::KeepRow,
align: Align::top_left(),
last_size: None,
size_cache: None,
last_size: Vec2::zero(),
width: None,
}
}
@ -127,7 +129,7 @@ impl TextView {
}
fn is_cache_valid(&self, size: Vec2) -> bool {
match self.last_size {
match self.size_cache {
None => false,
Some(ref last) => last.x.accept(size.x) && last.y.accept(size.y),
}
@ -163,6 +165,8 @@ impl TextView {
};
}
// This must be non-destructive, as it may be called
// multiple times during layout.
fn compute_rows(&mut self, size: Vec2) {
if self.is_cache_valid(size) {
return;
@ -170,7 +174,7 @@ impl TextView {
// Completely bust the cache
// Just in case we fail, we don't want to leave a bad cache.
self.last_size = None;
self.size_cache = None;
if size.x == 0 {
// Nothing we can do at this point.
@ -223,16 +227,12 @@ impl TextView {
// Build a fresh cache.
self.last_size = Some(SizeCache::build(my_size, size));
// Adjust scrolling, in case we're sticking to the bottom for instance.
self.scrollbase.set_heights(size.y, self.rows.len());
self.adjust_scroll();
self.size_cache = Some(SizeCache::build(my_size, size));
}
// Invalidates the cache, so next call will recompute everything.
fn invalidate(&mut self) {
self.last_size = None;
self.size_cache = None;
}
}
@ -273,14 +273,14 @@ impl View for TextView {
..
} if self.scrollbase.can_scroll_down() =>
{
self.scrollbase.scroll_down(5)
self.scrollbase.scroll_down(5);
}
Event::Mouse {
event: MouseEvent::WheelUp,
..
} if self.scrollbase.can_scroll_up() =>
{
self.scrollbase.scroll_up(5)
self.scrollbase.scroll_up(5);
}
Event::Mouse {
event: MouseEvent::Press(MouseButton::Left),
@ -288,10 +288,8 @@ impl View for TextView {
offset,
} if position
.checked_sub(offset)
.and_then(|position| {
self.width.map(
|width| self.scrollbase.start_drag(position, width),
)
.map(|position| {
self.scrollbase.start_drag(position, self.last_size.x)
})
.unwrap_or(false) =>
{
@ -325,7 +323,7 @@ impl View for TextView {
}
fn needs_relayout(&self) -> bool {
self.last_size.is_none()
self.size_cache.is_none()
}
fn required_size(&mut self, size: Vec2) -> Vec2 {
@ -347,7 +345,10 @@ impl View for TextView {
fn layout(&mut self, size: Vec2) {
// Compute the text rows.
self.last_size = size;
self.compute_rows(size);
// Adjust scrolling, in case we're sticking to the bottom for instance.
self.scrollbase.set_heights(size.y, self.rows.len());
self.adjust_scroll();
}
}