Skip to content

Commit

Permalink
implement MenuItem from cocoa example (https://github.com/servo/core-…
Browse files Browse the repository at this point in the history
  • Loading branch information
hacknus committed Jan 26, 2024
1 parent 6b0782c commit 3ab6281
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions crates/eframe/src/native/app_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,14 @@ fn set_app_icon_windows(icon_data: &IconData) -> AppIconStatus {
/// Set icon & app title for `MacOS` applications.
#[cfg(target_os = "macos")]
#[allow(unsafe_code)]
fn set_title_and_icon_mac(title: &str, icon_data: Option<&IconData>) -> AppIconStatus {
fn set_title_and_icon_mac(_title: &str, icon_data: Option<&IconData>) -> AppIconStatus {
use crate::icon_data::IconDataExt as _;
crate::profile_function!();

use cocoa::{
appkit::{NSApp, NSApplication, NSImage, NSMenu, NSWindow},
base::{id, nil},
foundation::{NSData, NSString},
appkit::{NSApp, NSApplication, NSImage, NSMenu, NSMenuItem},
base::{nil, selector},
foundation::{NSData, NSString, NSAutoreleasePool, NSProcessInfo},
};
use objc::{msg_send, sel, sel_impl};

let png_bytes = if let Some(icon_data) = icon_data {
match icon_data.to_png_bytes() {
Expand Down Expand Up @@ -246,17 +244,25 @@ fn set_title_and_icon_mac(title: &str, icon_data: Option<&IconData>) -> AppIconS
}

// Change the title in the top bar - for python processes this would be again "python" otherwise.
let main_menu = app.mainMenu();
if !main_menu.is_null() {
let item = main_menu.itemAtIndex_(0);
if !item.is_null() {
let app_menu: id = msg_send![item, submenu];
if !app_menu.is_null() {
crate::profile_scope!("setTitle_");
app_menu.setTitle_(NSString::alloc(nil).init_str(title));
}
}
}

// create Menu Bar
let menubar = NSMenu::new(nil).autorelease();
let app_menu_item = NSMenuItem::new(nil).autorelease();
menubar.addItem_(app_menu_item);
app.setMainMenu_(menubar);

// create Application menu
let app_menu = NSMenu::new(nil).autorelease();
let quit_prefix = NSString::alloc(nil).init_str("Quit ");
let quit_title =
quit_prefix.stringByAppendingString_(NSProcessInfo::processInfo(nil).processName());
let quit_action = selector("terminate:");
let quit_key = NSString::alloc(nil).init_str("q");
let quit_item = NSMenuItem::alloc(nil)
.initWithTitle_action_keyEquivalent_(quit_title, quit_action, quit_key)
.autorelease();
app_menu.addItem_(quit_item);
app_menu_item.setSubmenu_(app_menu);

// The title in the Dock apparently can't be changed.
// At least these people didn't figure it out either:
Expand Down

0 comments on commit 3ab6281

Please sign in to comment.