Add scrollbar_padding to ScrollBase

And use it in TextArea
This commit is contained in:
Alexandre Bury 2016-08-02 00:49:59 -07:00
parent 93ceb17c21
commit bd78418083
5 changed files with 24 additions and 15 deletions

View File

@ -9,7 +9,7 @@ fn main() {
// Read the list of cities from separate file, and fill the view with it. // Read the list of cities from separate file, and fill the view with it.
// (We include the file at compile-time to avoid runtime read errors.) // (We include the file at compile-time to avoid runtime read errors.)
let content = include_str!("../assets/cities.txt"); let content = include_str!("../assets/cities.txt");
select.add_all_str(content.split('\n')); select.add_all_str(content.lines());
// Sets the callback for when "Enter" is pressed. // Sets the callback for when "Enter" is pressed.
select.set_on_submit(show_next_window); select.set_on_submit(show_next_window);

View File

@ -24,6 +24,9 @@ pub struct ScrollBase {
/// (Useful when each item includes its own side borders, /// (Useful when each item includes its own side borders,
/// to draw the scrollbar inside.) /// to draw the scrollbar inside.)
pub scrollbar_offset: usize, pub scrollbar_offset: usize,
/// Blank between the text and the scrollbar.
pub right_padding: usize,
} }
impl ScrollBase { impl ScrollBase {
@ -34,6 +37,7 @@ impl ScrollBase {
content_height: 0, content_height: 0,
view_height: 0, view_height: 0,
scrollbar_offset: 0, scrollbar_offset: 0,
right_padding: 1,
} }
} }
@ -42,8 +46,16 @@ impl ScrollBase {
/// Used by views that draw their side borders in the children. /// Used by views that draw their side borders in the children.
/// Pushing the scrollbar to the left allows it to stay inside /// Pushing the scrollbar to the left allows it to stay inside
/// the borders. /// the borders.
pub fn bar_offset(mut self, padding: usize) -> Self { pub fn scrollbar_offset(mut self, offset: usize) -> Self {
self.scrollbar_offset = padding; 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;
self self
} }
@ -143,7 +155,8 @@ impl ScrollBase {
if printer.size.x < 2 { if printer.size.x < 2 {
return; return;
} }
printer.size.x - 2 + self.scrollbar_offset // We have to remove the bar width and the padding.
printer.size.x - 1 - self.right_padding
} else { } else {
printer.size.x printer.size.x
}; };
@ -183,7 +196,7 @@ impl ScrollBase {
let scrollbar_x = printer.size.x - 1 - self.scrollbar_offset; let scrollbar_x = printer.size.x - 1 - self.scrollbar_offset;
printer.print_vline((scrollbar_x, 0), printer.size.y, "|"); printer.print_vline((scrollbar_x, 0), printer.size.y, "|");
printer.with_color(color, |printer| { printer.with_color(color, |printer| {
printer.print_vline((scrollbar_x, start), height, " "); printer.print_vline((scrollbar_x, start), height, "");
}); });
} }
} }

View File

@ -7,7 +7,7 @@ use Cursive;
use With; use With;
use menu::{MenuItem, MenuTree}; use menu::{MenuItem, MenuTree};
use Printer; use Printer;
use view::{View, Position, ScrollBase}; use view::{Position, ScrollBase, View};
use views::KeyEventView; use views::KeyEventView;
use align::Align; use align::Align;
use vec::Vec2; use vec::Vec2;
@ -29,7 +29,7 @@ impl MenuPopup {
MenuPopup { MenuPopup {
menu: menu, menu: menu,
focus: 0, focus: 0,
scrollbase: ScrollBase::new().bar_offset(1), scrollbase: ScrollBase::new().scrollbar_offset(1).right_padding(0),
align: Align::top_left(), align: Align::top_left(),
on_dismiss: None, on_dismiss: None,
on_action: None, on_action: None,

View File

@ -241,7 +241,7 @@ impl<T: 'static> SelectView<T> {
} }
fn focus_down(&mut self, n: usize) { fn focus_down(&mut self, n: usize) {
let focus = min(self.focus() + n, self.items.len()); let focus = min(self.focus() + n, self.items.len() - 1);
self.focus.set(focus); self.focus.set(focus);
} }
} }

View File

@ -52,7 +52,7 @@ impl TextArea {
width: 0, width: 0,
}], }],
enabled: true, enabled: true,
scrollbase: ScrollBase::new(), scrollbase: ScrollBase::new().right_padding(0),
last_size: None, last_size: None,
cursor: 0, cursor: 0,
} }
@ -287,12 +287,8 @@ impl View for TextArea {
self.move_left(); self.move_left();
} }
} }
Event::Ctrl(Key::Home) => { Event::Ctrl(Key::Home) => self.cursor = 0,
self.cursor = 0 Event::Ctrl(Key::End) => self.cursor = self.content.len(),
}
Event::Ctrl(Key::End) => {
self.cursor = self.content.len()
}
Event::Key(Key::Home) => { Event::Key(Key::Home) => {
self.cursor = self.rows[self.selected_row()].start self.cursor = self.rows[self.selected_row()].start
} }