Add mouse support to TextArea

This commit is contained in:
Alexandre Bury 2017-10-12 20:41:34 -07:00
parent def6b3af27
commit 67a6640142
2 changed files with 27 additions and 2 deletions

View File

@ -35,6 +35,11 @@ impl SizeCache {
}
}
/// Returns the value in the cache.
pub fn value(self) -> usize {
self.value
}
/// Creates a new bi-dimensional cache.
///
/// It will stay valid for the same request, and compatible ones.

View File

@ -2,12 +2,12 @@
use {Printer, With, XY};
use direction::Direction;
use event::{Event, EventResult, Key};
use event::{Event, EventResult, Key, MouseEvent};
use odds::vec::VecExt;
use theme::{ColorStyle, Effect};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use utils::{prefix, LinesIterator, Row};
use utils::{prefix, simple_prefix, LinesIterator, Row};
use vec::Vec2;
use view::{ScrollBase, SizeCache, View};
@ -460,6 +460,26 @@ impl View for TextArea {
Event::Key(Key::Right) if self.cursor < self.content.len() => {
self.move_right()
}
Event::Mouse {
event: MouseEvent::Press(_),
position,
offset,
} if position.fits_in_rect(
offset,
self.last_size
.map(|s| s.map(SizeCache::value))
.unwrap_or_else(Vec2::zero),
) =>
{
position.checked_sub(offset).map(|position| {
let y = position.y + self.scrollbase.start_line;
let x = position.x;
let row = &self.rows[y];
let content = &self.content[row.start..row.end];
self.cursor = row.start + simple_prefix(content, x).length;
});
}
_ => return EventResult::Ignored,
}