Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

cocoa: Add some bare-bones menus on the Mac to conform better to the Apple Human Interface Guidelines. #88

Merged
merged 2 commits into from
May 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 112 additions & 2 deletions src/api/cocoa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use cocoa::appkit::NSEventSubtype::*;

use core_foundation::base::TCFType;
use core_foundation::string::CFString;
use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName};
use core_foundation::bundle::{CFBundle, CFBundleGetBundleWithIdentifier};
use core_foundation::bundle::{CFBundleGetFunctionPointerForName};

use core_graphics::display::{CGAssociateMouseAndMouseCursorPosition, CGMainDisplayID, CGDisplayPixelsHigh, CGWarpMouseCursorPosition};

Expand All @@ -43,6 +44,7 @@ use std::sync::Mutex;
use std::ascii::AsciiExt;
use std::ops::Deref;
use std::path::PathBuf;
use std::env;

use events::ElementState::{Pressed, Released};
use events::Event::{Awakened, MouseInput, MouseMoved, ReceivedCharacter, KeyboardInput};
Expand Down Expand Up @@ -190,6 +192,7 @@ impl Drop for WindowDelegate {
#[derive(Default)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub activation_policy: ActivationPolicy,
pub app_name: Option<String>,
}

pub struct Window {
Expand Down Expand Up @@ -301,6 +304,7 @@ impl Window {
}

let app = match Window::create_app(pl_attribs.activation_policy,
pl_attribs.app_name.as_ref().map(|name| &**name),
win_attribs.icon.clone()) {
Some(app) => app,
None => { return Err(OsError(format!("Couldn't create NSApplication"))); },
Expand Down Expand Up @@ -361,13 +365,25 @@ impl Window {
Ok(window)
}

fn create_app(activation_policy: ActivationPolicy, icon_path: Option<PathBuf>) -> Option<id> {
fn create_app(activation_policy: ActivationPolicy,
app_name: Option<&str>,
icon_path: Option<PathBuf>)
-> Option<id> {
unsafe {
let app = NSApp();
if app == nil {
None
} else {
app.setActivationPolicy_(activation_policy.into());

// Set `CFBundleName` appropriately.
if let Some(app_name) = app_name {
let info_dictionary = CFBundle::main_bundle().info_dictionary();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did that require a change in core-foundation-rs?

info_dictionary.set_value(
NSString::alloc(nil).init_str("CFBundleName") as *const _,
NSString::alloc(nil).init_str(app_name) as *const _);
}

Copy link

@paulrouget paulrouget May 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if that is not overridden by the entry in info.plist, it might give some headache to the person who is going to work on the .app packaging. Make sure to mention this commit in servo/servo#9918.

if let Some(icon_path) = icon_path {
if let Some(icon_path) = icon_path.to_str() {
let icon_path = NSString::alloc(nil).init_str(icon_path);
Expand All @@ -378,6 +394,9 @@ impl Window {
}
}
app.finishLaunching();

Window::create_menus(app_name);

Some(app)
}
}
Expand Down Expand Up @@ -599,6 +618,97 @@ impl Window {
}
}

fn create_menus(app_name: Option<&str>) {
unsafe {
let main_menu = NSMenu::alloc(nil).init();

let app_name = match app_name {
None => {
match env::current_exe().ok().and_then(|path| {
path.file_name().and_then(|name| name.to_str()
.map(|name| name.to_owned()))
}) {
None => "Glutin".to_owned(),
Some(name) => name,
}
}
Some(name) => name.to_owned(),
};
let application_menu_name = NSString::alloc(nil).init_str(&app_name);

let application_menu = NSMenu::alloc(nil).initWithTitle_(application_menu_name);
let empty_string = NSString::alloc(nil).init_str("");
application_menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("About ")
.stringByAppendingString_(application_menu_name),
sel!(orderFrontStandardAboutPanel:),
empty_string);
application_menu.addItem_(NSMenuItem::separatorItem(nil));
let services_string = NSString::alloc(nil).init_str("Services");
let menu = NSMenu::alloc(nil).initWithTitle_(services_string);
let item = NSMenuItem::alloc(nil).init();
item.setTitle_(services_string);
item.setSubmenu_(menu);
application_menu.addItem_(item);
NSApp().setServicesMenu_(menu);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the Service menu actually working?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but the HIG require it to be present I believe.

application_menu.addItem_(NSMenuItem::separatorItem(nil));
application_menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("Hide ")
.stringByAppendingString_(application_menu_name),
sel!(hide:),
NSString::alloc(nil).init_str("h"));
let item = application_menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("Hide Others"),
sel!(hideOtherApplications:),
NSString::alloc(nil).init_str("h"));
item.setKeyEquivalentModifierMask_(NSCommandKeyMask | NSAlternateKeyMask);
application_menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("Show All"),
sel!(unhideAllApplications:),
empty_string);
application_menu.addItem_(NSMenuItem::separatorItem(nil));
application_menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("Quit ")
.stringByAppendingString_(application_menu_name),
sel!(terminate:),
NSString::alloc(nil).init_str("q"));
let item = NSMenuItem::alloc(nil).init();
item.setTitle_(application_menu_name);
item.setSubmenu_(application_menu);
main_menu.addItem_(item);

let view_string = NSString::alloc(nil).init_str("View");
let menu = NSMenu::alloc(nil).initWithTitle_(view_string);
let item = menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("Enter Full Screen"),
sel!(toggleFullScreen:),
NSString::alloc(nil).init_str("f"));
item.setKeyEquivalentModifierMask_(NSCommandKeyMask | NSControlKeyMask);
let item = NSMenuItem::alloc(nil).init();
item.setTitle_(view_string);
item.setSubmenu_(menu);
main_menu.addItem_(item);

let window_string = NSString::alloc(nil).init_str("Window");
let menu = NSMenu::alloc(nil).initWithTitle_(window_string);
menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("Minimize"),
sel!(performMiniaturize:),
NSString::alloc(nil).init_str("m"));
menu.addItemWithTitle_action_keyEquivalent(
NSString::alloc(nil).init_str("Zoom"),
sel!(performZoom:),
empty_string);
let item = NSMenuItem::alloc(nil).init();
item.setTitle_(window_string);
item.setSubmenu_(menu);
main_menu.addItem_(item);

NSApp().setMainMenu_(main_menu);
NSApp().setWindowsMenu_(menu);
}
}

pub fn set_title(&self, title: &str) {
unsafe {
let title = IdRef::new(NSString::alloc(nil).init_str(title));
Expand Down
8 changes: 8 additions & 0 deletions src/os/macos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl From<ActivationPolicy> for NSApplicationActivationPolicy {
/// Additional methods on `WindowBuilder` that are specific to MacOS.
pub trait WindowBuilderExt<'a> {
fn with_activation_policy(mut self, activation_policy: ActivationPolicy) -> WindowBuilder<'a>;
fn with_app_name(mut self, app_name: String) -> WindowBuilder<'a>;
}

impl<'a> WindowBuilderExt<'a> for WindowBuilder<'a> {
Expand All @@ -62,4 +63,11 @@ impl<'a> WindowBuilderExt<'a> for WindowBuilder<'a> {
self.platform_specific.activation_policy = activation_policy;
self
}

/// Sets the name of the application in the menu bar and Dock
#[inline]
fn with_app_name(mut self, app_name: String) -> WindowBuilder<'a> {
self.platform_specific.app_name = Some(app_name);
self
}
}