use windows_sys::Win32::Foundation::{HWND, HINSTANCE};
use windows_sys::Win32::UI::WindowsAndMessaging::{
GetWindowLongPtrW, GWLP_USERDATA, LoadImageW, IMAGE_ICON, LR_DEFAULTSIZE, LR_SHARED, HICON,
};
use windows_sys::Win32::UI::Shell::StrFormatByteSizeW;
pub use crate::utils::to_wstring;
pub use crate::utils::ToPcwstr as ToWide;
#[inline]
pub unsafe fn get_window_state<'a, T>(hwnd: HWND) -> Option<&'a mut T> { unsafe {
let ptr = GetWindowLongPtrW(hwnd, GWLP_USERDATA);
if ptr == 0 {
None
} else {
Some(&mut *(ptr as *mut T))
}
}}
#[inline]
pub unsafe fn load_app_icon(instance: HINSTANCE) -> HICON { unsafe {
LoadImageW(
instance,
1 as *const u16,
IMAGE_ICON,
0, 0,
LR_DEFAULTSIZE | LR_SHARED,
)
}}
pub fn format_size(bytes: u64) -> Vec<u16> {
let mut buffer: [u16; 32] = [0; 32];
unsafe {
let size_i64 = if bytes > i64::MAX as u64 {
i64::MAX
} else {
bytes as i64
};
let ptr = StrFormatByteSizeW(size_i64, buffer.as_mut_ptr(), buffer.len() as u32);
if ptr.is_null() {
return vec![0];
}
let len = buffer.iter().position(|&c| c == 0).unwrap_or(buffer.len());
buffer[..=len].to_vec() }
}
pub unsafe fn run_message_loop() {
use windows_sys::Win32::UI::WindowsAndMessaging::{
GetMessageW, TranslateMessage, DispatchMessageW, MSG
};
let mut msg: MSG = unsafe { std::mem::zeroed() };
while unsafe { GetMessageW(&mut msg, std::ptr::null_mut(), 0, 0) } > 0 {
unsafe {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
}