Specify "Clear Menu" through label option

Like a bit of a hack right now, conforming to the existing menu item definition
This commit is contained in:
Zhuo Lu 2017-11-18 22:54:20 -08:00 committed by Cheng Zhao
parent 970babbfd7
commit 0b9cea334f
3 changed files with 18 additions and 4 deletions

View file

@ -27,6 +27,7 @@ class AtomMenuModel;
atom::AtomMenuModel* model_; // weak atom::AtomMenuModel* model_; // weak
base::scoped_nsobject<NSMenu> menu_; base::scoped_nsobject<NSMenu> menu_;
NSMenuItem* openRecentMenuItem_; NSMenuItem* openRecentMenuItem_;
NSMenuItem* openRecentClearMenuMenuItem_; // weak
BOOL isMenuOpen_; BOOL isMenuOpen_;
BOOL useDefaultAccelerator_; BOOL useDefaultAccelerator_;
base::Callback<void()> closeCallback; base::Callback<void()> closeCallback;

View file

@ -96,6 +96,8 @@ Role kRolesMap[] = {
itemWithTitle:@"Electron"] submenu] itemWithTitle:@"Open Recent"]; itemWithTitle:@"Electron"] submenu] itemWithTitle:@"Open Recent"];
[openRecentMenuItem_ setHidden:NO]; [openRecentMenuItem_ setHidden:NO];
[openRecentMenuItem_ retain]; [openRecentMenuItem_ retain];
openRecentClearMenuMenuItem_ =
[[openRecentMenuItem_ submenu] itemWithTitle:@"Clear Menu"];
} }
model_ = model; model_ = model;
@ -153,11 +155,22 @@ Role kRolesMap[] = {
base::string16 role = model->GetRoleAt(index); base::string16 role = model->GetRoleAt(index);
if (role == base::ASCIIToUTF16("openrecent")) { if (role == base::ASCIIToUTF16("openrecent")) {
// Special role for recent documents // Remove singleton menu item from parent menu
[[openRecentMenuItem_ menu] removeItem:openRecentMenuItem_]; [[openRecentMenuItem_ menu] removeItem:openRecentMenuItem_];
[openRecentMenuItem_ setTitle:label];
[[openRecentMenuItem_ submenu] setTitle:label]; // Label formatted as "Open Recent\tClear Menu"
NSArray *titles = [label componentsSeparatedByString:@"\t"];
// Open recent
[openRecentMenuItem_ setTitle:titles[0]];
[[openRecentMenuItem_ submenu] setTitle:titles[0]];
[menu insertItem:openRecentMenuItem_ atIndex:index]; [menu insertItem:openRecentMenuItem_ atIndex:index];
// Clear menu item only displayed if specified in label
bool clearMenuVisible = [titles count] > 1;
[openRecentClearMenuMenuItem_ setHidden:!clearMenuVisible];
if (clearMenuVisible) [openRecentClearMenuMenuItem_ setTitle:titles[1]];
return; return;
} }

View file

@ -103,7 +103,7 @@ const roles = {
label: 'Services' label: 'Services'
}, },
openrecent: { openrecent: {
label: 'Open Recent' label: 'Open Recent\tClear Menu'
}, },
startspeaking: { startspeaking: {
label: 'Start Speaking' label: 'Start Speaking'