#![expect(unsafe_code)]
use std::{
ffi::c_void,
sync::atomic::{AtomicIsize, Ordering},
};
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use windows_sys::Win32::UI::WindowsAndMessaging::{MSG, TranslateAcceleratorW};
use winit::window::Window;
static ACCELERATORS: AtomicIsize = AtomicIsize::new(0);
static MENU_WINDOW: AtomicIsize = AtomicIsize::new(0);
pub(crate) fn attach(menu: &muda::Menu, window: &dyn Window) -> Option<isize> {
let handle = window.window_handle().ok()?;
let RawWindowHandle::Win32(handle) = handle.as_raw() else {
return None;
};
let hwnd = handle.hwnd.get();
if let Err(error) = unsafe { menu.init_for_hwnd(hwnd) } {
log::warn!("cranpose: the menu bar could not show: {error}");
return None;
}
ACCELERATORS.store(menu.haccel(), Ordering::Relaxed);
MENU_WINDOW.store(hwnd, Ordering::Relaxed);
Some(hwnd)
}
pub(crate) fn detach(menu: &muda::Menu, hwnd: isize) {
ACCELERATORS.store(0, Ordering::Relaxed);
MENU_WINDOW.store(0, Ordering::Relaxed);
if let Err(error) = unsafe { menu.remove_for_hwnd(hwnd) } {
log::warn!("cranpose: the menu bar could not go: {error}");
}
}
pub(crate) fn translate_shortcut(message: *const c_void) -> bool {
let accelerators = ACCELERATORS.load(Ordering::Relaxed);
if accelerators == 0 || message.is_null() {
return false;
}
let window = MENU_WINDOW.load(Ordering::Relaxed);
unsafe {
TranslateAcceleratorW(
window as *mut c_void,
accelerators as *mut c_void,
message.cast::<MSG>(),
) != 0
}
}
#[cfg(feature = "robot")]
pub(crate) fn choose(labels: &[String]) -> Result<(), String> {
use windows_sys::Win32::UI::WindowsAndMessaging::{
GetMenu, GetMenuItemID, GetSubMenu, SendMessageW, WM_COMMAND,
};
let window = MENU_WINDOW.load(Ordering::Relaxed);
if window == 0 {
return Err("no menu bar shows".to_owned());
}
let (last, parents) = labels.split_last().ok_or("no menu item named")?;
let mut menu = unsafe { GetMenu(window as *mut c_void) };
for title in parents {
let position = position_of(menu, title).ok_or_else(|| format!("no menu {title:?}"))?;
menu = unsafe { GetSubMenu(menu, position) };
if menu.is_null() {
return Err(format!("{title:?} opens no menu"));
}
}
let position = position_of(menu, last).ok_or_else(|| format!("no menu item {last:?}"))?;
let id = unsafe { GetMenuItemID(menu, position) };
if id == u32::MAX {
return Err(format!("{last:?} opens a menu"));
}
unsafe { SendMessageW(window as *mut c_void, WM_COMMAND, id as usize, 0) };
Ok(())
}
#[cfg(feature = "robot")]
fn position_of(
menu: windows_sys::Win32::UI::WindowsAndMessaging::HMENU,
label: &str,
) -> Option<i32> {
use windows_sys::Win32::UI::WindowsAndMessaging::{
GetMenuItemCount, GetMenuStringW, MF_BYPOSITION,
};
if menu.is_null() {
return None;
}
let count = unsafe { GetMenuItemCount(menu) };
let mut text = [0u16; 256];
(0..count).find(|&position| {
let length = unsafe {
GetMenuStringW(
menu,
position as u32,
text.as_mut_ptr(),
text.len() as i32,
MF_BYPOSITION,
)
};
let shown = String::from_utf16_lossy(&text[..length.max(0) as usize]);
shown.split('\t').next() == Some(label)
})
}