use std::collections::HashMap;
use std::sync::{Mutex, MutexGuard};
use tauri::{Runtime, Window};
pub(crate) struct WebviewRegistry<R: Runtime> {
entries: Mutex<HashMap<String, Window<R>>>,
}
impl<R: Runtime> Default for WebviewRegistry<R> {
fn default() -> Self {
Self {
entries: Mutex::new(HashMap::new()),
}
}
}
impl<R: Runtime> WebviewRegistry<R> {
pub(crate) fn register(&self, webview_label: &str, window: Window<R>) {
self.lock().insert(webview_label.to_string(), window);
}
pub(crate) fn remove_window(&self, window_label: &str) {
self.lock()
.retain(|_, window| window.label() != window_label);
}
pub(crate) fn contains(&self, webview_label: &str) -> bool {
self.lock().contains_key(webview_label)
}
pub(crate) fn window(&self, webview_label: &str) -> Option<Window<R>> {
self.lock().get(webview_label).cloned()
}
pub(crate) fn entries(&self) -> Vec<(String, Window<R>)> {
self.lock()
.iter()
.map(|(label, window)| (label.clone(), window.clone()))
.collect()
}
fn lock(&self) -> MutexGuard<'_, HashMap<String, Window<R>>> {
self.entries.lock().expect("webview registry mutex")
}
}