mirror of
https://github.com/tauri-apps/winit.git
synced 2026-02-04 02:11:19 +01:00
Windows fix
This commit is contained in:
@@ -1,58 +1,112 @@
|
||||
use crate::menu::{Menu, MenuItem};
|
||||
use raw_window_handle::RawWindowHandle;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use winapi::shared::{basetsd, minwindef, windef};
|
||||
use winapi::um::{commctrl, winuser};
|
||||
use winapi::{
|
||||
ctypes::c_void,
|
||||
shared::{
|
||||
basetsd, minwindef, windef,
|
||||
guiddef::REFIID,
|
||||
minwindef::{DWORD, UINT, ULONG},
|
||||
windef::{HWND, POINTL},
|
||||
winerror::S_OK,
|
||||
},
|
||||
um::{
|
||||
commctrl, winuser,
|
||||
objidl::IDataObject,
|
||||
oleidl::{IDropTarget, IDropTargetVtbl, DROPEFFECT_COPY, DROPEFFECT_NONE},
|
||||
shellapi, unknwnbase,
|
||||
winnt::HRESULT,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn initialize<T: 'static>(
|
||||
use std::{
|
||||
ptr,
|
||||
sync::atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
|
||||
use crate::menu::{Menu, MenuItem};
|
||||
use crate::{event::Event, window::WindowId as SuperWindowId};
|
||||
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MenuHandlerData {
|
||||
window: HWND,
|
||||
send_event: Box<dyn Fn(Event<'static, ()>)>,
|
||||
}
|
||||
|
||||
impl MenuHandlerData {
|
||||
fn send_event(&self, event: Event<'static, ()>) {
|
||||
(self.send_event)(event);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MenuHandler {
|
||||
pub data: *mut MenuHandlerData,
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
impl MenuHandler {
|
||||
pub fn new(window: HWND, send_event: Box<dyn Fn(Event<'static, ()>)>) -> MenuHandler {
|
||||
let data = Box::new(FileDropHandlerData {
|
||||
window,
|
||||
send_event,
|
||||
});
|
||||
MenuHandler {
|
||||
data: Box::into_raw(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn initialize(
|
||||
menu: Vec<Menu>,
|
||||
window_handle: RawWindowHandle,
|
||||
event_loop_runner: EventLoopRunnerShared<T>,
|
||||
menu_handler: MenuHandler,
|
||||
) {
|
||||
dbg!(menu);
|
||||
|
||||
commctrl::SetWindowSubclass(
|
||||
handle.hwnd as *mut _,
|
||||
Some(subclass_proc),
|
||||
0,
|
||||
sender as basetsd::DWORD_PTR,
|
||||
);
|
||||
|
||||
let testing_menu = winuser::CreateMenu();
|
||||
let subitem = winuser::MENUITEMINFOW {
|
||||
cbSize: std::mem::size_of::<winuser::MENUITEMINFOW>() as u32,
|
||||
fMask: winuser::MIIM_STRING | winuser::MIIM_ID,
|
||||
fType: winuser::MFT_STRING,
|
||||
fState: winuser::MFS_ENABLED,
|
||||
wID: 5, // Received on low-word of wParam when WM_COMMAND
|
||||
hSubMenu: std::ptr::null_mut(),
|
||||
hbmpChecked: std::ptr::null_mut(),
|
||||
hbmpUnchecked: std::ptr::null_mut(),
|
||||
dwItemData: 0,
|
||||
dwTypeData: to_wstring("&Close\tAlt+C").as_mut_ptr(),
|
||||
cch: 5,
|
||||
hbmpItem: std::ptr::null_mut(),
|
||||
};
|
||||
winuser::InsertMenuItemW(testing_menu, 0, 0, &subitem as *const _);
|
||||
|
||||
let system_menu = winuser::CreateMenu();
|
||||
let item = winuser::MENUITEMINFOW {
|
||||
cbSize: std::mem::size_of::<winuser::MENUITEMINFOW>() as u32,
|
||||
fMask: winuser::MIIM_STRING | winuser::MIIM_SUBMENU,
|
||||
fType: winuser::MFT_STRING,
|
||||
fState: winuser::MFS_ENABLED,
|
||||
wID: 0,
|
||||
hSubMenu: testing_menu,
|
||||
hbmpChecked: std::ptr::null_mut(),
|
||||
hbmpUnchecked: std::ptr::null_mut(),
|
||||
dwItemData: 0,
|
||||
dwTypeData: to_wstring("Outer").as_mut_ptr(),
|
||||
cch: 5,
|
||||
hbmpItem: std::ptr::null_mut(),
|
||||
};
|
||||
winuser::InsertMenuItemW(system_menu, 0, 0, &item as *const _);
|
||||
|
||||
winuser::SetMenu(handle.hwnd as *mut _, system_menu);
|
||||
if let RawWindowHandle::Windows(handle) = window_handle {
|
||||
commctrl::SetWindowSubclass(
|
||||
handle.hwnd as *mut _,
|
||||
Some(subclass_proc),
|
||||
0,
|
||||
sender as basetsd::DWORD_PTR,
|
||||
);
|
||||
|
||||
let testing_menu = winuser::CreateMenu();
|
||||
let subitem = winuser::MENUITEMINFOW {
|
||||
cbSize: std::mem::size_of::<winuser::MENUITEMINFOW>() as u32,
|
||||
fMask: winuser::MIIM_STRING | winuser::MIIM_ID,
|
||||
fType: winuser::MFT_STRING,
|
||||
fState: winuser::MFS_ENABLED,
|
||||
wID: 5, // Received on low-word of wParam when WM_COMMAND
|
||||
hSubMenu: std::ptr::null_mut(),
|
||||
hbmpChecked: std::ptr::null_mut(),
|
||||
hbmpUnchecked: std::ptr::null_mut(),
|
||||
dwItemData: 0,
|
||||
dwTypeData: to_wstring("&Close\tAlt+C").as_mut_ptr(),
|
||||
cch: 5,
|
||||
hbmpItem: std::ptr::null_mut(),
|
||||
};
|
||||
winuser::InsertMenuItemW(testing_menu, 0, 0, &subitem as *const _);
|
||||
|
||||
let system_menu = winuser::CreateMenu();
|
||||
let item = winuser::MENUITEMINFOW {
|
||||
cbSize: std::mem::size_of::<winuser::MENUITEMINFOW>() as u32,
|
||||
fMask: winuser::MIIM_STRING | winuser::MIIM_SUBMENU,
|
||||
fType: winuser::MFT_STRING,
|
||||
fState: winuser::MFS_ENABLED,
|
||||
wID: 0,
|
||||
hSubMenu: testing_menu,
|
||||
hbmpChecked: std::ptr::null_mut(),
|
||||
hbmpUnchecked: std::ptr::null_mut(),
|
||||
dwItemData: 0,
|
||||
dwTypeData: to_wstring("Outer").as_mut_ptr(),
|
||||
cch: 5,
|
||||
hbmpItem: std::ptr::null_mut(),
|
||||
};
|
||||
winuser::InsertMenuItemW(system_menu, 0, 0, &item as *const _);
|
||||
|
||||
winuser::SetMenu(handle.hwnd as *mut _, system_menu);
|
||||
}
|
||||
}
|
||||
|
||||
fn to_wstring(str: &str) -> Vec<u16> {
|
||||
|
||||
@@ -137,7 +137,7 @@ impl Window {
|
||||
|
||||
pub fn set_menu(&self, new_menu: Option<Vec<Menu>>) {
|
||||
if let Some(window_menu) = menu {
|
||||
menu::initialize(window_menu);
|
||||
//menu::initialize(window_menu);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -857,7 +857,17 @@ unsafe fn init<T: 'static>(
|
||||
if let Some(window_menu) = attributes.window_menu {
|
||||
let event_loop_runner = event_loop.runner_shared.clone();
|
||||
let window_handle = win.raw_window_handle();
|
||||
menu::initialize(window_menu, window_handle, event_loop_runner);
|
||||
|
||||
let menu_handler = MenuHandler::new(
|
||||
win.window.0,
|
||||
Box::new(move |event| {
|
||||
if let Ok(e) = event.map_nonuser_event() {
|
||||
event_loop_runner.send_event(e)
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
menu::initialize(window_menu, window_handle, menu_handler);
|
||||
}
|
||||
|
||||
Ok(win)
|
||||
|
||||
Reference in New Issue
Block a user