use crate::popup::Popup; use std::cell::{RefCell, RefMut}; use std::collections::HashMap; use std::rc::Rc; /// Global application state shared across all bars. /// /// Data that needs to be accessed from anywhere /// that is not otherwise accessible should be placed on here. #[derive(Debug)] pub struct GlobalState { popups: HashMap, Rc>>, } impl GlobalState { pub(crate) fn new() -> Self { Self { popups: HashMap::new(), } } pub fn popups(&self) -> &HashMap, Rc>> { &self.popups } pub fn popups_mut(&mut self) -> &mut HashMap, Rc>> { &mut self.popups } pub fn with_popup_mut(&self, monitor_name: &str, f: F) -> Option where F: FnOnce(RefMut) -> T, { let popup = self.popups().get(monitor_name); if let Some(popup) = popup { let popup = popup.borrow_mut(); Some(f(popup)) } else { None } } }