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.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2015-09-01 11:48:11 +00:00
|
|
|
#include <map>
|
2020-10-20 01:33:06 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-09-01 11:48:11 +00:00
|
|
|
|
2020-10-20 01:33:06 +00:00
|
|
|
#include "base/files/file_path.h"
|
2020-05-27 20:54:52 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
2015-08-10 04:39:05 +00:00
|
|
|
#include "base/observer_list.h"
|
2018-10-25 06:43:50 +00:00
|
|
|
#include "base/observer_list_types.h"
|
2021-06-03 08:05:04 +00:00
|
|
|
#include "third_party/abseil-cpp/absl/types/optional.h"
|
2015-08-10 04:39:05 +00:00
|
|
|
#include "ui/base/models/simple_menu_model.h"
|
2020-10-20 01:33:06 +00:00
|
|
|
#include "url/gurl.h"
|
2015-08-10 04:39:05 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2019-06-26 17:18:53 +00:00
|
|
|
class ElectronMenuModel : public ui::SimpleMenuModel {
|
2015-08-10 04:39:05 +00:00
|
|
|
public:
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-20 01:33:06 +00:00
|
|
|
struct SharingItem {
|
|
|
|
SharingItem();
|
|
|
|
SharingItem(SharingItem&&);
|
|
|
|
SharingItem(const SharingItem&) = delete;
|
|
|
|
~SharingItem();
|
|
|
|
|
2021-06-03 08:05:04 +00:00
|
|
|
absl::optional<std::vector<std::string>> texts;
|
|
|
|
absl::optional<std::vector<GURL>> urls;
|
|
|
|
absl::optional<std::vector<base::FilePath>> file_paths;
|
2020-10-20 01:33:06 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
class Delegate : public ui::SimpleMenuModel::Delegate {
|
|
|
|
public:
|
2018-04-17 23:03:51 +00:00
|
|
|
~Delegate() override {}
|
2016-07-02 02:47:40 +00:00
|
|
|
|
|
|
|
virtual bool GetAcceleratorForCommandIdWithParams(
|
|
|
|
int command_id,
|
|
|
|
bool use_default_accelerator,
|
|
|
|
ui::Accelerator* accelerator) const = 0;
|
|
|
|
|
2018-11-26 18:43:55 +00:00
|
|
|
virtual bool ShouldRegisterAcceleratorForCommandId(
|
|
|
|
int command_id) const = 0;
|
|
|
|
|
2019-02-28 17:00:54 +00:00
|
|
|
virtual bool ShouldCommandIdWorkWhenHidden(int command_id) const = 0;
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-20 01:33:06 +00:00
|
|
|
virtual bool GetSharingItemForCommandId(int command_id,
|
|
|
|
SharingItem* item) const = 0;
|
|
|
|
#endif
|
|
|
|
|
2016-07-02 02:47:40 +00:00
|
|
|
private:
|
|
|
|
// ui::SimpleMenuModel::Delegate:
|
2018-04-18 10:09:45 +00:00
|
|
|
bool GetAcceleratorForCommandId(
|
|
|
|
int command_id,
|
|
|
|
ui::Accelerator* accelerator) const override;
|
2015-08-10 04:39:05 +00:00
|
|
|
};
|
|
|
|
|
2018-10-25 06:43:50 +00:00
|
|
|
class Observer : public base::CheckedObserver {
|
2015-08-10 04:39:05 +00:00
|
|
|
public:
|
2018-10-25 06:43:50 +00:00
|
|
|
~Observer() override {}
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2018-01-27 14:35:58 +00:00
|
|
|
// Notifies the menu will open.
|
|
|
|
virtual void OnMenuWillShow() {}
|
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
// Notifies the menu has been closed.
|
2018-01-27 14:35:58 +00:00
|
|
|
virtual void OnMenuWillClose() {}
|
2015-08-10 04:39:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit ElectronMenuModel(Delegate* delegate);
|
2019-06-26 17:18:53 +00:00
|
|
|
~ElectronMenuModel() override;
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
ElectronMenuModel(const ElectronMenuModel&) = delete;
|
|
|
|
ElectronMenuModel& operator=(const ElectronMenuModel&) = delete;
|
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
|
|
|
|
void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
|
|
|
|
|
2022-08-17 18:35:53 +00:00
|
|
|
void SetToolTip(size_t index, const std::u16string& toolTip);
|
|
|
|
std::u16string GetToolTipAt(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);
|
|
|
|
std::u16string GetSecondaryLabelAt(size_t index) const override;
|
|
|
|
bool GetAcceleratorAtWithParams(size_t index,
|
2016-07-02 02:47:40 +00:00
|
|
|
bool use_default_accelerator,
|
|
|
|
ui::Accelerator* accelerator) const;
|
2022-08-17 18:35:53 +00:00
|
|
|
bool ShouldRegisterAcceleratorAt(size_t index) const;
|
|
|
|
bool WorksWhenHiddenAt(size_t index) const;
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-10-20 01:33:06 +00:00
|
|
|
// Return the SharingItem of menu item.
|
2022-08-17 18:35:53 +00:00
|
|
|
bool GetSharingItemAt(size_t index, SharingItem* item) const;
|
2020-10-20 01:33:06 +00:00
|
|
|
// Set/Get the SharingItem of this menu.
|
|
|
|
void SetSharingItem(SharingItem item);
|
2021-06-03 08:05:04 +00:00
|
|
|
const absl::optional<SharingItem>& GetSharingItem() const;
|
2020-10-20 01:33:06 +00:00
|
|
|
#endif
|
2015-09-01 11:48:11 +00:00
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
// ui::SimpleMenuModel:
|
2017-01-23 08:53:53 +00:00
|
|
|
void MenuWillClose() override;
|
2018-01-27 14:35:58 +00:00
|
|
|
void MenuWillShow() override;
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2020-05-27 20:54:52 +00:00
|
|
|
base::WeakPtr<ElectronMenuModel> GetWeakPtr() {
|
|
|
|
return weak_factory_.GetWeakPtr();
|
|
|
|
}
|
|
|
|
|
2016-07-06 23:04:18 +00:00
|
|
|
using SimpleMenuModel::GetSubmenuModelAt;
|
2022-08-17 18:35:53 +00:00
|
|
|
ElectronMenuModel* GetSubmenuModelAt(size_t index);
|
2016-07-06 23:04:18 +00:00
|
|
|
|
2015-08-10 04:39:05 +00:00
|
|
|
private:
|
|
|
|
Delegate* delegate_; // weak ref.
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2021-06-03 08:05:04 +00:00
|
|
|
absl::optional<SharingItem> sharing_item_;
|
2020-10-20 01:33:06 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::map<int, std::u16string> toolTips_; // command id -> tooltip
|
|
|
|
std::map<int, std::u16string> roles_; // command id -> role
|
|
|
|
std::map<int, std::u16string> sublabels_; // command id -> sublabel
|
2015-09-02 07:16:49 +00:00
|
|
|
base::ObserverList<Observer> observers_;
|
2015-08-10 04:39:05 +00:00
|
|
|
|
2020-05-27 20:54:52 +00:00
|
|
|
base::WeakPtrFactory<ElectronMenuModel> weak_factory_{this};
|
2015-08-10 04:39:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace electron
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
|