2016-10-02 22:22:29 +00:00
|
|
|
use Printer;
|
2017-10-12 19:35:57 +00:00
|
|
|
use div::div_up;
|
2016-06-28 05:10:59 +00:00
|
|
|
use std::cmp::{max, min};
|
2016-07-01 06:38:01 +00:00
|
|
|
use theme::ColorStyle;
|
2015-05-31 23:38:53 +00:00
|
|
|
use vec::Vec2;
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Provide scrolling functionalities to a view.
|
2016-07-21 05:08:06 +00:00
|
|
|
///
|
|
|
|
/// You're not supposed to use this directly,
|
|
|
|
/// but it can be helpful if you create your own Views.
|
2016-06-28 05:40:11 +00:00
|
|
|
#[derive(Default)]
|
2015-05-31 23:38:53 +00:00
|
|
|
pub struct ScrollBase {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// First line visible
|
2015-05-31 23:38:53 +00:00
|
|
|
pub start_line: usize,
|
2017-10-12 01:06:58 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Content height
|
2015-05-31 23:38:53 +00:00
|
|
|
pub content_height: usize,
|
2017-10-12 01:06:58 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Number of lines displayed
|
2015-05-31 23:38:53 +00:00
|
|
|
pub view_height: usize,
|
2017-10-12 01:06:58 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Padding for the scrollbar
|
|
|
|
///
|
|
|
|
/// If present, the scrollbar will be shifted
|
2016-08-02 07:36:27 +00:00
|
|
|
/// `scrollbar_offset` columns to the left.
|
2016-07-12 03:26:33 +00:00
|
|
|
///
|
|
|
|
/// (Useful when each item includes its own side borders,
|
|
|
|
/// to draw the scrollbar inside.)
|
2016-08-02 07:36:27 +00:00
|
|
|
pub scrollbar_offset: usize,
|
2016-08-02 07:49:59 +00:00
|
|
|
|
|
|
|
/// Blank between the text and the scrollbar.
|
|
|
|
pub right_padding: usize,
|
2017-10-12 01:06:58 +00:00
|
|
|
|
|
|
|
/// Initial position of the cursor when dragging.
|
2017-10-12 21:32:48 +00:00
|
|
|
pub thumb_grab: Option<usize>,
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:44:40 +00:00
|
|
|
/// Defines the scrolling behaviour on content or size change
|
|
|
|
pub enum ScrollStrategy {
|
|
|
|
/// Keeps the same row number
|
|
|
|
KeepRow,
|
|
|
|
/// Sticks to the top.
|
|
|
|
StickToTop,
|
|
|
|
/// Sticks to the bottom of the view.
|
|
|
|
StickToBottom,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ScrollStrategy {
|
|
|
|
fn default() -> Self {
|
|
|
|
ScrollStrategy::KeepRow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
impl ScrollBase {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Creates a new, uninitialized scrollbar.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
ScrollBase {
|
|
|
|
start_line: 0,
|
|
|
|
content_height: 0,
|
|
|
|
view_height: 0,
|
2016-08-02 07:36:27 +00:00
|
|
|
scrollbar_offset: 0,
|
2016-08-02 07:49:59 +00:00
|
|
|
right_padding: 1,
|
2017-10-12 21:32:48 +00:00
|
|
|
thumb_grab: None,
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Shifts the scrollbar toward the inside of the view.
|
|
|
|
///
|
|
|
|
/// Used by views that draw their side borders in the children.
|
|
|
|
/// Pushing the scrollbar to the left allows it to stay inside
|
|
|
|
/// the borders.
|
2016-08-02 07:49:59 +00:00
|
|
|
pub fn scrollbar_offset(mut self, offset: usize) -> Self {
|
|
|
|
self.scrollbar_offset = offset;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the number of blank cells between the text and the scrollbar.
|
|
|
|
///
|
|
|
|
/// Defaults to 1.
|
|
|
|
pub fn right_padding(mut self, padding: usize) -> Self {
|
|
|
|
self.right_padding = padding;
|
2016-07-03 02:37:38 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Call this method whem the content or the view changes.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn set_heights(&mut self, view_height: usize, content_height: usize) {
|
|
|
|
self.view_height = view_height;
|
|
|
|
self.content_height = content_height;
|
|
|
|
|
2017-10-14 00:53:39 +00:00
|
|
|
// eprintln!("Setting heights: {} in {}", content_height, view_height);
|
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
if self.scrollable() {
|
2017-10-12 01:06:58 +00:00
|
|
|
self.start_line =
|
|
|
|
min(self.start_line, self.content_height - self.view_height);
|
2015-05-31 23:38:53 +00:00
|
|
|
} else {
|
|
|
|
self.start_line = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Returns `TRUE` if the view needs to scroll.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn scrollable(&self) -> bool {
|
|
|
|
self.view_height < self.content_height
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Returns `TRUE` unless we are at the top.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn can_scroll_up(&self) -> bool {
|
|
|
|
self.start_line > 0
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Returns `TRUE` unless we are at the bottom.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn can_scroll_down(&self) -> bool {
|
|
|
|
self.start_line + self.view_height < self.content_height
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Scroll to the top of the view.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn scroll_top(&mut self) {
|
|
|
|
self.start_line = 0;
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Makes sure that the given line is visible, scrolling if needed.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn scroll_to(&mut self, y: usize) {
|
|
|
|
if y >= self.start_line + self.view_height {
|
|
|
|
self.start_line = 1 + y - self.view_height;
|
|
|
|
} else if y < self.start_line {
|
|
|
|
self.start_line = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Scroll to the bottom of the view.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn scroll_bottom(&mut self) {
|
2017-02-26 22:53:50 +00:00
|
|
|
if self.scrollable() {
|
|
|
|
self.start_line = self.content_height - self.view_height;
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 02:11:21 +00:00
|
|
|
/// Scroll down by the given number of line.
|
|
|
|
///
|
|
|
|
/// Never further than the bottom of the view.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn scroll_down(&mut self, n: usize) {
|
2017-02-26 22:53:50 +00:00
|
|
|
if self.scrollable() {
|
2017-10-12 01:06:58 +00:00
|
|
|
self.start_line = min(
|
|
|
|
self.start_line + n,
|
|
|
|
self.content_height - self.view_height,
|
|
|
|
);
|
2017-02-26 22:53:50 +00:00
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 01:06:58 +00:00
|
|
|
/// Scrolls down until the scrollbar thumb is at the given location.
|
|
|
|
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.
|
2017-10-12 19:30:59 +00:00
|
|
|
// eprintln!("Scrolling to {}", thumb_y);
|
2017-10-12 01:06:58 +00:00
|
|
|
self.start_line = min(
|
2017-10-12 19:35:57 +00:00
|
|
|
div_up(
|
|
|
|
(1 + self.content_height - self.view_height) * thumb_y,
|
|
|
|
(self.view_height - thumb_height + 1),
|
|
|
|
),
|
2017-10-12 01:06:58 +00:00
|
|
|
self.content_height - self.view_height,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-07-11 02:11:21 +00:00
|
|
|
/// Scroll up by the given number of lines.
|
|
|
|
///
|
|
|
|
/// Never above the top of the view.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn scroll_up(&mut self, n: usize) {
|
2017-02-26 22:53:50 +00:00
|
|
|
if self.scrollable() {
|
|
|
|
self.start_line -= min(self.start_line, n);
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 01:06:58 +00:00
|
|
|
/// Starts scrolling from the given cursor position.
|
|
|
|
pub fn start_drag(&mut self, position: Vec2, width: usize) -> bool {
|
|
|
|
// First: are we on the correct column?
|
2017-10-12 23:41:45 +00:00
|
|
|
let scrollbar_x = self.scrollbar_x(width);
|
|
|
|
// eprintln!("Grabbed {} for {}", position.x, scrollbar_x);
|
|
|
|
if position.x != scrollbar_x {
|
2017-10-12 01:06:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now, did we hit the thumb? Or should we direct-jump?
|
|
|
|
let height = self.scrollbar_thumb_height();
|
|
|
|
let thumb_y = self.scrollbar_thumb_y(height);
|
|
|
|
|
|
|
|
if position.y >= thumb_y && position.y < thumb_y + height {
|
|
|
|
// Grabbed!
|
2017-10-12 21:32:48 +00:00
|
|
|
self.thumb_grab = Some(position.y - thumb_y);
|
2017-10-12 01:06:58 +00:00
|
|
|
} else {
|
|
|
|
// Just jump a bit...
|
2017-10-12 21:32:48 +00:00
|
|
|
self.thumb_grab = Some((height - 1) / 2);
|
|
|
|
// eprintln!("Grabbed at {}", self.thumb_grab);
|
2017-10-12 19:30:00 +00:00
|
|
|
self.drag(position);
|
2017-10-12 01:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Keeps scrolling by dragging the cursor.
|
|
|
|
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.
|
2017-10-12 19:30:59 +00:00
|
|
|
// eprintln!("Dragged: {:?}", position);
|
2017-10-14 00:53:39 +00:00
|
|
|
// eprintln!("thumb: {:?}", self.thumb_grab);
|
2017-10-12 21:32:48 +00:00
|
|
|
if let Some(grab) = self.thumb_grab {
|
|
|
|
let height = self.scrollbar_thumb_height();
|
|
|
|
self.scroll_to_thumb(position.y.saturating_sub(grab), height);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stops grabbing the scrollbar.
|
|
|
|
pub fn release_grab(&mut self) {
|
|
|
|
self.thumb_grab = None;
|
2017-10-12 01:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
/// Draws the scroll bar and the content using the given drawer.
|
|
|
|
///
|
|
|
|
/// `line_drawer` will be called once for each line that needs to be drawn.
|
|
|
|
/// It will be given the absolute ID of the item to draw..
|
|
|
|
/// It will also be given a printer with the correct offset,
|
|
|
|
/// so it should only print on the first line.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-10-11 17:11:40 +00:00
|
|
|
/// ```no_run
|
2015-06-04 18:40:35 +00:00
|
|
|
/// # use cursive::view::ScrollBase;
|
2016-07-14 06:25:54 +00:00
|
|
|
/// # use cursive::Printer;
|
2015-06-06 01:08:05 +00:00
|
|
|
/// # use cursive::theme;
|
2016-10-10 21:08:07 +00:00
|
|
|
/// # use cursive::backend::{self, Backend};
|
2015-06-04 18:40:35 +00:00
|
|
|
/// # let scrollbase = ScrollBase::new();
|
2016-10-10 18:23:00 +00:00
|
|
|
/// # let b = backend::Concrete::init();
|
2017-06-13 01:03:52 +00:00
|
|
|
/// # let t = theme::load_default();
|
|
|
|
/// # let printer = Printer::new((5,1), &t, &b);
|
2015-06-04 18:40:35 +00:00
|
|
|
/// # let printer = &printer;
|
|
|
|
/// let lines = ["Line 1", "Line number 2"];
|
2015-05-31 23:58:55 +00:00
|
|
|
/// scrollbase.draw(printer, |printer, i| {
|
|
|
|
/// printer.print((0,0), lines[i]);
|
|
|
|
/// });
|
|
|
|
/// ```
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn draw<F>(&self, printer: &Printer, line_drawer: F)
|
2017-10-12 01:06:58 +00:00
|
|
|
where
|
|
|
|
F: Fn(&Printer, usize),
|
2015-05-31 23:38:53 +00:00
|
|
|
{
|
2016-07-17 05:05:28 +00:00
|
|
|
if self.view_height == 0 {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-31 23:58:55 +00:00
|
|
|
// Print the content in a sub_printer
|
2017-10-12 01:06:58 +00:00
|
|
|
let max_y =
|
|
|
|
min(self.view_height, self.content_height - self.start_line);
|
2016-03-15 22:37:57 +00:00
|
|
|
let w = if self.scrollable() {
|
2016-08-02 07:49:59 +00:00
|
|
|
// We have to remove the bar width and the padding.
|
2017-08-14 23:32:01 +00:00
|
|
|
printer.size.x.saturating_sub(1 + self.right_padding)
|
2016-03-15 22:37:57 +00:00
|
|
|
} else {
|
|
|
|
printer.size.x
|
|
|
|
};
|
2017-08-14 23:32:01 +00:00
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
for y in 0..max_y {
|
|
|
|
// Y is the actual coordinate of the line.
|
|
|
|
// The item ID is then Y + self.start_line
|
2017-10-12 01:06:58 +00:00
|
|
|
line_drawer(
|
|
|
|
&printer.sub_printer(Vec2::new(0, y), Vec2::new(w, 1), true),
|
|
|
|
y + self.start_line,
|
|
|
|
);
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-31 23:58:55 +00:00
|
|
|
// And draw the scrollbar if needed
|
2015-05-31 23:38:53 +00:00
|
|
|
if self.view_height < self.content_height {
|
2016-07-11 02:11:21 +00:00
|
|
|
// We directly compute the size of the scrollbar
|
|
|
|
// (that way we avoid using floats).
|
2015-05-31 23:38:53 +00:00
|
|
|
// (ratio) * max_height
|
|
|
|
// Where ratio is ({start or end} / content.height)
|
2017-10-12 01:06:58 +00:00
|
|
|
let height = self.scrollbar_thumb_height();
|
|
|
|
let start = self.scrollbar_thumb_y(height);
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
let color = if printer.focused {
|
2016-07-01 06:38:01 +00:00
|
|
|
ColorStyle::Highlight
|
2016-03-15 22:37:57 +00:00
|
|
|
} else {
|
2016-07-01 06:38:01 +00:00
|
|
|
ColorStyle::HighlightInactive
|
2016-03-15 22:37:57 +00:00
|
|
|
};
|
2015-06-03 00:44:31 +00:00
|
|
|
|
2017-10-12 01:06:58 +00:00
|
|
|
let scrollbar_x = self.scrollbar_x(printer.size.x);
|
2017-10-12 23:41:45 +00:00
|
|
|
// eprintln!("Drawing bar at x={}", scrollbar_x);
|
2017-10-12 01:06:58 +00:00
|
|
|
|
|
|
|
// The background
|
2016-07-03 02:37:38 +00:00
|
|
|
printer.print_vline((scrollbar_x, 0), printer.size.y, "|");
|
2017-10-12 01:06:58 +00:00
|
|
|
|
|
|
|
// The scrollbar thumb
|
2015-06-03 00:44:31 +00:00
|
|
|
printer.with_color(color, |printer| {
|
2016-08-02 07:49:59 +00:00
|
|
|
printer.print_vline((scrollbar_x, start), height, "▒");
|
2015-06-03 00:44:31 +00:00
|
|
|
});
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 01:06:58 +00:00
|
|
|
|
|
|
|
/// Returns the X position of the scrollbar, given the size available.
|
|
|
|
///
|
|
|
|
/// Note that this does not depend whether or
|
|
|
|
/// not a scrollbar will actually be present.
|
|
|
|
pub fn scrollbar_x(&self, total_size: usize) -> usize {
|
|
|
|
total_size.saturating_sub(1 + self.scrollbar_offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the height of the scrollbar thumb.
|
|
|
|
pub fn scrollbar_thumb_height(&self) -> usize {
|
|
|
|
max(1, self.view_height * self.view_height / self.content_height)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the y position of the scrollbar thumb.
|
|
|
|
pub fn scrollbar_thumb_y(&self, scrollbar_thumb_height: usize) -> usize {
|
|
|
|
let steps = self.view_height - scrollbar_thumb_height + 1;
|
|
|
|
steps * self.start_line / (1 + self.content_height - self.view_height)
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|