refactor(example): clean up wry example (#108)

This commit is contained in:
Linda_pp 2023-08-30 23:43:06 +09:00 committed by GitHub
parent bc4c86bfbb
commit 89154aede0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,7 +15,7 @@ use wry::application::platform::unix::WindowExtUnix;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
use wry::application::platform::windows::{EventLoopBuilderExtWindows, WindowExtWindows}; use wry::application::platform::windows::{EventLoopBuilderExtWindows, WindowExtWindows};
use wry::application::{ use wry::application::{
event::{ElementState, Event, MouseButton, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoopBuilder}, event_loop::{ControlFlow, EventLoopBuilder},
window::{Window, WindowBuilder}, window::{Window, WindowBuilder},
}; };
@ -50,13 +50,13 @@ fn main() -> wry::Result<()> {
.with_title("Window 2") .with_title("Window 2")
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
let window2_id = window2.id();
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
let app_m = Submenu::new("App", true); let app_m = Submenu::new("App", true);
menu_bar.append(&app_m); menu_bar.append(&app_m).unwrap();
app_m.append_items(&[ app_m
.append_items(&[
&PredefinedMenuItem::about(None, None), &PredefinedMenuItem::about(None, None),
&PredefinedMenuItem::separator(), &PredefinedMenuItem::separator(),
&PredefinedMenuItem::services(None), &PredefinedMenuItem::services(None),
@ -66,14 +66,17 @@ fn main() -> wry::Result<()> {
&PredefinedMenuItem::show_all(None), &PredefinedMenuItem::show_all(None),
&PredefinedMenuItem::separator(), &PredefinedMenuItem::separator(),
&PredefinedMenuItem::quit(None), &PredefinedMenuItem::quit(None),
]); ])
.unwrap();
} }
let file_m = Submenu::new("&File", true); let file_m = Submenu::new("&File", true);
let edit_m = Submenu::new("&Edit", true); let edit_m = Submenu::new("&Edit", true);
let window_m = Submenu::new("&Window", true); let window_m = Submenu::new("&Window", true);
menu_bar.append_items(&[&file_m, &edit_m, &window_m]); menu_bar
.append_items(&[&file_m, &edit_m, &window_m])
.unwrap();
let custom_i_1 = MenuItem::new( let custom_i_1 = MenuItem::new(
"C&ustom 1", "C&ustom 1",
@ -103,16 +106,19 @@ fn main() -> wry::Result<()> {
let cut_i = PredefinedMenuItem::cut(None); let cut_i = PredefinedMenuItem::cut(None);
let paste_i = PredefinedMenuItem::paste(None); let paste_i = PredefinedMenuItem::paste(None);
file_m.append_items(&[ file_m
.append_items(&[
&custom_i_1, &custom_i_1,
&image_item, &image_item,
&window_m, &window_m,
&PredefinedMenuItem::separator(), &PredefinedMenuItem::separator(),
&check_custom_i_1, &check_custom_i_1,
&check_custom_i_2, &check_custom_i_2,
]); ])
.unwrap();
window_m.append_items(&[ window_m
.append_items(&[
&PredefinedMenuItem::minimize(None), &PredefinedMenuItem::minimize(None),
&PredefinedMenuItem::maximize(None), &PredefinedMenuItem::maximize(None),
&PredefinedMenuItem::close_window(Some("Close")), &PredefinedMenuItem::close_window(Some("Close")),
@ -129,19 +135,32 @@ fn main() -> wry::Result<()> {
&check_custom_i_3, &check_custom_i_3,
&image_item, &image_item,
&custom_i_1, &custom_i_1,
]); ])
.unwrap();
edit_m.append_items(&[&copy_i, &PredefinedMenuItem::separator(), &paste_i]); edit_m
.append_items(&[
&copy_i,
&PredefinedMenuItem::separator(),
&cut_i,
&PredefinedMenuItem::separator(),
&paste_i,
])
.unwrap();
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
menu_bar.init_for_hwnd(window.hwnd() as _); menu_bar.init_for_hwnd(window.hwnd() as _).unwrap();
menu_bar.init_for_hwnd(window2.hwnd() as _); menu_bar.init_for_hwnd(window2.hwnd() as _).unwrap();
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
menu_bar.init_for_gtk_window(window.gtk_window(), window.default_vbox()); menu_bar
menu_bar.init_for_gtk_window(window2.gtk_window(), window2.default_vbox()); .init_for_gtk_window(window.gtk_window(), window.default_vbox())
.unwrap();
menu_bar
.init_for_gtk_window(window2.gtk_window(), window2.default_vbox())
.unwrap();
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
@ -205,7 +224,6 @@ fn main() -> wry::Result<()> {
.unwrap(); .unwrap();
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{
if let Some(menu_bar) = menu_bar if let Some(menu_bar) = menu_bar
.clone() .clone()
.gtk_menubar_for_gtk_window(window.gtk_window()) .gtk_menubar_for_gtk_window(window.gtk_window())
@ -213,7 +231,6 @@ fn main() -> wry::Result<()> {
use gtk::prelude::*; use gtk::prelude::*;
y += menu_bar.allocated_height(); y += menu_bar.allocated_height();
} }
}
show_context_menu( show_context_menu(
window, window,
@ -248,8 +265,11 @@ fn main() -> wry::Result<()> {
if let Ok(event) = menu_channel.try_recv() { if let Ok(event) = menu_channel.try_recv() {
if event.id == custom_i_1.id() { if event.id == custom_i_1.id() {
custom_i_1 custom_i_1
.set_accelerator(Some(Accelerator::new(Some(Modifiers::SHIFT), Code::KeyF))); .set_accelerator(Some(Accelerator::new(Some(Modifiers::SHIFT), Code::KeyF)))
file_m.insert(&MenuItem::new("New Menu Item", true, None), 2); .unwrap();
file_m
.insert(&MenuItem::new("New Menu Item", true, None), 2)
.unwrap();
} }
println!("{event:?}"); println!("{event:?}");
} }