Skip to content

Commit

Permalink
fix unknown menu item error when clicking 'About' item
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Aug 17, 2024
1 parent cf1ddab commit 53741cb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions v2/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub enum MenuItem {
EditConfig,
#[cfg(not(target_os = "macos"))]
ToggleMenuBar,
About,
}

pub trait RawMessageWriter {
Expand Down
1 change: 1 addition & 0 deletions v2/src/shiba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ where
Help => self.renderer.send_message(MessageToRenderer::Help)?,
OpenRepo => self.opener.open("https://github.com/rhysd/Shiba")?,
EditConfig => self.open_config()?,
About => unreachable!("'About' menu event is never emitted for now"),
}
Ok(RenderingFlow::Continue)
}
Expand Down
20 changes: 12 additions & 8 deletions v2/src/wry/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn metadata() -> AboutMetadata {

#[cfg(not(target_os = "macos"))]
{
m.authors = Some(vec![env!("CARGO_PKG_AUTHORS").into()]);
m.authors = Some(vec![env!("CARGO_PKG_AUTHORS").into()]); // This implementation is only correct when only one person is listed in `authors` array.
m.comments = Some(env!("CARGO_PKG_DESCRIPTION").into());
m.license = Some(env!("CARGO_PKG_LICENSE").into());
m.website = Some(env!("CARGO_PKG_HOMEPAGE").into());
Expand Down Expand Up @@ -58,10 +58,11 @@ impl MenuEvents {
let Ok(event) = self.receiver.try_recv() else {
return Ok(None);
};
let Some(id) = self.ids.get(&event.id).copied() else {
anyhow::bail!("Unknown menu item ID in event {:?}: {:?}", event, self.ids);
};
Ok(Some(id))
match self.ids.get(&event.id).copied() {
Some(AppMenuItem::About) => Ok(None), // This is predefined item and was already handled by OS
Some(id) => Ok(Some(id)),
None => anyhow::bail!("Unknown menu item ID in event {:?}: {:?}", event, self.ids),
}
}
}

Expand Down Expand Up @@ -112,6 +113,8 @@ impl Menu {
let always_on_top = no_accel("Pin/Unpin On Top");
let guide = no_accel("Show Guide…");
let open_repo = no_accel("Open Repository Page");
// Though this is a predefined item, `MenuEvent` is emitted when the item is clicked
let about = PredefinedMenuItem::about(Some("About Shiba"), Some(metadata()));

// Menu bar structure
let window_menu = Submenu::with_items(
Expand Down Expand Up @@ -142,7 +145,7 @@ impl Menu {
"Shiba",
true,
&[
&PredefinedMenuItem::about(Some("About Shiba"), Some(metadata())),
&about,
&PredefinedMenuItem::separator(),
&settings,
&PredefinedMenuItem::separator(),
Expand All @@ -167,7 +170,7 @@ impl Menu {
#[cfg(not(target_os = "macos"))]
&PredefinedMenuItem::separator(),
#[cfg(not(target_os = "macos"))]
&PredefinedMenuItem::about(Some("About Shiba"), Some(metadata())),
&about,
#[cfg(target_os = "windows")]
&PredefinedMenuItem::separator(),
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -236,8 +239,9 @@ impl Menu {
(guide.into_id(), Help),
(open_repo.into_id(), OpenRepo),
(settings.into_id(), EditConfig),
(about.into_id(), About),
#[cfg(not(target_os = "macos"))]
(toggle_menu_bar.into_id(), ToggleMenuBar),
(toggle_menu_bar.into_id(), ToggleMenuBar),
]
});

Expand Down

0 comments on commit 53741cb

Please sign in to comment.