update exmaples

This commit is contained in:
amrbashir 2022-05-05 17:30:35 +02:00
parent cff1103cf4
commit b5598554a0
No known key found for this signature in database
GPG key ID: BBD7A47A2003FF33
2 changed files with 74 additions and 19 deletions

View file

@ -8,8 +8,14 @@ use tao::{
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::WindowBuilder, window::WindowBuilder,
}; };
enum UserEvent {
MenuEvent(u64),
}
fn main() { fn main() {
let event_loop = EventLoop::new(); let event_loop = EventLoop::<UserEvent>::with_user_event();
let window = WindowBuilder::new().build(&event_loop).unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap();
let window2 = WindowBuilder::new().build(&event_loop).unwrap(); let window2 = WindowBuilder::new().build(&event_loop).unwrap();
@ -17,10 +23,14 @@ fn main() {
let mut file_menu = menu_bar.add_submenu("File", true); let mut file_menu = menu_bar.add_submenu("File", true);
let mut edit_menu = menu_bar.add_submenu("Edit", true); let mut edit_menu = menu_bar.add_submenu("Edit", true);
let _open_item = file_menu.add_text_item("Open", true, |_| {}); let mut open_item = file_menu.add_text_item("Open", true, |_| {});
let _save_item = file_menu.add_text_item("Save", true, |i| {
i.set_enabled(false); let proxy = event_loop.create_proxy();
i.set_label("Save disabled"); let mut counter = 0;
let save_item = file_menu.add_text_item("Save", true, move |i| {
counter += 1;
i.set_label(format!("Save triggered {} times", counter));
let _ = proxy.send_event(UserEvent::MenuEvent(i.id()));
}); });
let _quit_item = file_menu.add_text_item("Quit", true, |_| {}); let _quit_item = file_menu.add_text_item("Quit", true, |_| {});
@ -28,12 +38,17 @@ fn main() {
let _cut_item = edit_menu.add_text_item("Cut", true, |_| {}); let _cut_item = edit_menu.add_text_item("Cut", true, |_| {});
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{
menu_bar.init_for_hwnd(window.hwnd() as _); menu_bar.init_for_hwnd(window.hwnd() as _);
menu_bar.init_for_hwnd(window2.hwnd() as _);
}
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{
menu_bar.init_for_gtk_window(window.gtk_window()); menu_bar.init_for_gtk_window(window.gtk_window());
#[cfg(target_os = "linux")]
menu_bar.init_for_gtk_window(window2.gtk_window()); menu_bar.init_for_gtk_window(window2.gtk_window());
}
let mut open_item_disabled = false;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
@ -45,6 +60,20 @@ fn main() {
Event::MainEventsCleared => { Event::MainEventsCleared => {
window.request_redraw(); window.request_redraw();
} }
Event::UserEvent(e) => match e {
UserEvent::MenuEvent(id) => {
if id == save_item.id() {
println!("Save menu item triggered");
if !open_item_disabled {
println!("Open item disabled!");
open_item.set_enabled(false);
open_item_disabled = true;
}
}
}
},
_ => (), _ => (),
} }
}) })

View file

@ -6,18 +6,29 @@ use winit::{
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::WindowBuilder, window::WindowBuilder,
}; };
enum UserEvent {
MenuEvent(u64),
}
fn main() { fn main() {
let event_loop = EventLoop::new(); let event_loop = EventLoop::<UserEvent>::with_user_event();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let _window = WindowBuilder::new().build(&event_loop).unwrap();
let _window2 = WindowBuilder::new().build(&event_loop).unwrap();
let mut menu_bar = Menu::new(); let mut menu_bar = Menu::new();
let mut file_menu = menu_bar.add_submenu("File", true); let mut file_menu = menu_bar.add_submenu("File", true);
let mut edit_menu = menu_bar.add_submenu("Edit", true); let mut edit_menu = menu_bar.add_submenu("Edit", true);
let _open_item = file_menu.add_text_item("Open", true, |_| {}); let mut open_item = file_menu.add_text_item("Open", true, |_| {});
let _save_item = file_menu.add_text_item("Save", true, |i| {
i.set_enabled(false); let proxy = event_loop.create_proxy();
i.set_label("Save disabled"); let mut counter = 0;
let save_item = file_menu.add_text_item("Save", true, move |i| {
counter += 1;
i.set_label(format!("Save triggered {} times", counter));
let _ = proxy.send_event(UserEvent::MenuEvent(i.id()));
}); });
let _quit_item = file_menu.add_text_item("Quit", true, |_| {}); let _quit_item = file_menu.add_text_item("Quit", true, |_| {});
@ -25,8 +36,12 @@ fn main() {
let _cut_item = edit_menu.add_text_item("Cut", true, |_| {}); let _cut_item = edit_menu.add_text_item("Cut", true, |_| {});
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
menu_bar.init_for_hwnd(window.hwnd() as _); {
menu_bar.init_for_hwnd(_window.hwnd() as _);
menu_bar.init_for_hwnd(_window2.hwnd() as _);
}
let mut open_item_disabled = false;
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
@ -35,9 +50,20 @@ fn main() {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
} => *control_flow = ControlFlow::Exit, } => *control_flow = ControlFlow::Exit,
Event::MainEventsCleared => {
window.request_redraw(); Event::UserEvent(e) => match e {
UserEvent::MenuEvent(id) => {
if id == save_item.id() {
println!("Save menu item triggered");
if !open_item_disabled {
println!("Open item disabled!");
open_item.set_enabled(false);
open_item_disabled = true;
} }
}
}
},
_ => (), _ => (),
} }
}) })