Skip to content

Commit ca2db0c

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 0b2dabc commit ca2db0c

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
@@ -171,6 +171,8 @@
171171
'src/api/menu/menu.h',
172172
'src/api/menu/menu_delegate.cc',
173173
'src/api/menu/menu_delegate.h',
174+
'src/api/menu/menu_delegate_mac.h',
175+
'src/api/menu/menu_delegate_mac.mm',
174176
'src/api/menu/menu_mac.mm',
175177
'src/api/menu/menu_views.cc',
176178
'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
@@ -66,6 +66,14 @@ void MenuItem::Call(const std::string& method,
6666
int object_id = 0;
6767
arguments.GetInteger(0, &object_id);
6868
SetSubmenu(dispatcher_host()->GetApiObject<Menu>(object_id));
69+
} else if (method == "SetKey") {
70+
std::string key;
71+
arguments.GetString(0, &key);
72+
SetKey(key);
73+
} else if (method == "SetModifiers") {
74+
std::string mod;
75+
arguments.GetString(0, &mod);
76+
SetModifiers(mod);
6977
} else {
7078
NOTREACHED() << "Invalid call to MenuItem method:" << method
7179
<< " 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
@@ -124,6 +124,22 @@ MenuItem.prototype.__defineSetter__('tooltip', function(val) {
124124
this.handleSetter('tooltip', 'SetTooltip', String, val);
125125
});
126126

127+
MenuItem.prototype.__defineGetter__('key', function() {
128+
return this.handleGetter('key');
129+
});
130+
131+
MenuItem.prototype.__defineSetter__('key', function(val) {
132+
this.handleSetter('key', 'SetKey', String, val);
133+
});
134+
135+
MenuItem.prototype.__defineGetter__('modifiers', function() {
136+
return this.handleGetter('modifiers');
137+
});
138+
139+
MenuItem.prototype.__defineSetter__('modifiers', function(val) {
140+
this.handleSetter('modifiers', 'SetModifiers', String, val);
141+
});
142+
127143
MenuItem.prototype.__defineGetter__('checked', function() {
128144
if (this.type != 'checkbox')
129145
return undefined;

src/api/menuitem/menuitem_mac.mm

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

8686
std::string modifiers;
8787
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];
88+
SetModifiers(modifiers);
9989
}
10090

10191
int menu_id;
@@ -125,6 +115,21 @@
125115

126116
void MenuItem::SetKey(const std::string& key) {
127117
[menu_item_ setKeyEquivalent:[NSString stringWithUTF8String:key.c_str()]];
118+
VLOG(1) << "setkey: " << key;
119+
}
120+
121+
void MenuItem::SetModifiers(const std::string& modifiers) {
122+
NSUInteger mask = 0;
123+
NSString* nsmodifiers = [NSString stringWithUTF8String:modifiers.c_str()];
124+
if([nsmodifiers rangeOfString:@"shift"].location != NSNotFound)
125+
mask = mask|NSShiftKeyMask;
126+
if([nsmodifiers rangeOfString:@"cmd"].location != NSNotFound)
127+
mask = mask|NSCommandKeyMask;
128+
if([nsmodifiers rangeOfString:@"alt"].location != NSNotFound)
129+
mask = mask|NSAlternateKeyMask;
130+
if([nsmodifiers rangeOfString:@"ctrl"].location != NSNotFound)
131+
mask = mask|NSControlKeyMask;
132+
[menu_item_ setKeyEquivalentModifierMask:mask];
128133
}
129134

130135
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 {

0 commit comments

Comments
 (0)