use std::cell::{Cell, RefCell};
use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::sync::Arc;
use std::time::Instant;
use slate_platform::{DefaultWindow, PhysicalSize, Window, WindowId};
use slate_reactive::{ObserverId, Signal};
use slate_renderer::{Renderer, Scene};
use slate_text::GlyphCache;
use crate::erased_view::ErasedView;
use crate::event::{Handlers, ImeHandlers, KeyHandlers, MouseHandlers};
use crate::focus::FocusRegistry;
use crate::focus_ring::FocusBounds;
use crate::hit_test::HitTestList;
use crate::image_cache::ImageCache;
use crate::ime::{CachedImeQuery, ImeRegistry, PendingImeOp};
use crate::layout::LayoutTree;
use crate::types::{AccessibilityNode, ElementId};
use super::types::RecoveryState;
pub struct WindowState {
pub id: WindowId,
pub window: Arc<DefaultWindow>,
pub renderer: RefCell<Option<Renderer>>,
pub view: RefCell<Option<Box<dyn ErasedView>>>,
pub(crate) glyph_cache: RefCell<GlyphCache>,
pub(crate) image_cache: RefCell<ImageCache>,
pub layout_tree: RefCell<LayoutTree>,
pub hit_test_list: RefCell<HitTestList>,
pub a11y_nodes: RefCell<Vec<AccessibilityNode>>,
pub scene: RefCell<Scene>,
pub(crate) handler_map: RefCell<HashMap<ElementId, Handlers>>,
pub(crate) mouse_handler_map: RefCell<HashMap<ElementId, MouseHandlers>>,
pub parent_map: RefCell<HashMap<ElementId, ElementId>>,
pub hovered_element: RefCell<Option<ElementId>>,
pub button_state: RefCell<u8>,
pub capture_target: RefCell<Option<ElementId>>,
pub explicit_capture: RefCell<bool>,
pub last_mouse_pos: RefCell<Option<(f32, f32)>>,
pub coalesced_move_pos: RefCell<Option<(f32, f32)>>,
pub last_dispatched_move_pos: RefCell<Option<(f32, f32)>>,
pub view_observer_id: ObserverId,
pub(crate) key_handler_map: RefCell<HashMap<ElementId, KeyHandlers>>,
pub(crate) focus_registry: Rc<RefCell<FocusRegistry>>,
pub(crate) focus_bounds: RefCell<HashMap<ElementId, FocusBounds>>,
pub(crate) ime_registry: Rc<RefCell<ImeRegistry>>,
pub(crate) ime_handler_map: RefCell<HashMap<ElementId, ImeHandlers>>,
pub(crate) ime_registered_ids: RefCell<HashSet<ElementId>>,
pub(crate) cached_ime_query: RefCell<CachedImeQuery>,
pub(crate) pending_ime_ops: RefCell<Vec<PendingImeOp>>,
pub recovery_state: RefCell<RecoveryState>,
pub skip_draws: Cell<bool>,
pub last_successful_recovery_at: Cell<Option<Instant>>,
pub last_wgpu_callback_loss_at: Cell<Option<Instant>>,
pub last_adapter_check_at: Cell<Option<Instant>>,
pub renderer_generation: Signal<u64>,
pub rendering: Cell<bool>,
pub sync_resize: Cell<bool>,
pub last_resize_size: Cell<Option<PhysicalSize>>,
}
impl WindowState {
pub fn new(window: Arc<DefaultWindow>, runtime: Arc<slate_reactive::Runtime>) -> Self {
let id = window.id();
let view_observer_id = runtime.next_observer_id();
Self {
id,
window,
renderer: RefCell::new(None),
view: RefCell::new(None),
glyph_cache: RefCell::new(GlyphCache::new()),
image_cache: RefCell::new(ImageCache::new()),
layout_tree: RefCell::new(LayoutTree::new()),
hit_test_list: RefCell::new(HitTestList::new()),
a11y_nodes: RefCell::new(Vec::new()),
scene: RefCell::new(Scene::new()),
handler_map: RefCell::new(HashMap::new()),
mouse_handler_map: RefCell::new(HashMap::new()),
parent_map: RefCell::new(HashMap::new()),
hovered_element: RefCell::new(None),
button_state: RefCell::new(0),
capture_target: RefCell::new(None),
explicit_capture: RefCell::new(false),
last_mouse_pos: RefCell::new(None),
coalesced_move_pos: RefCell::new(None),
last_dispatched_move_pos: RefCell::new(None),
view_observer_id,
key_handler_map: RefCell::new(HashMap::new()),
focus_registry: Rc::new(RefCell::new(FocusRegistry::new())),
focus_bounds: RefCell::new(HashMap::new()),
ime_registry: Rc::new(RefCell::new(ImeRegistry::new())),
ime_handler_map: RefCell::new(HashMap::new()),
ime_registered_ids: RefCell::new(HashSet::new()),
cached_ime_query: RefCell::new(CachedImeQuery::default()),
pending_ime_ops: RefCell::new(Vec::new()),
recovery_state: RefCell::new(RecoveryState::NotLost),
skip_draws: Cell::new(false),
last_successful_recovery_at: Cell::new(None),
last_wgpu_callback_loss_at: Cell::new(None),
last_adapter_check_at: Cell::new(None),
renderer_generation: Signal::new(runtime, 0u64),
rendering: Cell::new(false),
sync_resize: Cell::new(false),
last_resize_size: Cell::new(None),
}
}
}