From 88d352033ba571126a11bc681ee3b346b7579916 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Mon, 6 Mar 2023 15:21:36 +0200 Subject: [PATCH] fix(Windows): dispatch menu event for icon menu item (#53) * fix(Windows): dispatch menu event for icon menu item * default dispatch to true and improve about dialog --- .changes/icon-menu-item-dispatch.md | 5 ++ src/platform_impl/windows/mod.rs | 83 ++++++++++++++++------------- 2 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 .changes/icon-menu-item-dispatch.md diff --git a/.changes/icon-menu-item-dispatch.md b/.changes/icon-menu-item-dispatch.md new file mode 100644 index 0000000..abed92d --- /dev/null +++ b/.changes/icon-menu-item-dispatch.md @@ -0,0 +1,5 @@ +--- +"muda": "patch" +--- + +On Windows, fix `MenuEvent` not triggered for `IconMenuItem`. diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index b5f1126..1178cc3 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -1074,17 +1074,17 @@ unsafe extern "system" fn menu_subclass_proc( if let Some(item) = item { ret = 0; - let mut dispatch = false; + let mut dispatch = true; { let mut item = item.borrow_mut(); - match item.type_ { - MenuItemType::Normal => { - dispatch = true; - } - MenuItemType::Check => { - dispatch = true; + if item.type_ == MenuItemType::Predefined { + dispatch = false; + } + + match item.type_ { + MenuItemType::Check => { let checked = !item.checked; item.set_checked(checked); } @@ -1112,35 +1112,46 @@ unsafe extern "system" fn menu_subclass_proc( PostQuitMessage(0); } PredfinedMenuItemType::About(Some(metadata)) => { - MessageBoxW( - hwnd, - encode_wide(format!( - r#" - {} -version: {} -authors: {} -license: {} -website: {} {} - {} - {} - "#, - metadata.name.as_deref().unwrap_or_default(), - metadata.version.as_deref().unwrap_or_default(), - metadata.authors.as_deref().unwrap_or_default().join(","), - metadata.license.as_deref().unwrap_or_default(), - metadata.website_label.as_deref().unwrap_or_default(), - metadata.website.as_deref().unwrap_or_default(), - metadata.comments.as_deref().unwrap_or_default(), - metadata.copyright.as_deref().unwrap_or_default(), - )) - .as_ptr(), - encode_wide(format!( - "About {}", - metadata.name.as_deref().unwrap_or_default() - )) - .as_ptr(), - MB_ICONINFORMATION, - ); + use std::fmt::Write; + + let mut message = String::new(); + if let Some(name) = &metadata.name { + let _ = writeln!(&mut message, "Name: {}", name); + } + if let Some(version) = &metadata.version { + let _ = writeln!(&mut message, "Version: {}", version); + } + if let Some(authors) = &metadata.authors { + let _ = writeln!(&mut message, "Authors: {}", authors.join(", ")); + } + if let Some(license) = &metadata.license { + let _ = writeln!(&mut message, "License: {}", license); + } + match (&metadata.website_label, &metadata.website) { + (Some(label), None) => { + let _ = writeln!(&mut message, "Website: {}", label); + } + (None, Some(url)) => { + let _ = writeln!(&mut message, "Website: {}", url); + } + (Some(label), Some(url)) => { + let _ = writeln!(&mut message, "Website: {} {}", label, url); + } + _ => {} + } + if let Some(comments) = &metadata.comments { + let _ = writeln!(&mut message, "\n{}", comments); + } + if let Some(copyright) = &metadata.copyright { + let _ = writeln!(&mut message, "\n{}", copyright); + } + + let message = encode_wide(message); + let title = encode_wide(format!( + "About {}", + metadata.name.as_deref().unwrap_or_default() + )); + MessageBoxW(hwnd, message.as_ptr(), title.as_ptr(), MB_ICONINFORMATION); } _ => {}