use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
use winit::window::Window;
pub type Hwnd = isize;
#[repr(C)]
pub struct Msg {
pub hwnd: Hwnd,
pub message: u32,
pub wparam: usize,
pub lparam: isize,
pub time: u32,
pub pt_x: i32,
pub pt_y: i32,
}
impl Msg {
pub fn empty() -> Msg {
Msg { hwnd: 0, message: 0, wparam: 0, lparam: 0, time: 0, pt_x: 0, pt_y: 0 }
}
}
#[link(name = "user32")]
unsafe extern "system" {
pub fn RegisterHotKey(hwnd: Hwnd, id: i32, modifiers: u32, vk: u32) -> i32;
pub fn UnregisterHotKey(hwnd: Hwnd, id: i32) -> i32;
pub fn GetMessageW(msg: *mut Msg, hwnd: Hwnd, min: u32, max: u32) -> i32;
pub fn PostThreadMessageW(thread: u32, msg: u32, wparam: usize, lparam: isize) -> i32;
pub fn GetForegroundWindow() -> Hwnd;
pub fn IsChild(parent: Hwnd, child: Hwnd) -> i32;
pub fn GetWindow(hwnd: Hwnd, command: u32) -> Hwnd;
pub fn GetWindowLongPtrW(hwnd: Hwnd, index: i32) -> isize;
pub fn SetWindowLongPtrW(hwnd: Hwnd, index: i32, value: isize) -> isize;
pub fn SetWindowPos(hwnd: Hwnd, after: Hwnd, x: i32, y: i32, cx: i32, cy: i32, flags: u32) -> i32;
}
#[link(name = "kernel32")]
unsafe extern "system" {
pub fn GetCurrentThreadId() -> u32;
pub fn CloseHandle(handle: isize) -> i32;
pub fn GetStdHandle(which: u32) -> isize;
}
pub fn handle_of(window: &Window) -> Option<Hwnd> {
match window.window_handle().ok()?.as_raw() {
RawWindowHandle::Win32(h) => Some(h.hwnd.get()),
_ => None,
}
}
pub fn is_in_front(window: Hwnd) -> bool {
const GW_OWNER: u32 = 4;
unsafe {
let front = GetForegroundWindow();
if front == 0 || IsChild(window, front) != 0 {
return front != 0;
}
let mut at = front;
for _ in 0..4 {
if at == window {
return true;
}
at = GetWindow(at, GW_OWNER);
if at == 0 {
break;
}
}
false
}
}