Add mouse support to EditView

This commit is contained in:
Alexandre Bury 2017-10-12 17:38:21 -07:00
parent 0150ebfc9e
commit aaf41f3ec4
2 changed files with 23 additions and 2 deletions

View File

@ -95,13 +95,20 @@ where
prefix(iter.rev(), width, delimiter) prefix(iter.rev(), width, delimiter)
} }
/// Computes the length (number of bytes) and width of a suffix that fits in the given `width`. /// Computes a suffix that fits in the given `width`.
/// ///
/// Breaks between any two graphemes. /// Breaks between any two graphemes.
pub fn simple_suffix(text: &str, width: usize) -> Prefix { pub fn simple_suffix(text: &str, width: usize) -> Prefix {
suffix(text.graphemes(true), width, "") suffix(text.graphemes(true), width, "")
} }
/// Computes the longest prefix of the given text that fits in the given width.
///
/// Breaks between any two graphemes.
pub fn simple_prefix(text: &str, width: usize) -> Prefix {
prefix(text.graphemes(true), width, "")
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use utils; use utils;

View File

@ -8,7 +8,7 @@ use std::rc::Rc;
use theme::{ColorStyle, Effect}; use theme::{ColorStyle, Effect};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
use utils::simple_suffix; use utils::{simple_prefix, simple_suffix};
use vec::Vec2; use vec::Vec2;
use view::View; use view::View;
@ -571,6 +571,20 @@ impl View for EditView {
cb(s, &content); cb(s, &content);
}); });
} }
Event::Mouse {
event: MouseEvent::Press(_),
position,
offset,
} if position.fits_in_rect(offset, (self.last_length, 1)) =>
{
position.checked_sub(offset).map(|position| {
self.cursor = self.offset
+ simple_prefix(
&self.content[self.offset..],
position.x,
).length;
});
}
_ => return EventResult::Ignored, _ => return EventResult::Ignored,
} }