use crate::direction::Direction;
use crate::event::{Callback, Event, EventResult, Key, MouseEvent};
use crate::rect::Rect;
use crate::theme::{ColorStyle, Effect};
use crate::utils::lines::simple::{simple_prefix, simple_suffix};
use crate::view::View;
use crate::Vec2;
use crate::{Cursive, Printer, With};
use std::cell::RefCell;
use std::rc::Rc;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
pub type OnEdit = dyn Fn(&mut Cursive, &str, usize);
pub type OnSubmit = dyn Fn(&mut Cursive, &str);
pub struct EditView {
content: Rc<String>,
cursor: usize,
offset: usize,
max_content_width: Option<usize>,
last_length: usize,
on_edit: Option<Rc<OnEdit>>,
on_submit: Option<Rc<OnSubmit>>,
secret: bool,
filler: String,
enabled: bool,
style: ColorStyle,
}
new_default!(EditView);
impl EditView {
pub fn new() -> Self {
EditView {
content: Rc::new(String::new()),
cursor: 0,
offset: 0,
last_length: 0, on_edit: None,
on_submit: None,
max_content_width: None,
secret: false,
filler: "_".to_string(),
enabled: true,
style: ColorStyle::secondary(),
}
}
pub fn set_max_content_width(&mut self, width: Option<usize>) {
self.max_content_width = width;
}
pub fn max_content_width(self, width: usize) -> Self {
self.with(|s| s.set_max_content_width(Some(width)))
}
pub fn set_secret(&mut self, secret: bool) {
self.secret = secret;
}
pub fn secret(self) -> Self {
self.with(|s| s.set_secret(true))
}
pub fn set_filler<S: Into<String>>(&mut self, filler: S) {
self.filler = filler.into();
}
pub fn filler<S: Into<String>>(self, filler: S) -> Self {
self.with(|s| s.set_filler(filler))
}
pub fn disable(&mut self) {
self.enabled = false;
}
pub fn disabled(self) -> Self {
self.with(Self::disable)
}
pub fn enable(&mut self) {
self.enabled = true;
}
pub fn set_style(&mut self, style: ColorStyle) {
self.style = style;
}
pub fn style(self, style: ColorStyle) -> Self {
self.with(|s| s.set_style(style))
}
pub fn set_on_edit_mut<F>(&mut self, callback: F)
where
F: FnMut(&mut Cursive, &str, usize) + 'static,
{
self.set_on_edit(immut3!(callback));
}
pub fn set_on_edit<F>(&mut self, callback: F)
where
F: Fn(&mut Cursive, &str, usize) + 'static,
{
self.on_edit = Some(Rc::new(callback));
}
pub fn on_edit_mut<F>(self, callback: F) -> Self
where
F: FnMut(&mut Cursive, &str, usize) + 'static,
{
self.with(|v| v.set_on_edit_mut(callback))
}
pub fn on_edit<F>(self, callback: F) -> Self
where
F: Fn(&mut Cursive, &str, usize) + 'static,
{
self.with(|v| v.set_on_edit(callback))
}
pub fn set_on_submit_mut<F>(&mut self, callback: F)
where
F: FnMut(&mut Cursive, &str) + 'static,
{
let callback = RefCell::new(callback);
self.set_on_submit(move |s, text| {
if let Ok(mut f) = callback.try_borrow_mut() {
(&mut *f)(s, text);
}
});
}
pub fn set_on_submit<F>(&mut self, callback: F)
where
F: Fn(&mut Cursive, &str) + 'static,
{
self.on_submit = Some(Rc::new(callback));
}
pub fn on_submit_mut<F>(self, callback: F) -> Self
where
F: FnMut(&mut Cursive, &str) + 'static,
{
self.with(|v| v.set_on_submit_mut(callback))
}
pub fn on_submit<F>(self, callback: F) -> Self
where
F: Fn(&mut Cursive, &str) + 'static,
{
self.with(|v| v.set_on_submit(callback))
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
pub fn set_content<S: Into<String>>(&mut self, content: S) -> Callback {
let content = content.into();
let len = content.len();
self.content = Rc::new(content);
self.offset = 0;
self.set_cursor(len);
self.make_edit_cb().unwrap_or_else(Callback::dummy)
}
pub fn get_content(&self) -> Rc<String> {
Rc::clone(&self.content)
}
pub fn content<S: Into<String>>(mut self, content: S) -> Self {
self.set_content(content);
self
}
pub fn set_cursor(&mut self, cursor: usize) {
self.cursor = cursor;
self.keep_cursor_in_view();
}
pub fn insert(&mut self, ch: char) -> Callback {
if let Some(width) = self.max_content_width {
if ch.width().unwrap_or(0) + self.content.width() > width {
return Callback::dummy();
}
}
Rc::make_mut(&mut self.content).insert(self.cursor, ch);
self.cursor += ch.len_utf8();
self.keep_cursor_in_view();
self.make_edit_cb().unwrap_or_else(Callback::dummy)
}
pub fn remove(&mut self, len: usize) -> Callback {
let start = self.cursor;
let end = self.cursor + len;
for _ in Rc::make_mut(&mut self.content).drain(start..end) {}
self.keep_cursor_in_view();
self.make_edit_cb().unwrap_or_else(Callback::dummy)
}
fn make_edit_cb(&self) -> Option<Callback> {
self.on_edit.clone().map(|cb| {
let content = Rc::clone(&self.content);
let cursor = self.cursor;
Callback::from_fn(move |s| {
cb(s, &content, cursor);
})
})
}
fn keep_cursor_in_view(&mut self) {
if self.cursor < self.offset {
self.offset = self.cursor;
} else {
let c_len = self.content[self.cursor..]
.graphemes(true)
.map(UnicodeWidthStr::width)
.next()
.unwrap_or(1);
let available = match self.last_length.checked_sub(c_len) {
Some(s) => s,
None => return,
};
let suffix_length = simple_suffix(
&self.content[self.offset..self.cursor],
available,
)
.length;
assert!(suffix_length <= self.cursor);
self.offset = self.cursor - suffix_length;
assert!(self.cursor >= self.offset);
}
if self.content[self.offset..].width() < self.last_length {
assert!(self.last_length >= 1);
let suffix_length =
simple_suffix(&self.content, self.last_length - 1).length;
assert!(self.content.len() >= suffix_length);
self.offset = self.content.len() - suffix_length;
}
}
}
fn make_small_stars(length: usize) -> &'static str {
&"****"[..length]
}
impl View for EditView {
fn draw(&self, printer: &Printer<'_, '_>) {
assert_eq!(
printer.size.x, self.last_length,
"Was promised {}, received {}",
self.last_length, printer.size.x
);
let width = self.content.width();
printer.with_color(self.style, |printer| {
let effect = if self.enabled && printer.enabled {
Effect::Reverse
} else {
Effect::Simple
};
printer.with_effect(effect, |printer| {
if width < self.last_length {
assert!(printer.size.x >= width);
if self.secret {
printer.print_hline((0, 0), width, "*");
} else {
printer.print((0, 0), &self.content);
}
let filler_len =
(printer.size.x - width) / self.filler.width();
printer.print_hline(
(width, 0),
filler_len,
self.filler.as_str(),
);
} else {
let content = &self.content[self.offset..];
let display_bytes = content
.graphemes(true)
.scan(0, |w, g| {
*w += g.width();
if *w > self.last_length {
None
} else {
Some(g)
}
})
.map(str::len)
.sum();
let content = &content[..display_bytes];
let width = content.width();
if self.secret {
printer.print_hline((0, 0), width, "*");
} else {
printer.print((0, 0), content);
}
if width < self.last_length {
let filler_len =
(self.last_length - width) / self.filler.width();
printer.print_hline(
(width, 0),
filler_len,
self.filler.as_str(),
);
}
}
});
if printer.focused {
let c: &str = if self.cursor == self.content.len() {
&self.filler
} else {
let selected = self.content[self.cursor..]
.graphemes(true)
.next()
.unwrap_or_else(|| {
panic!(
"Found no char at cursor {} in {}",
self.cursor, &self.content
)
});
if self.secret {
make_small_stars(selected.width())
} else {
selected
}
};
let offset = self.content[self.offset..self.cursor].width();
printer.print((offset, 0), c);
}
});
}
fn layout(&mut self, size: Vec2) {
self.last_length = size.x;
}
fn take_focus(&mut self, _: Direction) -> bool {
self.enabled
}
fn on_event(&mut self, event: Event) -> EventResult {
match event {
Event::Char(ch) => {
return EventResult::Consumed(Some(self.insert(ch)));
}
Event::Key(Key::Home) => self.set_cursor(0),
Event::Key(Key::End) => {
let len = self.content.len();
self.set_cursor(len);
}
Event::Key(Key::Left) if self.cursor > 0 => {
let len = self.content[..self.cursor]
.graphemes(true)
.last()
.unwrap()
.len();
let cursor = self.cursor - len;
self.set_cursor(cursor);
}
Event::Key(Key::Right) if self.cursor < self.content.len() => {
let len = self.content[self.cursor..]
.graphemes(true)
.next()
.unwrap()
.len();
let cursor = self.cursor + len;
self.set_cursor(cursor);
}
Event::Key(Key::Backspace) if self.cursor > 0 => {
let len = self.content[..self.cursor]
.graphemes(true)
.last()
.unwrap()
.len();
self.cursor -= len;
return EventResult::Consumed(Some(self.remove(len)));
}
Event::Key(Key::Del) if self.cursor < self.content.len() => {
let len = self.content[self.cursor..]
.graphemes(true)
.next()
.unwrap()
.len();
return EventResult::Consumed(Some(self.remove(len)));
}
Event::Key(Key::Enter) if self.on_submit.is_some() => {
let cb = self.on_submit.clone().unwrap();
let content = Rc::clone(&self.content);
return EventResult::with_cb(move |s| {
cb(s, &content);
});
}
Event::Mouse {
event: MouseEvent::Press(_),
position,
offset,
} if position.fits_in_rect(offset, (self.last_length, 1)) => {
if let Some(position) = position.checked_sub(offset) {
self.cursor = self.offset
+ simple_prefix(
&self.content[self.offset..],
position.x,
)
.length;
}
}
_ => return EventResult::Ignored,
}
EventResult::Consumed(self.make_edit_cb())
}
fn important_area(&self, _: Vec2) -> Rect {
let char_width = if self.cursor >= self.content.len() {
1
} else {
self.content[self.cursor..]
.graphemes(true)
.next()
.unwrap()
.width()
};
let x = self.content[..self.cursor].width();
Rect::from_size((x, 0), (char_width, 1))
}
}