2023-07-16 18:57:00 +01:00
|
|
|
use crate::popup::Popup;
|
2023-08-25 22:55:00 +01:00
|
|
|
use std::cell::{RefCell, RefMut};
|
2023-07-16 18:57:00 +01:00
|
|
|
use std::collections::HashMap;
|
2023-08-25 22:55:00 +01:00
|
|
|
use std::rc::Rc;
|
2023-07-16 18:57:00 +01:00
|
|
|
|
|
|
|
/// 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 {
|
2023-08-25 22:55:00 +01:00
|
|
|
popups: HashMap<Box<str>, Rc<RefCell<Popup>>>,
|
2023-07-16 18:57:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GlobalState {
|
|
|
|
pub(crate) fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
popups: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:55:00 +01:00
|
|
|
pub fn popups(&self) -> &HashMap<Box<str>, Rc<RefCell<Popup>>> {
|
2023-07-16 18:57:00 +01:00
|
|
|
&self.popups
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:55:00 +01:00
|
|
|
pub fn popups_mut(&mut self) -> &mut HashMap<Box<str>, Rc<RefCell<Popup>>> {
|
2023-07-16 18:57:00 +01:00
|
|
|
&mut self.popups
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_popup_mut<F, T>(&self, monitor_name: &str, f: F) -> Option<T>
|
|
|
|
where
|
2023-08-25 22:55:00 +01:00
|
|
|
F: FnOnce(RefMut<Popup>) -> T,
|
2023-07-16 18:57:00 +01:00
|
|
|
{
|
|
|
|
let popup = self.popups().get(monitor_name);
|
|
|
|
|
|
|
|
if let Some(popup) = popup {
|
2023-08-25 22:55:00 +01:00
|
|
|
let popup = popup.borrow_mut();
|
2023-07-16 18:57:00 +01:00
|
|
|
Some(f(popup))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|