Skip to content

Instantly share code, notes, and snippets.

@nightfly19
Created February 21, 2022 09:21
Show Gist options
  • Save nightfly19/b656b249e1770cb2735b03b1cbbd6607 to your computer and use it in GitHub Desktop.
Save nightfly19/b656b249e1770cb2735b03b1cbbd6607 to your computer and use it in GitHub Desktop.
Window wrapper for SDL2 and WGPU
use sdl2::video::Window;
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use raw_window_handle::AppKitHandle;
pub struct WindowWrapper<'a>(pub &'a Window);
unsafe impl<'a> HasRawWindowHandle for WindowWrapper<'a> {
#[cfg(not(target_os = "macos"))]
/// all non-mac platforms work correctly, so return the handle directly
fn raw_window_handle(&self) -> RawWindowHandle {
self.0.raw_window_handle()
}
#[cfg(target_os = "macos")]
/// do some work on appkit to get the root NSView for the NSWindow returned by sdl2
fn raw_window_handle(&self) -> RawWindowHandle {
use objc::{msg_send, sel, sel_impl};
use objc::runtime::Object;
let handle = self.0.raw_window_handle();
match handle {
RawWindowHandle::AppKit(appkit_handle) => {
unsafe {
let mut new_handle = appkit_handle.clone();
new_handle.ns_view = msg_send![appkit_handle.ns_window as *mut Object, contentView];
RawWindowHandle::AppKit(new_handle)
}
}
_ => unreachable!()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment