aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/infrastructure/electron/impl/setApplicationMenu.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/infrastructure/electron/impl/setApplicationMenu.ts')
-rw-r--r--packages/main/src/infrastructure/electron/impl/setApplicationMenu.ts177
1 files changed, 177 insertions, 0 deletions
diff --git a/packages/main/src/infrastructure/electron/impl/setApplicationMenu.ts b/packages/main/src/infrastructure/electron/impl/setApplicationMenu.ts
new file mode 100644
index 0000000..7e45401
--- /dev/null
+++ b/packages/main/src/infrastructure/electron/impl/setApplicationMenu.ts
@@ -0,0 +1,177 @@
1/*
2 * Copyright (C) 2022 Kristóf Marussy <kristof@marussy.com>
3 *
4 * This file is part of Sophie.
5 *
6 * Sophie is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, version 3.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: AGPL-3.0-only
19 */
20
21import { Menu, MenuItemConstructorOptions } from 'electron';
22import { autorun } from 'mobx';
23import { addDisposer } from 'mobx-state-tree';
24
25import type MainStore from '../../../stores/MainStore.js';
26
27export default function setApplicationMenu(
28 store: MainStore,
29 devMode: boolean,
30 isMac: boolean,
31): void {
32 const dispose = autorun(() => {
33 const translation = store.useTranslation();
34 if (!translation.ready) {
35 return;
36 }
37 const { t } = translation;
38
39 const { settings, shared, visibleService } = store;
40 const { selectedService } = settings;
41 const {
42 canSwitchServices,
43 canToggleLocationBar,
44 locationBarVisible,
45 services,
46 } = shared;
47
48 const template: MenuItemConstructorOptions[] = [
49 ...(isMac ? ([{ role: 'appMenu' }] as MenuItemConstructorOptions[]) : []),
50 { role: 'fileMenu' },
51 { role: 'editMenu' },
52 {
53 role: 'viewMenu',
54 submenu: [
55 {
56 label: t<string>('menu.view.showLocationBar'),
57 accelerator: 'CommandOrControl+Shift+L',
58 type: 'checkbox',
59 checked: locationBarVisible,
60 enabled: canToggleLocationBar,
61 click() {
62 settings.toggleLocationBar();
63 },
64 },
65 { type: 'separator' },
66 {
67 label: t<string>('menu.view.reload'),
68 accelerator: 'CommandOrControl+R',
69 enabled: selectedService !== undefined,
70 click() {
71 selectedService?.reload(false);
72 },
73 },
74 {
75 label: t<string>('menu.view.forceReload'),
76 accelerator: 'CommandOrControl+Shift+R',
77 enabled: selectedService !== undefined,
78 click() {
79 selectedService?.reload(true);
80 },
81 },
82 {
83 label: t<string>('menu.view.toggleDeveloperTools'),
84 accelerator: 'CommandOrControl+Shift+I',
85 enabled: visibleService !== undefined,
86 click() {
87 visibleService?.toggleDeveloperTools();
88 },
89 },
90 { type: 'separator' },
91 ...(devMode
92 ? ([
93 {
94 role: 'forceReload',
95 label: t<string>('menu.view.reloadSophie'),
96 accelerator: 'CommandOrControl+Shift+Alt+R',
97 },
98 {
99 role: 'toggleDevTools',
100 label: t<string>('menu.view.toggleSophieDeveloperTools'),
101 accelerator: 'CommandOrControl+Shift+Alt+I',
102 },
103 { type: 'separator' },
104 ] as MenuItemConstructorOptions[])
105 : []),
106 { role: 'togglefullscreen' },
107 ],
108 },
109 {
110 label: t<string>('menu.servicesMenu'),
111 submenu: [
112 {
113 label: t<string>('menu.services.nextService'),
114 accelerator: 'CommandOrControl+Tab',
115 enabled: canSwitchServices,
116 click() {
117 shared.activateNextService();
118 },
119 },
120 {
121 label: t<string>('menu.services.previousService'),
122 accelerator: 'CommandOrControl+Shift+Tab',
123 enabled: canSwitchServices,
124 click() {
125 shared.activatePreviousService();
126 },
127 },
128 ...(services.length > 0
129 ? ([
130 { type: 'separator' },
131 ...services.map(
132 (service, index): MenuItemConstructorOptions => ({
133 label: service.config.name,
134 type: 'radio',
135 ...(index < 9
136 ? {
137 accelerator: `CommandOrControl+${index + 1}`,
138 }
139 : {}),
140 checked: selectedService === service,
141 click() {
142 settings.setSelectedService(service);
143 },
144 }),
145 ),
146 ] as MenuItemConstructorOptions[])
147 : []),
148 ],
149 },
150 { role: 'windowMenu' },
151 {
152 role: 'help',
153 submenu: [
154 {
155 label: t<string>('menu.help.gitlab'),
156 click() {
157 store.openWebpageInBrowser();
158 },
159 },
160 ...(isMac
161 ? []
162 : ([
163 {
164 role: 'about',
165 click() {
166 store.openAboutDialog();
167 },
168 },
169 ] as MenuItemConstructorOptions[])),
170 ],
171 },
172 ];
173 const menu = Menu.buildFromTemplate(template);
174 Menu.setApplicationMenu(menu);
175 });
176 addDisposer(store, dispose);
177}