use Printer;
use With;
use XY;
use align::*;
use direction::Direction;
use event::*;
use owning_ref::{ArcRef, OwningHandle};
use std::ops::Deref;
use std::sync::{Mutex, MutexGuard};
use std::sync::Arc;
use theme::Effect;
use unicode_width::UnicodeWidthStr;
use utils::lines::spans::{LinesIterator, Row};
use utils::markup::StyledString;
use vec::Vec2;
use view::{ScrollBase, ScrollStrategy, SizeCache, View};
#[derive(Clone)]
pub struct TextContent {
content: Arc<Mutex<TextContentInner>>,
}
impl TextContent {
pub fn new<S>(content: S) -> Self
where
S: Into<StyledString>,
{
let content = content.into();
TextContent {
content: Arc::new(Mutex::new(TextContentInner {
content,
size_cache: None,
})),
}
}
}
pub struct TextContentRef {
handle: OwningHandle<
ArcRef<Mutex<TextContentInner>>,
MutexGuard<'static, TextContentInner>,
>,
}
impl Deref for TextContentRef {
type Target = StyledString;
fn deref(&self) -> &StyledString {
&self.handle.content
}
}
impl TextContent {
pub fn set_content<S>(&mut self, content: S)
where
S: Into<StyledString>,
{
self.with_content(|c| *c = content.into());
}
pub fn append<S>(&mut self, content: S)
where
S: Into<StyledString>,
{
self.with_content(|c| c.append(content))
}
pub fn get_content(&self) -> TextContentRef {
TextContentInner::get_content(&self.content)
}
fn with_content<F, O>(&mut self, f: F) -> O
where
F: FnOnce(&mut StyledString) -> O,
{
let mut lock = self.content.lock().unwrap();
let out = f(&mut lock.content);
lock.size_cache = None;
out
}
}
struct TextContentInner {
content: StyledString,
size_cache: Option<XY<SizeCache>>,
}
impl TextContentInner {
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()
}),
}
}
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),
}
}
}
pub struct TextView {
content: Arc<Mutex<TextContentInner>>,
rows: Vec<Row>,
align: Align,
effect: Effect,
scrollable: bool,
scrollbase: ScrollBase,
scroll_strategy: ScrollStrategy,
last_size: Vec2,
width: Option<usize>,
}
impl TextView {
pub fn new<S>(content: S) -> Self
where
S: Into<StyledString>,
{
Self::new_with_content(TextContent::new(content))
}
pub fn new_with_content(content: TextContent) -> Self {
TextView {
content: content.content,
effect: Effect::Simple,
rows: Vec::new(),
scrollable: true,
scrollbase: ScrollBase::new(),
scroll_strategy: ScrollStrategy::KeepRow,
align: Align::top_left(),
last_size: Vec2::zero(),
width: None,
}
}
pub fn empty() -> Self {
TextView::new("")
}
pub fn set_scrollable(&mut self, scrollable: bool) {
self.scrollable = scrollable;
}
pub fn scrollable(self, scrollable: bool) -> Self {
self.with(|s| s.set_scrollable(scrollable))
}
pub fn set_effect(&mut self, effect: Effect) {
self.effect = effect;
}
pub fn effect(self, effect: Effect) -> Self {
self.with(|s| s.set_effect(effect))
}
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
}
pub fn center(mut self) -> Self {
self.align = Align::center();
self
}
pub fn content<S>(self, content: S) -> Self
where
S: Into<StyledString>,
{
self.with(|s| s.set_content(content))
}
pub fn set_content<S>(&mut self, content: S)
where
S: Into<StyledString>,
{
self.content.lock().unwrap().content = content.into();
self.invalidate();
}
pub fn append<S>(&mut self, content: S)
where
S: Into<StyledString>,
{
self.content.lock().unwrap().content.append(content.into());
self.invalidate();
}
pub fn get_content(&self) -> TextContentRef {
TextContentInner::get_content(&self.content)
}
pub fn get_shared_content(&mut self) -> TextContent {
TextContent {
content: Arc::clone(&self.content),
}
}
pub fn set_scroll_strategy(&mut self, strategy: ScrollStrategy) {
self.scroll_strategy = strategy;
self.adjust_scroll();
}
pub fn scroll_strategy(self, strategy: ScrollStrategy) -> Self {
self.with(|s| s.set_scroll_strategy(strategy))
}
pub fn scroll_up(&mut self, n: usize) {
self.scrollbase.scroll_up(n);
}
pub fn scroll_down(&mut self, n: usize) {
self.scrollbase.scroll_down(n);
}
pub fn scroll_bottom(&mut self) {
self.scrollbase.scroll_bottom();
}
pub fn scroll_top(&mut self) {
self.scrollbase.scroll_top();
}
fn adjust_scroll(&mut self) {
match self.scroll_strategy {
ScrollStrategy::StickToTop => self.scrollbase.scroll_top(),
ScrollStrategy::StickToBottom => self.scrollbase.scroll_bottom(),
ScrollStrategy::KeepRow => (),
};
}
fn compute_rows(&mut self, size: Vec2) {
let mut content = self.content.lock().unwrap();
if content.is_cache_valid(size) {
return;
}
content.size_cache = None;
if size.x == 0 {
return;
}
self.rows = LinesIterator::new(&content.content, size.x).collect();
let mut scrollbar_width = 0;
if self.scrollable && self.rows.len() > size.y {
scrollbar_width = 2;
let available = match size.x.checked_sub(scrollbar_width) {
Some(s) => s,
None => return,
};
self.rows =
LinesIterator::new(&content.content, available).collect();
if self.rows.is_empty() && !content.content.is_empty() {
return;
}
}
self.width = self.rows
.iter()
.map(|row| row.width)
.max()
.map(|w| w + scrollbar_width);
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;
}
content.size_cache = Some(SizeCache::build(my_size, size));
}
fn invalidate(&mut self) {
let mut content = self.content.lock().unwrap();
content.size_cache = None;
}
}
impl View for TextView {
fn draw(&self, printer: &Printer) {
let h = self.rows.len();
let offset = self.align.v.get_offset(h, printer.size.y);
let printer = &printer.offset((0, offset), true);
let content = self.content.lock().unwrap();
printer.with_effect(self.effect, |printer| {
self.scrollbase.draw(printer, |printer, i| {
let row = &self.rows[i];
let l = row.width;
let mut x = self.align.h.get_offset(l, printer.size.x);
for span in row.resolve(&content.content) {
printer.with_style(*span.attr, |printer| {
printer.print((x, 0), span.content);
x += span.content.width();
});
}
});
});
}
fn on_event(&mut self, event: Event) -> EventResult {
if !self.scrollable || !self.scrollbase.scrollable() {
return EventResult::Ignored;
}
match event {
Event::Key(Key::Home) => self.scrollbase.scroll_top(),
Event::Key(Key::End) => self.scrollbase.scroll_bottom(),
Event::Key(Key::Up) if self.scrollbase.can_scroll_up() => {
self.scrollbase.scroll_up(1)
}
Event::Key(Key::Down) if self.scrollbase.can_scroll_down() => {
self.scrollbase.scroll_down(1)
}
Event::Mouse {
event: MouseEvent::WheelDown,
..
} if self.scrollbase.can_scroll_down() =>
{
self.scrollbase.scroll_down(5);
}
Event::Mouse {
event: MouseEvent::WheelUp,
..
} if self.scrollbase.can_scroll_up() =>
{
self.scrollbase.scroll_up(5);
}
Event::Mouse {
event: MouseEvent::Press(MouseButton::Left),
position,
offset,
} if position
.checked_sub(offset)
.map(|position| {
self.scrollbase.start_drag(position, self.last_size.x)
})
.unwrap_or(false) =>
{
}
Event::Mouse {
event: MouseEvent::Hold(MouseButton::Left),
position,
offset,
} => {
let position = position.saturating_sub(offset);
self.scrollbase.drag(position);
}
Event::Mouse {
event: MouseEvent::Release(MouseButton::Left),
..
} => {
self.scrollbase.release_grab();
}
Event::Key(Key::PageDown) => self.scrollbase.scroll_down(10),
Event::Key(Key::PageUp) => self.scrollbase.scroll_up(10),
_ => return EventResult::Ignored,
}
self.scroll_strategy = ScrollStrategy::KeepRow;
EventResult::Consumed(None)
}
fn needs_relayout(&self) -> bool {
let content = self.content.lock().unwrap();
content.size_cache.is_none()
}
fn required_size(&mut self, size: Vec2) -> Vec2 {
self.compute_rows(size);
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
}
fn take_focus(&mut self, _: Direction) -> bool {
self.scrollbase.scrollable()
}
fn layout(&mut self, size: Vec2) {
self.last_size = size;
self.compute_rows(size);
let available_height = if self.scrollable {
size.y
} else {
self.rows.len()
};
self.scrollbase
.set_heights(available_height, self.rows.len());
self.adjust_scroll();
}
}