2016-07-14 06:25:54 +00:00
|
|
|
use Printer;
|
2016-10-02 22:22:29 +00:00
|
|
|
use With;
|
|
|
|
use XY;
|
2015-06-02 00:48:29 +00:00
|
|
|
use align::*;
|
2016-10-02 22:22:29 +00:00
|
|
|
use direction::Direction;
|
2015-05-28 01:04:33 +00:00
|
|
|
use event::*;
|
2016-07-26 06:54:33 +00:00
|
|
|
|
2016-07-04 23:04:32 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
|
2016-10-02 22:22:29 +00:00
|
|
|
use utils::{LinesIterator, Row};
|
|
|
|
use vec::Vec2;
|
|
|
|
use view::{SizeCache, View};
|
|
|
|
use view::ScrollBase;
|
|
|
|
|
2016-07-13 04:01:11 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// A simple view showing a fixed text
|
|
|
|
pub struct TextView {
|
|
|
|
content: String,
|
2015-05-25 08:10:43 +00:00
|
|
|
rows: Vec<Row>,
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2015-06-02 00:48:29 +00:00
|
|
|
align: Align,
|
|
|
|
|
2016-07-17 00:08:17 +00:00
|
|
|
// If `false`, disable scrolling.
|
|
|
|
scrollable: bool,
|
|
|
|
|
2016-06-28 05:10:59 +00:00
|
|
|
// ScrollBase make many scrolling-related things easier
|
2015-05-31 23:38:53 +00:00
|
|
|
scrollbase: ScrollBase,
|
2016-07-13 04:01:11 +00:00
|
|
|
last_size: Option<XY<SizeCache>>,
|
2016-07-10 01:23:58 +00:00
|
|
|
width: Option<usize>,
|
2015-05-25 08:10:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// If the last character is a newline, strip it.
|
2016-07-30 19:58:25 +00:00
|
|
|
fn strip_last_newline(content: &mut String) {
|
|
|
|
if content.ends_with('\n') {
|
|
|
|
content.pop().unwrap();
|
2015-05-25 01:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
impl TextView {
|
|
|
|
/// Creates a new TextView with the given content.
|
2016-07-30 19:58:25 +00:00
|
|
|
pub fn new<S: Into<String>>(content: S) -> Self {
|
|
|
|
let mut content = content.into();
|
|
|
|
strip_last_newline(&mut content);
|
2015-05-15 00:41:17 +00:00
|
|
|
TextView {
|
2016-07-30 19:58:25 +00:00
|
|
|
content: content,
|
2015-05-25 08:10:43 +00:00
|
|
|
rows: Vec::new(),
|
2016-07-17 00:08:17 +00:00
|
|
|
scrollable: true,
|
2015-05-31 23:38:53 +00:00
|
|
|
scrollbase: ScrollBase::new(),
|
2015-06-02 00:48:29 +00:00
|
|
|
align: Align::top_left(),
|
2016-07-10 01:23:58 +00:00
|
|
|
last_size: None,
|
|
|
|
width: None,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-17 00:08:17 +00:00
|
|
|
/// Enable or disable the view's scrolling capabilities.
|
|
|
|
///
|
|
|
|
/// When disabled, the view will never attempt to scroll
|
|
|
|
/// (and will always ask for the full height).
|
|
|
|
pub fn set_scrollable(&mut self, scrollable: bool) {
|
|
|
|
self.scrollable = scrollable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable or disable the view's scrolling capabilities.
|
|
|
|
///
|
|
|
|
/// When disabled, the view will never attempt to scroll
|
|
|
|
/// (and will always ask for the full height).
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn scrollable(self, scrollable: bool) -> Self {
|
|
|
|
self.with(|s| s.set_scrollable(scrollable))
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:36:51 +00:00
|
|
|
/// Sets the horizontal alignment for this view.
|
2015-06-02 00:48:29 +00:00
|
|
|
pub fn h_align(mut self, h: HAlign) -> Self {
|
|
|
|
self.align.h = h;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:36:51 +00:00
|
|
|
/// Sets the vertical alignment for this view.
|
2015-06-02 00:48:29 +00:00
|
|
|
pub fn v_align(mut self, v: VAlign) -> Self {
|
|
|
|
self.align.v = v;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:36:51 +00:00
|
|
|
/// Sets the alignment for this view.
|
2015-06-02 00:48:29 +00:00
|
|
|
pub fn align(mut self, a: Align) -> Self {
|
|
|
|
self.align = a;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2016-07-28 06:58:57 +00:00
|
|
|
/// Center the text horizontally and vertically inside the view.
|
|
|
|
pub fn center(mut self) -> Self {
|
|
|
|
self.align = Align::center();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:46:38 +00:00
|
|
|
/// Replace the text in this view.
|
2016-07-30 19:58:25 +00:00
|
|
|
pub fn set_content<S: Into<String>>(&mut self, content: S) {
|
|
|
|
let mut content = content.into();
|
|
|
|
strip_last_newline(&mut content);
|
|
|
|
self.content = content;
|
2016-07-10 01:26:52 +00:00
|
|
|
self.invalidate();
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-23 23:46:38 +00:00
|
|
|
/// Returns the current text in this view.
|
|
|
|
pub fn get_content(&self) -> &str {
|
|
|
|
&self.content
|
|
|
|
}
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
fn is_cache_valid(&self, size: Vec2) -> bool {
|
|
|
|
match self.last_size {
|
|
|
|
None => false,
|
2016-07-13 04:01:11 +00:00
|
|
|
Some(ref last) => last.x.accept(size.x) && last.y.accept(size.y),
|
2016-07-10 01:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_rows(&mut self, size: Vec2) {
|
|
|
|
if !self.is_cache_valid(size) {
|
2016-07-17 05:05:28 +00:00
|
|
|
self.last_size = None;
|
2016-07-10 01:23:58 +00:00
|
|
|
// Recompute
|
2016-07-17 05:05:28 +00:00
|
|
|
|
|
|
|
if size.x == 0 {
|
|
|
|
// Nothing we can do at this poing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
self.rows = LinesIterator::new(&self.content, size.x).collect();
|
|
|
|
let mut scrollbar = 0;
|
2016-07-17 00:08:17 +00:00
|
|
|
if self.scrollable && self.rows.len() > size.y {
|
2016-07-10 01:23:58 +00:00
|
|
|
scrollbar = 2;
|
2016-07-17 05:05:28 +00:00
|
|
|
if size.x < scrollbar {
|
|
|
|
// Again, this is a lost cause.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
// If we're too high, include a scrollbar
|
2016-07-10 01:36:07 +00:00
|
|
|
self.rows = LinesIterator::new(&self.content,
|
|
|
|
size.x - scrollbar)
|
2016-07-10 01:23:58 +00:00
|
|
|
.collect();
|
2016-07-17 05:05:28 +00:00
|
|
|
if self.rows.is_empty() && !self.content.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-10 01:23:58 +00:00
|
|
|
}
|
|
|
|
|
2016-07-17 00:08:17 +00:00
|
|
|
// Desired width, including the scrollbar.
|
2016-07-10 01:36:07 +00:00
|
|
|
self.width = self.rows
|
|
|
|
.iter()
|
|
|
|
.map(|row| row.width)
|
|
|
|
.max()
|
|
|
|
.map(|w| w + scrollbar);
|
2016-07-10 01:23:58 +00:00
|
|
|
|
2016-07-13 04:01:11 +00:00
|
|
|
// Our resulting size.
|
2016-07-17 00:08:17 +00:00
|
|
|
// We can't go lower, width-wise.
|
|
|
|
|
|
|
|
let mut my_size = Vec2::new(self.width.unwrap_or(0),
|
|
|
|
self.rows.len());
|
|
|
|
|
|
|
|
if self.scrollable && my_size.y > size.y {
|
|
|
|
my_size.y = size.y;
|
|
|
|
}
|
|
|
|
|
2016-07-17 05:05:28 +00:00
|
|
|
|
|
|
|
// println_stderr!("my: {:?} | si: {:?}", my_size, size);
|
2016-07-13 08:19:05 +00:00
|
|
|
self.last_size = Some(SizeCache::build(my_size, size));
|
2016-07-10 01:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-10 01:26:52 +00:00
|
|
|
|
|
|
|
// Invalidates the cache, so next call will recompute everything.
|
|
|
|
fn invalidate(&mut self) {
|
|
|
|
self.last_size = None;
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:00:20 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
impl View for TextView {
|
2016-07-16 06:44:38 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2015-06-02 00:48:29 +00:00
|
|
|
|
|
|
|
let h = self.rows.len();
|
2015-06-02 21:23:51 +00:00
|
|
|
let offset = self.align.v.get_offset(h, printer.size.y);
|
2016-07-10 01:36:07 +00:00
|
|
|
let printer =
|
|
|
|
&printer.sub_printer(Vec2::new(0, offset), printer.size, true);
|
2015-06-02 00:48:29 +00:00
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
self.scrollbase.draw(printer, |printer, i| {
|
|
|
|
let row = &self.rows[i];
|
2015-06-02 00:48:29 +00:00
|
|
|
let text = &self.content[row.start..row.end];
|
2016-07-04 23:04:32 +00:00
|
|
|
let l = text.width();
|
2015-06-02 21:23:51 +00:00
|
|
|
let x = self.align.h.get_offset(l, printer.size.x);
|
2016-03-15 22:37:57 +00:00
|
|
|
printer.print((x, 0), text);
|
2015-05-31 23:38:53 +00:00
|
|
|
});
|
2015-05-26 05:35:50 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2015-05-31 23:38:53 +00:00
|
|
|
if !self.scrollbase.scrollable() {
|
2015-05-26 05:35:50 +00:00
|
|
|
return EventResult::Ignored;
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
match event {
|
2016-06-28 05:40:11 +00:00
|
|
|
Event::Key(Key::Home) => self.scrollbase.scroll_top(),
|
|
|
|
Event::Key(Key::End) => self.scrollbase.scroll_bottom(),
|
2016-07-05 04:30:13 +00:00
|
|
|
Event::Key(Key::Up) if self.scrollbase.can_scroll_up() => {
|
|
|
|
self.scrollbase.scroll_up(1)
|
|
|
|
}
|
2016-06-28 05:40:11 +00:00
|
|
|
Event::Key(Key::Down) if self.scrollbase
|
2016-07-10 01:36:07 +00:00
|
|
|
.can_scroll_down() => self.scrollbase.scroll_down(1),
|
2016-06-28 05:40:11 +00:00
|
|
|
Event::Key(Key::PageDown) => self.scrollbase.scroll_down(10),
|
|
|
|
Event::Key(Key::PageUp) => self.scrollbase.scroll_up(10),
|
2015-05-26 05:35:50 +00:00
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
}
|
|
|
|
|
2016-06-28 05:40:11 +00:00
|
|
|
EventResult::Consumed(None)
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 00:41:49 +00:00
|
|
|
fn needs_relayout(&self) -> bool {
|
2016-07-13 04:01:11 +00:00
|
|
|
self.last_size.is_none()
|
2016-07-11 00:41:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
fn get_min_size(&mut self, size: Vec2) -> Vec2 {
|
|
|
|
self.compute_rows(size);
|
2016-07-17 00:08:17 +00:00
|
|
|
|
|
|
|
// This is what we'd like
|
|
|
|
let mut ideal = Vec2::new(self.width.unwrap_or(0), self.rows.len());
|
|
|
|
|
|
|
|
if self.scrollable && ideal.y > size.y {
|
|
|
|
ideal.y = size.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
ideal
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2015-05-25 08:10:43 +00:00
|
|
|
|
2016-07-15 03:27:15 +00:00
|
|
|
fn take_focus(&mut self, _: Direction) -> bool {
|
2015-05-31 23:38:53 +00:00
|
|
|
self.scrollbase.scrollable()
|
2015-05-31 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:10:43 +00:00
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
// Compute the text rows.
|
2016-07-10 01:23:58 +00:00
|
|
|
self.compute_rows(size);
|
2015-05-31 23:38:53 +00:00
|
|
|
self.scrollbase.set_heights(size.y, self.rows.len());
|
2015-05-25 08:10:43 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|