feat: add menu item role palette and header (#45538)

* feat: add menu item role `palette` and `header`

* adds comments

* refactors new role items to new item types

* docs: custom type

* docs: note types only available on mac 14+

---------

Co-authored-by: Samuel Maddock <smaddock@slack-corp.com>
This commit is contained in:
Gellert Hegyi
2025-05-23 18:43:49 +02:00
committed by GitHub
parent 2248de847d
commit b9b96a96f7
10 changed files with 61 additions and 8 deletions

View File

@@ -213,6 +213,10 @@ void Menu::SetRole(int index, const std::u16string& role) {
model_->SetRole(index, role);
}
void Menu::SetCustomType(int index, const std::u16string& customType) {
model_->SetCustomType(index, customType);
}
void Menu::Clear() {
model_->Clear();
}
@@ -286,6 +290,7 @@ void Menu::FillObjectTemplate(v8::Isolate* isolate,
.SetMethod("setSublabel", &Menu::SetSublabel)
.SetMethod("setToolTip", &Menu::SetToolTip)
.SetMethod("setRole", &Menu::SetRole)
.SetMethod("setCustomType", &Menu::SetCustomType)
.SetMethod("clear", &Menu::Clear)
.SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
.SetMethod("getItemCount", &Menu::GetItemCount)

View File

@@ -116,6 +116,7 @@ class Menu : public gin::Wrappable<Menu>,
void SetSublabel(int index, const std::u16string& sublabel);
void SetToolTip(int index, const std::u16string& toolTip);
void SetRole(int index, const std::u16string& role);
void SetCustomType(int index, const std::u16string& customType);
void Clear();
int GetIndexOfCommandId(int command_id) const;
int GetItemCount() const;

View File

@@ -330,6 +330,17 @@ NSArray* ConvertSharingItemToNS(const SharingItem& item) {
}
}
std::u16string role = model->GetRoleAt(index);
electron::ElectronMenuModel::ItemType type = model->GetTypeAt(index);
std::u16string customType = model->GetCustomTypeAt(index);
// The sectionHeaderWithTitle menu item is only available in macOS 14.0+.
if (@available(macOS 14, *)) {
if (customType == u"header") {
item = [NSMenuItem sectionHeaderWithTitle:label];
}
}
// If the menu item has an icon, set it.
ui::ImageModel icon = model->GetIconAt(index);
if (icon.IsImage())
@@ -338,9 +349,6 @@ NSArray* ConvertSharingItemToNS(const SharingItem& item) {
std::u16string toolTip = model->GetToolTipAt(index);
[item setToolTip:base::SysUTF16ToNSString(toolTip)];
std::u16string role = model->GetRoleAt(index);
electron::ElectronMenuModel::ItemType type = model->GetTypeAt(index);
if (role == u"services") {
std::u16string title = u"Services";
NSString* sub_label = l10n_util::FixUpWindowsStyleLabel(title);
@@ -372,6 +380,14 @@ NSArray* ConvertSharingItemToNS(const SharingItem& item) {
NSMenu* submenu = MenuHasVisibleItems(submenuModel)
? [self menuFromModel:submenuModel]
: MakeEmptySubmenu();
// NSMenuPresentationStylePalette is only available in macOS 14.0+.
if (@available(macOS 14, *)) {
if (customType == u"palette") {
submenu.presentationStyle = NSMenuPresentationStylePalette;
}
}
[submenu setTitle:[item title]];
[item setSubmenu:submenu];

View File

@@ -37,6 +37,18 @@ std::u16string ElectronMenuModel::GetToolTipAt(size_t index) {
return iter == std::end(toolTips_) ? std::u16string() : iter->second;
}
void ElectronMenuModel::SetCustomType(size_t index,
const std::u16string& customType) {
int command_id = GetCommandIdAt(index);
customTypes_[command_id] = customType;
}
std::u16string ElectronMenuModel::GetCustomTypeAt(size_t index) {
const int command_id = GetCommandIdAt(index);
const auto iter = customTypes_.find(command_id);
return iter == std::end(customTypes_) ? std::u16string() : iter->second;
}
void ElectronMenuModel::SetRole(size_t index, const std::u16string& role) {
int command_id = GetCommandIdAt(index);
roles_[command_id] = role;

View File

@@ -84,6 +84,8 @@ class ElectronMenuModel : public ui::SimpleMenuModel {
void SetToolTip(size_t index, const std::u16string& toolTip);
std::u16string GetToolTipAt(size_t index);
void SetCustomType(size_t index, const std::u16string& customType);
std::u16string GetCustomTypeAt(size_t index);
void SetRole(size_t index, const std::u16string& role);
std::u16string GetRoleAt(size_t index);
void SetSecondaryLabel(size_t index, const std::u16string& sublabel);
@@ -125,6 +127,8 @@ class ElectronMenuModel : public ui::SimpleMenuModel {
base::flat_map<int, std::u16string> toolTips_; // command id -> tooltip
base::flat_map<int, std::u16string> roles_; // command id -> role
base::flat_map<int, std::u16string> sublabels_; // command id -> sublabel
base::flat_map<int, std::u16string>
customTypes_; // command id -> custom type
base::ObserverList<Observer> observers_;
base::WeakPtrFactory<ElectronMenuModel> weak_factory_{this};