Add some doc.

This commit is contained in:
Alexandre Bury 2015-06-03 15:36:51 -07:00
parent 781d9c1a03
commit a77a40558f
5 changed files with 26 additions and 0 deletions

View File

@ -1,11 +1,13 @@
//! Tools to control view alignment
/// Specifies the alignment along both horizontal and vertical directions.
pub struct Align {
pub h: HAlign,
pub v: VAlign,
}
impl Align {
/// Creates a new Align object from the given horizontal and vertical alignments.
pub fn new(h: HAlign, v: VAlign) -> Self {
Align {
h: h,
@ -13,33 +15,40 @@ impl Align {
}
}
/// Creates a top-left alignment.
pub fn top_left() -> Self {
Align::new(HAlign::Left, VAlign::Top)
}
/// Creates a top-right alignment.
pub fn top_right() -> Self {
Align::new(HAlign::Right, VAlign::Top)
}
/// Creates a bottom-left alignment.
pub fn bot_left() -> Self {
Align::new(HAlign::Left, VAlign::Bottom)
}
/// Creates a bottom-right alignment.
pub fn bot_right() -> Self {
Align::new(HAlign::Right, VAlign::Top)
}
/// Creates an alignment centered both horizontally and vertically.
pub fn center() -> Self {
Align::new(HAlign::Center, VAlign::Center)
}
}
/// Horizontal alignment
pub enum HAlign {
Left,
Center,
Right,
}
/// Vertical alignment
pub enum VAlign {
Top,
Center,
@ -47,6 +56,8 @@ pub enum VAlign {
}
impl HAlign {
/// To draw a view with size `content` in a printer with size `container`, this returns the
/// offset to start printing the view at.
pub fn get_offset(&self, content: usize, container: usize) -> usize {
match *self {
HAlign::Left => 0,
@ -57,6 +68,8 @@ impl HAlign {
}
impl VAlign {
/// To draw a view with size `content` in a printer with size `container`, this returns the
/// offset to start printing the view at.
pub fn get_offset(&self, content: usize, container: usize) -> usize {
match *self {
VAlign::Top => 0,

View File

@ -68,6 +68,7 @@ pub enum Key {
}
impl Key {
/// Returns the Key enum corresponding to the given ncurses event.
pub fn from_ncurses(ch: i32) -> Self {
match ch {
// Tab is '\t'
@ -173,6 +174,7 @@ pub enum Event {
KeyEvent(Key),
}
/// Generic trait to convert a value to an event.
pub trait ToEvent {
fn to_event(self) -> Event;
}

View File

@ -62,12 +62,16 @@ impl Dialog {
self
}
/// Sets the horizontal alignment for the buttons, if any.
/// Only works if the buttons are as a row at the bottom of the dialog.
pub fn h_align(mut self, h: HAlign) -> Self {
self.align.h = h;
self
}
/// Sets the vertical alignment for the buttons, if any.
/// Only works if the buttons are as a column to the right of the dialog.
pub fn v_align(mut self, v: VAlign) -> Self {
self.align.v = v;

View File

@ -56,18 +56,22 @@ impl <T: 'static> SelectView<T> {
self
}
/// Sets the alignment for this view.
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
/// Sets the vertical alignment for this view.
/// (If the ciew is given too much space vertically.)
pub fn v_align(mut self, v: VAlign) -> Self {
self.align.v = v;
self
}
/// Sets the horizontal alignment for this view.
pub fn h_align(mut self, h: HAlign) -> Self {
self.align.h = h;

View File

@ -63,18 +63,21 @@ impl TextView {
}
}
/// Sets the horizontal alignment for this view.
pub fn h_align(mut self, h: HAlign) -> Self {
self.align.h = h;
self
}
/// Sets the vertical alignment for this view.
pub fn v_align(mut self, v: VAlign) -> Self {
self.align.v = v;
self
}
/// Sets the alignment for this view.
pub fn align(mut self, a: Align) -> Self {
self.align = a;