diff --git a/examples/select.rs b/examples/select.rs index c398348..acc9c02 100644 --- a/examples/select.rs +++ b/examples/select.rs @@ -4,13 +4,14 @@ use std::fs::File; use std::io::{BufReader,BufRead}; use cursive::Cursive; +use cursive::align::HAlign; use cursive::view::{Dialog,SelectView,TextView,BoxView}; 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().h_align(HAlign::Center); // Read the list of cities from separate file, and fill the view with it. let file = File::open("assets/cities.txt").unwrap(); diff --git a/src/align.rs b/src/align.rs index fe1cd85..0a1dd98 100644 --- a/src/align.rs +++ b/src/align.rs @@ -33,3 +33,23 @@ pub enum VAlign { Center, Bottom, } + +impl HAlign { + pub fn get_offset(&self, content: usize, container: usize) -> usize { + match *self { + HAlign::Left => 0, + HAlign::Center => (container - content)/2, + HAlign::Right => (container - content), + } + } +} + +impl VAlign { + pub fn get_offset(&self, content: usize, container: usize) -> usize { + match *self { + VAlign::Top => 0, + VAlign::Center => (container - content)/2, + VAlign::Bottom => (container - content), + } + } +} diff --git a/src/view/select_view.rs b/src/view/select_view.rs index 3d62019..3dc576c 100644 --- a/src/view/select_view.rs +++ b/src/view/select_view.rs @@ -3,6 +3,7 @@ use std::rc::Rc; use color; use ::Cursive; +use align::*; use view::{View,IdView,SizeRequest,DimensionRequest}; use event::{Event,EventResult,Key}; use vec::Vec2; @@ -31,6 +32,7 @@ pub struct SelectView { focus: usize, scrollbase: ScrollBase, select_cb: Option>>, + align: Align, } impl SelectView { @@ -41,6 +43,7 @@ impl SelectView { focus: 0, scrollbase: ScrollBase::new(), select_cb: None, + align: Align::top_left(), } } @@ -53,6 +56,24 @@ impl SelectView { self } + pub fn align(mut self, align: Align) -> Self { + self.align = align; + + self + } + + pub fn v_align(mut self, v: VAlign) -> Self { + self.align.v = v; + + self + } + + pub fn h_align(mut self, h: HAlign) -> Self { + self.align.h = h; + + self + } + /// Returns the value of the currently selected item. Panics if the list is empty. pub fn selection(&self) -> Rc { self.items[self.focus].value.clone() @@ -92,6 +113,10 @@ impl SelectView { impl View for SelectView { fn draw(&mut self, printer: &Printer) { + let h = self.items.len(); + let offset = self.align.v.get_offset(h, printer.size.y); + let printer = &printer.sub_printer(Vec2::new(0,offset), printer.size, true); + self.scrollbase.draw(printer, |printer,i| { let style = if i == self.focus { if printer.focused { color::HIGHLIGHT } @@ -100,9 +125,11 @@ impl View for SelectView { color::PRIMARY }; printer.with_color(style, |printer| { - printer.print((0,0), &self.items[i].label); - let x:usize = self.items[i].label.chars().count(); - printer.print_hline((x,0), printer.size.x-x, ' ' as u64); + let l = self.items[i].label.chars().count(); + let x = self.align.h.get_offset(l, printer.size.x); + printer.print_hline((0,0), x, ' ' as u64); + printer.print((x,0), &self.items[i].label); + printer.print_hline((x+l,0), printer.size.x-l-x, ' ' as u64); }); }); } diff --git a/src/view/text_view.rs b/src/view/text_view.rs index 17041e5..b91a090 100644 --- a/src/view/text_view.rs +++ b/src/view/text_view.rs @@ -206,22 +206,14 @@ impl View for TextView { fn draw(&mut self, printer: &Printer) { let h = self.rows.len(); - let offset = match self.align.v { - VAlign::Top => 0, - VAlign::Center => (printer.size.y - h)/2, - VAlign::Bottom => printer.size.y - h, - }; + let offset = self.align.v.get_offset(h, printer.size.y); let printer = &printer.sub_printer(Vec2::new(0,offset), printer.size, true); self.scrollbase.draw(printer, |printer, i| { let row = &self.rows[i]; let text = &self.content[row.start..row.end]; let l = text.chars().count(); - let x = match self.align.h { - HAlign::Left => 0, - HAlign::Center => (printer.size.x-l)/2, - HAlign::Right => printer.size.x-l, - }; + let x = self.align.h.get_offset(l, printer.size.x); printer.print((x,0), text); }); }