use cranpose_app_shell::AppShell;
use cranpose_render_wgpu::WgpuRenderer;
use raw_window_handle::{
DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle,
};
use winit::event_loop::EventLoopProxy;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DesktopWindowId {
Primary,
Native(cranpose_core::NodeId),
}
pub struct DesktopWindow<'w> {
id: DesktopWindowId,
window: &'w dyn winit::window::Window,
}
impl DesktopWindow<'_> {
pub fn id(&self) -> DesktopWindowId {
self.id
}
}
impl HasWindowHandle for DesktopWindow<'_> {
fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> {
self.window.window_handle()
}
}
impl HasDisplayHandle for DesktopWindow<'_> {
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
self.window.display_handle()
}
}
#[derive(Clone)]
pub struct DesktopWaker {
proxy: EventLoopProxy,
}
impl DesktopWaker {
pub fn wake(&self) {
self.proxy.wake_up();
}
}
pub struct DesktopIntegrationContext<'a> {
app: &'a mut AppShell<WgpuRenderer>,
proxy: &'a EventLoopProxy,
}
impl DesktopIntegrationContext<'_> {
pub fn update<T>(&mut self, update: impl FnOnce() -> T) -> Result<T, &'static str> {
run_as_event_handler(self.app, self.proxy, update)
}
pub fn waker(&self) -> DesktopWaker {
DesktopWaker {
proxy: self.proxy.clone(),
}
}
}
pub(crate) fn run_as_event_handler<T>(
app: &mut AppShell<WgpuRenderer>,
proxy: &EventLoopProxy,
update: impl FnOnce() -> T,
) -> Result<T, &'static str> {
let _event_handler = cranpose_core::enter_event_handler_scope();
let result = app
.app_context()
.enter(|| cranpose_core::run_in_mutable_snapshot(update));
app.mark_dirty();
proxy.wake_up();
result
}
pub trait DesktopIntegration: 'static {
fn window_opened(
&mut self,
window: &DesktopWindow<'_>,
context: &mut DesktopIntegrationContext<'_>,
) {
let _ = (window, context);
}
fn window_closed(&mut self, id: DesktopWindowId, context: &mut DesktopIntegrationContext<'_>) {
let _ = (id, context);
}
fn about_to_wait(&mut self, context: &mut DesktopIntegrationContext<'_>) {
let _ = context;
}
}
#[derive(Default)]
pub(crate) struct Integration {
app: Option<Box<dyn DesktopIntegration>>,
#[cfg(all(
feature = "media-backend",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
media: super::media_controls::MediaControls,
}
impl Integration {
pub(crate) fn new(integration: Option<Box<dyn DesktopIntegration>>) -> Self {
Self {
app: integration,
#[cfg(all(
feature = "media-backend",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
media: super::media_controls::MediaControls::default(),
}
}
pub(crate) fn window_opened(
&mut self,
id: DesktopWindowId,
window: &dyn winit::window::Window,
app: &mut AppShell<WgpuRenderer>,
proxy: &EventLoopProxy,
) {
#[cfg(all(
feature = "media-backend",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
self.media.window_opened(
id,
window,
DesktopWaker {
proxy: proxy.clone(),
},
);
if let Some(integration) = &mut self.app {
let window = DesktopWindow { id, window };
integration.window_opened(&window, &mut DesktopIntegrationContext { app, proxy });
}
}
pub(crate) fn window_closed(
&mut self,
id: DesktopWindowId,
app: &mut AppShell<WgpuRenderer>,
proxy: &EventLoopProxy,
) {
#[cfg(all(
feature = "media-backend",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
self.media.window_closed(id);
if let Some(integration) = &mut self.app {
integration.window_closed(id, &mut DesktopIntegrationContext { app, proxy });
}
}
pub(crate) fn about_to_wait(
&mut self,
app: &mut AppShell<WgpuRenderer>,
proxy: &EventLoopProxy,
) {
#[cfg(all(
feature = "media-backend",
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
self.media.about_to_wait();
if let Some(integration) = &mut self.app {
integration.about_to_wait(&mut DesktopIntegrationContext { app, proxy });
}
}
}