Skip to content

Commit e869670

Browse files
committed
fix(q): migration of the windows module into the q crate
#4145
1 parent 45c9858 commit e869670

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

packages/rust/q/src/windows/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod windows_gui_options;
2+
pub mod windows_wry_browser_options;
3+
pub use windows_wry_browser_options::WindowsWryBrowserOptions;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use godot::prelude::*;
2+
use core::ffi::c_void;
3+
use godot::classes::{CanvasLayer, Window, DisplayServer};
4+
use godot::classes::display_server::HandleType;
5+
6+
#[cfg(target_os = "windows")]
7+
use windows::Win32::Foundation::{ HWND, COLORREF };
8+
#[cfg(target_os = "windows")]
9+
use windows::Win32::UI::WindowsAndMessaging::{
10+
GetForegroundWindow,
11+
GetWindowLongW,
12+
SetLayeredWindowAttributes,
13+
SetWindowLongW,
14+
GWL_EXSTYLE,
15+
LWA_ALPHA,
16+
WS_EX_LAYERED,
17+
};
18+
19+
#[cfg(target_os = "windows")]
20+
pub fn set_windows_opacity(transparency_value: f64, gui_manager: &Gd<CanvasLayer>) {
21+
unsafe {
22+
let hwnd: HWND = HWND(DisplayServer::singleton().window_get_native_handle(HandleType::WINDOW_HANDLE) as *mut c_void);
23+
if hwnd.0.is_null() {
24+
godot_print!("[Windows] ERROR: Failed to get active window handle.");
25+
return;
26+
}
27+
28+
let ex_style = GetWindowLongW(hwnd, GWL_EXSTYLE);
29+
SetWindowLongW(hwnd, GWL_EXSTYLE, ex_style | (WS_EX_LAYERED.0 as i32));
30+
31+
let alpha_value = (transparency_value.clamp(0.0, 1.0) * 255.0) as u8;
32+
33+
if SetLayeredWindowAttributes(hwnd, COLORREF(0), alpha_value, LWA_ALPHA).is_err() {
34+
godot_print!("[Windows] ERROR: Failed to set window opacity.");
35+
} else {
36+
godot_print!(
37+
"[Windows] Window opacity set to {} (alpha {}).",
38+
transparency_value,
39+
alpha_value
40+
);
41+
}
42+
}
43+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)