mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add comments
This commit is contained in:
parent
8ae34b05dd
commit
0849ae6efa
@ -136,6 +136,9 @@ impl TextView {
|
|||||||
self.with(|s| s.set_scroll_strategy(strategy))
|
self.with(|s| s.set_scroll_strategy(strategy))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply the scrolling strategy to the current scroll position.
|
||||||
|
//
|
||||||
|
// Called when computing rows and when applying a new strategy.
|
||||||
fn adjust_scroll(&mut self) {
|
fn adjust_scroll(&mut self) {
|
||||||
match self.scroll_strategy {
|
match self.scroll_strategy {
|
||||||
ScrollStrategy::StickToTop => self.scrollbase.scroll_top(),
|
ScrollStrategy::StickToTop => self.scrollbase.scroll_top(),
|
||||||
@ -145,56 +148,69 @@ impl TextView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
self.last_size = None;
|
return;
|
||||||
// Recompute
|
}
|
||||||
|
|
||||||
if size.x == 0 {
|
// Completely bust the cache
|
||||||
// Nothing we can do at this poing.
|
// Just in case we fail, we don't want to leave a bad cache.
|
||||||
|
self.last_size = None;
|
||||||
|
|
||||||
|
if size.x == 0 {
|
||||||
|
// Nothing we can do at this point.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// First attempt: naively hope that we won't need a scrollbar_width
|
||||||
|
// (This means we try to use the entire available width for text).
|
||||||
|
self.rows = LinesIterator::new(&self.content, size.x).collect();
|
||||||
|
|
||||||
|
// Width taken by the scrollbar. Without a scrollbar, it's 0.
|
||||||
|
let mut scrollbar_width = 0;
|
||||||
|
|
||||||
|
if self.scrollable && self.rows.len() > size.y {
|
||||||
|
// We take 1 column for the bar itself + 1 spacing column
|
||||||
|
scrollbar_width = 2;
|
||||||
|
|
||||||
|
if size.x < scrollbar_width {
|
||||||
|
// Again, this is a lost cause.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.rows = LinesIterator::new(&self.content, size.x).collect();
|
// If we're too high, include a scrollbar_width
|
||||||
let mut scrollbar = 0;
|
let available = size.x - scrollbar_width;
|
||||||
if self.scrollable && self.rows.len() > size.y {
|
self.rows = LinesIterator::new(&self.content, available).collect();
|
||||||
scrollbar = 2;
|
|
||||||
if size.x < scrollbar {
|
|
||||||
// Again, this is a lost cause.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're too high, include a scrollbar
|
if self.rows.is_empty() && !self.content.is_empty() {
|
||||||
self.rows = LinesIterator::new(&self.content,
|
// We have some content, we we didn't find any row for it?
|
||||||
size.x - scrollbar)
|
// This probably means we couldn't even make a single row
|
||||||
.collect();
|
// (for instance we only have 1 column and we have a wide character).
|
||||||
if self.rows.is_empty() && !self.content.is_empty() {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Desired width, including the scrollbar.
|
|
||||||
self.width = self.rows
|
|
||||||
.iter()
|
|
||||||
.map(|row| row.width)
|
|
||||||
.max()
|
|
||||||
.map(|w| w + scrollbar);
|
|
||||||
|
|
||||||
// Our resulting size.
|
|
||||||
// We can't go lower, width-wise.
|
|
||||||
|
|
||||||
let mut my_size = Vec2::new(self.width.unwrap_or(0),
|
|
||||||
self.rows.len());
|
|
||||||
|
|
||||||
if self.scrollable && my_size.y > size.y {
|
|
||||||
my_size.y = size.y;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// println_stderr!("my: {:?} | si: {:?}", my_size, size);
|
|
||||||
self.last_size = Some(SizeCache::build(my_size, size));
|
|
||||||
self.scrollbase.set_heights(size.y, self.rows.len());
|
|
||||||
self.adjust_scroll();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Desired width, including the scrollbar_width.
|
||||||
|
self.width = self.rows
|
||||||
|
.iter()
|
||||||
|
.map(|row| row.width)
|
||||||
|
.max()
|
||||||
|
.map(|w| w + scrollbar_width);
|
||||||
|
|
||||||
|
// The entire "virtual" size (includes all rows)
|
||||||
|
let mut my_size = Vec2::new(self.width.unwrap_or(0), self.rows.len());
|
||||||
|
|
||||||
|
// If we're scrolling, cap the the available size.
|
||||||
|
if self.scrollable && my_size.y > size.y {
|
||||||
|
my_size.y = size.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invalidates the cache, so next call will recompute everything.
|
// Invalidates the cache, so next call will recompute everything.
|
||||||
|
Loading…
Reference in New Issue
Block a user