forked from adamlaska/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelectron_menu_model.h
More file actions
131 lines (103 loc) · 3.97 KB
/
electron_menu_model.h
File metadata and controls
131 lines (103 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
#define ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_
#include <map>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/base/models/simple_menu_model.h"
#include "url/gurl.h"
namespace electron {
class ElectronMenuModel : public ui::SimpleMenuModel {
public:
#if BUILDFLAG(IS_MAC)
struct SharingItem {
SharingItem();
SharingItem(SharingItem&&);
SharingItem(const SharingItem&) = delete;
~SharingItem();
absl::optional<std::vector<std::string>> texts;
absl::optional<std::vector<GURL>> urls;
absl::optional<std::vector<base::FilePath>> file_paths;
};
#endif
class Delegate : public ui::SimpleMenuModel::Delegate {
public:
~Delegate() override {}
virtual bool GetAcceleratorForCommandIdWithParams(
int command_id,
bool use_default_accelerator,
ui::Accelerator* accelerator) const = 0;
virtual bool ShouldRegisterAcceleratorForCommandId(
int command_id) const = 0;
virtual bool ShouldCommandIdWorkWhenHidden(int command_id) const = 0;
#if BUILDFLAG(IS_MAC)
virtual bool GetSharingItemForCommandId(int command_id,
SharingItem* item) const = 0;
#endif
private:
// ui::SimpleMenuModel::Delegate:
bool GetAcceleratorForCommandId(
int command_id,
ui::Accelerator* accelerator) const override;
};
class Observer : public base::CheckedObserver {
public:
~Observer() override {}
// Notifies the menu will open.
virtual void OnMenuWillShow() {}
// Notifies the menu has been closed.
virtual void OnMenuWillClose() {}
};
explicit ElectronMenuModel(Delegate* delegate);
~ElectronMenuModel() override;
// disable copy
ElectronMenuModel(const ElectronMenuModel&) = delete;
ElectronMenuModel& operator=(const ElectronMenuModel&) = delete;
void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
void SetToolTip(int index, const std::u16string& toolTip);
std::u16string GetToolTipAt(int index);
void SetRole(int index, const std::u16string& role);
std::u16string GetRoleAt(int index);
void SetSecondaryLabel(int index, const std::u16string& sublabel);
std::u16string GetSecondaryLabelAt(int index) const override;
bool GetAcceleratorAtWithParams(int index,
bool use_default_accelerator,
ui::Accelerator* accelerator) const;
bool ShouldRegisterAcceleratorAt(int index) const;
bool WorksWhenHiddenAt(int index) const;
#if BUILDFLAG(IS_MAC)
// Return the SharingItem of menu item.
bool GetSharingItemAt(int index, SharingItem* item) const;
// Set/Get the SharingItem of this menu.
void SetSharingItem(SharingItem item);
const absl::optional<SharingItem>& GetSharingItem() const;
#endif
// ui::SimpleMenuModel:
void MenuWillClose() override;
void MenuWillShow() override;
base::WeakPtr<ElectronMenuModel> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
using SimpleMenuModel::GetSubmenuModelAt;
ElectronMenuModel* GetSubmenuModelAt(int index);
private:
Delegate* delegate_; // weak ref.
#if BUILDFLAG(IS_MAC)
absl::optional<SharingItem> sharing_item_;
#endif
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
base::ObserverList<Observer> observers_;
base::WeakPtrFactory<ElectronMenuModel> weak_factory_{this};
};
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_UI_ELECTRON_MENU_MODEL_H_