add toolTip property for MenuItem (#19099)

This commit is contained in:
Micha Hanselmann 2019-07-11 01:56:22 -07:00 committed by Cheng Zhao
parent 692df804cf
commit 06d48514c6
8 changed files with 41 additions and 3 deletions

View file

@ -20,6 +20,7 @@ See [`Menu`](menu.md) for examples.
`radio`. `radio`.
* `label` String (optional) * `label` String (optional)
* `sublabel` String (optional) * `sublabel` String (optional)
* `toolTip` String (optional) _macOS_ - Hover text for this menu item.
* `accelerator` [Accelerator](accelerator.md) (optional) * `accelerator` [Accelerator](accelerator.md) (optional)
* `icon` ([NativeImage](native-image.md) | String) (optional) * `icon` ([NativeImage](native-image.md) | String) (optional)
* `enabled` Boolean (optional) - If false, the menu item will be greyed out and * `enabled` Boolean (optional) - If false, the menu item will be greyed out and
@ -166,6 +167,10 @@ item's icon, if set.
A `String` indicating the item's sublabel, this property can be dynamically changed. A `String` indicating the item's sublabel, this property can be dynamically changed.
#### `menuItem.toolTip` _macOS_
A `String` indicating the item's hover text.
#### `menuItem.enabled` #### `menuItem.enabled`
A `Boolean` indicating whether the item is enabled, this property can be A `Boolean` indicating whether the item is enabled, this property can be

View file

@ -33,6 +33,7 @@ const MenuItem = function (options) {
this.overrideProperty('label', roles.getDefaultLabel(this.role)) this.overrideProperty('label', roles.getDefaultLabel(this.role))
this.overrideProperty('sublabel', '') this.overrideProperty('sublabel', '')
this.overrideProperty('toolTip', '')
this.overrideProperty('enabled', true) this.overrideProperty('enabled', true)
this.overrideProperty('visible', true) this.overrideProperty('visible', true)
this.overrideProperty('checked', false) this.overrideProperty('checked', false)

View file

@ -121,6 +121,7 @@ Menu.prototype.insert = function (pos, item) {
// set item properties // set item properties
if (item.sublabel) this.setSublabel(pos, item.sublabel) if (item.sublabel) this.setSublabel(pos, item.sublabel)
if (item.toolTip) this.setToolTip(pos, item.toolTip)
if (item.icon) this.setIcon(pos, item.icon) if (item.icon) this.setIcon(pos, item.icon)
if (item.role) this.setRole(pos, item.role) if (item.role) this.setRole(pos, item.role)

View file

@ -141,6 +141,10 @@ void Menu::SetSublabel(int index, const base::string16& sublabel) {
model_->SetSublabel(index, sublabel); model_->SetSublabel(index, sublabel);
} }
void Menu::SetToolTip(int index, const base::string16& toolTip) {
model_->SetToolTip(index, toolTip);
}
void Menu::SetRole(int index, const base::string16& role) { void Menu::SetRole(int index, const base::string16& role) {
model_->SetRole(index, role); model_->SetRole(index, role);
} }
@ -169,6 +173,10 @@ base::string16 Menu::GetSublabelAt(int index) const {
return model_->GetSublabelAt(index); return model_->GetSublabelAt(index);
} }
base::string16 Menu::GetToolTipAt(int index) const {
return model_->GetToolTipAt(index);
}
base::string16 Menu::GetAcceleratorTextAt(int index) const { base::string16 Menu::GetAcceleratorTextAt(int index) const {
ui::Accelerator accelerator; ui::Accelerator accelerator;
model_->GetAcceleratorAtWithParams(index, true, &accelerator); model_->GetAcceleratorAtWithParams(index, true, &accelerator);
@ -212,6 +220,7 @@ void Menu::BuildPrototype(v8::Isolate* isolate,
.SetMethod("insertSubMenu", &Menu::InsertSubMenuAt) .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
.SetMethod("setIcon", &Menu::SetIcon) .SetMethod("setIcon", &Menu::SetIcon)
.SetMethod("setSublabel", &Menu::SetSublabel) .SetMethod("setSublabel", &Menu::SetSublabel)
.SetMethod("setToolTip", &Menu::SetToolTip)
.SetMethod("setRole", &Menu::SetRole) .SetMethod("setRole", &Menu::SetRole)
.SetMethod("clear", &Menu::Clear) .SetMethod("clear", &Menu::Clear)
.SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId) .SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
@ -219,6 +228,7 @@ void Menu::BuildPrototype(v8::Isolate* isolate,
.SetMethod("getCommandIdAt", &Menu::GetCommandIdAt) .SetMethod("getCommandIdAt", &Menu::GetCommandIdAt)
.SetMethod("getLabelAt", &Menu::GetLabelAt) .SetMethod("getLabelAt", &Menu::GetLabelAt)
.SetMethod("getSublabelAt", &Menu::GetSublabelAt) .SetMethod("getSublabelAt", &Menu::GetSublabelAt)
.SetMethod("getToolTipAt", &Menu::GetToolTipAt)
.SetMethod("getAcceleratorTextAt", &Menu::GetAcceleratorTextAt) .SetMethod("getAcceleratorTextAt", &Menu::GetAcceleratorTextAt)
.SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt) .SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
.SetMethod("isEnabledAt", &Menu::IsEnabledAt) .SetMethod("isEnabledAt", &Menu::IsEnabledAt)

View file

@ -86,6 +86,7 @@ class Menu : public mate::TrackableObject<Menu>,
Menu* menu); Menu* menu);
void SetIcon(int index, const gfx::Image& image); void SetIcon(int index, const gfx::Image& image);
void SetSublabel(int index, const base::string16& sublabel); void SetSublabel(int index, const base::string16& sublabel);
void SetToolTip(int index, const base::string16& toolTip);
void SetRole(int index, const base::string16& role); void SetRole(int index, const base::string16& role);
void Clear(); void Clear();
int GetIndexOfCommandId(int command_id); int GetIndexOfCommandId(int command_id);
@ -93,6 +94,7 @@ class Menu : public mate::TrackableObject<Menu>,
int GetCommandIdAt(int index) const; int GetCommandIdAt(int index) const;
base::string16 GetLabelAt(int index) const; base::string16 GetLabelAt(int index) const;
base::string16 GetSublabelAt(int index) const; base::string16 GetSublabelAt(int index) const;
base::string16 GetToolTipAt(int index) const;
base::string16 GetAcceleratorTextAt(int index) const; base::string16 GetAcceleratorTextAt(int index) const;
bool IsItemCheckedAt(int index) const; bool IsItemCheckedAt(int index) const;
bool IsEnabledAt(int index) const; bool IsEnabledAt(int index) const;

View file

@ -19,6 +19,19 @@ AtomMenuModel::AtomMenuModel(Delegate* delegate)
AtomMenuModel::~AtomMenuModel() {} AtomMenuModel::~AtomMenuModel() {}
void AtomMenuModel::SetToolTip(int index, const base::string16& toolTip) {
int command_id = GetCommandIdAt(index);
toolTips_[command_id] = toolTip;
}
base::string16 AtomMenuModel::GetToolTipAt(int index) {
int command_id = GetCommandIdAt(index);
if (base::Contains(toolTips_, command_id))
return toolTips_[command_id];
else
return base::string16();
}
void AtomMenuModel::SetRole(int index, const base::string16& role) { void AtomMenuModel::SetRole(int index, const base::string16& role) {
int command_id = GetCommandIdAt(index); int command_id = GetCommandIdAt(index);
roles_[command_id] = role; roles_[command_id] = role;

View file

@ -53,6 +53,8 @@ class AtomMenuModel : public ui::SimpleMenuModel {
void AddObserver(Observer* obs) { observers_.AddObserver(obs); } void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); } void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
void SetToolTip(int index, const base::string16& toolTip);
base::string16 GetToolTipAt(int index);
void SetRole(int index, const base::string16& role); void SetRole(int index, const base::string16& role);
base::string16 GetRoleAt(int index); base::string16 GetRoleAt(int index);
bool GetAcceleratorAtWithParams(int index, bool GetAcceleratorAtWithParams(int index,
@ -71,7 +73,8 @@ class AtomMenuModel : public ui::SimpleMenuModel {
private: private:
Delegate* delegate_; // weak ref. Delegate* delegate_; // weak ref.
std::map<int, base::string16> roles_; // command id -> role std::map<int, base::string16> toolTips_; // command id -> tooltip
std::map<int, base::string16> roles_; // command id -> role
base::ObserverList<Observer> observers_; base::ObserverList<Observer> observers_;
DISALLOW_COPY_AND_ASSIGN(AtomMenuModel); DISALLOW_COPY_AND_ASSIGN(AtomMenuModel);

View file

@ -233,6 +233,9 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
if (model->GetIconAt(index, &icon) && !icon.IsEmpty()) if (model->GetIconAt(index, &icon) && !icon.IsEmpty())
[item setImage:icon.ToNSImage()]; [item setImage:icon.ToNSImage()];
base::string16 toolTip = model->GetToolTipAt(index);
[item setToolTip:base::SysUTF16ToNSString(toolTip)];
base::string16 role = model->GetRoleAt(index); base::string16 role = model->GetRoleAt(index);
electron::AtomMenuModel::ItemType type = model->GetTypeAt(index); electron::AtomMenuModel::ItemType type = model->GetTypeAt(index);
@ -344,8 +347,8 @@ static base::scoped_nsobject<NSMenu> recentDocumentsMenuSwap_;
DCHECK(model); DCHECK(model);
if (model) { if (model) {
NSEvent* event = [NSApp currentEvent]; NSEvent* event = [NSApp currentEvent];
model->ActivatedAt(modelIndex, model->ActivatedAt(modelIndex, ui::EventFlagsFromNSEventWithModifiers(
ui::EventFlagsFromNSEventWithModifiers(event, [event modifierFlags])); event, [event modifierFlags]));
} }
} }