|
| 1 | +use godot::prelude::*; |
| 2 | +use godot::classes::{ DisplayServer }; |
| 3 | +use godot::classes::display_server::HandleType; |
| 4 | +use core::ffi::c_void; |
| 5 | + |
| 6 | +#[cfg(target_os = "windows")] |
| 7 | +use windows::Win32::Foundation::HWND; |
| 8 | + |
| 9 | +#[cfg(target_os = "windows")] |
| 10 | +use windows::Win32::UI::WindowsAndMessaging::{ |
| 11 | + GetWindowLongPtrW, |
| 12 | + GetWindowLongW, |
| 13 | + GWLP_HWNDPARENT, |
| 14 | + GWL_HWNDPARENT, |
| 15 | + IsWindow, |
| 16 | +}; |
| 17 | + |
| 18 | +#[cfg(target_os = "windows")] |
| 19 | +use std::num::{ NonZero, NonZeroIsize }; |
| 20 | + |
| 21 | +#[cfg(target_os = "windows")] |
| 22 | +use raw_window_handle::{ |
| 23 | + Win32WindowHandle, |
| 24 | + WindowHandle, |
| 25 | + RawWindowHandle, |
| 26 | + HasWindowHandle, |
| 27 | + HandleError, |
| 28 | +}; |
| 29 | + |
| 30 | +#[cfg(target_os = "windows")] |
| 31 | +pub struct WindowsWryBrowserOptions; |
| 32 | +#[cfg(target_os = "windows")] |
| 33 | +impl HasWindowHandle for WindowsWryBrowserOptions { |
| 34 | + fn window_handle(&self) -> Result<WindowHandle<'_>, HandleError> { |
| 35 | + let display_server = DisplayServer::singleton(); |
| 36 | + let window_handle = display_server.window_get_native_handle(HandleType::WINDOW_HANDLE); |
| 37 | + |
| 38 | + godot_print!("[WindowsWryBrowserOptions] Retrieved window handle: {:?}", window_handle); |
| 39 | + |
| 40 | + if window_handle == 0 { |
| 41 | + godot_error!("[WindowsWryBrowserOptions] Invalid window handle (0)"); |
| 42 | + return Err(HandleError::Unavailable); |
| 43 | + } |
| 44 | + |
| 45 | + let non_zero_window_handle = NonZero::new(window_handle).ok_or_else(|| { |
| 46 | + godot_error!("[WindowsWryBrowserOptions] Failed to create NonZero window handle."); |
| 47 | + HandleError::Unavailable |
| 48 | + })?; |
| 49 | + |
| 50 | + let non_zero_isize = NonZeroIsize::try_from(non_zero_window_handle).map_err(|_| { |
| 51 | + godot_error!("[WindowsWryBrowserOptions] Failed to convert window handle to NonZeroIsize."); |
| 52 | + HandleError::Unavailable |
| 53 | + })?; |
| 54 | + |
| 55 | + godot_print!("[WindowsWryBrowserOptions] NonZeroIsize: {:?}", non_zero_isize); |
| 56 | + |
| 57 | + unsafe { |
| 58 | + Ok(WindowHandle::borrow_raw(RawWindowHandle::Win32(Win32WindowHandle::new(non_zero_isize)))) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments