mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-27 11:16:03 +00:00
Add ListView
Also added `examples/list_view.rs`.
This commit is contained in:
parent
d633684e41
commit
a22c92a1a1
27
examples/list_view.rs
Normal file
27
examples/list_view.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
extern crate cursive;
|
||||||
|
|
||||||
|
use cursive::Cursive;
|
||||||
|
use cursive::With;
|
||||||
|
use cursive::view::{ListView, Dialog, EditView, TextView, LinearLayout};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut siv = Cursive::new();
|
||||||
|
|
||||||
|
siv.add_layer(Dialog::new(ListView::new()
|
||||||
|
.child("Name", EditView::new().min_length(10))
|
||||||
|
.child("Email", LinearLayout::horizontal()
|
||||||
|
.child(EditView::new().min_length(15))
|
||||||
|
.child(TextView::new("@"))
|
||||||
|
.child(EditView::new().min_length(10)))
|
||||||
|
.delimiter()
|
||||||
|
.with(|list| {
|
||||||
|
for i in 0..50 {
|
||||||
|
list.add_child(&format!("Item {}", i), EditView::new());
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.title("Please fill out this form")
|
||||||
|
.button("Ok", |s| s.quit()));
|
||||||
|
|
||||||
|
siv.run();
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
extern crate cursive;
|
extern crate cursive;
|
||||||
|
|
||||||
use cursive::Cursive;
|
use cursive::Cursive;
|
||||||
|
use cursive::With;
|
||||||
use cursive::menu::MenuTree;
|
use cursive::menu::MenuTree;
|
||||||
use cursive::view::Dialog;
|
use cursive::view::Dialog;
|
||||||
use cursive::view::TextView;
|
use cursive::view::TextView;
|
||||||
|
@ -35,6 +35,14 @@ impl EventResult {
|
|||||||
pub fn with_cb<F: 'static + Fn(&mut Cursive)>(f: F) -> Self {
|
pub fn with_cb<F: 'static + Fn(&mut Cursive)>(f: F) -> Self {
|
||||||
EventResult::Consumed(Some(Rc::new(f)))
|
EventResult::Consumed(Some(Rc::new(f)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if `self` is `EventResult::Consumed`.
|
||||||
|
pub fn is_consumed(&self) -> bool {
|
||||||
|
match *self {
|
||||||
|
EventResult::Consumed(_) => true,
|
||||||
|
EventResult::Ignored => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A non-character key on the keyboard
|
/// A non-character key on the keyboard
|
||||||
|
@ -74,6 +74,7 @@ pub mod direction;
|
|||||||
mod printer;
|
mod printer;
|
||||||
mod menubar;
|
mod menubar;
|
||||||
mod xy;
|
mod xy;
|
||||||
|
mod with;
|
||||||
|
|
||||||
mod div;
|
mod div;
|
||||||
mod utf8;
|
mod utf8;
|
||||||
@ -81,6 +82,7 @@ mod utf8;
|
|||||||
mod backend;
|
mod backend;
|
||||||
|
|
||||||
pub use xy::XY;
|
pub use xy::XY;
|
||||||
|
pub use with::With;
|
||||||
pub use printer::Printer;
|
pub use printer::Printer;
|
||||||
|
|
||||||
use backend::{Backend, NcursesBackend};
|
use backend::{Backend, NcursesBackend};
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
//! Module to build menus.
|
//! Module to build menus.
|
||||||
|
|
||||||
|
use With;
|
||||||
use Cursive;
|
use Cursive;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use event::Callback;
|
use event::Callback;
|
||||||
@ -105,12 +106,4 @@ impl MenuTree {
|
|||||||
pub fn subtree(self, title: &str, tree: MenuTree) -> Self {
|
pub fn subtree(self, title: &str, tree: MenuTree) -> Self {
|
||||||
self.with(|menu| menu.add_subtree(title, tree))
|
self.with(|menu| menu.add_subtree(title, tree))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calls the given closure on `self`.
|
|
||||||
///
|
|
||||||
/// Useful in a function chain.
|
|
||||||
pub fn with<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
|
|
||||||
f(&mut self);
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ impl EditView {
|
|||||||
|
|
||||||
impl View for EditView {
|
impl View for EditView {
|
||||||
fn draw(&self, printer: &Printer) {
|
fn draw(&self, printer: &Printer) {
|
||||||
assert!(printer.size.x == self.last_length);
|
assert!(printer.size.x == self.last_length, "Was promised {}, received {}", self.last_length, printer.size.x);
|
||||||
|
|
||||||
let width = self.content.width();
|
let width = self.content.width();
|
||||||
printer.with_color(ColorStyle::Secondary, |printer| {
|
printer.with_color(ColorStyle::Secondary, |printer| {
|
||||||
|
@ -108,10 +108,10 @@ impl LinearLayout {
|
|||||||
|
|
||||||
/// Returns a cyclic mutable iterator starting with the child in focus
|
/// Returns a cyclic mutable iterator starting with the child in focus
|
||||||
fn iter_mut<'a>(&'a mut self, from_focus: bool,
|
fn iter_mut<'a>(&'a mut self, from_focus: bool,
|
||||||
direction: direction::Relative)
|
source: direction::Relative)
|
||||||
-> Box<Iterator<Item = (usize, &mut Child)> + 'a> {
|
-> Box<Iterator<Item = (usize, &mut Child)> + 'a> {
|
||||||
|
|
||||||
match direction {
|
match source {
|
||||||
direction::Relative::Front => {
|
direction::Relative::Front => {
|
||||||
let start = if from_focus {
|
let start = if from_focus {
|
||||||
self.focus
|
self.focus
|
||||||
|
268
src/view/list_view.rs
Normal file
268
src/view/list_view.rs
Normal file
@ -0,0 +1,268 @@
|
|||||||
|
use Printer;
|
||||||
|
use With;
|
||||||
|
use vec::Vec2;
|
||||||
|
use view::View;
|
||||||
|
use direction;
|
||||||
|
use view::scroll::ScrollBase;
|
||||||
|
use event::{Event, EventResult, Key};
|
||||||
|
|
||||||
|
enum Child {
|
||||||
|
Row(String, Box<View>),
|
||||||
|
Delimiter,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Child {
|
||||||
|
fn label(&self) -> &str {
|
||||||
|
match *self {
|
||||||
|
Child::Row(ref label, _) => label,
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(&mut self) -> Option<&mut Box<View>> {
|
||||||
|
match *self {
|
||||||
|
Child::Row(_, ref mut view) => Some(view),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Currently unused
|
||||||
|
/*
|
||||||
|
fn is_delimiter(&self) -> bool {
|
||||||
|
match *self {
|
||||||
|
Child::Row(_, _) => false,
|
||||||
|
Child::Delimiter => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Displays a scrollable list of elements.
|
||||||
|
pub struct ListView {
|
||||||
|
children: Vec<Child>,
|
||||||
|
scrollbase: ScrollBase,
|
||||||
|
focus: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ListView {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ListView {
|
||||||
|
/// Creates a new, empty `ListView`.
|
||||||
|
pub fn new() -> Self {
|
||||||
|
ListView {
|
||||||
|
children: Vec::new(),
|
||||||
|
scrollbase: ScrollBase::new(),
|
||||||
|
focus: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a view to the end of the list.
|
||||||
|
pub fn add_child<V: View + 'static>(&mut self, label: &str, view: V) {
|
||||||
|
self.children.push(Child::Row(label.to_string(), Box::new(view)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a view to the end of the list.
|
||||||
|
///
|
||||||
|
/// Chainable variant.
|
||||||
|
pub fn child<V: View + 'static>(self, label: &str, view: V) -> Self {
|
||||||
|
self.with(|s| s.add_child(label, view))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a delimiter to the end of the list.
|
||||||
|
pub fn add_delimiter(&mut self) {
|
||||||
|
self.children.push(Child::Delimiter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a delimiter to the end of the list.
|
||||||
|
///
|
||||||
|
/// Chainable variant.
|
||||||
|
pub fn delimiter(self) -> Self {
|
||||||
|
self.with(Self::add_delimiter)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn iter_mut<'a>(&'a mut self, from_focus: bool,
|
||||||
|
source: direction::Relative)
|
||||||
|
-> Box<Iterator<Item = (usize, &mut Child)> + 'a> {
|
||||||
|
|
||||||
|
match source {
|
||||||
|
direction::Relative::Front => {
|
||||||
|
let start = if from_focus {
|
||||||
|
self.focus
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
Box::new(self.children.iter_mut().enumerate().skip(start))
|
||||||
|
}
|
||||||
|
direction::Relative::Back => {
|
||||||
|
let end = if from_focus {
|
||||||
|
self.focus + 1
|
||||||
|
} else {
|
||||||
|
self.children.len()
|
||||||
|
};
|
||||||
|
Box::new(self.children[..end].iter_mut().enumerate().rev())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_focus(&mut self, n: usize, source: direction::Direction)
|
||||||
|
-> EventResult {
|
||||||
|
let i = if let Some(i) =
|
||||||
|
source.relative(direction::Orientation::Vertical)
|
||||||
|
.and_then(|rel| {
|
||||||
|
// The iterator starts at the focused element.
|
||||||
|
// We don't want that one.
|
||||||
|
self.iter_mut(true, rel)
|
||||||
|
.skip(1)
|
||||||
|
.filter_map(|p| try_focus(p, source))
|
||||||
|
.take(n)
|
||||||
|
.last()
|
||||||
|
}) {
|
||||||
|
i
|
||||||
|
} else {
|
||||||
|
return EventResult::Ignored;
|
||||||
|
};
|
||||||
|
self.focus = i;
|
||||||
|
self.scrollbase.scroll_to(self.focus);
|
||||||
|
|
||||||
|
EventResult::Consumed(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn try_focus((i, child): (usize, &mut Child), source: direction::Direction)
|
||||||
|
-> Option<usize> {
|
||||||
|
match *child {
|
||||||
|
Child::Delimiter => None,
|
||||||
|
Child::Row(_, ref mut view) => {
|
||||||
|
if view.take_focus(source) {
|
||||||
|
Some(i)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl View for ListView {
|
||||||
|
fn draw(&self, printer: &Printer) {
|
||||||
|
let offset = self.children
|
||||||
|
.iter()
|
||||||
|
.map(Child::label)
|
||||||
|
.map(str::len)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0) + 1;
|
||||||
|
|
||||||
|
self.scrollbase.draw(printer, |printer, i| {
|
||||||
|
match self.children[i] {
|
||||||
|
Child::Row(ref label, ref view) => {
|
||||||
|
printer.print((0, 0), label);
|
||||||
|
view.draw(&printer.offset((offset, 0), i == self.focus));
|
||||||
|
}
|
||||||
|
Child::Delimiter => (),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_min_size(&mut self, req: Vec2) -> Vec2 {
|
||||||
|
let label_size = self.children
|
||||||
|
.iter()
|
||||||
|
.map(Child::label)
|
||||||
|
.map(str::len)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0);
|
||||||
|
let view_size = self.children
|
||||||
|
.iter_mut()
|
||||||
|
.filter_map(Child::view)
|
||||||
|
.map(|v| v.get_min_size(req).x)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
if self.children.len() > req.y {
|
||||||
|
Vec2::new(label_size + 1 + view_size + 2, req.y)
|
||||||
|
} else {
|
||||||
|
Vec2::new(label_size + 1 + view_size, self.children.len())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout(&mut self, size: Vec2) {
|
||||||
|
self.scrollbase.set_heights(size.y, self.children.len());
|
||||||
|
|
||||||
|
let label_size = self.children
|
||||||
|
.iter()
|
||||||
|
.map(Child::label)
|
||||||
|
.map(str::len)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0);
|
||||||
|
let mut available = size.x - label_size - 1;
|
||||||
|
|
||||||
|
if self.children.len() > size.y {
|
||||||
|
available -= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
for child in self.children.iter_mut().filter_map(Child::view) {
|
||||||
|
child.layout(Vec2::new(available, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_event(&mut self, event: Event) -> EventResult {
|
||||||
|
if let Child::Row(_, ref mut view) = self.children[self.focus] {
|
||||||
|
let result = view.on_event(event);
|
||||||
|
if result.is_consumed() {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::Key(Key::Up) if self.focus > 0 => {
|
||||||
|
self.move_focus(1, direction::Direction::down())
|
||||||
|
}
|
||||||
|
Event::Key(Key::Down) if self.focus + 1 < self.children.len() => {
|
||||||
|
self.move_focus(1, direction::Direction::up())
|
||||||
|
}
|
||||||
|
Event::Key(Key::PageUp) => {
|
||||||
|
self.move_focus(10, direction::Direction::down())
|
||||||
|
}
|
||||||
|
Event::Key(Key::PageDown) => {
|
||||||
|
self.move_focus(10, direction::Direction::up())
|
||||||
|
}
|
||||||
|
Event::Key(Key::Home) |
|
||||||
|
Event::Ctrl(Key::Home) => {
|
||||||
|
self.move_focus(usize::max_value(),
|
||||||
|
direction::Direction::back())
|
||||||
|
}
|
||||||
|
Event::Key(Key::End) |
|
||||||
|
Event::Ctrl(Key::End) => {
|
||||||
|
self.move_focus(usize::max_value(),
|
||||||
|
direction::Direction::front())
|
||||||
|
}
|
||||||
|
Event::Key(Key::Tab) => {
|
||||||
|
self.move_focus(1, direction::Direction::front())
|
||||||
|
}
|
||||||
|
Event::Shift(Key::Tab) => {
|
||||||
|
self.move_focus(1, direction::Direction::back())
|
||||||
|
}
|
||||||
|
_ => EventResult::Ignored,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn take_focus(&mut self, source: direction::Direction) -> bool {
|
||||||
|
let rel = source.relative(direction::Orientation::Vertical);
|
||||||
|
let i = if let Some(i) = self.iter_mut(rel.is_none(),
|
||||||
|
rel.unwrap_or(direction::Relative::Front))
|
||||||
|
.filter_map(|p| try_focus(p, source))
|
||||||
|
.next() {
|
||||||
|
i
|
||||||
|
} else {
|
||||||
|
// No one wants to be in focus
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
self.focus = i;
|
||||||
|
self.scrollbase.scroll_to(self.focus);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
@ -54,6 +54,7 @@ mod full_view;
|
|||||||
mod id_view;
|
mod id_view;
|
||||||
mod key_event_view;
|
mod key_event_view;
|
||||||
mod linear_layout;
|
mod linear_layout;
|
||||||
|
mod list_view;
|
||||||
mod menu_popup;
|
mod menu_popup;
|
||||||
mod shadow_view;
|
mod shadow_view;
|
||||||
mod select_view;
|
mod select_view;
|
||||||
@ -83,6 +84,7 @@ pub use self::edit_view::EditView;
|
|||||||
pub use self::full_view::FullView;
|
pub use self::full_view::FullView;
|
||||||
pub use self::key_event_view::KeyEventView;
|
pub use self::key_event_view::KeyEventView;
|
||||||
pub use self::linear_layout::LinearLayout;
|
pub use self::linear_layout::LinearLayout;
|
||||||
|
pub use self::list_view::ListView;
|
||||||
pub use self::menu_popup::MenuPopup;
|
pub use self::menu_popup::MenuPopup;
|
||||||
pub use self::view_path::ViewPath;
|
pub use self::view_path::ViewPath;
|
||||||
pub use self::select_view::SelectView;
|
pub use self::select_view::SelectView;
|
||||||
|
@ -134,7 +134,7 @@ impl ScrollBase {
|
|||||||
let max_y = min(self.view_height,
|
let max_y = min(self.view_height,
|
||||||
self.content_height - self.start_line);
|
self.content_height - self.start_line);
|
||||||
let w = if self.scrollable() {
|
let w = if self.scrollable() {
|
||||||
printer.size.x - 1 // TODO: 2
|
printer.size.x - 2 + self.scrollbar_padding // TODO: 2
|
||||||
} else {
|
} else {
|
||||||
printer.size.x
|
printer.size.x
|
||||||
};
|
};
|
||||||
|
11
src/with.rs
Normal file
11
src/with.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
/// Generic trait to enable chainable API
|
||||||
|
pub trait With: Sized {
|
||||||
|
/// Calls the given closure on `self`.
|
||||||
|
fn with<F: FnOnce(&mut Self)>(mut self, f: F) -> Self {
|
||||||
|
f(&mut self);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <T: Sized> With for T {}
|
Loading…
Reference in New Issue
Block a user