electron/shell/browser/atom_browser_main_parts_mac.mm

86 lines
2.7 KiB
Text
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/atom_browser_main_parts.h"
#include "shell/browser/atom_paths.h"
#import "shell/browser/mac/atom_application.h"
#include "shell/browser/mac/atom_application_delegate.h"
namespace electron {
namespace {
base::scoped_nsobject<NSMenuItem> CreateMenuItem(NSString* title,
SEL action,
NSString* key_equivalent) {
return base::scoped_nsobject<NSMenuItem>([[NSMenuItem alloc]
initWithTitle:title
action:action
keyEquivalent:key_equivalent]);
}
// The App Menu refers to the dropdown titled "Electron".
base::scoped_nsobject<NSMenu> BuildAppMenu() {
// The title is not used, as the title will always be the name of the App.
base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]);
NSString* app_name = [[[NSBundle mainBundle] infoDictionary]
objectForKey:(id)kCFBundleNameKey];
base::scoped_nsobject<NSMenuItem> item =
CreateMenuItem([NSString stringWithFormat:@"Quit %@", app_name],
@selector(terminate:), @"q");
[menu addItem:item];
return menu;
}
base::scoped_nsobject<NSMenu> BuildEmptyMainMenu() {
base::scoped_nsobject<NSMenu> main_menu([[NSMenu alloc] initWithTitle:@""]);
using Builder = base::scoped_nsobject<NSMenu> (*)();
static const Builder kBuilderFuncs[] = {&BuildAppMenu};
for (auto* builder : kBuilderFuncs) {
NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:@""
action:NULL
keyEquivalent:@""] autorelease];
item.submenu = builder();
[main_menu addItem:item];
}
return main_menu;
}
} // namespace
void AtomBrowserMainParts::PreMainMessageLoopStart() {
// Set our own application delegate.
2014-11-16 15:04:31 +00:00
AtomApplicationDelegate* delegate = [[AtomApplicationDelegate alloc] init];
2016-08-05 20:57:33 +00:00
[NSApp setDelegate:delegate];
PreMainMessageLoopStartCommon();
// Prevent Cocoa from turning command-line arguments into
// |-application:openFiles:|, since we already handle them directly.
[[NSUserDefaults standardUserDefaults]
2018-04-20 18:47:04 +00:00
setObject:@"NO"
forKey:@"NSTreatUnknownArgumentsAsOpen"];
}
void AtomBrowserMainParts::FreeAppDelegate() {
[[NSApp delegate] release];
[NSApp setDelegate:nil];
}
void AtomBrowserMainParts::RegisterURLHandler() {
[[AtomApplication sharedApplication] registerURLHandler];
}
void AtomBrowserMainParts::InitializeEmptyApplicationMenu() {
base::scoped_nsobject<NSMenu> main_menu = BuildEmptyMainMenu();
[[NSApplication sharedApplication] setMainMenu:main_menu];
}
} // namespace electron