Add panel view

This commit is contained in:
Alexandre Bury 2016-07-25 13:35:46 -07:00
parent 6c71ac14b0
commit c1c9322319
4 changed files with 41 additions and 7 deletions

View File

@ -17,12 +17,11 @@ fn main() {
let n_max = 1000; let n_max = 1000;
s.find_id::<Dialog>("dialog") s.pop_layer();
.unwrap() s.add_layer(Panel::new(FullView::full_width(ProgressBar::new()
.set_content(ProgressBar::new()
.range(0, n_max) .range(0, n_max)
.with_value(value.clone()) .with_value(value.clone())
.with_callback(cb.clone())); .with_callback(cb.clone()))));
// Spawn a thread to process things in the background. // Spawn a thread to process things in the background.
thread::spawn(move || { thread::spawn(move || {
@ -41,10 +40,10 @@ fn main() {
})) }))
.title("Progress bar example") .title("Progress bar example")
.padding((0,0,1,1)) .padding((0, 0, 1, 1))
.with_id("dialog")); .with_id("dialog"));
siv.set_fps(10); siv.set_fps(30);
siv.run(); siv.run();
} }

View File

@ -9,7 +9,7 @@
pub use {Cursive, Printer, With}; pub use {Cursive, Printer, With};
pub use event::{Event, Key}; pub use event::{Event, Key};
pub use view::{BoxView, Button, Checkbox, Dialog, EditView, FullView, IdView, pub use view::{BoxView, Button, Checkbox, Dialog, EditView, FullView, IdView,
Identifiable, KeyEventView, LinearLayout, ListView, Identifiable, KeyEventView, LinearLayout, ListView, Panel,
ProgressBar, SelectView, Selector, TextView, View}; ProgressBar, SelectView, Selector, TextView, View};
pub use vec::Vec2; pub use vec::Vec2;
pub use menu::MenuTree; pub use menu::MenuTree;

View File

@ -58,6 +58,7 @@ mod linear_layout;
mod list_view; mod list_view;
mod menubar; mod menubar;
mod menu_popup; mod menu_popup;
mod panel;
mod progress_bar; mod progress_bar;
mod shadow_view; mod shadow_view;
mod select_view; mod select_view;
@ -92,6 +93,7 @@ pub use self::list_view::ListView;
pub use self::menubar::Menubar; pub use self::menubar::Menubar;
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::panel::Panel;
pub use self::progress_bar::ProgressBar; pub use self::progress_bar::ProgressBar;
pub use self::select_view::SelectView; pub use self::select_view::SelectView;
pub use self::shadow_view::ShadowView; pub use self::shadow_view::ShadowView;

33
src/view/panel.rs Normal file
View File

@ -0,0 +1,33 @@
use Printer;
use vec::Vec2;
use view::{View, ViewWrapper};
/// Draws a border around a wrapped view.
pub struct Panel<V: View> {
view: V,
}
impl<V: View> Panel<V> {
/// Creates a new panel around the given view.
pub fn new(view: V) -> Self {
Panel {
view: view,
}
}
}
impl<V: View> ViewWrapper for Panel<V> {
wrap_impl!(&self.view);
fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 {
// TODO: make borders conditional?
self.view.get_min_size(req - (2, 2)) + (2, 2)
}
fn wrap_draw(&self, printer: &Printer) {
printer.print_box((0, 0), printer.size);
self.view
.draw(&printer.sub_printer((1, 1), printer.size - (2, 2), true));
}
}