use iced::widget::text_editor::{self, Content};
use crate::widget::select_only;
#[derive(Debug, Default)]
pub struct Selectables {
items: Vec<(String, Content)>,
}
impl Selectables {
pub fn new() -> Self {
Self::default()
}
pub fn bind(&mut self, id: impl Into<String>, text: impl AsRef<str>) {
let id = id.into();
let text = text.as_ref();
if let Some((_, content)) = self.items.iter_mut().find(|(k, _)| *k == id) {
if content.text() == text {
return;
}
*content = Content::with_text(text);
return;
}
self.items.push((id, Content::with_text(text)));
}
pub fn ensure(&mut self, id: impl Into<String>, text: impl AsRef<str>) -> &Content {
let id = id.into();
self.bind(id.clone(), text);
self.get(&id).expect("ensure just bound id")
}
pub fn retain(&mut self, mut keep: impl FnMut(&str) -> bool) {
self.items.retain(|(k, _)| keep(k.as_str()));
}
pub fn unbind(&mut self, id: &str) -> bool {
if let Some(i) = self.items.iter().position(|(k, _)| k == id) {
self.items.remove(i);
true
} else {
false
}
}
pub fn contains(&self, id: &str) -> bool {
self.items.iter().any(|(k, _)| k == id)
}
pub fn get(&self, id: &str) -> Option<&Content> {
self.items.iter().find(|(k, _)| k == id).map(|(_, c)| c)
}
pub fn get_mut(&mut self, id: &str) -> Option<&mut Content> {
self.items.iter_mut().find(|(k, _)| k == id).map(|(_, c)| c)
}
pub fn perform(&mut self, id: &str, action: text_editor::Action) {
if let Some(content) = self.get_mut(id) {
content.perform(select_only(action));
}
}
pub fn copy(&self, id: &str) -> String {
self.get(id)
.map(|content| content.selection().unwrap_or_else(|| content.text()))
.unwrap_or_default()
}
pub fn first_selection(&self) -> Option<String> {
self.items.iter().find_map(|(_, c)| c.selection())
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced::widget::text_editor::{Action, Edit};
#[test]
fn bind_get_and_copy_full_text() {
let mut fields = Selectables::new();
assert!(fields.is_empty());
fields.bind("path", "a/b");
assert_eq!(fields.len(), 1);
assert!(fields.contains("path"));
assert_eq!(fields.get("path").map(|c| c.text()), Some("a/b".into()));
assert_eq!(fields.copy("path"), "a/b");
assert_eq!(fields.first_selection(), None);
}
#[test]
fn bind_same_text_keeps_selection() {
let mut fields = Selectables::new();
fields.bind("body", "hello");
fields.perform("body", Action::SelectAll);
assert_eq!(fields.first_selection().as_deref(), Some("hello"));
fields.bind("body", "hello");
assert_eq!(fields.first_selection().as_deref(), Some("hello"));
assert_eq!(fields.copy("body"), "hello");
}
#[test]
fn bind_new_text_replaces_the_buffer() {
let mut fields = Selectables::new();
fields.bind("body", "hello");
fields.perform("body", Action::SelectAll);
fields.bind("body", "other");
assert_eq!(fields.get("body").map(|c| c.text()), Some("other".into()));
assert_eq!(fields.first_selection(), None);
assert_eq!(fields.copy("body"), "other");
}
#[test]
fn perform_drops_typing() {
let mut fields = Selectables::new();
fields.bind("body", "keep");
fields.perform("body", Action::Edit(Edit::Insert('x')));
assert_eq!(fields.get("body").map(|c| c.text()), Some("keep".into()));
}
#[test]
fn a_left_click_clears_the_range() {
let mut fields = Selectables::new();
fields.bind("body", "alpha beta");
fields.perform("body", Action::SelectAll);
assert_eq!(fields.first_selection().as_deref(), Some("alpha beta"));
fields.perform("body", Action::Click(iced::Point::new(4.0, 0.0)));
assert_eq!(fields.first_selection(), None);
assert_eq!(fields.copy("body"), "alpha beta");
}
#[test]
fn first_selection_walks_bind_order() {
let mut fields = Selectables::new();
fields.bind("path", "p");
fields.bind("body", "hello");
fields.perform("body", Action::SelectAll);
assert_eq!(fields.first_selection().as_deref(), Some("hello"));
fields.perform("path", Action::SelectAll);
assert_eq!(fields.first_selection().as_deref(), Some("p"));
let _ = fields.get_mut("body");
}
#[test]
fn unbound_get_is_none_and_perform_is_noop() {
let mut fields = Selectables::new();
assert!(!fields.contains("missing"));
assert!(fields.get("missing").is_none());
assert!(fields.get_mut("missing").is_none());
fields.perform("missing", Action::SelectAll);
assert_eq!(fields.copy("missing"), "");
assert!(fields.is_empty());
}
#[test]
fn ensure_binds_lazily_and_retain_drops_closed() {
let mut fields = Selectables::new();
assert_eq!(fields.ensure("card.3", "body three").text(), "body three");
assert!(fields.contains("card.3"));
fields.ensure("card.1", "one");
fields.ensure("card.9", "nine");
fields.retain(|id| id == "card.3" || id == "card.1");
assert!(fields.contains("card.3"));
assert!(fields.contains("card.1"));
assert!(!fields.contains("card.9"));
assert!(fields.unbind("card.1"));
assert!(!fields.unbind("card.1"));
assert_eq!(fields.len(), 1);
}
}