2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2013-08-14 14:24:21 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-08-14 14:24:21 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#import "shell/browser/ui/cocoa/electron_menu_controller.h"
|
2013-08-14 14:24:21 +00:00
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
#include <string>
|
2020-01-29 17:03:53 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
#include "base/logging.h"
|
2020-05-27 20:54:52 +00:00
|
|
|
#include "base/mac/foundation_util.h"
|
2013-08-14 14:24:21 +00:00
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2015-09-01 11:48:11 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2019-01-12 01:00:43 +00:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2018-02-25 22:19:40 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2020-10-20 01:33:06 +00:00
|
|
|
#include "net/base/mac/url_conversions.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/mac/electron_application.h"
|
2020-10-20 01:33:06 +00:00
|
|
|
#include "shell/browser/native_window.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/electron_menu_model.h"
|
2020-10-20 01:33:06 +00:00
|
|
|
#include "shell/browser/window_list.h"
|
2013-08-14 14:24:21 +00:00
|
|
|
#include "ui/base/accelerators/accelerator.h"
|
|
|
|
#include "ui/base/l10n/l10n_util_mac.h"
|
2015-07-29 04:01:27 +00:00
|
|
|
#include "ui/events/cocoa/cocoa_event_utils.h"
|
2021-06-02 07:32:48 +00:00
|
|
|
#include "ui/events/keycodes/keyboard_code_conversion_mac.h"
|
2013-08-14 14:24:21 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
2019-02-08 21:19:01 +00:00
|
|
|
#include "ui/strings/grit/ui_strings.h"
|
2013-08-14 14:24:21 +00:00
|
|
|
|
2018-02-25 22:19:40 +00:00
|
|
|
using content::BrowserThread;
|
2020-10-20 01:33:06 +00:00
|
|
|
using SharingItem = electron::ElectronMenuModel::SharingItem;
|
2018-02-25 22:19:40 +00:00
|
|
|
|
2015-09-01 11:48:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Role {
|
|
|
|
SEL selector;
|
|
|
|
const char* role;
|
|
|
|
};
|
|
|
|
Role kRolesMap[] = {
|
2018-04-20 18:47:04 +00:00
|
|
|
{@selector(orderFrontStandardAboutPanel:), "about"},
|
|
|
|
{@selector(hide:), "hide"},
|
|
|
|
{@selector(hideOtherApplications:), "hideothers"},
|
|
|
|
{@selector(unhideAllApplications:), "unhide"},
|
|
|
|
{@selector(arrangeInFront:), "front"},
|
|
|
|
{@selector(undo:), "undo"},
|
|
|
|
{@selector(redo:), "redo"},
|
|
|
|
{@selector(cut:), "cut"},
|
|
|
|
{@selector(copy:), "copy"},
|
|
|
|
{@selector(paste:), "paste"},
|
|
|
|
{@selector(delete:), "delete"},
|
|
|
|
{@selector(pasteAndMatchStyle:), "pasteandmatchstyle"},
|
|
|
|
{@selector(selectAll:), "selectall"},
|
2022-01-06 18:41:28 +00:00
|
|
|
{@selector(orderFrontSubstitutionsPanel:), "showsubstitutions"},
|
|
|
|
{@selector(toggleAutomaticQuoteSubstitution:), "togglesmartquotes"},
|
|
|
|
{@selector(toggleAutomaticDashSubstitution:), "togglesmartdashes"},
|
|
|
|
{@selector(toggleAutomaticTextReplacement:), "toggletextreplacement"},
|
2018-04-20 18:47:04 +00:00
|
|
|
{@selector(startSpeaking:), "startspeaking"},
|
|
|
|
{@selector(stopSpeaking:), "stopspeaking"},
|
|
|
|
{@selector(performMiniaturize:), "minimize"},
|
|
|
|
{@selector(performClose:), "close"},
|
|
|
|
{@selector(performZoom:), "zoom"},
|
|
|
|
{@selector(terminate:), "quit"},
|
|
|
|
// ↓ is intentionally not `toggleFullScreen`. The macOS full screen menu
|
|
|
|
// item behaves weird. If we use `toggleFullScreen`, then the menu item will
|
|
|
|
// use the default label, and not take the one provided.
|
|
|
|
{@selector(toggleFullScreenMode:), "togglefullscreen"},
|
|
|
|
{@selector(toggleTabBar:), "toggletabbar"},
|
|
|
|
{@selector(selectNextTab:), "selectnexttab"},
|
|
|
|
{@selector(selectPreviousTab:), "selectprevioustab"},
|
|
|
|
{@selector(mergeAllWindows:), "mergeallwindows"},
|
|
|
|
{@selector(moveTabToNewWindow:), "movetabtonewwindow"},
|
|
|
|
{@selector(clearRecentDocuments:), "clearrecentdocuments"},
|
2015-09-01 11:48:11 +00:00
|
|
|
};
|
|
|
|
|
2019-02-08 21:19:01 +00:00
|
|
|
// Called when adding a submenu to the menu and checks if the submenu, via its
|
|
|
|
// |model|, has visible child items.
|
|
|
|
bool MenuHasVisibleItems(const electron::ElectronMenuModel* model) {
|
|
|
|
int count = model->GetItemCount();
|
|
|
|
for (int index = 0; index < count; index++) {
|
|
|
|
if (model->IsVisibleAt(index))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when an empty submenu is created. This inserts a menu item labeled
|
|
|
|
// "(empty)" into the submenu. Matches Windows behavior.
|
|
|
|
NSMenu* MakeEmptySubmenu() {
|
|
|
|
base::scoped_nsobject<NSMenu> submenu([[NSMenu alloc] initWithTitle:@""]);
|
|
|
|
NSString* empty_menu_title =
|
|
|
|
l10n_util::GetNSString(IDS_APP_MENU_EMPTY_SUBMENU);
|
|
|
|
|
|
|
|
[submenu addItemWithTitle:empty_menu_title action:NULL keyEquivalent:@""];
|
|
|
|
[[submenu itemAtIndex:0] setEnabled:NO];
|
|
|
|
return submenu.autorelease();
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
// Convert an SharingItem to an array of NSObjects.
|
|
|
|
NSArray* ConvertSharingItemToNS(const SharingItem& item) {
|
|
|
|
NSMutableArray* result = [NSMutableArray array];
|
|
|
|
if (item.texts) {
|
|
|
|
for (const std::string& str : *item.texts)
|
|
|
|
[result addObject:base::SysUTF8ToNSString(str)];
|
|
|
|
}
|
|
|
|
if (item.file_paths) {
|
|
|
|
for (const base::FilePath& path : *item.file_paths)
|
|
|
|
[result addObject:base::mac::FilePathToNSURL(path)];
|
|
|
|
}
|
|
|
|
if (item.urls) {
|
|
|
|
for (const GURL& url : *item.urls)
|
|
|
|
[result addObject:net::NSURLWithGURL(url)];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-09-01 11:48:11 +00:00
|
|
|
} // namespace
|
2013-08-14 14:24:21 +00:00
|
|
|
|
2020-05-27 20:54:52 +00:00
|
|
|
// This class stores a base::WeakPtr<electron::ElectronMenuModel> as an
|
|
|
|
// Objective-C object, which allows it to be stored in the representedObject
|
|
|
|
// field of an NSMenuItem.
|
|
|
|
@interface WeakPtrToElectronMenuModelAsNSObject : NSObject
|
|
|
|
+ (instancetype)weakPtrForModel:(electron::ElectronMenuModel*)model;
|
|
|
|
+ (electron::ElectronMenuModel*)getFrom:(id)instance;
|
|
|
|
- (instancetype)initWithModel:(electron::ElectronMenuModel*)model;
|
|
|
|
- (electron::ElectronMenuModel*)menuModel;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation WeakPtrToElectronMenuModelAsNSObject {
|
|
|
|
base::WeakPtr<electron::ElectronMenuModel> _model;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)weakPtrForModel:(electron::ElectronMenuModel*)model {
|
|
|
|
return [[[WeakPtrToElectronMenuModelAsNSObject alloc] initWithModel:model]
|
|
|
|
autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (electron::ElectronMenuModel*)getFrom:(id)instance {
|
|
|
|
return
|
|
|
|
[base::mac::ObjCCastStrict<WeakPtrToElectronMenuModelAsNSObject>(instance)
|
|
|
|
menuModel];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithModel:(electron::ElectronMenuModel*)model {
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_model = model->GetWeakPtr();
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (electron::ElectronMenuModel*)menuModel {
|
|
|
|
return _model.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-11-21 00:15:43 +00:00
|
|
|
// Menu item is located for ease of removing it from the parent owner
|
2017-11-21 18:52:24 +00:00
|
|
|
static base::scoped_nsobject<NSMenuItem> recentDocumentsMenuItem_;
|
2017-11-21 00:15:43 +00:00
|
|
|
|
2017-11-21 02:07:11 +00:00
|
|
|
// Submenu retained to be swapped back to |recentDocumentsMenuItem_|
|
2017-11-21 18:52:24 +00:00
|
|
|
static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
|
2017-11-21 00:15:43 +00:00
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
@implementation ElectronMenuController
|
|
|
|
|
2020-05-27 20:54:52 +00:00
|
|
|
- (electron::ElectronMenuModel*)model {
|
|
|
|
return model_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setModel:(electron::ElectronMenuModel*)model {
|
|
|
|
model_ = model->GetWeakPtr();
|
|
|
|
}
|
2013-08-14 14:24:21 +00:00
|
|
|
|
2020-05-27 20:54:52 +00:00
|
|
|
- (instancetype)initWithModel:(electron::ElectronMenuModel*)model
|
|
|
|
useDefaultAccelerator:(BOOL)use {
|
2013-08-14 14:24:21 +00:00
|
|
|
if ((self = [super init])) {
|
2020-05-27 20:54:52 +00:00
|
|
|
model_ = model->GetWeakPtr();
|
2016-07-02 02:47:40 +00:00
|
|
|
isMenuOpen_ = NO;
|
|
|
|
useDefaultAccelerator_ = use;
|
2013-08-14 14:24:21 +00:00
|
|
|
[self menu];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
[menu_ setDelegate:nil];
|
|
|
|
|
|
|
|
// Close the menu if it is still open. This could happen if a tab gets closed
|
|
|
|
// while its context menu is still open.
|
|
|
|
[self cancel];
|
|
|
|
|
2020-05-27 20:54:52 +00:00
|
|
|
model_ = nullptr;
|
2013-08-14 14:24:21 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2019-11-20 11:17:39 +00:00
|
|
|
- (void)setCloseCallback:(base::OnceClosure)callback {
|
|
|
|
closeCallback = std::move(callback);
|
2017-02-16 18:58:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 02:47:40 +00:00
|
|
|
- (void)populateWithModel:(electron::ElectronMenuModel*)model {
|
2014-11-16 12:24:29 +00:00
|
|
|
if (!menu_)
|
|
|
|
return;
|
|
|
|
|
2019-10-03 05:25:14 +00:00
|
|
|
// Locate & retain the recent documents menu item
|
2017-11-21 00:15:43 +00:00
|
|
|
if (!recentDocumentsMenuItem_) {
|
2021-04-27 21:27:34 +00:00
|
|
|
std::u16string title = u"Open Recent";
|
2019-11-04 22:20:31 +00:00
|
|
|
NSString* openTitle = l10n_util::FixUpWindowsStyleLabel(title);
|
|
|
|
|
|
|
|
recentDocumentsMenuItem_.reset([[[[[NSApp mainMenu]
|
|
|
|
itemWithTitle:@"Electron"] submenu] itemWithTitle:openTitle] retain]);
|
2017-11-19 05:52:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 20:54:52 +00:00
|
|
|
model_ = model->GetWeakPtr();
|
2014-11-16 12:24:29 +00:00
|
|
|
[menu_ removeAllItems];
|
|
|
|
|
|
|
|
const int count = model->GetItemCount();
|
|
|
|
for (int index = 0; index < count; index++) {
|
2016-07-02 02:47:40 +00:00
|
|
|
if (model->GetTypeAt(index) == electron::ElectronMenuModel::TYPE_SEPARATOR)
|
2014-11-16 12:24:29 +00:00
|
|
|
[self addSeparatorToMenu:menu_ atIndex:index];
|
|
|
|
else
|
|
|
|
[self addItemToMenu:menu_ atIndex:index fromModel:model];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
- (void)cancel {
|
|
|
|
if (isMenuOpen_) {
|
|
|
|
[menu_ cancelTracking];
|
|
|
|
isMenuOpen_ = NO;
|
2020-05-27 20:54:52 +00:00
|
|
|
if (model_)
|
|
|
|
model_->MenuWillClose();
|
2018-06-29 21:48:26 +00:00
|
|
|
if (!closeCallback.is_null()) {
|
2022-05-17 16:48:40 +00:00
|
|
|
content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE,
|
|
|
|
std::move(closeCallback));
|
2018-06-29 21:48:26 +00:00
|
|
|
}
|
2013-08-14 14:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a NSMenu from the given model. If the model has submenus, this can
|
|
|
|
// be invoked recursively.
|
2016-07-02 02:47:40 +00:00
|
|
|
- (NSMenu*)menuFromModel:(electron::ElectronMenuModel*)model {
|
2013-08-14 14:24:21 +00:00
|
|
|
NSMenu* menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
|
|
|
|
|
|
|
|
const int count = model->GetItemCount();
|
|
|
|
for (int index = 0; index < count; index++) {
|
2016-07-02 02:47:40 +00:00
|
|
|
if (model->GetTypeAt(index) == electron::ElectronMenuModel::TYPE_SEPARATOR)
|
2013-08-14 14:24:21 +00:00
|
|
|
[self addSeparatorToMenu:menu atIndex:index];
|
|
|
|
else
|
|
|
|
[self addItemToMenu:menu atIndex:index fromModel:model];
|
|
|
|
}
|
|
|
|
|
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a separator item at the given index. As the separator doesn't need
|
|
|
|
// anything from the model, this method doesn't need the model index as the
|
|
|
|
// other method below does.
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)addSeparatorToMenu:(NSMenu*)menu atIndex:(int)index {
|
2013-08-14 14:24:21 +00:00
|
|
|
NSMenuItem* separator = [NSMenuItem separatorItem];
|
|
|
|
[menu insertItem:separator atIndex:index];
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:07:11 +00:00
|
|
|
// Empties the source menu items to the destination.
|
2018-04-20 18:47:04 +00:00
|
|
|
- (void)moveMenuItems:(NSMenu*)source to:(NSMenu*)destination {
|
2019-05-02 12:05:37 +00:00
|
|
|
const NSInteger count = [source numberOfItems];
|
|
|
|
for (NSInteger index = 0; index < count; index++) {
|
2017-11-21 00:15:43 +00:00
|
|
|
NSMenuItem* removedItem = [[[source itemAtIndex:0] retain] autorelease];
|
|
|
|
[source removeItemAtIndex:0];
|
|
|
|
[destination addItem:removedItem];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:07:11 +00:00
|
|
|
// Replaces the item's submenu instance with the singleton recent documents
|
|
|
|
// menu. Previously replaced menu items will be recovered.
|
2017-11-21 00:15:43 +00:00
|
|
|
- (void)replaceSubmenuShowingRecentDocuments:(NSMenuItem*)item {
|
2019-10-21 21:11:09 +00:00
|
|
|
NSMenu* recentDocumentsMenu =
|
|
|
|
[[[recentDocumentsMenuItem_ submenu] retain] autorelease];
|
2017-11-21 00:15:43 +00:00
|
|
|
|
|
|
|
// Remove menu items in recent documents back to swap menu
|
2018-04-20 18:47:04 +00:00
|
|
|
[self moveMenuItems:recentDocumentsMenu to:recentDocumentsMenuSwap_];
|
2017-11-21 00:15:43 +00:00
|
|
|
// Swap back the submenu
|
2017-11-21 02:07:11 +00:00
|
|
|
[recentDocumentsMenuItem_ setSubmenu:recentDocumentsMenuSwap_];
|
2017-11-21 00:15:43 +00:00
|
|
|
|
|
|
|
// Retain the item's submenu for a future recovery
|
2017-11-21 18:52:24 +00:00
|
|
|
recentDocumentsMenuSwap_.reset([[item submenu] retain]);
|
2017-11-21 00:15:43 +00:00
|
|
|
|
|
|
|
// Repopulate with items from the submenu to be replaced
|
2018-04-20 18:47:04 +00:00
|
|
|
[self moveMenuItems:recentDocumentsMenuSwap_ to:recentDocumentsMenu];
|
2017-11-29 03:38:07 +00:00
|
|
|
// Update the submenu's title
|
|
|
|
[recentDocumentsMenu setTitle:[recentDocumentsMenuSwap_ title]];
|
2017-11-21 00:15:43 +00:00
|
|
|
// Replace submenu
|
|
|
|
[item setSubmenu:recentDocumentsMenu];
|
|
|
|
|
2019-10-03 05:25:14 +00:00
|
|
|
DCHECK_EQ([item action], @selector(submenuAction:));
|
|
|
|
DCHECK_EQ([item target], recentDocumentsMenu);
|
|
|
|
|
2017-11-21 00:15:43 +00:00
|
|
|
// Remember the new menu item that carries the recent documents menu
|
2017-11-21 18:52:24 +00:00
|
|
|
recentDocumentsMenuItem_.reset([item retain]);
|
2017-11-21 00:15:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
// Fill the menu with Share Menu items.
|
|
|
|
- (NSMenu*)createShareMenuForItem:(const SharingItem&)item {
|
|
|
|
NSArray* items = ConvertSharingItemToNS(item);
|
|
|
|
if ([items count] == 0)
|
|
|
|
return MakeEmptySubmenu();
|
|
|
|
base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] init]);
|
|
|
|
NSArray* services = [NSSharingService sharingServicesForItems:items];
|
|
|
|
for (NSSharingService* service in services)
|
|
|
|
[menu addItem:[self menuItemForService:service withItems:items]];
|
|
|
|
return menu.autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a menu item that calls |service| when invoked.
|
|
|
|
- (NSMenuItem*)menuItemForService:(NSSharingService*)service
|
|
|
|
withItems:(NSArray*)items {
|
|
|
|
base::scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc]
|
|
|
|
initWithTitle:service.menuItemTitle
|
|
|
|
action:@selector(performShare:)
|
|
|
|
keyEquivalent:@""]);
|
|
|
|
[item setTarget:self];
|
|
|
|
[item setImage:service.image];
|
|
|
|
[item setRepresentedObject:@{@"service" : service, @"items" : items}];
|
|
|
|
return item.autorelease();
|
|
|
|
}
|
|
|
|
|
2021-06-29 23:28:16 +00:00
|
|
|
- (base::scoped_nsobject<NSMenuItem>)
|
|
|
|
makeMenuItemForIndex:(NSInteger)index
|
|
|
|
fromModel:(electron::ElectronMenuModel*)model {
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string label16 = model->GetLabelAt(index);
|
2013-08-14 14:24:21 +00:00
|
|
|
NSString* label = l10n_util::FixUpWindowsStyleLabel(label16);
|
2017-11-19 05:52:52 +00:00
|
|
|
|
2018-04-20 18:47:04 +00:00
|
|
|
base::scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc]
|
|
|
|
initWithTitle:label
|
|
|
|
action:@selector(itemSelected:)
|
|
|
|
keyEquivalent:@""]);
|
2013-08-14 14:24:21 +00:00
|
|
|
|
|
|
|
// If the menu item has an icon, set it.
|
2020-04-13 23:39:26 +00:00
|
|
|
ui::ImageModel icon = model->GetIconAt(index);
|
|
|
|
if (icon.IsImage())
|
|
|
|
[item setImage:icon.GetImage().ToNSImage()];
|
2013-08-14 14:24:21 +00:00
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string toolTip = model->GetToolTipAt(index);
|
2019-07-11 08:56:22 +00:00
|
|
|
[item setToolTip:base::SysUTF16ToNSString(toolTip)];
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string role = model->GetRoleAt(index);
|
2016-07-02 02:47:40 +00:00
|
|
|
electron::ElectronMenuModel::ItemType type = model->GetTypeAt(index);
|
2018-11-10 17:47:55 +00:00
|
|
|
|
2021-04-27 21:27:34 +00:00
|
|
|
if (role == u"services") {
|
|
|
|
std::u16string title = u"Services";
|
2022-10-03 20:21:00 +00:00
|
|
|
NSString* sub_label = l10n_util::FixUpWindowsStyleLabel(title);
|
2018-11-10 17:47:55 +00:00
|
|
|
|
|
|
|
[item setTarget:nil];
|
|
|
|
[item setAction:nil];
|
2022-10-03 20:21:00 +00:00
|
|
|
NSMenu* submenu = [[[NSMenu alloc] initWithTitle:sub_label] autorelease];
|
2018-11-10 17:47:55 +00:00
|
|
|
[item setSubmenu:submenu];
|
|
|
|
[NSApp setServicesMenu:submenu];
|
2021-04-27 21:27:34 +00:00
|
|
|
} else if (role == u"sharemenu") {
|
2020-10-20 01:33:06 +00:00
|
|
|
SharingItem sharing_item;
|
|
|
|
model->GetSharingItemAt(index, &sharing_item);
|
|
|
|
[item setTarget:nil];
|
|
|
|
[item setAction:nil];
|
|
|
|
[item setSubmenu:[self createShareMenuForItem:sharing_item]];
|
2019-02-08 20:54:39 +00:00
|
|
|
} else if (type == electron::ElectronMenuModel::TYPE_SUBMENU &&
|
|
|
|
model->IsVisibleAt(index)) {
|
2019-02-09 02:07:08 +00:00
|
|
|
// We need to specifically check that the submenu top-level item has been
|
|
|
|
// enabled as it's not validated by validateUserInterfaceItem
|
|
|
|
if (!model->IsEnabledAt(index))
|
|
|
|
[item setEnabled:NO];
|
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
// Recursively build a submenu from the sub-model at this index.
|
|
|
|
[item setTarget:nil];
|
|
|
|
[item setAction:nil];
|
2018-04-20 18:47:04 +00:00
|
|
|
electron::ElectronMenuModel* submenuModel =
|
|
|
|
static_cast<electron::ElectronMenuModel*>(
|
|
|
|
model->GetSubmenuModelAt(index));
|
2019-02-08 21:19:01 +00:00
|
|
|
NSMenu* submenu = MenuHasVisibleItems(submenuModel)
|
|
|
|
? [self menuFromModel:submenuModel]
|
|
|
|
: MakeEmptySubmenu();
|
2013-08-14 14:33:18 +00:00
|
|
|
[submenu setTitle:[item title]];
|
2013-08-14 14:24:21 +00:00
|
|
|
[item setSubmenu:submenu];
|
2013-08-14 14:33:18 +00:00
|
|
|
|
2015-09-01 11:48:11 +00:00
|
|
|
// Set submenu's role.
|
2021-04-27 21:27:34 +00:00
|
|
|
if ((role == u"window" || role == u"windowmenu") && [submenu numberOfItems])
|
2013-08-14 14:33:18 +00:00
|
|
|
[NSApp setWindowsMenu:submenu];
|
2021-04-27 21:27:34 +00:00
|
|
|
else if (role == u"help")
|
2013-10-17 02:15:57 +00:00
|
|
|
[NSApp setHelpMenu:submenu];
|
2021-04-27 21:27:34 +00:00
|
|
|
else if (role == u"recentdocuments")
|
2017-11-21 00:15:43 +00:00
|
|
|
[self replaceSubmenuShowingRecentDocuments:item];
|
2013-08-14 14:24:21 +00:00
|
|
|
} else {
|
|
|
|
// The MenuModel works on indexes so we can't just set the command id as the
|
|
|
|
// tag like we do in other menus. Also set the represented object to be
|
|
|
|
// the model so hierarchical menus check the correct index in the correct
|
|
|
|
// model. Setting the target to |self| allows this class to participate
|
|
|
|
// in validation of the menu items.
|
|
|
|
[item setTag:index];
|
2020-05-27 20:54:52 +00:00
|
|
|
[item setRepresentedObject:[WeakPtrToElectronMenuModelAsNSObject
|
|
|
|
weakPtrForModel:model]];
|
2013-08-14 14:24:21 +00:00
|
|
|
ui::Accelerator accelerator;
|
2018-04-20 18:47:04 +00:00
|
|
|
if (model->GetAcceleratorAtWithParams(index, useDefaultAccelerator_,
|
|
|
|
&accelerator)) {
|
2021-06-02 07:32:48 +00:00
|
|
|
// Note that we are not using Chromium's
|
|
|
|
// GetKeyEquivalentAndModifierMaskFromAccelerator API,
|
|
|
|
// because it will convert Shift+Character to ShiftedCharacter, for
|
|
|
|
// example Shift+/ would be converted to ?, which is against macOS HIG.
|
|
|
|
// See also https://github.com/electron/electron/issues/21790.
|
|
|
|
NSUInteger modifier_mask = 0;
|
|
|
|
if (accelerator.IsCtrlDown())
|
|
|
|
modifier_mask |= NSEventModifierFlagControl;
|
|
|
|
if (accelerator.IsAltDown())
|
|
|
|
modifier_mask |= NSEventModifierFlagOption;
|
|
|
|
if (accelerator.IsCmdDown())
|
|
|
|
modifier_mask |= NSEventModifierFlagCommand;
|
|
|
|
unichar character;
|
|
|
|
if (accelerator.shifted_char) {
|
|
|
|
// When a shifted char is explicitly specified, for example Ctrl+Plus,
|
|
|
|
// use the shifted char directly.
|
|
|
|
character = static_cast<unichar>(*accelerator.shifted_char);
|
|
|
|
} else {
|
|
|
|
// Otherwise use the unshifted combinations, for example Ctrl+Shift+=.
|
|
|
|
if (accelerator.IsShiftDown())
|
|
|
|
modifier_mask |= NSEventModifierFlagShift;
|
|
|
|
ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(), modifier_mask,
|
|
|
|
nullptr, &character);
|
|
|
|
}
|
|
|
|
[item setKeyEquivalent:[NSString stringWithFormat:@"%C", character]];
|
2018-10-02 22:22:48 +00:00
|
|
|
[item setKeyEquivalentModifierMask:modifier_mask];
|
2013-08-14 14:24:21 +00:00
|
|
|
}
|
2015-09-01 11:48:11 +00:00
|
|
|
|
2022-06-02 18:43:40 +00:00
|
|
|
[(id)item
|
|
|
|
setAllowsKeyEquivalentWhenHidden:(model->WorksWhenHiddenAt(index))];
|
2019-02-28 17:00:54 +00:00
|
|
|
|
2015-09-01 11:48:11 +00:00
|
|
|
// Set menu item's role.
|
2016-08-08 17:09:45 +00:00
|
|
|
[item setTarget:self];
|
|
|
|
if (!role.empty()) {
|
2015-09-01 11:48:11 +00:00
|
|
|
for (const Role& pair : kRolesMap) {
|
|
|
|
if (role == base::ASCIIToUTF16(pair.role)) {
|
2016-08-08 17:09:45 +00:00
|
|
|
[item setTarget:nil];
|
2015-09-01 11:48:11 +00:00
|
|
|
[item setAction:pair.selector];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-14 14:24:21 +00:00
|
|
|
}
|
2021-06-29 23:28:16 +00:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds an item or a hierarchical menu to the item at the |index|,
|
|
|
|
// associated with the entry in the model identified by |modelIndex|.
|
|
|
|
- (void)addItemToMenu:(NSMenu*)menu
|
|
|
|
atIndex:(NSInteger)index
|
|
|
|
fromModel:(electron::ElectronMenuModel*)model {
|
|
|
|
[menu insertItem:[self makeMenuItemForIndex:index fromModel:model]
|
|
|
|
atIndex:index];
|
2013-08-14 14:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called before the menu is to be displayed to update the state (enabled,
|
2019-02-28 17:00:54 +00:00
|
|
|
// radio, etc) of each item in the menu.
|
2013-08-14 14:24:21 +00:00
|
|
|
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item {
|
|
|
|
SEL action = [item action];
|
2020-10-20 01:33:06 +00:00
|
|
|
if (action == @selector(performShare:))
|
|
|
|
return YES;
|
2013-08-14 14:24:21 +00:00
|
|
|
if (action != @selector(itemSelected:))
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
NSInteger modelIndex = [item tag];
|
2020-05-27 20:54:52 +00:00
|
|
|
electron::ElectronMenuModel* model = [WeakPtrToElectronMenuModelAsNSObject
|
|
|
|
getFrom:[(id)item representedObject]];
|
2013-08-14 14:24:21 +00:00
|
|
|
DCHECK(model);
|
|
|
|
if (model) {
|
|
|
|
BOOL checked = model->IsItemCheckedAt(modelIndex);
|
|
|
|
DCHECK([(id)item isKindOfClass:[NSMenuItem class]]);
|
2018-10-03 07:24:28 +00:00
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
[(id)item setState:(checked ? NSOnState : NSOffState)];
|
|
|
|
[(id)item setHidden:(!model->IsVisibleAt(modelIndex))];
|
2018-10-03 07:24:28 +00:00
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
return model->IsEnabledAt(modelIndex);
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the user chooses a particular menu item. |sender| is the menu
|
|
|
|
// item chosen.
|
|
|
|
- (void)itemSelected:(id)sender {
|
|
|
|
NSInteger modelIndex = [sender tag];
|
2018-04-20 18:47:04 +00:00
|
|
|
electron::ElectronMenuModel* model =
|
2020-05-27 20:54:52 +00:00
|
|
|
[WeakPtrToElectronMenuModelAsNSObject getFrom:[sender representedObject]];
|
2013-08-14 14:24:21 +00:00
|
|
|
DCHECK(model);
|
|
|
|
if (model) {
|
2015-07-29 04:01:27 +00:00
|
|
|
NSEvent* event = [NSApp currentEvent];
|
2019-07-11 08:56:22 +00:00
|
|
|
model->ActivatedAt(modelIndex, ui::EventFlagsFromNSEventWithModifiers(
|
|
|
|
event, [event modifierFlags]));
|
2013-08-14 14:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
// Performs the share action using the sharing service represented by |sender|.
|
|
|
|
- (void)performShare:(NSMenuItem*)sender {
|
|
|
|
NSDictionary* object =
|
|
|
|
base::mac::ObjCCastStrict<NSDictionary>([sender representedObject]);
|
|
|
|
NSSharingService* service =
|
|
|
|
base::mac::ObjCCastStrict<NSSharingService>(object[@"service"]);
|
|
|
|
NSArray* items = base::mac::ObjCCastStrict<NSArray>(object[@"items"]);
|
|
|
|
[service setDelegate:self];
|
|
|
|
[service performWithItems:items];
|
|
|
|
}
|
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
- (NSMenu*)menu {
|
2014-11-16 13:06:16 +00:00
|
|
|
if (menu_)
|
|
|
|
return menu_.get();
|
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
if (model_ && model_->GetSharingItem()) {
|
|
|
|
NSMenu* menu = [self createShareMenuForItem:*model_->GetSharingItem()];
|
|
|
|
menu_.reset([menu retain]);
|
|
|
|
} else {
|
|
|
|
menu_.reset([[NSMenu alloc] initWithTitle:@""]);
|
|
|
|
if (model_)
|
|
|
|
[self populateWithModel:model_.get()];
|
|
|
|
}
|
2014-11-16 13:06:16 +00:00
|
|
|
[menu_ setDelegate:self];
|
2013-08-14 14:24:21 +00:00
|
|
|
return menu_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isMenuOpen {
|
|
|
|
return isMenuOpen_;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)menuWillOpen:(NSMenu*)menu {
|
|
|
|
isMenuOpen_ = YES;
|
2020-05-27 20:54:52 +00:00
|
|
|
if (model_)
|
|
|
|
model_->MenuWillShow();
|
2013-08-14 14:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)menuDidClose:(NSMenu*)menu {
|
|
|
|
if (isMenuOpen_) {
|
|
|
|
isMenuOpen_ = NO;
|
2019-09-04 20:42:23 +00:00
|
|
|
if (model_)
|
|
|
|
model_->MenuWillClose();
|
2018-04-20 18:47:04 +00:00
|
|
|
// Post async task so that itemSelected runs before the close callback
|
|
|
|
// deletes the controller from the map which deallocates it
|
2018-02-25 22:19:40 +00:00
|
|
|
if (!closeCallback.is_null()) {
|
2022-05-17 16:48:40 +00:00
|
|
|
content::GetUIThreadTaskRunner({})->PostTask(FROM_HERE,
|
|
|
|
std::move(closeCallback));
|
2018-02-25 22:19:40 +00:00
|
|
|
}
|
2013-08-14 14:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
// NSSharingServiceDelegate
|
|
|
|
|
|
|
|
- (NSWindow*)sharingService:(NSSharingService*)service
|
|
|
|
sourceWindowForShareItems:(NSArray*)items
|
|
|
|
sharingContentScope:(NSSharingContentScope*)scope {
|
|
|
|
// Return the current active window.
|
|
|
|
const auto& list = electron::WindowList::GetWindows();
|
|
|
|
for (electron::NativeWindow* window : list) {
|
|
|
|
if (window->IsFocused())
|
|
|
|
return window->GetNativeWindow().GetNativeNSWindow();
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2013-08-14 14:24:21 +00:00
|
|
|
@end
|