use crate::kept::{UiCell, UiLocal};
use crate::ViewId;
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
FocusStep {
forward: bool,
},
FocusChanged(Option<ViewId>),
AppearanceChanged,
Action(ViewId),
Redraw,
}
type Sink = Box<dyn Fn(Event)>;
thread_local! {
static SINK: UiCell<Option<Sink>> = const { UiCell::new(None) };
}
pub fn set_event_sink(sink: impl Fn(Event) + 'static) {
SINK.with_borrow_mut(|s| *s = Some(Box::new(sink)));
}
pub(crate) fn emit(event: Event) {
SINK.with_borrow(|s| {
if let Some(sink) = s {
sink(event);
}
});
}
thread_local! {
static REDRAW_QUEUED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
pub fn request_redraw() {
if !REDRAW_QUEUED.with(|q| q.replace(true)) {
emit(Event::Redraw);
}
}
pub(crate) fn frame_started() {
REDRAW_QUEUED.with(|q| q.set(false));
}