2015-05-31 23:38:53 +00:00
|
|
|
use std::cmp::max;
|
2015-05-18 18:18:04 +00:00
|
|
|
|
2015-05-18 18:51:30 +00:00
|
|
|
use vec::Vec2;
|
2016-07-02 07:47:38 +00:00
|
|
|
use view::View;
|
2015-05-15 18:58:47 +00:00
|
|
|
use printer::Printer;
|
2015-06-02 00:48:29 +00:00
|
|
|
use align::*;
|
2015-05-28 01:04:33 +00:00
|
|
|
use event::*;
|
2015-05-31 23:38:53 +00:00
|
|
|
use super::scroll::ScrollBase;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2016-07-04 23:04:32 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
2016-07-05 04:30:13 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2016-07-04 23:04:32 +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-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-10 01:23:58 +00:00
|
|
|
last_size: Option<Vec2>,
|
|
|
|
width: Option<usize>,
|
2015-05-25 08:10:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// Subset of the main content representing a row on the display.
|
2015-05-25 08:10:43 +00:00
|
|
|
struct Row {
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
2016-07-10 01:23:58 +00:00
|
|
|
width: usize,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// If the last character is a newline, strip it.
|
2015-05-25 01:51:49 +00:00
|
|
|
fn strip_last_newline(content: &str) -> &str {
|
|
|
|
if !content.is_empty() && content.chars().last().unwrap() == '\n' {
|
|
|
|
&content[..content.len() - 1]
|
|
|
|
} else {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Returns the number of lines required to display the given text with the
|
|
|
|
/// specified maximum line width.
|
2015-05-15 00:48:24 +00:00
|
|
|
fn get_line_span(line: &str, max_width: usize) -> usize {
|
2015-05-25 08:24:40 +00:00
|
|
|
// TODO: this method is stupid. Look at LinesIterator and do the same
|
|
|
|
// (Or use a common function? Better!)
|
2015-05-15 00:41:17 +00:00
|
|
|
let mut lines = 1;
|
|
|
|
let mut length = 0;
|
2016-07-04 23:04:32 +00:00
|
|
|
for l in line.split(' ').map(|word| word.width()) {
|
2015-05-15 00:48:24 +00:00
|
|
|
length += l;
|
2015-05-25 08:30:18 +00:00
|
|
|
if length > max_width {
|
2015-05-15 00:48:24 +00:00
|
|
|
length = l;
|
|
|
|
lines += 1;
|
|
|
|
}
|
2015-05-24 03:12:11 +00:00
|
|
|
length += 1;
|
2015-05-15 00:48:24 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
lines
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextView {
|
|
|
|
/// Creates a new TextView with the given content.
|
|
|
|
pub fn new(content: &str) -> Self {
|
2015-05-25 01:51:49 +00:00
|
|
|
let content = strip_last_newline(content);
|
2015-05-15 00:41:17 +00:00
|
|
|
TextView {
|
|
|
|
content: content.to_string(),
|
2015-05-25 08:10:43 +00:00
|
|
|
rows: Vec::new(),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:46:38 +00:00
|
|
|
/// Replace the text in this view.
|
2015-05-23 17:33:29 +00:00
|
|
|
pub fn set_content(&mut self, content: &str) {
|
2015-05-25 01:51:49 +00:00
|
|
|
let content = strip_last_newline(content);
|
2015-05-23 17:33:29 +00:00
|
|
|
self.content = content.to_string();
|
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
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Returns the number of lines required to display the content
|
|
|
|
/// with the given width.
|
2015-05-15 00:48:24 +00:00
|
|
|
fn get_num_lines(&self, max_width: usize) -> usize {
|
2016-03-15 22:37:57 +00:00
|
|
|
self.content
|
2016-06-28 05:40:11 +00:00
|
|
|
.split('\n')
|
2015-05-15 00:48:24 +00:00
|
|
|
.map(|line| get_line_span(line, max_width))
|
2015-05-15 00:41:17 +00:00
|
|
|
.fold(0, |sum, x| sum + x)
|
|
|
|
}
|
|
|
|
|
2015-05-25 08:24:40 +00:00
|
|
|
// In the absence of any constraint, what size would we like?
|
2015-05-18 18:18:04 +00:00
|
|
|
fn get_ideal_size(&self) -> Vec2 {
|
2015-05-18 22:31:55 +00:00
|
|
|
let mut max_width = 0;
|
2015-05-18 18:18:04 +00:00
|
|
|
let mut height = 0;
|
|
|
|
|
2016-06-28 05:40:11 +00:00
|
|
|
for line in self.content.split('\n') {
|
2015-05-18 18:18:04 +00:00
|
|
|
height += 1;
|
2016-07-04 23:04:32 +00:00
|
|
|
max_width = max(max_width, line.width());
|
2015-05-18 18:18:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 22:31:55 +00:00
|
|
|
Vec2::new(max_width, height)
|
2015-05-18 18:18:04 +00:00
|
|
|
}
|
2016-07-10 01:23:58 +00:00
|
|
|
|
|
|
|
fn is_cache_valid(&self, size: Vec2) -> bool {
|
|
|
|
match self.last_size {
|
|
|
|
None => false,
|
|
|
|
Some(last) => if last.x != size.x {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
(last.y < self.rows.len()) == (size.y < self.rows.len())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_rows(&mut self, size: Vec2) {
|
|
|
|
if !self.is_cache_valid(size) {
|
|
|
|
// Recompute
|
|
|
|
self.rows = LinesIterator::new(&self.content, size.x).collect();
|
|
|
|
let mut scrollbar = 0;
|
|
|
|
if self.rows.len() > size.y {
|
|
|
|
scrollbar = 2;
|
|
|
|
// If we're too high, include a scrollbar
|
|
|
|
self.rows = LinesIterator::new(&self.content, size.x - scrollbar)
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
self.width = self.rows.iter().map(|row| row.width).max().map(|w| w + scrollbar);
|
|
|
|
|
|
|
|
self.last_size = Some(size);
|
|
|
|
}
|
|
|
|
}
|
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-25 08:24:40 +00:00
|
|
|
// Given a multiline string, and a given maximum width,
|
|
|
|
// iterates on the computed rows.
|
2015-05-15 22:00:20 +00:00
|
|
|
struct LinesIterator<'a> {
|
2015-05-25 08:10:43 +00:00
|
|
|
content: &'a str,
|
2015-05-15 22:00:20 +00:00
|
|
|
start: usize,
|
|
|
|
width: usize,
|
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<'a> LinesIterator<'a> {
|
2015-05-25 08:24:40 +00:00
|
|
|
// Start an iterator on the given content.
|
2015-05-25 08:10:43 +00:00
|
|
|
fn new(content: &'a str, width: usize) -> Self {
|
|
|
|
LinesIterator {
|
|
|
|
content: content,
|
|
|
|
width: width,
|
|
|
|
start: 0,
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<'a> Iterator for LinesIterator<'a> {
|
2015-05-25 08:10:43 +00:00
|
|
|
type Item = Row;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Row> {
|
|
|
|
if self.start >= self.content.len() {
|
2015-05-25 08:24:40 +00:00
|
|
|
// This is the end.
|
2015-05-25 08:10:43 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = self.start;
|
|
|
|
let content = &self.content[self.start..];
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
let next = content.find('\n').unwrap_or(content.len());
|
|
|
|
let content = &content[..next];
|
|
|
|
|
|
|
|
let line_width = content.width();
|
|
|
|
if line_width <= self.width {
|
|
|
|
// We found a newline before the allowed limit.
|
|
|
|
// Break early.
|
|
|
|
self.start += next + 1;
|
|
|
|
return Some(Row {
|
|
|
|
start: start,
|
|
|
|
end: next + start,
|
|
|
|
width: line_width,
|
|
|
|
});
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
2015-05-25 08:10:43 +00:00
|
|
|
|
2016-07-05 04:30:13 +00:00
|
|
|
// Keep adding indivisible tokens
|
|
|
|
let head_bytes = match head_bytes(content.split(' '), self.width, " ") {
|
|
|
|
0 => head_bytes(content.graphemes(true), self.width, ""),
|
|
|
|
other => { self.start += 1; other },
|
2015-05-29 00:32:28 +00:00
|
|
|
};
|
2015-05-25 08:10:43 +00:00
|
|
|
|
2016-07-05 04:30:13 +00:00
|
|
|
self.start += head_bytes;
|
2016-06-28 05:40:11 +00:00
|
|
|
|
|
|
|
Some(Row {
|
2015-05-25 08:10:43 +00:00
|
|
|
start: start,
|
2016-07-05 04:30:13 +00:00
|
|
|
end: start + head_bytes,
|
2016-07-10 01:23:58 +00:00
|
|
|
width: self.width,
|
2016-06-28 05:40:11 +00:00
|
|
|
})
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
impl View for TextView {
|
2015-05-31 04:05:34 +00:00
|
|
|
fn draw(&mut 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-05 04:30:13 +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-05 04:30:13 +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-10 01:23:58 +00:00
|
|
|
fn get_min_size(&mut self, size: Vec2) -> Vec2 {
|
2016-07-02 07:47:38 +00:00
|
|
|
// If we have no directive, ask for a single big line.
|
|
|
|
// TODO: what if the text has newlines??
|
|
|
|
// Don't _force_ the max width, but take it if we have to.
|
2016-07-10 01:23:58 +00:00
|
|
|
|
|
|
|
self.compute_rows(size);
|
|
|
|
Vec2::new(self.width.unwrap_or(0), self.rows.len())
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2015-05-25 08:10:43 +00:00
|
|
|
|
2015-05-31 04:53:25 +00:00
|
|
|
fn take_focus(&mut self) -> 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
|
|
|
}
|
2016-07-05 04:30:13 +00:00
|
|
|
|
|
|
|
fn head_bytes<'a, I: Iterator<Item=&'a str>>(iter: I, width: usize, overhead: &str)
|
|
|
|
-> usize {
|
|
|
|
let overhead_width = overhead.width();
|
|
|
|
let overhead_len = overhead.len();
|
|
|
|
|
|
|
|
let sum = iter.scan(0, |w, token| {
|
|
|
|
*w += token.width();
|
|
|
|
if *w > width {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
// Add a space
|
|
|
|
*w += overhead_width;
|
|
|
|
Some(token)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|token| token.len() + overhead_len)
|
|
|
|
.fold(0, |a, b| a + b);
|
|
|
|
|
|
|
|
// We counted overhead_len once too many times,
|
|
|
|
// but only if the iterator was non empty.
|
|
|
|
if sum == 0 {
|
|
|
|
sum
|
|
|
|
} else {
|
|
|
|
sum - overhead_len
|
|
|
|
}
|
|
|
|
}
|