mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Minor fixes
And some doc
This commit is contained in:
parent
323805b52f
commit
c8b2bc943d
@ -7,6 +7,8 @@ use cursive::Cursive;
|
|||||||
use cursive::view::{Dialog,SelectView,TextView,Selector,BoxView};
|
use cursive::view::{Dialog,SelectView,TextView,Selector,BoxView};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
// To keep things simple, little error management is done here.
|
||||||
|
// If you have an error, be sure to run this from the crate root, not from a sub directory.
|
||||||
|
|
||||||
let mut select = SelectView::new();
|
let mut select = SelectView::new();
|
||||||
|
|
||||||
@ -19,10 +21,13 @@ fn main() {
|
|||||||
|
|
||||||
let mut siv = Cursive::new();
|
let mut siv = Cursive::new();
|
||||||
|
|
||||||
|
// Let's add a BoxView to keep the list at a reasonable size - it can scroll anyway.
|
||||||
siv.add_layer(Dialog::new(BoxView::new((20,10), select.with_id("city")))
|
siv.add_layer(Dialog::new(BoxView::new((20,10), select.with_id("city")))
|
||||||
.title("Where are you from?")
|
.title("Where are you from?")
|
||||||
.button("Ok", |s| {
|
.button("Ok", |s| {
|
||||||
|
// Find the SelectView, gets its selection.
|
||||||
let city = s.find::<SelectView>(&Selector::Id("city")).unwrap().selection().to_string();
|
let city = s.find::<SelectView>(&Selector::Id("city")).unwrap().selection().to_string();
|
||||||
|
// And show the next window.
|
||||||
s.pop_layer();
|
s.pop_layer();
|
||||||
s.add_layer(Dialog::new(TextView::new(&format!("{} is a great city!", city)))
|
s.add_layer(Dialog::new(TextView::new(&format!("{} is a great city!", city)))
|
||||||
.button("Quit", |s| s.quit()));
|
.button("Quit", |s| s.quit()));
|
||||||
|
@ -4,6 +4,7 @@ use color;
|
|||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
use printer::Printer;
|
use printer::Printer;
|
||||||
|
|
||||||
|
/// Provide scrolling functionalities to a view.
|
||||||
pub struct ScrollBase {
|
pub struct ScrollBase {
|
||||||
pub start_line: usize,
|
pub start_line: usize,
|
||||||
pub content_height: usize,
|
pub content_height: usize,
|
||||||
@ -19,6 +20,7 @@ impl ScrollBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Call this method whem the content or the view changes.
|
||||||
pub fn set_heights(&mut self, view_height: usize, content_height: usize) {
|
pub fn set_heights(&mut self, view_height: usize, content_height: usize) {
|
||||||
self.view_height = view_height;
|
self.view_height = view_height;
|
||||||
self.content_height = content_height;
|
self.content_height = content_height;
|
||||||
@ -30,22 +32,27 @@ impl ScrollBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `TRUE` if the view needs to scroll.
|
||||||
pub fn scrollable(&self) -> bool {
|
pub fn scrollable(&self) -> bool {
|
||||||
self.view_height < self.content_height
|
self.view_height < self.content_height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `TRUE` unless we are at the top.
|
||||||
pub fn can_scroll_up(&self) -> bool {
|
pub fn can_scroll_up(&self) -> bool {
|
||||||
self.start_line > 0
|
self.start_line > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `TRUE` unless we are at the bottom.
|
||||||
pub fn can_scroll_down(&self) -> bool {
|
pub fn can_scroll_down(&self) -> bool {
|
||||||
self.start_line + self.view_height < self.content_height
|
self.start_line + self.view_height < self.content_height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Scroll to the top of the view.
|
||||||
pub fn scroll_top(&mut self) {
|
pub fn scroll_top(&mut self) {
|
||||||
self.start_line = 0;
|
self.start_line = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Makes sure that the given line is visible, scrolling if needed.
|
||||||
pub fn scroll_to(&mut self, y: usize) {
|
pub fn scroll_to(&mut self, y: usize) {
|
||||||
if y >= self.start_line + self.view_height {
|
if y >= self.start_line + self.view_height {
|
||||||
self.start_line = 1 + y - self.view_height;
|
self.start_line = 1 + y - self.view_height;
|
||||||
@ -54,26 +61,48 @@ impl ScrollBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Scroll to the bottom of the view.
|
||||||
pub fn scroll_bottom(&mut self) {
|
pub fn scroll_bottom(&mut self) {
|
||||||
self.start_line = self.content_height - self.view_height;
|
self.start_line = self.content_height - self.view_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Scroll down by the given number of line, never going further than the bottom of the view.
|
||||||
pub fn scroll_down(&mut self, n: usize) {
|
pub fn scroll_down(&mut self, n: usize) {
|
||||||
self.start_line = min(self.start_line+n, self.content_height - self.view_height);
|
self.start_line = min(self.start_line+n, self.content_height - self.view_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Scroll up by the given number of lines, never going above the top of the view.
|
||||||
pub fn scroll_up(&mut self, n: usize) {
|
pub fn scroll_up(&mut self, n: usize) {
|
||||||
self.start_line -= min(self.start_line, n);
|
self.start_line -= min(self.start_line, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// scrollbase.draw(printer, |printer, i| {
|
||||||
|
/// printer.print((0,0), lines[i]);
|
||||||
|
/// });
|
||||||
|
/// ```
|
||||||
pub fn draw<F>(&self, printer: &Printer, line_drawer: F)
|
pub fn draw<F>(&self, printer: &Printer, line_drawer: F)
|
||||||
where F: Fn(&Printer,usize)
|
where F: Fn(&Printer,usize)
|
||||||
{
|
{
|
||||||
for y in 0..self.view_height {
|
// Print the content in a sub_printer
|
||||||
|
let max_y = min(self.view_height, self.content_height - self.start_line);
|
||||||
|
for y in 0..max_y {
|
||||||
|
// Y is the actual coordinate of the line.
|
||||||
|
// The item ID is then Y + self.start_line
|
||||||
line_drawer(&printer.sub_printer(Vec2::new(0,y),printer.size,true), y+self.start_line);
|
line_drawer(&printer.sub_printer(Vec2::new(0,y),printer.size,true), y+self.start_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// And draw the scrollbar if needed
|
||||||
if self.view_height < self.content_height {
|
if self.view_height < self.content_height {
|
||||||
// We directly compute the size of the scrollbar (this allow use to avoid using floats).
|
// We directly compute the size of the scrollbar (this allow use to avoid using floats).
|
||||||
// (ratio) * max_height
|
// (ratio) * max_height
|
||||||
|
@ -65,13 +65,15 @@ impl <T> SelectView<T> {
|
|||||||
|
|
||||||
impl SelectView<String> {
|
impl SelectView<String> {
|
||||||
/// For String-based SelectView, this is a convenient method to use the label as value.
|
/// For String-based SelectView, this is a convenient method to use the label as value.
|
||||||
|
pub fn add_item_str(&mut self, label: &str) {
|
||||||
|
self.add_item(label, label.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Chainable variant of add_item_str
|
||||||
pub fn item_str(self, label: &str) -> Self {
|
pub fn item_str(self, label: &str) -> Self {
|
||||||
self.item(label, label.to_string())
|
self.item(label, label.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_item_str(&mut self, label: &str) {
|
|
||||||
self.add_item(label, label.to_string());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> View for SelectView<T> {
|
impl <T> View for SelectView<T> {
|
||||||
|
Loading…
Reference in New Issue
Block a user