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

View File

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

View File

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