This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
forked from rust-windowing/glutin
-
Notifications
You must be signed in to change notification settings - Fork 33
cocoa: Add some bare-bones menus on the Mac to conform better to the Apple Human Interface Guidelines. #88
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
||
|
@@ -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}; | ||
|
@@ -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 { | ||
|
@@ -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"))); }, | ||
|
@@ -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(); | ||
info_dictionary.set_value( | ||
NSString::alloc(nil).init_str("CFBundleName") as *const _, | ||
NSString::alloc(nil).init_str(app_name) as *const _); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -378,6 +394,9 @@ impl Window { | |
} | ||
} | ||
app.finishLaunching(); | ||
|
||
Window::create_menus(app_name); | ||
|
||
Some(app) | ||
} | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the Service menu actually working? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?