Skip to content

Commit 12419eb

Browse files
committed
[OSX] menu proposal from Chris
make ClearMenu non-pure virtual as well
1 parent 95488e9 commit 12419eb

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"
@@ -102,6 +102,7 @@ class MenuItem : public Base {
102102
void SetLabel(const std::string& label);
103103
void SetIcon(const std::string& icon);
104104
void SetTooltip(const std::string& tooltip);
105+
void SetKey(const std::string& key);
105106
void SetEnabled(bool enabled);
106107
void SetChecked(bool checked);
107108
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
@@ -265,6 +265,8 @@ void Window::Call(const std::string& method,
265265
int id;
266266
if (arguments.GetInteger(0, &id))
267267
shell_->window()->SetMenu(dispatcher_host()->GetApiObject<Menu>(id));
268+
} else if (method == "ClearMenu") {
269+
shell_->window()->ClearMenu();
268270
} else if (method == "Reload") {
269271
int type;
270272
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
@@ -96,6 +96,7 @@ class NativeWindow {
9696
virtual void SetKiosk(bool kiosk) = 0;
9797
virtual bool IsKiosk() = 0;
9898
virtual void SetMenu(nwapi::Menu* menu) = 0;
99+
virtual void ClearMenu() {}
99100
virtual void SetInitialFocus(bool accept_focus) = 0;
100101
virtual bool InitialFocus() = 0;
101102

src/browser/native_window_mac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class NativeWindowCocoa : public NativeWindow {
6868
virtual void SetKiosk(bool kiosk) OVERRIDE;
6969
virtual bool IsKiosk() OVERRIDE;
7070
virtual void SetMenu(nwapi::Menu* menu) OVERRIDE;
71+
virtual void ClearMenu() OVERRIDE;
7172
virtual void SetToolbarButtonEnabled(TOOLBAR_BUTTON button,
7273
bool enabled) OVERRIDE;
7374
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
@@ -651,7 +651,7 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
651651
if (kiosk) {
652652
NSApplicationPresentationOptions options =
653653
NSApplicationPresentationHideDock +
654-
NSApplicationPresentationHideMenuBar +
654+
NSApplicationPresentationHideMenuBar +
655655
NSApplicationPresentationDisableAppleMenu +
656656
NSApplicationPresentationDisableProcessSwitching +
657657
NSApplicationPresentationDisableForceQuit +
@@ -672,15 +672,17 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
672672
}
673673

674674
void NativeWindowCocoa::SetMenu(nwapi::Menu* menu) {
675-
bool no_edit_menu = false;
676-
shell_->GetPackage()->root()->GetBoolean("no-edit-menu", &no_edit_menu);
675+
if(menu == nil) {
676+
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
677+
[NSApp setMainMenu:menu];
678+
} else {
679+
[NSApp setMainMenu:menu->menu_];
680+
}
681+
}
677682

678-
StandardMenusMac standard_menus(shell_->GetPackage()->GetName());
679-
[NSApp setMainMenu:menu->menu_];
680-
standard_menus.BuildAppleMenu();
681-
if (!no_edit_menu)
682-
standard_menus.BuildEditMenu();
683-
standard_menus.BuildWindowMenu();
683+
void NativeWindowCocoa::ClearMenu() {
684+
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
685+
[NSApp setMainMenu:menu];
684686
}
685687

686688
void NativeWindowCocoa::SetInitialFocus(bool accept_focus) {
@@ -738,7 +740,7 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
738740
if (toolbar_delegate_)
739741
[toolbar_delegate_ setUrl:base::SysUTF8ToNSString(url)];
740742
}
741-
743+
742744
void NativeWindowCocoa::SetToolbarIsLoading(bool loading) {
743745
if (toolbar_delegate_)
744746
[toolbar_delegate_ setIsLoading:loading];
@@ -792,7 +794,7 @@ - (NSRect)contentRectForFrameRect:(NSRect)frameRect {
792794
event.type == content::NativeWebKeyboardEvent::Char)
793795
return;
794796

795-
797+
796798
DVLOG(1) << "NativeWindowCocoa::HandleKeyboardEvent - redispatch";
797799

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

0 commit comments

Comments
 (0)