Skip to content

Commit

Permalink
[osx] update menuitems after setting main menu
Browse files Browse the repository at this point in the history
On Mac, the system will add several menu items to Edit menu, like
`Emoji & Symbols` or `Start Dictation...`. These items are added
after setting main menu. This patch will update menu items after
setting main menu. Then developers can iterate items under edit
menu and remove corresponding items.

fixed #2812
  • Loading branch information
Cong Liu committed Jul 20, 2016
1 parent 69a472a commit 043e1f5
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 17 deletions.
2 changes: 2 additions & 0 deletions nw.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
}],
['OS=="mac"', {
'sources': [
'src/api/base/base_mac.h',
'src/api/base/base_mac.mm',
'src/api/nw_window_api_mac.mm',
'src/api/nw_menu_api_mac.mm',
'src/api/menuitem/menuitem_mac.mm',
Expand Down
10 changes: 10 additions & 0 deletions src/api/base/base_mac.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef CONTENT_NW_SRC_API_BASE_BASE_MAC_H_
#define CONTENT_NW_SRC_API_BASE_BASE_MAC_H_

#import <Foundation/Foundation.h>

@interface NSObject (AssociatedObject)
@property (nonatomic, assign) void* associatedObject;
@end

#endif // CONTENT_NW_SRC_API_BASE_BASE_MAC_H_
53 changes: 53 additions & 0 deletions src/api/base/base_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#import "content/nw/src/api/base/base_mac.h"
#import <objc/runtime.h>

@interface CppWrapper : NSObject {
void* _obj;
}
+ (id) createFromCppObject:(void*) obj;
- (id) initWithCppObject:(void*) obj;
- (void*) obj;
@end

@implementation CppWrapper

+ (id) createFromCppObject:(void*) obj {
return [[CppWrapper alloc] initWithCppObject:obj];
}

- (id) initWithCppObject:(void*) obj {
_obj = obj;
return self;
}

- (void*) obj {
return _obj;
}

@end

@implementation NSObject (AssociatedObject)
@dynamic associatedObject;

- (void)setAssociatedObject:(void*)object {
objc_setAssociatedObject(self, @selector(associatedObject), [CppWrapper createFromCppObject:object], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void*)associatedObject {
CppWrapper* wrapper = objc_getAssociatedObject(self, @selector(associatedObject));
return wrapper == nil ? nil : [wrapper obj];
}

// - (void)setAssociatedCppObject:(void*)obj {
// [self setAssociatedObject: [CppWrapper createFromCppObject:obj]];
// }

// - (void*)associatedCppObject {
// id obj = [self associatedObject];
// if ([obj isKindOfClass: [CppWrapper class]]) {
// return [(CppWrapper*)obj obj];
// }
// return nullptr;
// }

@end
4 changes: 4 additions & 0 deletions src/api/menu/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ class Menu : public Base {
const base::ListValue& arguments,
content::RenderFrameHost* rvh = nullptr) override;

#if defined(OS_MACOSX)
static Menu* GetMenuFromNative(NSMenu* menu);
#endif

#if defined(OS_WIN) || defined(OS_LINUX)
void UpdateKeys(views::FocusManager *focus_manager);
ui::NwMenuModel* model() { return menu_model_.get(); }
Expand Down
7 changes: 7 additions & 0 deletions src/api/menu/menu_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@
#include "content/public/browser/web_contents.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/render_frame_host.h"
#include "content/nw/src/api/base/base_mac.h"
#include "content/nw/src/api/object_manager.h"
#include "content/nw/src/api/menu/menu_delegate_mac.h"
#include "content/nw/src/api/menuitem/menuitem.h"

namespace nw {

// static
Menu* Menu::GetMenuFromNative(NSMenu* menu) {
return (Menu*)[menu associatedObject];
}

void Menu::Create(const base::DictionaryValue& option) {
menu_ = [[NSMenu alloc] initWithTitle:@"NW Menu"];
[menu_ setAutoenablesItems:NO];
[menu_ setAllowsContextMenuPlugIns:NO];
menu_delegate_ = [[NWMenuDelegate alloc] initWithMenu:this];
[menu_ setDelegate:menu_delegate_];
[menu_ setAssociatedObject: this];
}

void Menu::Destroy() {
Expand Down
6 changes: 6 additions & 0 deletions src/api/menuitem/menuitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ class MenuItem : public Base {
const base::ListValue& arguments,
base::ListValue* result) override;

#if defined(OS_MACOSX)
static std::unique_ptr<base::DictionaryValue> CreateFromNative(NSMenuItem* menu_item, Menu* menu, int index);
static MenuItem* GetMenuItemFromNative(NSMenuItem* menu_item);
#endif

#if defined(OS_WIN) || defined(OS_LINUX)
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
bool CanHandleAccelerators() const override;
Expand Down Expand Up @@ -104,6 +109,7 @@ class MenuItem : public Base {
NSMenuItem* menu_item_;
MenuItemDelegate* delegate_;
bool iconIsTemplate;
bool native_;

#elif defined(OS_WIN) || defined(OS_LINUX)
friend class MenuDelegate;
Expand Down
71 changes: 71 additions & 0 deletions src/api/menuitem/menuitem_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

#include "content/nw/src/api/menuitem/menuitem.h"

#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
#import <Cocoa/Cocoa.h>
#include "content/nw/src/api/base/base_mac.h"
#include "content/nw/src/api/object_manager.h"
#include "content/nw/src/api/menu/menu.h"
#include "content/nw/src/api/menuitem/menuitem_delegate_mac.h"
Expand All @@ -33,6 +35,10 @@
std::string type;
option.GetString("type", &type);
type_ = type;
native_ = false;
option.GetBoolean("native", &native_);

if (native_) return;

if (type == "separator") {
menu_item_ = [NSMenuItem separatorItem];
Expand Down Expand Up @@ -97,6 +103,69 @@
if (option.GetInteger("submenu", &menu_id))
SetSubmenu(object_manager()->GetApiObject<Menu>(menu_id));
}

[menu_item_ setAssociatedObject: this];
}

// static
std::unique_ptr<base::DictionaryValue> MenuItem::CreateFromNative(NSMenuItem* menu_item, Menu* menu, int index) {
std::unique_ptr<base::DictionaryValue> options(new base::DictionaryValue());

options->SetBoolean("native", true);

std::string type("normal");
if ([menu_item isSeparatorItem]) {
type = "separator";
} if ([menu_item state] == NSOnState) {
type = "checkbox";
}
options->SetString("type", type);

options->SetBoolean("checked", [menu_item state] == NSOnState);

options->SetString("label", base::SysNSStringToUTF8([menu_item title]));

if ([menu_item image] != nil) {
options->SetString("icon", "<native>");
options->SetBoolean("iconIsTemplate", [[menu_item image] isTemplate]);
}

options->SetString("tooltip", base::SysNSStringToUTF8([menu_item toolTip]));

options->SetBoolean("enabled", [menu_item isEnabled]);

NSUInteger mask = [menu_item keyEquivalentModifierMask];
if (mask != 0) {
std::stringstream s;
std::vector<std::string> modifiers;
if (mask & NSCommandKeyMask) modifiers.push_back("cmd");
if (mask & NSControlKeyMask) modifiers.push_back("ctrl");
if (mask & NSAlternateKeyMask) modifiers.push_back("alt");
if (mask & NSShiftKeyMask) modifiers.push_back("shift");
std::copy(modifiers.begin(), modifiers.end(), std::ostream_iterator<std::string>(s, "+"));
options->SetString("modifiers", s.str());
}

NSString* key = [menu_item keyEquivalent];
if (key != nil) {
options->SetString("key", base::SysNSStringToUTF8(key));
}

int menuitem_id = ObjectManager::AllocateId();
options->SetInteger("id", menuitem_id);

ObjectManager* manager = menu->object_manager();
manager->OnAllocateObject(menuitem_id, "MenuItem", *options, menu->extension_id_);
MenuItem* item = reinterpret_cast<MenuItem*>(manager->GetApiObject(menuitem_id));
item->menu_item_ = menu_item;
[menu_item setAssociatedObject: item];

return options;
}

// static
MenuItem* MenuItem::GetMenuItemFromNative(NSMenuItem* menu_item) {
return (MenuItem*)[menu_item associatedObject];
}

void MenuItem::OnClick() {
Expand All @@ -110,6 +179,8 @@
}

void MenuItem::Destroy() {
if (native_) return;

[menu_item_ release];
[delegate_ release];
}
Expand Down
9 changes: 8 additions & 1 deletion src/api/nw_current_window_internal.idl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ namespace nw.currentWindowInternal {
[nodoc] DOMString? datatype;
[nodoc] long? quality;
};

dictionary MenuPatch {
long menu;
long index;
object option;
};

interface Functions {
static void close(optional boolean force);
static void showDevToolsInternal(optional ShowDevToolsCallback callback);
Expand All @@ -27,7 +34,7 @@ namespace nw.currentWindowInternal {
static bool isKioskInternal();
static void capturePageInternal(optional CapturePageOptions options, optional CapturePageCallback callback);
static void clearMenu();
static void setMenu(long id);
static MenuPatch[] setMenu(long id);
static void reloadIgnoringCache();
static double getZoom();
static void setZoom(double level);
Expand Down
1 change: 1 addition & 0 deletions src/api/nw_object.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[implemented_in="content/nw/src/api/nw_object_api.h"]
namespace nw.Obj {
interface Functions {
static long allocateId();
static void create(long id, DOMString type, object options);
static void destroy(long id);
static void callObjectMethod(long id, DOMString type, DOMString method, any[] arguments);
Expand Down
11 changes: 11 additions & 0 deletions src/api/nw_object_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@

namespace extensions {

NwObjAllocateIdFunction::NwObjAllocateIdFunction() {
}

NwObjAllocateIdFunction::~NwObjAllocateIdFunction() {
}

bool NwObjAllocateIdFunction::RunNWSync(base::ListValue* response, std::string* error) {
response->AppendInteger(nw::ObjectManager::AllocateId());
return true;
}

NwObjCreateFunction::NwObjCreateFunction() {
}

Expand Down
13 changes: 13 additions & 0 deletions src/api/nw_object_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

namespace extensions {

class NwObjAllocateIdFunction : public NWSyncExtensionFunction {
public:
NwObjAllocateIdFunction();
bool RunNWSync(base::ListValue* response, std::string* error) override;

protected:
~NwObjAllocateIdFunction() override;

DECLARE_EXTENSION_FUNCTION("nw.Obj.allocateId", UNKNOWN)
private:
DISALLOW_COPY_AND_ASSIGN(NwObjAllocateIdFunction);
};

class NwObjCreateFunction : public NWSyncExtensionFunction {
public:
NwObjCreateFunction();
Expand Down
5 changes: 3 additions & 2 deletions src/api/nw_window_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ NwCurrentWindowInternalSetMenuFunction::NwCurrentWindowInternalSetMenuFunction()
NwCurrentWindowInternalSetMenuFunction::~NwCurrentWindowInternalSetMenuFunction() {
}

bool NwCurrentWindowInternalSetMenuFunction::RunAsync() {
bool NwCurrentWindowInternalSetMenuFunction::RunNWSync(base::ListValue* response, std::string* error) {
int id = 0;
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &id));
AppWindow* window = getAppWindow(this);
Expand All @@ -362,7 +362,7 @@ bool NwCurrentWindowInternalSetMenuFunction::RunAsync() {

window->menu_ = menu;
#if defined(OS_MACOSX)
NWChangeAppMenu(menu);
response->Append(NWChangeAppMenu(menu));
#endif

#if defined(OS_LINUX) || defined(OS_WIN)
Expand All @@ -377,6 +377,7 @@ bool NwCurrentWindowInternalSetMenuFunction::RunAsync() {
native_app_window_views->layout_();
native_app_window_views->SchedulePaint();
menu->UpdateKeys( native_app_window_views->widget()->GetFocusManager() );
response->Append(std::unique_ptr<base::ListValue>(new base::ListValue()));
#endif
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/api/nw_window_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ class NwCurrentWindowInternalClearMenuFunction : public AsyncExtensionFunction {
DISALLOW_COPY_AND_ASSIGN(NwCurrentWindowInternalClearMenuFunction);
};

class NwCurrentWindowInternalSetMenuFunction : public AsyncExtensionFunction {
class NwCurrentWindowInternalSetMenuFunction : public NWSyncExtensionFunction {
public:
NwCurrentWindowInternalSetMenuFunction();

protected:
~NwCurrentWindowInternalSetMenuFunction() override;

// ExtensionFunction:
bool RunAsync() override;
bool RunNWSync(base::ListValue* response, std::string* error) override;
DECLARE_EXTENSION_FUNCTION("nw.currentWindowInternal.setMenu", UNKNOWN)

private:
Expand Down
4 changes: 2 additions & 2 deletions src/api/object_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ using extensions::EventRouter;
namespace nw {

IDMap<Base, IDMapOwnPointer> nw::ObjectManager::objects_registry_;
int nw::ObjectManager::next_object_id_ = 1;
int nw::ObjectManager::next_object_id_ = 0;

ObjectManager* ObjectManager::Get(content::BrowserContext* context) {
return ObjectManagerFactory::GetForBrowserContext(context);
Expand Down Expand Up @@ -80,7 +80,7 @@ Base* ObjectManager::GetApiObject(int id) {

// static
int ObjectManager::AllocateId() {
return next_object_id_++;
return ++next_object_id_;
}

void ObjectManager::OnAllocateObject(int object_id,
Expand Down
4 changes: 3 additions & 1 deletion src/nw_content_mac.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _CONTENT_NW_CONTENT_MAC_H
#define _CONTENT_NW_CONTENT_MAC_H

#include "base/values.h"

namespace nw {
class Menu;
}
Expand All @@ -9,6 +11,6 @@ namespace extensions {
class NativeAppWindow;
}

void NWChangeAppMenu(nw::Menu* menu);
std::unique_ptr<base::ListValue> NWChangeAppMenu(nw::Menu* menu);
void NWSetNSWindowShowInTaskbar(extensions::NativeAppWindow* win, bool show);
#endif
Loading

0 comments on commit 043e1f5

Please sign in to comment.