use crate::{App, ElementId, FocusHandle, Global, OwnedMenu};
use std::cell::Cell;
use std::collections::HashSet;
pub struct GlobalState {
open_deferred_popovers: HashSet<ElementId>,
app_menus: Vec<OwnedMenu>,
suppress_text_selection: Cell<bool>,
}
impl Global for GlobalState {}
impl GlobalState {
pub(crate) fn new() -> Self {
Self {
open_deferred_popovers: HashSet::new(),
app_menus: Vec::new(),
suppress_text_selection: Cell::new(false),
}
}
pub fn global(cx: &mut App) -> &Self {
if !cx.has_global::<Self>() {
cx.set_global(Self::new());
}
cx.global::<Self>()
}
pub fn global_mut(cx: &mut App) -> &mut Self {
if !cx.has_global::<Self>() {
cx.set_global(Self::new());
}
cx.global_mut::<Self>()
}
pub(crate) fn register_deferred_popover(&mut self, focus_handle: &FocusHandle) {
self.open_deferred_popovers
.insert(format!("{focus_handle:?}").into());
}
pub(crate) fn unregister_deferred_popover(&mut self, focus_handle: &FocusHandle) {
let element_id: ElementId = format!("{focus_handle:?}").into();
self.open_deferred_popovers.remove(&element_id);
}
pub fn app_menus(&self) -> &[OwnedMenu] {
&self.app_menus
}
pub fn set_app_menus(&mut self, menus: Vec<OwnedMenu>) {
self.app_menus = menus;
}
pub fn suppress_text_selection(cx: &mut App) {
Self::global_mut(cx).suppress_text_selection.set(true);
}
pub fn clear_suppress_text_selection(cx: &mut App) {
Self::global_mut(cx).suppress_text_selection.set(false);
}
pub fn is_suppress_text_selection(cx: &mut App) -> bool {
Self::global(cx).suppress_text_selection.get()
}
}