Skip to content

Commit

Permalink
fix(menus): add app name to native Quit and Hide items on macOS, closes
Browse files Browse the repository at this point in the history
#536 (#570)

* feat(menus): add app name to native Quit and Hide items

* lint: run cargo fmt

* refactor(menus): cleaner app name implementation

* chore(menus): add changefile

* chore(menus): change changefile

* refactor(menus): keep original app_name function signature

* refactor(menus): add helper function `app_name_string`
  • Loading branch information
caesar committed Sep 25, 2022
1 parent d343abf commit 65f768e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changes/macos-menus-appname.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Add the application name to the "Quit" and "Hide" native menu items on macOS.
1 change: 1 addition & 0 deletions examples/custom_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ fn main() {
// add `my_sub_menu` children of `first_menu` with `Sub menu` title
first_menu.add_submenu("Sub menu", true, my_sub_menu);
first_menu.add_native_item(MenuItem::CloseWindow);
first_menu.add_native_item(MenuItem::Hide);
first_menu.add_native_item(MenuItem::Quit);

// create custom item `Selected and disabled` children of `second_menu`
Expand Down
9 changes: 6 additions & 3 deletions src/platform_impl/macos/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use crate::{
};

use super::{
app_state::AppState, event::EventWrapper, util::ns_string_to_rust, window::get_window_id,
app_state::AppState,
event::EventWrapper,
util::{app_name_string, ns_string_to_rust},
window::get_window_id,
};

static BLOCK_PTR: &str = "taoMenuItemBlockPtr";
Expand Down Expand Up @@ -197,7 +200,7 @@ impl Menu {
MenuItem::Quit => Some((
None,
make_menu_item(
"Quit",
format!("Quit {}", unsafe { app_name_string() }.unwrap_or_default()).trim(),
Some(selector("terminate:")),
Some(Accelerator::new(RawMods::Meta, KeyCode::KeyQ)),
menu_type,
Expand All @@ -206,7 +209,7 @@ impl Menu {
MenuItem::Hide => Some((
None,
make_menu_item(
"Hide",
format!("Hide {}", unsafe { app_name_string() }.unwrap_or_default()).trim(),
Some(selector("hide:")),
Some(Accelerator::new(RawMods::Meta, KeyCode::KeyH)),
menu_type,
Expand Down
14 changes: 9 additions & 5 deletions src/platform_impl/macos/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,23 @@ pub unsafe fn ns_string_to_rust(ns_string: id) -> String {
string.to_owned()
}

#[allow(dead_code)] // In case we want to use this function in the future
/// Gets the app's name from the `localizedName` property of `NSRunningApplication`
pub unsafe fn app_name() -> Option<id> {
let bundle: id = msg_send![class!(NSBundle), mainBundle];
let dict: id = msg_send![bundle, infoDictionary];
let key = ns_string_id_ref("CFBundleName");
let app_name: id = msg_send![dict, objectForKey:*key];
let app_class = class!(NSRunningApplication);
let app: id = msg_send![app_class, currentApplication];
let app_name: id = msg_send![app, localizedName];
if app_name != nil {
Some(app_name)
} else {
None
}
}

/// Gets the app's name as a `String` from the `localizedName` property of `NSRunningApplication`
pub unsafe fn app_name_string() -> Option<String> {
app_name().map(|name| ns_string_to_rust(name))
}

pub unsafe fn superclass<'a>(this: &'a Object) -> &'a Class {
let superclass: *const Class = msg_send![this, superclass];
&*superclass
Expand Down

0 comments on commit 65f768e

Please sign in to comment.