use std::cell::{Cell, RefCell};
use std::sync::Arc;
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use crate::deferred::{DeferredQueue, DeferredSlot};
use crate::jobs::{JobQueues, ScheduledJob};
use crate::reactive::cursor::CursorIcon;
use crate::renderer::TextMeasurer;
use crate::session_lock::{LockData, LockRequest};
use crate::surface::{SurfaceCommand, SurfaceId};
use crate::tree::WidgetId;
use crate::widget_ref::WidgetRef;
use crate::widgets::font::FontFamily;
thread_local! {
static APP: AppState = AppState::default();
}
pub(crate) fn with_app_state<R>(f: impl FnOnce(&AppState) -> R) -> R {
APP.with(f)
}
#[derive(Default)]
pub(crate) struct AppState {
pub(crate) pending_jobs: RefCell<JobQueues>,
pub(crate) scheduled_jobs: RefCell<Vec<ScheduledJob>>,
pub(crate) dirty_segments: RefCell<FxHashMap<WidgetId, SmallVec<[u32; 4]>>>,
pub(crate) surface_commands: DeferredQueue<SurfaceCommand>,
pub(crate) outgoing_cursor: DeferredSlot<CursorIcon>,
pub(crate) outgoing_clipboard: DeferredSlot<String>,
pub(crate) outgoing_primary: DeferredSlot<String>,
pub(crate) pending_focus: RefCell<Option<WidgetRef>>,
pub(crate) lock_request: DeferredSlot<LockRequest>,
pub(crate) current_cursor: Cell<CursorIcon>,
pub(crate) clipboard: RefCell<Option<String>>,
pub(crate) system_clipboard: RefCell<Option<String>>,
pub(crate) primary: RefCell<Option<String>>,
pub(crate) system_primary: RefCell<Option<String>>,
pub(crate) lock: RefCell<LockData>,
pub(crate) live_popups: RefCell<FxHashSet<SurfaceId>>,
pub(crate) widget_refs: RefCell<FxHashMap<WidgetId, WidgetRef>>,
pub(crate) text_measurer: RefCell<Option<TextMeasurer>>,
pub(crate) default_font_family: Cell<FontFamily>,
pub(crate) custom_fonts: RefCell<Vec<Arc<Vec<u8>>>>,
pub(crate) custom_font_hashes: RefCell<FxHashSet<u64>>,
pub(crate) fonts_consumed: Cell<bool>,
}
pub(crate) fn reset() {
with_app_state(|app| {
let AppState {
pending_jobs,
scheduled_jobs,
dirty_segments,
surface_commands,
outgoing_cursor,
outgoing_clipboard,
outgoing_primary,
pending_focus,
lock_request,
current_cursor,
clipboard,
system_clipboard,
primary,
system_primary,
lock,
live_popups,
widget_refs,
text_measurer,
default_font_family,
custom_fonts,
custom_font_hashes,
fonts_consumed,
} = app;
surface_commands.clear();
lock_request.clear();
lock.take();
widget_refs.take();
text_measurer.take();
pending_jobs.take();
scheduled_jobs.take();
dirty_segments.take();
outgoing_cursor.clear();
outgoing_clipboard.clear();
outgoing_primary.clear();
pending_focus.take();
current_cursor.take();
clipboard.take();
system_clipboard.take();
primary.take();
system_primary.take();
live_popups.take();
default_font_family.take();
custom_fonts.take();
custom_font_hashes.take();
fonts_consumed.take();
});
}