2015-08-10 04:39:05 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/ui/electron_menu_model.h"
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2015-09-01 11:48:11 +00:00
|
|
|
#include "base/stl_util.h"
|
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
namespace electron {
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-20 01:33:06 +00:00
|
|
|
ElectronMenuModel::SharingItem::SharingItem() = default;
|
|
|
|
ElectronMenuModel::SharingItem::SharingItem(SharingItem&&) = default;
|
|
|
|
ElectronMenuModel::SharingItem::~SharingItem() = default;
|
|
|
|
#endif
|
|
|
|
|
2018-09-19 11:10:26 +00:00
|
|
|
bool ElectronMenuModel::Delegate::GetAcceleratorForCommandId(
|
|
|
|
int command_id,
|
2018-04-17 23:47:47 +00:00
|
|
|
ui::Accelerator* accelerator) const {
|
2018-09-19 11:10:26 +00:00
|
|
|
return GetAcceleratorForCommandIdWithParams(command_id, false, accelerator);
|
2018-04-17 23:47:47 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
ElectronMenuModel::ElectronMenuModel(Delegate* delegate)
|
2018-04-18 01:55:30 +00:00
|
|
|
: ui::SimpleMenuModel(delegate), delegate_(delegate) {}
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
ElectronMenuModel::~ElectronMenuModel() = default;
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
void ElectronMenuModel::SetToolTip(size_t index,
|
|
|
|
const std::u16string& toolTip) {
|
2019-07-11 08:56:22 +00:00
|
|
|
int command_id = GetCommandIdAt(index);
|
|
|
|
toolTips_[command_id] = toolTip;
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
std::u16string ElectronMenuModel::GetToolTipAt(size_t index) {
|
2019-08-28 14:39:21 +00:00
|
|
|
const int command_id = GetCommandIdAt(index);
|
|
|
|
const auto iter = toolTips_.find(command_id);
|
2021-03-16 16:18:45 +00:00
|
|
|
return iter == std::end(toolTips_) ? std::u16string() : iter->second;
|
2019-07-11 08:56:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
void ElectronMenuModel::SetRole(size_t index, const std::u16string& role) {
|
2016-06-21 22:25:14 +00:00
|
|
|
int command_id = GetCommandIdAt(index);
|
|
|
|
roles_[command_id] = role;
|
2015-09-01 11:48:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
std::u16string ElectronMenuModel::GetRoleAt(size_t index) {
|
2019-08-28 14:39:21 +00:00
|
|
|
const int command_id = GetCommandIdAt(index);
|
|
|
|
const auto iter = roles_.find(command_id);
|
2021-03-16 16:18:45 +00:00
|
|
|
return iter == std::end(roles_) ? std::u16string() : iter->second;
|
2015-09-01 11:48:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
void ElectronMenuModel::SetSecondaryLabel(size_t index,
|
2021-03-16 16:18:45 +00:00
|
|
|
const std::u16string& sublabel) {
|
2019-10-04 06:58:54 +00:00
|
|
|
int command_id = GetCommandIdAt(index);
|
|
|
|
sublabels_[command_id] = sublabel;
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
std::u16string ElectronMenuModel::GetSecondaryLabelAt(size_t index) const {
|
2019-10-04 06:58:54 +00:00
|
|
|
int command_id = GetCommandIdAt(index);
|
|
|
|
const auto iter = sublabels_.find(command_id);
|
2021-03-16 16:18:45 +00:00
|
|
|
return iter == std::end(sublabels_) ? std::u16string() : iter->second;
|
2019-10-04 06:58:54 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 02:47:40 +00:00
|
|
|
bool ElectronMenuModel::GetAcceleratorAtWithParams(
|
2022-08-17 18:35:53 +00:00
|
|
|
size_t index,
|
2016-07-02 02:47:40 +00:00
|
|
|
bool use_default_accelerator,
|
|
|
|
ui::Accelerator* accelerator) const {
|
|
|
|
if (delegate_) {
|
|
|
|
return delegate_->GetAcceleratorForCommandIdWithParams(
|
|
|
|
GetCommandIdAt(index), use_default_accelerator, accelerator);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
bool ElectronMenuModel::ShouldRegisterAcceleratorAt(size_t index) const {
|
2018-11-26 18:43:55 +00:00
|
|
|
if (delegate_) {
|
|
|
|
return delegate_->ShouldRegisterAcceleratorForCommandId(
|
|
|
|
GetCommandIdAt(index));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
bool ElectronMenuModel::WorksWhenHiddenAt(size_t index) const {
|
2019-02-28 17:00:54 +00:00
|
|
|
if (delegate_) {
|
|
|
|
return delegate_->ShouldCommandIdWorkWhenHidden(GetCommandIdAt(index));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2022-08-17 18:35:53 +00:00
|
|
|
bool ElectronMenuModel::GetSharingItemAt(size_t index,
|
|
|
|
SharingItem* item) const {
|
2020-10-20 01:33:06 +00:00
|
|
|
if (delegate_)
|
|
|
|
return delegate_->GetSharingItemForCommandId(GetCommandIdAt(index), item);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElectronMenuModel::SetSharingItem(SharingItem item) {
|
|
|
|
sharing_item_.emplace(std::move(item));
|
|
|
|
}
|
|
|
|
|
2024-01-10 22:23:35 +00:00
|
|
|
const std::optional<ElectronMenuModel::SharingItem>&
|
2020-10-20 01:33:06 +00:00
|
|
|
ElectronMenuModel::GetSharingItem() const {
|
|
|
|
return sharing_item_;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-23 08:53:53 +00:00
|
|
|
void ElectronMenuModel::MenuWillClose() {
|
|
|
|
ui::SimpleMenuModel::MenuWillClose();
|
2018-01-27 14:35:58 +00:00
|
|
|
for (Observer& observer : observers_) {
|
|
|
|
observer.OnMenuWillClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElectronMenuModel::MenuWillShow() {
|
|
|
|
ui::SimpleMenuModel::MenuWillShow();
|
|
|
|
for (Observer& observer : observers_) {
|
|
|
|
observer.OnMenuWillShow();
|
|
|
|
}
|
2015-08-10 04:39:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
ElectronMenuModel* ElectronMenuModel::GetSubmenuModelAt(size_t index) {
|
2016-07-06 23:04:18 +00:00
|
|
|
return static_cast<ElectronMenuModel*>(
|
2016-07-07 21:28:01 +00:00
|
|
|
ui::SimpleMenuModel::GetSubmenuModelAt(index));
|
2016-07-06 23:04:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
} // namespace electron
|