-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.rs
78 lines (69 loc) · 2.56 KB
/
menu.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use winit::platform::windows::WindowBuilderExtWindows;
use winapi::um::winuser::{CreateMenu, AppendMenuW};
use winapi::um::winuser::{MF_STRING, MF_SEPARATOR, MF_POPUP};
use winapi::shared::basetsd::UINT_PTR;
const IDM_FILE_NEW: u16 = 1;
const IDM_FILE_OPEN: u16 = 2;
const IDM_FILE_QUIT: u16 = 3;
fn convert_widestring(input: &str) -> Vec<u16> {
let mut v: Vec<u16> = input.chars().filter_map(|s| {
use std::convert::TryInto;
(s as u32).try_into().ok()
}).collect();
v.push(0);
v
}
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
let menu = unsafe { CreateMenu() };
unsafe {
AppendMenuW(menu, MF_STRING, IDM_FILE_NEW.into(), convert_widestring("&New").as_ptr());
AppendMenuW(menu, MF_STRING, IDM_FILE_OPEN.into(), convert_widestring("&Open").as_ptr());
AppendMenuW(menu, MF_SEPARATOR, 0, core::ptr::null_mut());
AppendMenuW(menu, MF_STRING, IDM_FILE_QUIT.into(), convert_widestring("&Quit").as_ptr());
}
let menubar = unsafe { CreateMenu() };
unsafe {
AppendMenuW(menubar, MF_POPUP, menu as UINT_PTR, convert_widestring("&File").as_ptr());
}
let window = WindowBuilder::new()
.with_title("A fantastic window!")
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
.with_menu(menubar)
.build(&event_loop)
.unwrap();
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { event, window_id } if window_id == window.id() => {
match event {
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
},
WindowEvent::Command(IDM_FILE_NEW) => {
println!("MENU: new file clicked!");
},
WindowEvent::Command(IDM_FILE_OPEN) => {
println!("MENU: open clicked!");
},
WindowEvent::Command(IDM_FILE_QUIT) => {
println!("MENU: quit clicked!");
*control_flow = ControlFlow::Exit;
},
_ => { },
}
},
Event::MainEventsCleared => {
window.request_redraw();
}
_ => (),
}
});
}