use std::any::{Any, TypeId};
use crate::{
commands, core::CommandQueue, ext_event::ExtEventHost, Command, Data, Env, Event, ExtEventSink,
Handled, SingleUse, Target, WindowDesc, WindowHandle, WindowId,
};
pub struct DelegateCtx<'a> {
pub(crate) command_queue: &'a mut CommandQueue,
pub(crate) ext_event_host: &'a ExtEventHost,
pub(crate) app_data_type: TypeId,
}
impl<'a> DelegateCtx<'a> {
pub fn submit_command(&mut self, command: impl Into<Command>) {
self.command_queue
.push_back(command.into().default_to(Target::Global))
}
pub fn get_external_handle(&self) -> ExtEventSink {
self.ext_event_host.make_sink()
}
pub fn new_window<T: Any>(&mut self, desc: WindowDesc<T>) {
if self.app_data_type == TypeId::of::<T>() {
self.submit_command(
commands::NEW_WINDOW
.with(SingleUse::new(Box::new(desc)))
.to(Target::Global),
);
} else {
debug_panic!("DelegateCtx::new_window<T> - T must match the application data type.");
}
}
}
#[allow(unused)]
pub trait AppDelegate<T: Data> {
fn event(
&mut self,
ctx: &mut DelegateCtx,
window_id: WindowId,
event: Event,
data: &mut T,
env: &Env,
) -> Option<Event> {
Some(event)
}
fn command(
&mut self,
ctx: &mut DelegateCtx,
target: Target,
cmd: &Command,
data: &mut T,
env: &Env,
) -> Handled {
Handled::No
}
fn window_added(
&mut self,
id: WindowId,
handle: WindowHandle,
data: &mut T,
env: &Env,
ctx: &mut DelegateCtx,
) {
}
fn window_removed(&mut self, id: WindowId, data: &mut T, env: &Env, ctx: &mut DelegateCtx) {}
}