Add Dialog skeleton

This commit is contained in:
Alexandre Bury 2015-05-16 14:02:15 -07:00
parent 09a21803f8
commit 74f0fee9b6
8 changed files with 128 additions and 8 deletions

View File

@ -19,3 +19,7 @@ git = "https://github.com/jeaye/ncurses-rs"
[[example]] [[example]]
name = "hello_world" name = "hello_world"
path = "examples/hello_world.rs" path = "examples/hello_world.rs"
[[example]]
name = "dialog"
path = "examples/dialog.rs"

14
examples/dialog.rs Normal file
View File

@ -0,0 +1,14 @@
extern crate cursive;
use cursive::Cursive;
use cursive::view::{TextView,Dialog};
fn main() {
let mut siv = Cursive::new();
// Creates a dialog with a single "Quit" button
siv.add_layer(Dialog::new(TextView::new("Hello Dialog!"))
.button("Quit", |s,_| s.quit()));
siv.run();
}

View File

@ -28,6 +28,9 @@ pub mod printer;
pub mod vec2; pub mod vec2;
mod div; mod div;
mod margins;
pub use margins::Margins;
use std::any::Any; use std::any::Any;
use std::rc::Rc; use std::rc::Rc;

23
src/margins.rs Normal file
View File

@ -0,0 +1,23 @@
/// Fixed margins around a rectangular view.
pub struct Margins {
/// Left margin
pub left: u32,
/// Right margin
pub right: u32,
/// Top margin
pub top: u32,
/// Bottom margin
pub bottom: u32,
}
impl Margins {
/// Creates new margins.
pub fn new(left: u32, right: u32, top: u32, bottom: u32) -> Self {
Margins {
left: left,
right: right,
top: top,
bottom: bottom,
}
}
}

View File

@ -53,24 +53,26 @@ impl ToVec2 for (usize,usize) {
} }
} }
impl Add for Vec2 { impl <T: ToVec2> Add<T> for Vec2 {
type Output = Vec2; type Output = Vec2;
fn add(self, other: Vec2) -> Vec2 { fn add(self, other: T) -> Vec2 {
let ov = other.to_vec2();
Vec2 { Vec2 {
x: self.x + other.x, x: self.x + ov.x,
y: self.y + other.y, y: self.y + ov.y,
} }
} }
} }
impl Sub for Vec2 { impl <T: ToVec2> Sub<T> for Vec2 {
type Output = Vec2; type Output = Vec2;
fn sub(self, other: Vec2) -> Vec2 { fn sub(self, other: T) -> Vec2 {
let ov = other.to_vec2();
Vec2 { Vec2 {
x: self.x - other.x, x: self.x - ov.x,
y: self.y - other.y, y: self.y - ov.y,
} }
} }
} }

56
src/view/dialog.rs Normal file
View File

@ -0,0 +1,56 @@
use ::Cursive;
use view::{View,ViewPath,SizeRequest,DimensionRequest};
use vec2::{Vec2};
use printer::Printer;
enum Focus {
Content,
Button(usize),
Nothing,
}
pub struct Dialog {
content: Box<View>,
buttons: Vec<Box<View>>,
focus: Focus,
}
impl Dialog {
pub fn new<V: View + 'static>(view: V) -> Self {
Dialog {
content: Box::new(view),
buttons: Vec::new(),
focus: Focus::Nothing,
}
}
pub fn button<'a, F>(self, label: &'a str, cb: F) -> Self
where F: Fn(&mut Cursive, &ViewPath)
{
self
}
}
fn offset_request(request: DimensionRequest, offset: i32) -> DimensionRequest {
match request {
DimensionRequest::Fixed(w) => DimensionRequest::Fixed((w as i32 + offset) as u32),
DimensionRequest::AtMost(w) => DimensionRequest::AtMost((w as i32 + offset) as u32),
DimensionRequest::Unknown => DimensionRequest::Unknown,
}
}
impl View for Dialog {
fn draw(&self, printer: &Printer) {
}
fn get_min_size(&self, size: SizeRequest) -> Vec2 {
let content_req = SizeRequest {
w: offset_request(size.w, -2),
h: offset_request(size.h, -2),
};
let content_size = self.content.get_min_size(content_req);
content_size + (2u32,2u32)
}
}

View File

@ -5,6 +5,7 @@ mod stack_view;
mod text_view; mod text_view;
mod key_event_view; mod key_event_view;
mod view_path; mod view_path;
mod dialog;
use std::any::Any; use std::any::Any;
@ -13,6 +14,7 @@ pub use self::key_event_view::KeyEventView;
pub use self::box_view::BoxView; pub use self::box_view::BoxView;
pub use self::stack_view::StackView; pub use self::stack_view::StackView;
pub use self::text_view::TextView; pub use self::text_view::TextView;
pub use self::dialog::Dialog;
use event::EventResult; use event::EventResult;
use vec2::Vec2; use vec2::Vec2;

View File

@ -12,4 +12,20 @@ impl ViewPath {
path: Vec::new(), path: Vec::new(),
} }
} }
pub fn from<T: ToPath>(path: T) -> Self {
path.to_path()
}
}
pub trait ToPath {
fn to_path(self) -> ViewPath;
}
impl <'a> ToPath for &'a [usize] {
fn to_path(self) -> ViewPath {
ViewPath {
path: self.to_owned(),
}
}
} }