Added NSView for raw_window_handle on macOS

This commit is contained in:
Daniel Collin 2020-01-21 22:11:46 +01:00
parent b310fd372c
commit acc25a0176
2 changed files with 9 additions and 2 deletions

View file

@ -150,7 +150,7 @@ static bool create_shaders() {
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void* mfb_open(const char* name, int width, int height, uint32_t flags, int scale) void* mfb_open(const char* name, int width, int height, uint32_t flags, int scale, void** view_handle)
{ {
bool prev_init = s_init; bool prev_init = s_init;
@ -283,6 +283,8 @@ void* mfb_open(const char* name, int width, int height, uint32_t flags, int scal
if (!prev_init) if (!prev_init)
[NSApp finishLaunching]; [NSApp finishLaunching];
*view_handle = (void*)view;
//[pool drain]; //[pool drain];
return window; return window;

View file

@ -161,6 +161,7 @@ extern "C" {
height: u32, height: u32,
flags: u32, flags: u32,
scale: i32, scale: i32,
view_handle: *mut *const c_void,
) -> *mut c_void; ) -> *mut c_void;
fn mfb_set_title(window: *mut c_void, title: *const c_char); fn mfb_set_title(window: *mut c_void, title: *const c_char);
fn mfb_close(window: *mut c_void); fn mfb_close(window: *mut c_void);
@ -218,6 +219,7 @@ pub struct SharedData {
pub struct Window { pub struct Window {
window_handle: *mut c_void, window_handle: *mut c_void,
view_handle: *const c_void,
scale_factor: usize, scale_factor: usize,
pub shared_data: SharedData, pub shared_data: SharedData,
key_handler: KeyHandler, key_handler: KeyHandler,
@ -257,7 +259,7 @@ unsafe impl raw_window_handle::HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle { fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
let handle = raw_window_handle::macos::MacOSHandle { let handle = raw_window_handle::macos::MacOSHandle {
ns_window: self.window_handle as *mut _, ns_window: self.window_handle as *mut _,
ns_view: std::ptr::null_mut(), ns_view: self.view_handle as *mut _,
..raw_window_handle::macos::MacOSHandle::empty() ..raw_window_handle::macos::MacOSHandle::empty()
}; };
raw_window_handle::RawWindowHandle::MacOS(handle) raw_window_handle::RawWindowHandle::MacOS(handle)
@ -276,12 +278,14 @@ impl Window {
unsafe { unsafe {
let scale_factor = Self::get_scale_factor(width, height, opts.scale) as usize; let scale_factor = Self::get_scale_factor(width, height, opts.scale) as usize;
let mut view_handle = ptr::null();
let handle = mfb_open( let handle = mfb_open(
n.as_ptr(), n.as_ptr(),
width as u32, width as u32,
height as u32, height as u32,
window_flags::get_flags(opts), window_flags::get_flags(opts),
scale_factor as i32, scale_factor as i32,
&mut view_handle,
); );
if handle == ptr::null_mut() { if handle == ptr::null_mut() {
@ -290,6 +294,7 @@ impl Window {
Ok(Window { Ok(Window {
window_handle: handle, window_handle: handle,
view_handle,
scale_factor, scale_factor,
shared_data: SharedData { shared_data: SharedData {
bg_color: 0, bg_color: 0,