From b5598554a01c3553e95e9556229dd44c2afb7081 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Thu, 5 May 2022 17:30:35 +0200 Subject: [PATCH] update exmaples --- examples/tao.rs | 47 ++++++++++++++++++++++++++++++++++++++--------- examples/winit.rs | 46 ++++++++++++++++++++++++++++++++++++---------- 2 files changed, 74 insertions(+), 19 deletions(-) diff --git a/examples/tao.rs b/examples/tao.rs index 25c7d6c..0d1dabd 100644 --- a/examples/tao.rs +++ b/examples/tao.rs @@ -8,8 +8,14 @@ use tao::{ event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; + +enum UserEvent { + MenuEvent(u64), +} + fn main() { - let event_loop = EventLoop::new(); + let event_loop = EventLoop::::with_user_event(); + let window = 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 edit_menu = menu_bar.add_submenu("Edit", true); - let _open_item = file_menu.add_text_item("Open", true, |_| {}); - let _save_item = file_menu.add_text_item("Save", true, |i| { - i.set_enabled(false); - i.set_label("Save disabled"); + let mut open_item = file_menu.add_text_item("Open", true, |_| {}); + + let proxy = event_loop.create_proxy(); + 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, |_| {}); @@ -28,12 +38,17 @@ fn main() { let _cut_item = edit_menu.add_text_item("Cut", true, |_| {}); #[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")] - 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(window.gtk_window()); + menu_bar.init_for_gtk_window(window2.gtk_window()); + } + let mut open_item_disabled = false; event_loop.run(move |event, _, control_flow| { *control_flow = ControlFlow::Wait; @@ -45,6 +60,20 @@ fn main() { 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; + } + } + } + }, _ => (), } }) diff --git a/examples/winit.rs b/examples/winit.rs index 8c9a9ab..e134538 100644 --- a/examples/winit.rs +++ b/examples/winit.rs @@ -6,18 +6,29 @@ use winit::{ event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, }; + +enum UserEvent { + MenuEvent(u64), +} + fn main() { - let event_loop = EventLoop::new(); - let window = WindowBuilder::new().build(&event_loop).unwrap(); + let event_loop = EventLoop::::with_user_event(); + + let _window = WindowBuilder::new().build(&event_loop).unwrap(); + let _window2 = WindowBuilder::new().build(&event_loop).unwrap(); let mut menu_bar = Menu::new(); let mut file_menu = menu_bar.add_submenu("File", true); let mut edit_menu = menu_bar.add_submenu("Edit", true); - let _open_item = file_menu.add_text_item("Open", true, |_| {}); - let _save_item = file_menu.add_text_item("Save", true, |i| { - i.set_enabled(false); - i.set_label("Save disabled"); + let mut open_item = file_menu.add_text_item("Open", true, |_| {}); + + let proxy = event_loop.create_proxy(); + 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, |_| {}); @@ -25,8 +36,12 @@ fn main() { let _cut_item = edit_menu.add_text_item("Cut", true, |_| {}); #[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| { *control_flow = ControlFlow::Wait; @@ -35,9 +50,20 @@ fn main() { event: WindowEvent::CloseRequested, .. } => *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; + } + } + } + }, _ => (), } })