Skip to content

Commit 04ae819

Browse files
committed
[OSX] menu proposal from Chris
1 parent dcddd48 commit 04ae819

File tree

10 files changed

+89
-48
lines changed

10 files changed

+89
-48
lines changed

src/api/menu/menu_mac.mm

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2012 Intel Corp
22
// Copyright (c) 2012 The Chromium Authors
3-
//
4-
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
88
// pies of the Software, and to permit persons to whom the Software is furnished
99
// to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in al
1212
// l copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
1515
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
1616
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
@@ -35,13 +35,6 @@
3535
void Menu::Create(const base::DictionaryValue& option) {
3636
menu_ = [[NSMenu alloc] initWithTitle:@"NW Menu"];
3737
[menu_ setAutoenablesItems:NO];
38-
39-
std::string type;
40-
if (option.GetString("type", &type) && type == "menubar") {
41-
// Preserve the apple menu.
42-
[menu_ addItem:[[[NSMenuItem alloc]
43-
initWithTitle:@"" action:nil keyEquivalent:@""] autorelease]];
44-
}
4538
}
4639

4740
void Menu::Destroy() {

src/api/menuitem/menuitem.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2012 Intel Corp
22
// Copyright (c) 2012 The Chromium Authors
3-
//
4-
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
88
// pies of the Software, and to permit persons to whom the Software is furnished
99
// to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in al
1212
// l copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
1515
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
1616
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
@@ -19,7 +19,7 @@
1919
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020

2121
#ifndef CONTENT_NW_SRC_API_MENUITEM_MENUITEM_H_
22-
#define CONTENT_NW_SRC_API_MENUITEM_MENUITEM_H_
22+
#define CONTENT_NW_SRC_API_MENUITEM_MENUITEM_H_
2323

2424
#include "base/compiler_specific.h"
2525
#include "content/nw/src/api/base/base.h"
@@ -69,6 +69,7 @@ class MenuItem : public Base {
6969
void SetLabel(const std::string& label);
7070
void SetIcon(const std::string& icon);
7171
void SetTooltip(const std::string& tooltip);
72+
void SetKey(const std::string& key);
7273
void SetEnabled(bool enabled);
7374
void SetChecked(bool checked);
7475
void SetSubmenu(Menu* sub_menu);

src/api/menuitem/menuitem.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ function MenuItem(option) {
8383
option.tooltip = '';
8484
if (!option.hasOwnProperty('enabled'))
8585
option.enabled = true;
86+
if (!option.hasOwnProperty('key'))
87+
option.key = "";
88+
if (!option.hasOwnProperty('modifiers'))
89+
option.modifiers = "";
8690
}
8791
require('util').inherits(MenuItem, exports.Base);
8892

@@ -123,14 +127,14 @@ MenuItem.prototype.__defineSetter__('tooltip', function(val) {
123127
MenuItem.prototype.__defineGetter__('checked', function() {
124128
if (this.type != 'checkbox')
125129
return undefined;
126-
130+
127131
return this.handleGetter('checked');
128132
});
129133

130134
MenuItem.prototype.__defineSetter__('checked', function(val) {
131135
if (this.type != 'checkbox')
132136
throw new String("'checked' property is only available for checkbox");
133-
137+
134138
this.handleSetter('checked', 'SetChecked', Boolean, val);
135139
});
136140

src/api/menuitem/menuitem_delegate_mac.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2012 Intel Corp
22
// Copyright (c) 2012 The Chromium Authors
3-
//
4-
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
88
// pies of the Software, and to permit persons to whom the Software is furnished
99
// to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in al
1212
// l copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
1515
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
1616
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS

src/api/menuitem/menuitem_mac.mm

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2012 Intel Corp
22
// Copyright (c) 2012 The Chromium Authors
3-
//
4-
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
88
// pies of the Software, and to permit persons to whom the Software is furnished
99
// to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in al
1212
// l copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
1515
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
1616
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
@@ -43,13 +43,23 @@
4343
std::string label;
4444
option.GetString("label", &label);
4545

46-
menu_item_ = [[NSMenuItem alloc]
47-
initWithTitle:[NSString stringWithUTF8String:label.c_str()]
48-
action: @selector(invoke:)
49-
keyEquivalent: @""];
50-
51-
delegate_ = [[MenuItemDelegate alloc] initWithMenuItem:this];
52-
[menu_item_ setTarget:delegate_];
46+
std::string selector;
47+
option.GetString("selector", &selector);
48+
49+
if(!selector.empty()) {
50+
menu_item_ = [[NSMenuItem alloc]
51+
initWithTitle:[NSString stringWithUTF8String:label.c_str()]
52+
action: NSSelectorFromString([NSString stringWithUTF8String:selector.c_str()])
53+
keyEquivalent: @""];
54+
delegate_ = [MenuItemDelegate alloc];
55+
} else {
56+
menu_item_ = [[NSMenuItem alloc]
57+
initWithTitle:[NSString stringWithUTF8String:label.c_str()]
58+
action: @selector(invoke:)
59+
keyEquivalent: @""];
60+
delegate_ = [[MenuItemDelegate alloc] initWithMenuItem:this];
61+
[menu_item_ setTarget:delegate_];
62+
}
5363

5464
if (type == "checkbox") {
5565
bool checked = false;
@@ -69,6 +79,25 @@
6979
if (option.GetString("tooltip", &tooltip))
7080
SetTooltip(tooltip);
7181

82+
std::string key;
83+
if (option.GetString("key", &key))
84+
SetKey(key);
85+
86+
std::string modifiers;
87+
if (option.GetString("modifiers", &modifiers)) {
88+
NSUInteger mask = 0;
89+
NSString* nsmodifiers = [NSString stringWithUTF8String:modifiers.c_str()];
90+
if([nsmodifiers rangeOfString:@"shift"].location != NSNotFound)
91+
mask = mask|NSShiftKeyMask;
92+
if([nsmodifiers rangeOfString:@"cmd"].location != NSNotFound)
93+
mask = mask|NSCommandKeyMask;
94+
if([nsmodifiers rangeOfString:@"alt"].location != NSNotFound)
95+
mask = mask|NSAlternateKeyMask;
96+
if([nsmodifiers rangeOfString:@"ctrl"].location != NSNotFound)
97+
mask = mask|NSControlKeyMask;
98+
[menu_item_ setKeyEquivalentModifierMask:mask];
99+
}
100+
72101
int menu_id;
73102
if (option.GetInteger("submenu", &menu_id))
74103
SetSubmenu(dispatcher_host()->GetApiObject<Menu>(menu_id));
@@ -94,6 +123,10 @@
94123
[menu_item_ setTitle:[NSString stringWithUTF8String:label.c_str()]];
95124
}
96125

126+
void MenuItem::SetKey(const std::string& key) {
127+
[menu_item_ setKeyEquivalent:[NSString stringWithUTF8String:key.c_str()]];
128+
}
129+
97130
void MenuItem::SetIcon(const std::string& icon) {
98131
if (!icon.empty()) {
99132
NSImage* image = [[NSImage alloc]

src/api/window/window.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ void Window::Call(const std::string& method,
257257
int id;
258258
if (arguments.GetInteger(0, &id))
259259
shell_->window()->SetMenu(dispatcher_host()->GetApiObject<Menu>(id));
260+
} else if (method == "ClearMenu") {
261+
shell_->window()->ClearMenu();
260262
} else if (method == "Reload") {
261263
int type;
262264
if (arguments.GetInteger(0, &type))

src/api/window_bindings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ Window.prototype.__defineGetter__('zoomLevel', function() {
212212
});
213213

214214
Window.prototype.__defineSetter__('menu', function(menu) {
215+
if(!menu) {
216+
CallObjectMethod(this, "ClearMenu", []);
217+
return;
218+
}
215219
if (v8_util.getConstructorName(menu) != 'Menu')
216220
throw new String("'menu' property requries a valid Menu");
217221

src/browser/native_window.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2012 Intel Corp
22
// Copyright (c) 2012 The Chromium Authors
3-
//
4-
// Permission is hereby granted, free of charge, to any person obtaining a copy
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
88
// pies of the Software, and to permit persons to whom the Software is furnished
99
// to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in al
1212
// l copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
1515
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
1616
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
@@ -94,6 +94,7 @@ class NativeWindow {
9494
virtual void SetKiosk(bool kiosk) = 0;
9595
virtual bool IsKiosk() = 0;
9696
virtual void SetMenu(nwapi::Menu* menu) = 0;
97+
virtual void ClearMenu() = 0;
9798
virtual void SetInitialFocus(bool accept_focus) = 0;
9899
virtual bool InitialFocus() = 0;
99100

src/browser/native_window_mac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class NativeWindowCocoa : public NativeWindow {
6666
virtual void SetKiosk(bool kiosk) OVERRIDE;
6767
virtual bool IsKiosk() OVERRIDE;
6868
virtual void SetMenu(nwapi::Menu* menu) OVERRIDE;
69+
virtual void ClearMenu() OVERRIDE;
6970
virtual void SetToolbarButtonEnabled(TOOLBAR_BUTTON button,
7071
bool enabled) OVERRIDE;
7172
virtual void SetToolbarUrlEntry(const std::string& url) OVERRIDE;

src/browser/native_window_mac.mm

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
631631
if (kiosk) {
632632
NSApplicationPresentationOptions options =
633633
NSApplicationPresentationHideDock +
634-
NSApplicationPresentationHideMenuBar +
634+
NSApplicationPresentationHideMenuBar +
635635
NSApplicationPresentationDisableAppleMenu +
636636
NSApplicationPresentationDisableProcessSwitching +
637637
NSApplicationPresentationDisableForceQuit +
@@ -652,15 +652,17 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
652652
}
653653

654654
void NativeWindowCocoa::SetMenu(nwapi::Menu* menu) {
655-
bool no_edit_menu = false;
656-
shell_->GetPackage()->root()->GetBoolean("no-edit-menu", &no_edit_menu);
655+
if(menu == nil) {
656+
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
657+
[NSApp setMainMenu:menu];
658+
} else {
659+
[NSApp setMainMenu:menu->menu_];
660+
}
661+
}
657662

658-
StandardMenusMac standard_menus(shell_->GetPackage()->GetName());
659-
[NSApp setMainMenu:menu->menu_];
660-
standard_menus.BuildAppleMenu();
661-
if (!no_edit_menu)
662-
standard_menus.BuildEditMenu();
663-
standard_menus.BuildWindowMenu();
663+
void NativeWindowCocoa::ClearMenu() {
664+
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
665+
[NSApp setMainMenu:menu];
664666
}
665667

666668
void NativeWindowCocoa::SetInitialFocus(bool accept_focus) {
@@ -718,7 +720,7 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
718720
if (toolbar_delegate_)
719721
[toolbar_delegate_ setUrl:base::SysUTF8ToNSString(url)];
720722
}
721-
723+
722724
void NativeWindowCocoa::SetToolbarIsLoading(bool loading) {
723725
if (toolbar_delegate_)
724726
[toolbar_delegate_ setIsLoading:loading];
@@ -772,7 +774,7 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
772774
event.type == content::NativeWebKeyboardEvent::Char)
773775
return;
774776

775-
777+
776778
DVLOG(1) << "NativeWindowCocoa::HandleKeyboardEvent - redispatch";
777779

778780
// // The event handling to get this strictly right is a tangle; cheat here a bit

0 commit comments

Comments
 (0)