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::*;
|
2017-12-30 21:55:04 +00:00
|
|
|
use owning_ref::{ArcRef, OwningHandle};
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::sync::Arc;
|
2018-05-18 00:37:39 +00:00
|
|
|
use std::sync::{Mutex, MutexGuard};
|
2018-01-05 13:14:24 +00:00
|
|
|
use theme::Effect;
|
2018-01-13 18:36:56 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
use utils::lines::spans::{LinesIterator, Row};
|
|
|
|
use utils::markup::StyledString;
|
2016-10-02 22:22:29 +00:00
|
|
|
use vec::Vec2;
|
2017-10-12 01:06:58 +00:00
|
|
|
use view::{ScrollBase, ScrollStrategy, SizeCache, View};
|
2018-05-18 00:37:39 +00:00
|
|
|
use Printer;
|
|
|
|
use With;
|
|
|
|
use XY;
|
2016-07-13 04:01:11 +00:00
|
|
|
|
2017-12-31 18:49:13 +00:00
|
|
|
/// Provides access to the content of a [`TextView`].
|
|
|
|
///
|
|
|
|
/// Cloning this object will still point to the same content.
|
|
|
|
///
|
|
|
|
/// [`TextView`]: struct.TextView.html
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use cursive::views::{TextView, TextContent};
|
|
|
|
/// let mut content = TextContent::new("content");
|
|
|
|
/// let view = TextView::new_with_content(content.clone());
|
|
|
|
///
|
|
|
|
/// // Later, possibly in a different thread
|
|
|
|
/// content.set_content("new content");
|
2018-01-13 18:36:56 +00:00
|
|
|
/// assert!(content.get_content().source().contains("new"));
|
2017-12-31 18:49:13 +00:00
|
|
|
/// ```
|
2017-12-30 21:55:04 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct TextContent {
|
|
|
|
content: Arc<Mutex<TextContentInner>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextContent {
|
2018-01-10 22:58:29 +00:00
|
|
|
/// Creates a new text content around the given value.
|
|
|
|
///
|
|
|
|
/// Parses the given value.
|
2018-01-13 18:36:56 +00:00
|
|
|
pub fn new<S>(content: S) -> Self
|
2018-01-10 22:58:29 +00:00
|
|
|
where
|
2018-01-13 18:36:56 +00:00
|
|
|
S: Into<StyledString>,
|
2018-01-10 22:58:29 +00:00
|
|
|
{
|
2018-01-13 18:36:56 +00:00
|
|
|
let content = content.into();
|
2018-01-10 22:58:29 +00:00
|
|
|
|
2018-01-13 18:36:56 +00:00
|
|
|
TextContent {
|
2017-12-30 21:55:04 +00:00
|
|
|
content: Arc::new(Mutex::new(TextContentInner {
|
2018-01-10 22:58:29 +00:00
|
|
|
content,
|
2017-12-30 21:55:04 +00:00
|
|
|
size_cache: None,
|
|
|
|
})),
|
2018-01-13 18:36:56 +00:00
|
|
|
}
|
2017-12-30 21:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A reference to the text content.
|
|
|
|
///
|
2018-01-13 18:36:56 +00:00
|
|
|
/// This can be deref'ed into a [`StyledString`].
|
|
|
|
///
|
|
|
|
/// [`StyledString`]: ../utils/markup/type.StyledString.html
|
2017-12-30 22:03:42 +00:00
|
|
|
///
|
2017-12-30 21:55:04 +00:00
|
|
|
/// This keeps the content locked. Do not store this!
|
|
|
|
pub struct TextContentRef {
|
|
|
|
handle: OwningHandle<
|
|
|
|
ArcRef<Mutex<TextContentInner>>,
|
|
|
|
MutexGuard<'static, TextContentInner>,
|
|
|
|
>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for TextContentRef {
|
2018-01-13 18:36:56 +00:00
|
|
|
type Target = StyledString;
|
2017-12-30 21:55:04 +00:00
|
|
|
|
2018-01-13 18:36:56 +00:00
|
|
|
fn deref(&self) -> &StyledString {
|
2017-12-30 21:55:04 +00:00
|
|
|
&self.handle.content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextContent {
|
|
|
|
/// Replaces the content with the given value.
|
2018-01-10 22:58:29 +00:00
|
|
|
pub fn set_content<S>(&mut self, content: S)
|
|
|
|
where
|
2018-01-13 18:36:56 +00:00
|
|
|
S: Into<StyledString>,
|
2018-01-10 22:58:29 +00:00
|
|
|
{
|
2018-01-13 18:36:56 +00:00
|
|
|
self.with_content(|c| *c = content.into());
|
2017-12-30 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Append `content` to the end of a `TextView`.
|
2018-01-13 18:36:56 +00:00
|
|
|
pub fn append<S>(&mut self, content: S)
|
2018-01-10 22:58:29 +00:00
|
|
|
where
|
2018-01-13 18:36:56 +00:00
|
|
|
S: Into<StyledString>,
|
2018-01-10 22:58:29 +00:00
|
|
|
{
|
2018-01-13 18:36:56 +00:00
|
|
|
self.with_content(|c| c.append(content))
|
2017-12-30 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the content.
|
|
|
|
///
|
|
|
|
/// This locks the data while the returned value is alive,
|
|
|
|
/// so don't keep it too long.
|
|
|
|
pub fn get_content(&self) -> TextContentRef {
|
|
|
|
TextContentInner::get_content(&self.content)
|
|
|
|
}
|
|
|
|
|
2018-01-10 22:58:29 +00:00
|
|
|
fn with_content<F, O>(&mut self, f: F) -> O
|
2017-12-30 21:55:04 +00:00
|
|
|
where
|
2018-01-10 22:58:29 +00:00
|
|
|
F: FnOnce(&mut StyledString) -> O,
|
2017-12-30 21:55:04 +00:00
|
|
|
{
|
|
|
|
let mut lock = self.content.lock().unwrap();
|
|
|
|
|
2018-01-10 22:58:29 +00:00
|
|
|
let out = f(&mut lock.content);
|
2017-12-30 21:55:04 +00:00
|
|
|
|
|
|
|
lock.size_cache = None;
|
2018-01-10 22:58:29 +00:00
|
|
|
|
|
|
|
out
|
2017-12-30 21:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 22:47:56 +00:00
|
|
|
/// Internel representation of the content for a `TextView`.
|
2018-01-13 18:36:56 +00:00
|
|
|
///
|
2018-01-22 22:47:56 +00:00
|
|
|
/// This is mostly just a `StyledString`.
|
2018-01-13 18:36:56 +00:00
|
|
|
///
|
|
|
|
/// Can be shared (through a `Arc<Mutex>`).
|
2017-12-30 21:55:04 +00:00
|
|
|
struct TextContentInner {
|
2018-01-10 22:58:29 +00:00
|
|
|
// content: String,
|
|
|
|
content: StyledString,
|
|
|
|
|
|
|
|
// We keep the cache here so it can be busted when we change the content.
|
2017-12-30 21:55:04 +00:00
|
|
|
size_cache: Option<XY<SizeCache>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TextContentInner {
|
2018-01-13 18:36:56 +00:00
|
|
|
/// From a shareable content (Arc + Mutex), return a
|
2017-12-30 21:55:04 +00:00
|
|
|
fn get_content(content: &Arc<Mutex<TextContentInner>>) -> TextContentRef {
|
|
|
|
let arc_ref: ArcRef<Mutex<TextContentInner>> =
|
|
|
|
ArcRef::new(Arc::clone(content));
|
|
|
|
|
|
|
|
TextContentRef {
|
|
|
|
handle: OwningHandle::new_with_fn(arc_ref, |mutex| unsafe {
|
|
|
|
(*mutex).lock().unwrap()
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
2018-01-05 13:17:47 +00:00
|
|
|
|
|
|
|
fn is_cache_valid(&self, size: Vec2) -> bool {
|
|
|
|
match self.size_cache {
|
|
|
|
None => false,
|
|
|
|
Some(ref last) => last.x.accept(size.x) && last.y.accept(size.y),
|
|
|
|
}
|
|
|
|
}
|
2017-12-30 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 18:49:13 +00:00
|
|
|
/// A simple view showing a fixed text.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2017-12-31 18:49:13 +00:00
|
|
|
/// # use cursive::Cursive;
|
|
|
|
/// # use cursive::views::TextView;
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2017-12-31 18:49:13 +00:00
|
|
|
///
|
|
|
|
/// siv.add_layer(TextView::new("Hello world!"));
|
|
|
|
/// ```
|
2015-05-15 00:41:17 +00:00
|
|
|
pub struct TextView {
|
2017-12-30 21:55:04 +00:00
|
|
|
// content: String,
|
|
|
|
content: Arc<Mutex<TextContentInner>>,
|
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,
|
2018-01-05 13:14:24 +00:00
|
|
|
effect: Effect,
|
2015-06-02 00:48:29 +00:00
|
|
|
|
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,
|
2017-01-21 19:44:40 +00:00
|
|
|
scroll_strategy: ScrollStrategy,
|
2017-10-14 00:53:39 +00:00
|
|
|
last_size: Vec2,
|
2016-07-10 01:23:58 +00:00
|
|
|
width: Option<usize>,
|
2015-05-25 08:10:43 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
impl TextView {
|
|
|
|
/// Creates a new TextView with the given content.
|
2018-01-10 22:58:29 +00:00
|
|
|
pub fn new<S>(content: S) -> Self
|
|
|
|
where
|
2018-01-13 18:36:56 +00:00
|
|
|
S: Into<StyledString>,
|
2018-01-10 22:58:29 +00:00
|
|
|
{
|
2018-01-13 18:36:56 +00:00
|
|
|
Self::new_with_content(TextContent::new(content))
|
2017-12-30 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new TextView using the given `Arc<Mutex<String>>`.
|
2017-12-31 18:49:13 +00:00
|
|
|
///
|
|
|
|
/// If you kept a clone of the given content, you'll be able to update it
|
|
|
|
/// remotely.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use cursive::views::{TextView, TextContent};
|
|
|
|
/// let mut content = TextContent::new("content");
|
|
|
|
/// let view = TextView::new_with_content(content.clone());
|
|
|
|
///
|
|
|
|
/// // Later, possibly in a different thread
|
|
|
|
/// content.set_content("new content");
|
2018-01-13 18:36:56 +00:00
|
|
|
/// assert!(content.get_content().source().contains("new"));
|
2017-12-31 18:49:13 +00:00
|
|
|
/// ```
|
2017-12-30 21:55:04 +00:00
|
|
|
pub fn new_with_content(content: TextContent) -> Self {
|
2015-05-15 00:41:17 +00:00
|
|
|
TextView {
|
2017-12-31 13:14:19 +00:00
|
|
|
content: content.content,
|
2018-01-05 13:14:24 +00:00
|
|
|
effect: Effect::Simple,
|
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(),
|
2017-01-21 19:44:40 +00:00
|
|
|
scroll_strategy: ScrollStrategy::KeepRow,
|
2015-06-02 00:48:29 +00:00
|
|
|
align: Align::top_left(),
|
2017-10-14 00:53:39 +00:00
|
|
|
last_size: Vec2::zero(),
|
2016-07-10 01:23:58 +00:00
|
|
|
width: None,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:52:42 +00:00
|
|
|
/// Creates a new empty `TextView`.
|
|
|
|
pub fn empty() -> Self {
|
|
|
|
TextView::new("")
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2018-01-05 13:14:24 +00:00
|
|
|
/// Sets the effect for the entire content.
|
|
|
|
pub fn set_effect(&mut self, effect: Effect) {
|
|
|
|
self.effect = effect;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the effect for the entire content.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn effect(self, effect: Effect) -> Self {
|
|
|
|
self.with(|s| s.set_effect(effect))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:52:42 +00:00
|
|
|
/// Replace the text in this view.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
2018-01-10 22:58:29 +00:00
|
|
|
pub fn content<S>(self, content: S) -> Self
|
|
|
|
where
|
2018-01-15 22:13:30 +00:00
|
|
|
S: Into<StyledString>,
|
2018-01-10 22:58:29 +00:00
|
|
|
{
|
2018-01-13 18:36:56 +00:00
|
|
|
self.with(|s| s.set_content(content))
|
2018-01-10 22:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Replace the text in this view.
|
|
|
|
pub fn set_content<S>(&mut self, content: S)
|
|
|
|
where
|
2018-01-13 18:36:56 +00:00
|
|
|
S: Into<StyledString>,
|
2018-01-10 22:58:29 +00:00
|
|
|
{
|
2018-01-13 18:36:56 +00:00
|
|
|
self.content.lock().unwrap().content = content.into();
|
2016-07-10 01:26:52 +00:00
|
|
|
self.invalidate();
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:55:04 +00:00
|
|
|
/// Append `content` to the end of a `TextView`.
|
2018-01-13 18:36:56 +00:00
|
|
|
pub fn append<S>(&mut self, content: S)
|
2018-01-10 22:58:29 +00:00
|
|
|
where
|
2018-01-13 18:36:56 +00:00
|
|
|
S: Into<StyledString>,
|
2018-01-10 22:58:29 +00:00
|
|
|
{
|
2018-05-18 00:37:39 +00:00
|
|
|
self.content.lock().unwrap().content.append(content.into());
|
2017-01-24 04:22:43 +00:00
|
|
|
self.invalidate();
|
|
|
|
}
|
|
|
|
|
2015-05-23 23:46:38 +00:00
|
|
|
/// Returns the current text in this view.
|
2017-12-30 21:55:04 +00:00
|
|
|
pub fn get_content(&self) -> TextContentRef {
|
|
|
|
TextContentInner::get_content(&self.content)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a shared reference to the content, allowing content mutation.
|
|
|
|
pub fn get_shared_content(&mut self) -> TextContent {
|
|
|
|
// We take &mut here without really needing it,
|
|
|
|
// because it sort of "makes sense".
|
|
|
|
|
|
|
|
TextContent {
|
|
|
|
content: Arc::clone(&self.content),
|
|
|
|
}
|
2015-05-23 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 19:44:40 +00:00
|
|
|
/// Defines the way scrolling is adjusted on content or size change.
|
|
|
|
///
|
|
|
|
/// The scroll strategy defines how the scrolling position is adjusted
|
|
|
|
/// when the size of the view or the content change.
|
|
|
|
///
|
|
|
|
/// It is reset to `ScrollStrategy::KeepRow` whenever the user scrolls
|
|
|
|
/// manually.
|
|
|
|
pub fn set_scroll_strategy(&mut self, strategy: ScrollStrategy) {
|
|
|
|
self.scroll_strategy = strategy;
|
2017-01-21 19:46:56 +00:00
|
|
|
self.adjust_scroll();
|
2017-01-21 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Defines the way scrolling is adjusted on content or size change.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn scroll_strategy(self, strategy: ScrollStrategy) -> Self {
|
|
|
|
self.with(|s| s.set_scroll_strategy(strategy))
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:02:35 +00:00
|
|
|
/// Scroll up by `n` lines.
|
|
|
|
pub fn scroll_up(&mut self, n: usize) {
|
|
|
|
self.scrollbase.scroll_up(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll down by `n` lines.
|
|
|
|
pub fn scroll_down(&mut self, n: usize) {
|
|
|
|
self.scrollbase.scroll_down(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll to the bottom of the content.
|
|
|
|
pub fn scroll_bottom(&mut self) {
|
|
|
|
self.scrollbase.scroll_bottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Scroll to the top of the view.
|
|
|
|
pub fn scroll_top(&mut self) {
|
|
|
|
self.scrollbase.scroll_top();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Apply the scrolling strategy to the current scroll position.
|
|
|
|
///
|
|
|
|
/// Called when computing rows and when applying a new strategy.
|
2017-01-21 19:44:40 +00:00
|
|
|
fn adjust_scroll(&mut self) {
|
|
|
|
match self.scroll_strategy {
|
|
|
|
ScrollStrategy::StickToTop => self.scrollbase.scroll_top(),
|
|
|
|
ScrollStrategy::StickToBottom => self.scrollbase.scroll_bottom(),
|
|
|
|
ScrollStrategy::KeepRow => (),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-14 00:53:39 +00:00
|
|
|
// This must be non-destructive, as it may be called
|
|
|
|
// multiple times during layout.
|
2016-07-10 01:23:58 +00:00
|
|
|
fn compute_rows(&mut self, size: Vec2) {
|
2017-12-30 21:55:04 +00:00
|
|
|
let mut content = self.content.lock().unwrap();
|
2018-01-05 13:17:47 +00:00
|
|
|
if content.is_cache_valid(size) {
|
2017-01-21 23:06:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Completely bust the cache
|
|
|
|
// Just in case we fail, we don't want to leave a bad cache.
|
2017-12-30 21:55:04 +00:00
|
|
|
content.size_cache = None;
|
2017-01-21 23:06:31 +00:00
|
|
|
|
|
|
|
if size.x == 0 {
|
|
|
|
// Nothing we can do at this point.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First attempt: naively hope that we won't need a scrollbar_width
|
|
|
|
// (This means we try to use the entire available width for text).
|
2018-01-13 18:36:56 +00:00
|
|
|
self.rows = LinesIterator::new(&content.content, size.x).collect();
|
2017-01-21 23:06:31 +00:00
|
|
|
|
|
|
|
// Width taken by the scrollbar. Without a scrollbar, it's 0.
|
|
|
|
let mut scrollbar_width = 0;
|
|
|
|
|
|
|
|
if self.scrollable && self.rows.len() > size.y {
|
|
|
|
// We take 1 column for the bar itself + 1 spacing column
|
|
|
|
scrollbar_width = 2;
|
2016-07-17 05:05:28 +00:00
|
|
|
|
2017-01-21 23:06:31 +00:00
|
|
|
// If we're too high, include a scrollbar_width
|
2017-08-14 23:32:01 +00:00
|
|
|
let available = match size.x.checked_sub(scrollbar_width) {
|
|
|
|
Some(s) => s,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
|
2017-12-30 21:55:04 +00:00
|
|
|
self.rows =
|
2018-01-13 18:36:56 +00:00
|
|
|
LinesIterator::new(&content.content, available).collect();
|
2017-01-21 23:06:31 +00:00
|
|
|
|
2017-12-30 21:55:04 +00:00
|
|
|
if self.rows.is_empty() && !content.content.is_empty() {
|
2017-01-21 23:06:31 +00:00
|
|
|
// We have some content, we we didn't find any row for it?
|
|
|
|
// This probably means we couldn't even make a single row
|
2017-10-15 04:18:50 +00:00
|
|
|
// (for instance we only have 1 column and we have a wide
|
|
|
|
// character).
|
2017-01-21 23:06:31 +00:00
|
|
|
return;
|
2016-07-10 01:23:58 +00:00
|
|
|
}
|
2017-01-21 23:06:31 +00:00
|
|
|
}
|
2016-07-10 01:23:58 +00:00
|
|
|
|
2017-01-21 23:06:31 +00:00
|
|
|
// Desired width, including the scrollbar_width.
|
2018-06-11 06:29:10 +00:00
|
|
|
self.width = self
|
|
|
|
.rows
|
2017-01-21 23:06:31 +00:00
|
|
|
.iter()
|
|
|
|
.map(|row| row.width)
|
|
|
|
.max()
|
|
|
|
.map(|w| w + scrollbar_width);
|
2016-07-10 01:23:58 +00:00
|
|
|
|
2017-01-21 23:06:31 +00:00
|
|
|
// The entire "virtual" size (includes all rows)
|
|
|
|
let mut my_size = Vec2::new(self.width.unwrap_or(0), self.rows.len());
|
2016-07-17 00:08:17 +00:00
|
|
|
|
2017-01-21 23:06:31 +00:00
|
|
|
// If we're scrolling, cap the the available size.
|
|
|
|
if self.scrollable && my_size.y > size.y {
|
|
|
|
my_size.y = size.y;
|
|
|
|
}
|
2016-07-17 00:08:17 +00:00
|
|
|
|
2017-01-21 23:06:31 +00:00
|
|
|
// Build a fresh cache.
|
2017-12-30 21:55:04 +00:00
|
|
|
content.size_cache = 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) {
|
2017-12-30 21:55:04 +00:00
|
|
|
let mut content = self.content.lock().unwrap();
|
|
|
|
content.size_cache = None;
|
2016-07-10 01:26:52 +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();
|
2017-10-12 01:06:58 +00:00
|
|
|
// If the content is smaller than the view, align it somewhere.
|
2015-06-02 21:23:51 +00:00
|
|
|
let offset = self.align.v.get_offset(h, printer.size.y);
|
2018-04-03 23:38:54 +00:00
|
|
|
let printer = &printer.offset((0, offset));
|
2015-06-02 00:48:29 +00:00
|
|
|
|
2017-12-30 21:55:04 +00:00
|
|
|
let content = self.content.lock().unwrap();
|
|
|
|
|
2018-01-05 13:14:24 +00:00
|
|
|
printer.with_effect(self.effect, |printer| {
|
|
|
|
self.scrollbase.draw(printer, |printer, i| {
|
|
|
|
let row = &self.rows[i];
|
2018-01-10 22:58:29 +00:00
|
|
|
let l = row.width;
|
|
|
|
let mut x = self.align.h.get_offset(l, printer.size.x);
|
|
|
|
|
2018-02-17 00:32:54 +00:00
|
|
|
for span in row.resolve(&content.content) {
|
2018-01-13 18:36:56 +00:00
|
|
|
printer.with_style(*span.attr, |printer| {
|
2018-01-22 22:47:56 +00:00
|
|
|
printer.print((x, 0), span.content);
|
2018-01-13 18:36:56 +00:00
|
|
|
x += span.content.width();
|
2018-01-10 22:58:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
// let text = &content.content[row.start..row.end];
|
|
|
|
// printer.print((x, 0), text);
|
2018-01-05 13:14:24 +00:00
|
|
|
});
|
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 {
|
2018-01-08 18:02:35 +00:00
|
|
|
if !self.scrollable || !self.scrollbase.scrollable() {
|
2015-05-26 05:35:50 +00:00
|
|
|
return EventResult::Ignored;
|
|
|
|
}
|
|
|
|
|
2017-10-15 04:18:50 +00:00
|
|
|
// We have a scrollbar, otherwise the event would just be 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)
|
|
|
|
}
|
2017-10-12 01:06:58 +00:00
|
|
|
Event::Key(Key::Down) if self.scrollbase.can_scroll_down() => {
|
|
|
|
self.scrollbase.scroll_down(1)
|
|
|
|
}
|
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::WheelDown,
|
2017-10-13 18:22:02 +00:00
|
|
|
..
|
2017-10-12 01:06:58 +00:00
|
|
|
} if self.scrollbase.can_scroll_down() =>
|
|
|
|
{
|
2017-10-14 00:53:39 +00:00
|
|
|
self.scrollbase.scroll_down(5);
|
2017-10-12 01:06:58 +00:00
|
|
|
}
|
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::WheelUp,
|
2017-10-13 18:22:02 +00:00
|
|
|
..
|
2017-10-12 01:06:58 +00:00
|
|
|
} if self.scrollbase.can_scroll_up() =>
|
|
|
|
{
|
2017-10-14 00:53:39 +00:00
|
|
|
self.scrollbase.scroll_up(5);
|
2017-10-12 01:06:58 +00:00
|
|
|
}
|
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Press(MouseButton::Left),
|
|
|
|
position,
|
|
|
|
offset,
|
|
|
|
} if position
|
|
|
|
.checked_sub(offset)
|
2017-10-14 00:53:39 +00:00
|
|
|
.map(|position| {
|
2018-05-18 00:37:39 +00:00
|
|
|
self.scrollbase.start_drag(position, self.last_size.x)
|
2017-10-12 01:06:58 +00:00
|
|
|
})
|
|
|
|
.unwrap_or(false) =>
|
|
|
|
{
|
2017-10-12 21:32:48 +00:00
|
|
|
// Only consume the event if the mouse hits the scrollbar.
|
|
|
|
// Start scroll drag at the given position.
|
2017-10-12 01:06:58 +00:00
|
|
|
}
|
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Hold(MouseButton::Left),
|
|
|
|
position,
|
|
|
|
offset,
|
|
|
|
} => {
|
2017-10-12 21:32:48 +00:00
|
|
|
// If the mouse is dragged, we always consume the event.
|
2017-10-15 04:34:37 +00:00
|
|
|
let position = position.saturating_sub(offset);
|
|
|
|
self.scrollbase.drag(position);
|
2017-10-12 01:06:58 +00:00
|
|
|
}
|
2017-10-12 21:32:48 +00:00
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Release(MouseButton::Left),
|
2017-10-13 18:22:02 +00:00
|
|
|
..
|
2017-10-12 21:32:48 +00:00
|
|
|
} => {
|
|
|
|
self.scrollbase.release_grab();
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
|
2017-01-21 19:44:40 +00:00
|
|
|
// We just scrolled manually, so reset the scroll strategy.
|
|
|
|
self.scroll_strategy = ScrollStrategy::KeepRow;
|
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 {
|
2017-12-30 21:55:04 +00:00
|
|
|
let content = self.content.lock().unwrap();
|
|
|
|
content.size_cache.is_none()
|
2016-07-11 00:41:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn required_size(&mut self, size: Vec2) -> Vec2 {
|
2016-07-10 01:23:58 +00:00
|
|
|
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.
|
2017-10-14 00:53:39 +00:00
|
|
|
self.last_size = size;
|
2016-07-10 01:23:58 +00:00
|
|
|
self.compute_rows(size);
|
2018-01-08 18:02:35 +00:00
|
|
|
// Adjust scrolling, in case we're sticking to the bottom or something
|
|
|
|
let available_height = if self.scrollable {
|
|
|
|
size.y
|
|
|
|
} else {
|
|
|
|
self.rows.len()
|
|
|
|
};
|
|
|
|
|
2018-01-09 14:17:49 +00:00
|
|
|
self.scrollbase
|
|
|
|
.set_heights(available_height, self.rows.len());
|
2017-10-14 00:53:39 +00:00
|
|
|
self.adjust_scroll();
|
2015-05-25 08:10:43 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|