use crate::{HandlerError, WindowSize};
use std::cell::RefCell;
pub trait HostMainThreadCaller: Send + 'static {
fn call_main_thread(&mut self);
}
pub trait HostCallbacks: 'static {
fn request_resize(&mut self, new_size: WindowSize) -> Result<(), HandlerError>;
fn destroyed(&mut self);
}
pub struct Host {
#[cfg(target_os = "linux")]
pub(crate) main_thread: Option<Box<dyn HostMainThreadCaller>>,
pub(crate) callbacks: Option<RefCell<Box<dyn HostCallbacks>>>,
}
impl Default for Host {
fn default() -> Self {
Self::new()
}
}
impl Host {
#[inline]
pub fn new() -> Self {
Self {
#[cfg(target_os = "linux")]
main_thread: None,
callbacks: None,
}
}
#[inline]
pub fn with_main_thread(self, main_thread: impl HostMainThreadCaller) -> Self {
#[cfg(target_os = "linux")]
{
let mut this = self;
this.main_thread = Some(Box::new(main_thread));
this
}
#[cfg(not(target_os = "linux"))]
{
let _ = main_thread;
self
}
}
#[inline]
pub fn with_callbacks(mut self, callbacks: impl HostCallbacks) -> Self {
self.callbacks = Some(RefCell::new(Box::new(callbacks)));
self
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(crate) fn notify_destroyed(&self) {
let Some(callbacks) = &self.callbacks else { return };
let Ok(mut callbacks) = callbacks.try_borrow_mut() else { return };
callbacks.destroyed();
}
#[cfg(any(target_os = "macos", target_os = "windows"))]
pub(crate) fn request_resize(&self, new_size: WindowSize) -> Result<(), HandlerError> {
let Some(callbacks) = &self.callbacks else { return Ok(()) };
let Ok(mut callbacks) = callbacks.try_borrow_mut() else { return Ok(()) };
callbacks.request_resize(new_size)
}
}