From a2633f78ad5da6cfb2fd68c66df85fc47ff35056 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Mon, 2 Dec 2019 15:51:46 +0100 Subject: [PATCH] Raw window handle (#110) * Added support for raw-window-handle Ref #104 * Fixed some typos * Fixed more typos * windows fix * Another windows fix * Another typo * More typos * More windwos fixes * Yet anonther Windows fix --- Cargo.toml | 1 + src/os/macos/mod.rs | 13 +++++++++++++ src/os/unix/mod.rs | 12 ++++++++++++ src/os/windows/mod.rs | 18 +++++++++++------- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 10396ce..b1a734c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ cc = "1.0" [dependencies] cast = "0.2" time = "0.1.34" +raw-window-handle = "0.3.3" [target.'cfg(windows)'.dependencies.winapi] version = "0.3" diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index 0f8460c..146583b 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -1,5 +1,7 @@ #![cfg(target_os = "macos")] +extern crate raw_window_handle; + use error::Error; use key_handler::KeyHandler; use Result; @@ -243,6 +245,17 @@ unsafe extern "C" fn char_callback(window: *mut c_void, code_point: u32) { } } +unsafe impl raw_window_handle::HasRawWindowHandle for Window { + fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle { + let handle = raw_window_handle::macos::MacOSHandle { + ns_window: self.window_handle, + ns_view: std::ptr::null_mut(), + ..raw_window_handle::macos::MacOSHandle::empty() + }; + raw_window_handle::RawWindowHandle::MacOS(handle) + } +} + impl Window { pub fn new(name: &str, width: usize, height: usize, opts: WindowOptions) -> Result { let n = match CString::new(name) { diff --git a/src/os/unix/mod.rs b/src/os/unix/mod.rs index eaad05a..ad14d4a 100644 --- a/src/os/unix/mod.rs +++ b/src/os/unix/mod.rs @@ -10,6 +10,7 @@ extern crate cast; extern crate x11_dl; +extern crate raw_window_handle; use self::x11_dl::keysym::*; use self::x11_dl::xcursor; @@ -242,6 +243,17 @@ pub struct Window { menus: Vec, } +unsafe impl raw_window_handle::HasRawWindowHandle for Window { + fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle { + let handle = raw_window_handle::unix::XlibHandle { + window: self.handle, + display: self.d.display as *mut core::ffi::c_void, + ..raw_window_handle::unix::XlibHandle::empty() + }; + raw_window_handle::RawWindowHandle::Xlib(handle) + } +} + impl Window { pub fn new(name: &str, width: usize, height: usize, opts: WindowOptions) -> Result { let name = match CString::new(name) { diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index 727508e..547a94c 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -1,5 +1,6 @@ #![cfg(target_os = "windows")] +extern crate raw_window_handle; extern crate time; extern crate winapi; @@ -337,13 +338,16 @@ pub struct Window { cursors: [windef::HCURSOR; 8], } -// TranslateAccelerator is currently missing in win-rs -// #[cfg(target_family = "windows")] -// #[link(name = "user32")] -// #[allow(non_snake_case)] -// extern "system" { -// fn RemoveMenu(menu: HMENU, pos: UINT, flags: UINT) -> BOOL; -// } +unsafe impl raw_window_handle::HasRawWindowHandle for Window { + fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle { + let handle = raw_window_handle::windows::WindowsHandle { + hwnd: self.window.unwrap() as *mut raw::c_void, + hinstance: unsafe { libloaderapi::GetModuleHandleA(ptr::null()) } as *mut raw::c_void, + ..raw_window_handle::windows::WindowsHandle::empty() + }; + raw_window_handle::RawWindowHandle::Windows(handle) + } +} impl Window { fn open_window(