2022-06-10 22:09:56 +10:00
|
|
|
use muda::{menu_event_receiver, Menu, NativeMenuItem};
|
2022-06-07 21:05:20 +10:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
use winit::platform::macOS::EventLoopExtMacOS;
|
2022-05-05 21:50:22 +10:00
|
|
|
#[cfg(target_os = "windows")]
|
2022-06-07 21:05:20 +10:00
|
|
|
use winit::platform::windows::{EventLoopBuilderExtWindows, WindowExtWindows};
|
2022-05-05 21:50:22 +10:00
|
|
|
use winit::{
|
|
|
|
event::{Event, WindowEvent},
|
2022-06-07 21:05:20 +10:00
|
|
|
event_loop::{ControlFlow, EventLoopBuilder},
|
2022-05-05 21:50:22 +10:00
|
|
|
window::WindowBuilder,
|
|
|
|
};
|
2022-05-06 01:30:35 +10:00
|
|
|
|
2022-05-05 21:50:22 +10:00
|
|
|
fn main() {
|
2022-06-07 21:05:20 +10:00
|
|
|
let mut event_loop_builder = EventLoopBuilder::new();
|
|
|
|
|
|
|
|
let mut menu_bar = Menu::new();
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
{
|
2022-06-08 02:32:10 +10:00
|
|
|
let menu_bar_c = menu_bar.clone();
|
2022-06-07 21:05:20 +10:00
|
|
|
event_loop_builder.with_msg_hook(move |msg| {
|
|
|
|
use windows_sys::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, MSG};
|
|
|
|
unsafe {
|
|
|
|
let msg = msg as *mut MSG;
|
|
|
|
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar_c.haccel(), msg);
|
|
|
|
translated == 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-06-08 02:32:10 +10:00
|
|
|
|
2022-06-07 21:05:20 +10:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut event_loop = event_loop_builder.build();
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
event_loop.enable_default_menu_creation(false);
|
2022-05-06 01:30:35 +10:00
|
|
|
|
2022-05-07 00:25:45 +10:00
|
|
|
let window = WindowBuilder::new().build(&event_loop).unwrap();
|
2022-05-07 02:38:39 +10:00
|
|
|
let _window2 = WindowBuilder::new().build(&event_loop).unwrap();
|
2022-05-05 21:50:22 +10:00
|
|
|
|
2022-06-07 21:05:20 +10:00
|
|
|
let mut file_menu = menu_bar.add_submenu("&File", true);
|
2022-06-10 22:09:56 +10:00
|
|
|
let mut open_item = file_menu.add_text_item("&Open", true, None);
|
2022-06-07 21:05:20 +10:00
|
|
|
let mut save_item = file_menu.add_text_item("&Save", true, Some("CommandOrCtrl+S"));
|
2022-06-10 22:09:56 +10:00
|
|
|
file_menu.add_native_item(NativeMenuItem::Minimize);
|
|
|
|
file_menu.add_native_item(NativeMenuItem::CloseWindow);
|
|
|
|
file_menu.add_native_item(NativeMenuItem::Quit);
|
2022-05-05 21:50:22 +10:00
|
|
|
|
2022-06-10 22:09:56 +10:00
|
|
|
let mut edit_menu = menu_bar.add_submenu("&Edit", true);
|
|
|
|
edit_menu.add_native_item(NativeMenuItem::Cut);
|
|
|
|
edit_menu.add_native_item(NativeMenuItem::Copy);
|
|
|
|
edit_menu.add_native_item(NativeMenuItem::Paste);
|
|
|
|
edit_menu.add_native_item(NativeMenuItem::SelectAll);
|
2022-05-05 21:50:22 +10:00
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
2022-05-06 01:30:35 +10:00
|
|
|
{
|
2022-05-07 00:25:45 +10:00
|
|
|
menu_bar.init_for_hwnd(window.hwnd() as _);
|
2022-05-07 02:38:39 +10:00
|
|
|
menu_bar.init_for_hwnd(_window2.hwnd() as _);
|
2022-05-06 01:30:35 +10:00
|
|
|
}
|
2022-05-05 21:50:22 +10:00
|
|
|
|
2022-06-08 02:32:10 +10:00
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
{
|
|
|
|
menu_bar.init_for_nsapp();
|
|
|
|
}
|
|
|
|
|
2022-05-07 00:25:45 +10:00
|
|
|
let menu_channel = menu_event_receiver();
|
2022-05-06 01:30:35 +10:00
|
|
|
let mut open_item_disabled = false;
|
2022-05-07 02:38:39 +10:00
|
|
|
let mut counter = 0;
|
2022-05-07 00:25:45 +10:00
|
|
|
|
2022-05-05 21:50:22 +10:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
2022-06-10 22:09:56 +10:00
|
|
|
match event {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
Event::NewEvents(winit::event::StartCause::Init) => {
|
|
|
|
menu_bar.init_for_nsapp();
|
|
|
|
}
|
|
|
|
Event::WindowEvent {
|
|
|
|
event: WindowEvent::CloseRequested,
|
|
|
|
..
|
|
|
|
} => *control_flow = ControlFlow::Exit,
|
|
|
|
Event::MainEventsCleared => {
|
|
|
|
window.request_redraw();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
2022-05-07 00:25:45 +10:00
|
|
|
if let Ok(event) = menu_channel.try_recv() {
|
|
|
|
match event.id {
|
|
|
|
_ if event.id == save_item.id() => {
|
2022-05-07 06:57:58 +10:00
|
|
|
println!("Save menu item activated!");
|
2022-05-07 02:38:39 +10:00
|
|
|
counter += 1;
|
2022-06-07 21:05:20 +10:00
|
|
|
save_item.set_label(format!("&Save activated {counter} times"));
|
2022-05-07 02:38:39 +10:00
|
|
|
|
2022-05-07 00:25:45 +10:00
|
|
|
if !open_item_disabled {
|
|
|
|
println!("Open item disabled!");
|
|
|
|
open_item.set_enabled(false);
|
|
|
|
open_item_disabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2022-05-05 21:50:22 +10:00
|
|
|
})
|
|
|
|
}
|