use std::collections::HashMap;
pub trait PopupState {
fn is_open(&self, popup_id: &str) -> bool;
fn position(&self, popup_id: &str) -> (f64, f64);
fn selected_item(&self, popup_id: &str) -> Option<usize>;
fn selected_color(&self, popup_id: &str) -> Option<String>;
fn is_custom_mode(&self, popup_id: &str) -> bool;
fn set_open(&mut self, popup_id: &str, open: bool);
fn set_position(&mut self, popup_id: &str, pos: (f64, f64));
fn set_selected_item(&mut self, popup_id: &str, index: Option<usize>);
fn set_selected_color(&mut self, popup_id: &str, color: Option<String>);
fn set_custom_mode(&mut self, popup_id: &str, custom: bool);
}
#[derive(Clone, Debug, Default)]
pub struct SimplePopupState {
open: HashMap<String, bool>,
positions: HashMap<String, (f64, f64)>,
selected_items: HashMap<String, Option<usize>>,
selected_colors: HashMap<String, Option<String>>,
custom_modes: HashMap<String, bool>,
}
impl SimplePopupState {
pub fn new() -> Self {
Self {
open: HashMap::new(),
positions: HashMap::new(),
selected_items: HashMap::new(),
selected_colors: HashMap::new(),
custom_modes: HashMap::new(),
}
}
pub fn clear(&mut self, popup_id: &str) {
self.open.remove(popup_id);
self.positions.remove(popup_id);
self.selected_items.remove(popup_id);
self.selected_colors.remove(popup_id);
self.custom_modes.remove(popup_id);
}
pub fn clear_all(&mut self) {
self.open.clear();
self.positions.clear();
self.selected_items.clear();
self.selected_colors.clear();
self.custom_modes.clear();
}
}
impl PopupState for SimplePopupState {
fn is_open(&self, popup_id: &str) -> bool {
self.open.get(popup_id).copied().unwrap_or(false)
}
fn position(&self, popup_id: &str) -> (f64, f64) {
self.positions.get(popup_id).copied().unwrap_or((0.0, 0.0))
}
fn selected_item(&self, popup_id: &str) -> Option<usize> {
self.selected_items.get(popup_id).cloned().flatten()
}
fn selected_color(&self, popup_id: &str) -> Option<String> {
self.selected_colors.get(popup_id).cloned().flatten()
}
fn is_custom_mode(&self, popup_id: &str) -> bool {
self.custom_modes.get(popup_id).copied().unwrap_or(false)
}
fn set_open(&mut self, popup_id: &str, open: bool) {
self.open.insert(popup_id.to_string(), open);
}
fn set_position(&mut self, popup_id: &str, pos: (f64, f64)) {
self.positions.insert(popup_id.to_string(), pos);
}
fn set_selected_item(&mut self, popup_id: &str, index: Option<usize>) {
self.selected_items.insert(popup_id.to_string(), index);
}
fn set_selected_color(&mut self, popup_id: &str, color: Option<String>) {
self.selected_colors.insert(popup_id.to_string(), color);
}
fn set_custom_mode(&mut self, popup_id: &str, custom: bool) {
self.custom_modes.insert(popup_id.to_string(), custom);
}
}