From 1656cdd8f6d65a277e36f8ca358d79bae9c78a1b Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 1 Jun 2015 17:48:29 -0700 Subject: [PATCH] Add Align property to TextView Can change its horizontal and vertical alignment for when the allocated size is bigger than the requirements. --- src/align.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/view/text_view.rs | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/align.rs diff --git a/src/align.rs b/src/align.rs new file mode 100644 index 0000000..fe1cd85 --- /dev/null +++ b/src/align.rs @@ -0,0 +1,35 @@ +//! Tools to control view alignment + +pub struct Align { + pub h: HAlign, + pub v: VAlign, +} + +impl Align { + pub fn new(h: HAlign, v: VAlign) -> Self { + Align { + h: h, + v: v, + } + } + + pub fn top_left() -> Self { + Align::new(HAlign::Left, VAlign::Top) + } + + pub fn center() -> Self { + Align::new(HAlign::Center, VAlign::Center) + } +} + +pub enum HAlign { + Left, + Center, + Right, +} + +pub enum VAlign { + Top, + Center, + Bottom, +} diff --git a/src/lib.rs b/src/lib.rs index 5f5e257..ad59b75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ pub mod view; pub mod printer; pub mod vec; pub mod color; +pub mod align; mod div; mod utf8; diff --git a/src/view/text_view.rs b/src/view/text_view.rs index ef7919c..17041e5 100644 --- a/src/view/text_view.rs +++ b/src/view/text_view.rs @@ -4,6 +4,7 @@ use vec::Vec2; use view::{View,DimensionRequest,SizeRequest}; use div::*; use printer::Printer; +use align::*; use event::*; use super::scroll::ScrollBase; @@ -12,6 +13,8 @@ pub struct TextView { content: String, rows: Vec, + align: Align, + scrollbase: ScrollBase, } @@ -56,9 +59,28 @@ impl TextView { content: content.to_string(), rows: Vec::new(), scrollbase: ScrollBase::new(), + align: Align::top_left(), } } + pub fn h_align(mut self, h: HAlign) -> Self { + self.align.h = h; + + self + } + + pub fn v_align(mut self, v: VAlign) -> Self { + self.align.v = v; + + self + } + + pub fn align(mut self, a: Align) -> Self { + self.align = a; + + self + } + /// Replace the text in this view. pub fn set_content(&mut self, content: &str) { let content = strip_last_newline(content); @@ -182,9 +204,25 @@ impl <'a> Iterator for LinesIterator<'a> { 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 printer = &printer.sub_printer(Vec2::new(0,offset), printer.size, true); + self.scrollbase.draw(printer, |printer, i| { let row = &self.rows[i]; - printer.print((0,0), &self.content[row.start..row.end]); + 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, + }; + printer.print((x,0), text); }); }