use super::AppView;
use bevy::prelude::*;
use rustc_hash::FxHashMap;
use uuid::Uuid;
#[derive(Default)]
pub struct AppViews {
views: FxHashMap<Entity, AppViewWindow>,
entity_to_window: FxHashMap<Entity, Entity>,
}
pub struct AppViewWindow {
pub view: AppView,
pub id: Uuid,
}
impl AppViews {
pub fn new() -> Self {
Self::default()
}
#[cfg(target_os = "ios")]
pub fn create_window(&mut self, view_obj: super::IOSViewObj, entity: Entity) -> &AppViewWindow {
let view = AppView::new(view_obj);
let id = Uuid::new_v4();
let window = AppViewWindow { view, id };
self.views.insert(entity, window);
self.entity_to_window.insert(entity, entity);
self.views.get(&entity).unwrap()
}
#[cfg(target_os = "macos")]
pub fn create_window(
&mut self,
view_obj: super::MacOSViewObj,
entity: Entity,
) -> &AppViewWindow {
let view = AppView::new(view_obj);
let id = Uuid::new_v4();
let window = AppViewWindow { view, id };
self.views.insert(entity, window);
self.entity_to_window.insert(entity, entity);
self.views.get(&entity).unwrap()
}
pub fn get_view(&self, entity: Entity) -> Option<&AppViewWindow> {
self.entity_to_window
.get(&entity)
.and_then(|e| self.views.get(e))
}
pub fn remove_view(&mut self, entity: Entity) -> Option<AppViewWindow> {
if let Some(window_entity) = self.entity_to_window.remove(&entity) {
self.views.remove(&window_entity)
} else {
None
}
}
pub fn has_views(&self) -> bool {
!self.views.is_empty()
}
pub fn first_view(&self) -> Option<&AppViewWindow> {
self.views.values().next()
}
}