Skip to content

Commit 34d7d50

Browse files
committed
[OSX] menu 'show' event; set key and modifiers
Conflicts: nw.gypi src/api/menu/menu.h src/api/menu/menu_mac.mm src/browser/app_controller_mac.mm
1 parent 6af72e0 commit 34d7d50

File tree

11 files changed

+132
-11
lines changed

11 files changed

+132
-11
lines changed

nw.gypi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@
184184
'src/api/menu/menu.h',
185185
'src/api/menu/menu_delegate.cc',
186186
'src/api/menu/menu_delegate.h',
187+
'src/api/menu/menu_delegate_mac.h',
188+
'src/api/menu/menu_delegate_mac.mm',
187189
'src/api/menu/menu_mac.mm',
188190
'src/api/menu/menu_views.cc',
189191
'src/api/menuitem/menuitem.cc',

src/api/menu/menu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
#if defined(OS_MACOSX)
3636
#if __OBJC__
3737
@class NSMenu;
38+
@class NWMenuDelegate;
3839
#else
3940
class NSMenu;
41+
class NWMenuDelegate;
4042
#endif // __OBJC__
4143

4244
namespace nw {
@@ -118,6 +120,7 @@ class Menu : public Base {
118120
#if defined(OS_MACOSX)
119121
friend class nw::NativeWindowCocoa;
120122
NSMenu* menu_;
123+
NWMenuDelegate* menu_delegate_;
121124
#elif defined(OS_LINUX)
122125
friend class nw::NativeWindowAura;
123126

src/api/menu/menu_delegate_mac.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2012 Intel Corp
2+
// Copyright (c) 2012 The Chromium Authors
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
8+
// pies of the Software, and to permit persons to whom the Software is furnished
9+
// to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in al
12+
// l copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
15+
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
16+
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
17+
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
18+
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
21+
#ifndef CONTENT_NW_SRC_API_MENU_MENU_DELEGATE_MAC_H_
22+
#define CONTENT_NW_SRC_API_MENU_MENU_DELEGATE_MAC_H_
23+
24+
#import <Cocoa/Cocoa.h>
25+
26+
namespace nwapi {
27+
class Menu;
28+
}
29+
30+
@interface NWMenuDelegate : NSObject<NSMenuDelegate> {
31+
@private
32+
nwapi::Menu* nwmenu_;
33+
}
34+
35+
- (id)initWithMenu:(nwapi::Menu*)menu;
36+
37+
@end
38+
#endif // CONTENT_NW_SRC_API_MENU_MENU_DELEGATE_MAC_H_

src/api/menu/menu_delegate_mac.mm

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2012 Intel Corp
2+
// Copyright (c) 2012 The Chromium Authors
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
8+
// pies of the Software, and to permit persons to whom the Software is furnished
9+
// to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in al
12+
// l copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
15+
// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
16+
// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
17+
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
18+
// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
21+
#include "content/nw/src/api/dispatcher_host.h"
22+
#include "content/nw/src/api/menu/menu.h"
23+
#include "content/nw/src/api/menu/menu_delegate_mac.h"
24+
#include "content/nw/src/browser/native_window.h"
25+
#include "content/nw/src/nw_shell.h"
26+
27+
@implementation NWMenuDelegate
28+
29+
- (id)initWithMenu:(nwapi::Menu*) menu {
30+
if ((self = [super init])) {
31+
nwmenu_ = menu;
32+
}
33+
return self;
34+
}
35+
36+
- (void)menuNeedsUpdate:(NSMenu*)menu {
37+
base::ListValue args;
38+
nwmenu_->dispatcher_host()->SendEvent(nwmenu_, "show", args);
39+
}
40+
41+
@end

src/api/menu/menu_mac.mm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "base/values.h"
2626
#import <Cocoa/Cocoa.h>
2727
#include "content/public/browser/web_contents.h"
28+
#include "content/nw/src/api/dispatcher_host.h"
29+
#include "content/nw/src/api/menu/menu_delegate_mac.h"
2830
#include "content/nw/src/api/menuitem/menuitem.h"
2931
#include "content/nw/src/browser/native_window_mac.h"
3032
#include "content/nw/src/nw_shell.h"
@@ -34,10 +36,13 @@
3436
void Menu::Create(const base::DictionaryValue& option) {
3537
menu_ = [[NSMenu alloc] initWithTitle:@"NW Menu"];
3638
[menu_ setAutoenablesItems:NO];
39+
menu_delegate_ = [[NWMenuDelegate alloc] initWithMenu:this];
40+
[menu_ setDelegate:menu_delegate_];
3741
}
3842

3943
void Menu::Destroy() {
4044
[menu_ release];
45+
[menu_delegate_ release];
4146
}
4247

4348
void Menu::Append(MenuItem* menu_item) {

src/api/menuitem/menuitem.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ void MenuItem::Call(const std::string& method,
7070
int object_id = 0;
7171
arguments.GetInteger(0, &object_id);
7272
SetSubmenu(dispatcher_host()->GetApiObject<Menu>(object_id));
73+
} else if (method == "SetKey") {
74+
std::string key;
75+
arguments.GetString(0, &key);
76+
SetKey(key);
77+
} else if (method == "SetModifiers") {
78+
std::string mod;
79+
arguments.GetString(0, &mod);
80+
SetModifiers(mod);
7381
} else {
7482
NOTREACHED() << "Invalid call to MenuItem method:" << method
7583
<< " arguments:" << arguments;

src/api/menuitem/menuitem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class MenuItem : public Base {
7878
void SetIcon(const std::string& icon);
7979
void SetTooltip(const std::string& tooltip);
8080
void SetKey(const std::string& key);
81+
void SetModifiers(const std::string& modifiers);
8182
void SetEnabled(bool enabled);
8283
void SetChecked(bool checked);
8384
void SetSubmenu(Menu* sub_menu);

src/api/menuitem/menuitem.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ MenuItem.prototype.__defineSetter__('tooltip', function(val) {
137137
this.handleSetter('tooltip', 'SetTooltip', String, val);
138138
});
139139

140+
MenuItem.prototype.__defineGetter__('key', function() {
141+
return this.handleGetter('key');
142+
});
143+
144+
MenuItem.prototype.__defineSetter__('key', function(val) {
145+
this.handleSetter('key', 'SetKey', String, val);
146+
});
147+
148+
MenuItem.prototype.__defineGetter__('modifiers', function() {
149+
return this.handleGetter('modifiers');
150+
});
151+
152+
MenuItem.prototype.__defineSetter__('modifiers', function(val) {
153+
this.handleSetter('modifiers', 'SetModifiers', String, val);
154+
});
155+
140156
MenuItem.prototype.__defineGetter__('checked', function() {
141157
if (this.type != 'checkbox')
142158
return undefined;

src/api/menuitem/menuitem_mac.mm

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,7 @@
8989

9090
std::string modifiers;
9191
if (option.GetString("modifiers", &modifiers)) {
92-
NSUInteger mask = 0;
93-
NSString* nsmodifiers = [NSString stringWithUTF8String:modifiers.c_str()];
94-
if([nsmodifiers rangeOfString:@"shift"].location != NSNotFound)
95-
mask = mask|NSShiftKeyMask;
96-
if([nsmodifiers rangeOfString:@"cmd"].location != NSNotFound)
97-
mask = mask|NSCommandKeyMask;
98-
if([nsmodifiers rangeOfString:@"alt"].location != NSNotFound)
99-
mask = mask|NSAlternateKeyMask;
100-
if([nsmodifiers rangeOfString:@"ctrl"].location != NSNotFound)
101-
mask = mask|NSControlKeyMask;
102-
[menu_item_ setKeyEquivalentModifierMask:mask];
92+
SetModifiers(modifiers);
10393
}
10494

10595
int menu_id;
@@ -129,6 +119,21 @@
129119

130120
void MenuItem::SetKey(const std::string& key) {
131121
[menu_item_ setKeyEquivalent:[NSString stringWithUTF8String:key.c_str()]];
122+
VLOG(1) << "setkey: " << key;
123+
}
124+
125+
void MenuItem::SetModifiers(const std::string& modifiers) {
126+
NSUInteger mask = 0;
127+
NSString* nsmodifiers = [NSString stringWithUTF8String:modifiers.c_str()];
128+
if([nsmodifiers rangeOfString:@"shift"].location != NSNotFound)
129+
mask = mask|NSShiftKeyMask;
130+
if([nsmodifiers rangeOfString:@"cmd"].location != NSNotFound)
131+
mask = mask|NSCommandKeyMask;
132+
if([nsmodifiers rangeOfString:@"alt"].location != NSNotFound)
133+
mask = mask|NSAlternateKeyMask;
134+
if([nsmodifiers rangeOfString:@"ctrl"].location != NSNotFound)
135+
mask = mask|NSControlKeyMask;
136+
[menu_item_ setKeyEquivalentModifierMask:mask];
132137
}
133138

134139
void MenuItem::SetIcon(const std::string& icon) {

src/browser/native_window_mac.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
@class ShellNSWindow;
3232
@class ShellToolbarDelegate;
33+
@class NWMenuDelegate;
3334
class SkRegion;
3435

3536
namespace nw {

src/browser/native_window_mac.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#import "chrome/browser/ui/cocoa/custom_frame_view.h"
2727
#import "ui/base/cocoa/nsview_additions.h"
2828
#include "content/nw/src/api/menu/menu.h"
29+
#include "content/nw/src/api/menu/menu_delegate_mac.h"
2930
#include "content/nw/src/api/app/app.h"
3031
#include "content/nw/src/browser/chrome_event_processing_window.h"
3132
#include "content/nw/src/browser/native_window_helper_mac.h"

0 commit comments

Comments
 (0)