2015-05-26 22:46:05 +00:00
|
|
|
use ncurses;
|
|
|
|
|
|
|
|
use color;
|
2015-05-26 23:11:22 +00:00
|
|
|
use vec::Vec2;
|
|
|
|
use view::{View,SizeRequest};
|
2015-05-28 01:04:33 +00:00
|
|
|
use event::*;
|
2015-05-26 22:46:05 +00:00
|
|
|
use printer::Printer;
|
|
|
|
|
|
|
|
/// Displays an editable text.
|
|
|
|
pub struct EditView {
|
|
|
|
content: String,
|
|
|
|
cursor: usize,
|
2015-05-26 23:11:22 +00:00
|
|
|
min_length: usize,
|
2015-05-26 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EditView {
|
2015-05-26 23:11:22 +00:00
|
|
|
/// Creates a new, empty edit view.
|
2015-05-26 22:46:05 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
EditView {
|
|
|
|
content: String::new(),
|
|
|
|
cursor: 0,
|
2015-05-26 23:11:22 +00:00
|
|
|
min_length: 1,
|
2015-05-26 22:46:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:11:22 +00:00
|
|
|
/// Replace the entire content of the view with the given one.
|
2015-05-26 22:46:05 +00:00
|
|
|
pub fn set_content<'a>(&mut self, content: &'a str) {
|
|
|
|
self.content = content.to_string();
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:11:22 +00:00
|
|
|
/// Get the current text.
|
2015-05-26 22:46:05 +00:00
|
|
|
pub fn get_content(&self) -> &str {
|
|
|
|
&self.content
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:11:22 +00:00
|
|
|
/// Sets the current content to the given value. Convenient chainable method.
|
2015-05-26 22:46:05 +00:00
|
|
|
pub fn content<'a>(mut self, content: &'a str) -> Self {
|
|
|
|
self.set_content(content);
|
|
|
|
self
|
|
|
|
}
|
2015-05-26 23:11:22 +00:00
|
|
|
|
|
|
|
/// Sets the minimum length for this view.
|
|
|
|
/// (This applies to the layout, not the content.)
|
|
|
|
pub fn min_length(mut self, min_length: usize) -> Self {
|
|
|
|
self.min_length = min_length;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2015-05-26 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for EditView {
|
|
|
|
fn draw(&mut self, printer: &Printer, focused: bool) {
|
2015-05-27 04:45:00 +00:00
|
|
|
// let style = if focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE };
|
|
|
|
printer.with_color(color::SECONDARY, |printer| {
|
|
|
|
printer.with_style(ncurses::A_REVERSE(), |printer| {
|
|
|
|
printer.print((0,0), &self.content);
|
|
|
|
printer.print_hline((self.content.len(),0), printer.size.x-self.content.len(), '_' as u64);
|
|
|
|
});
|
2015-05-27 07:01:23 +00:00
|
|
|
|
|
|
|
// Now print cursor
|
|
|
|
if focused {
|
|
|
|
let c = if self.cursor == self.content.len() {
|
|
|
|
'_'
|
|
|
|
} else {
|
|
|
|
// Get the char from the string... Is it so hard?
|
2015-05-27 07:15:19 +00:00
|
|
|
self.content.chars().nth(self.cursor).unwrap()
|
2015-05-27 07:01:23 +00:00
|
|
|
};
|
|
|
|
printer.print_hline((self.cursor, 0), 1, c as u64);
|
|
|
|
}
|
2015-05-26 22:46:05 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-27 00:07:57 +00:00
|
|
|
fn get_min_size(&self, _: SizeRequest) -> Vec2 {
|
2015-05-26 23:11:22 +00:00
|
|
|
Vec2::new(self.min_length, 1)
|
|
|
|
}
|
|
|
|
|
2015-05-26 22:46:05 +00:00
|
|
|
fn take_focus(&mut self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
|
|
|
|
match event {
|
|
|
|
Event::CharEvent(ch) => {
|
|
|
|
self.content.insert(self.cursor, ch);
|
|
|
|
self.cursor += 1;
|
|
|
|
return EventResult::Consumed(None);
|
|
|
|
},
|
|
|
|
Event::KeyEvent(key) => match key {
|
|
|
|
Key::Home => self.cursor = 0,
|
|
|
|
Key::End => self.cursor = self.content.len(),
|
2015-05-28 21:44:10 +00:00
|
|
|
Key::Left if self.cursor > 0 => self.cursor -= 1,
|
|
|
|
Key::Right if self.cursor < self.content.len() => self.cursor += 1,
|
2015-05-28 01:04:33 +00:00
|
|
|
Key::Backspace if self.cursor > 0 => { self.cursor -= 1; self.content.remove(self.cursor); },
|
|
|
|
Key::Del if self.cursor < self.content.len() => { self.content.remove(self.cursor); },
|
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
|
|
|
|
},
|
2015-05-26 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
}
|