Skip to content

Commit 91ebb66

Browse files
Merge pull request #2621 from opossum-tool/dynamically-enable-menu-options
chore: dynamically enable menu options
2 parents 4ee0077 + dbe5b62 commit 91ebb66

File tree

2 files changed

+108
-13
lines changed

2 files changed

+108
-13
lines changed

src/ElectronBackend/main/listeners.ts

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
setGlobalBackendState,
4747
} from './globalBackendState';
4848
import logger from './logger';
49+
import { activateMenuItems } from './menu';
4950

5051
const outputFileEnding = '_attributions.json';
5152
const jsonGzipFileExtension = '.json.gz';
@@ -139,6 +140,7 @@ export async function handleOpeningFile(
139140
}
140141

141142
await openFile(mainWindow, filePath);
143+
activateMenuItems();
142144
}
143145

144146
function initializeGlobalBackendState(

src/ElectronBackend/main/menu.ts

+106-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,61 @@ import {
2525
} from './notice-document-helpers';
2626
import { UserSettings } from './user-settings';
2727

28+
const INITIALLY_DISABLED_MENU_ITEMS = [
29+
'save',
30+
'projectMetadata',
31+
'projectStatistics',
32+
'followUp',
33+
'compactComponentList',
34+
'detailedComponentList',
35+
'spdxYAML',
36+
'spdxJSON',
37+
'selectAll',
38+
'searchAttributions',
39+
'searchSignals',
40+
'searchResourcesAll',
41+
'searchResourceLinked',
42+
] as const;
43+
44+
type Item = { label: string; id: string };
45+
46+
const INITIALLY_DISABLED_ITEMS_INFO: Record<
47+
(typeof INITIALLY_DISABLED_MENU_ITEMS)[number],
48+
Item
49+
> = {
50+
save: { label: 'Save', id: 'save' },
51+
followUp: { label: 'Follow-Up', id: 'follow-up' },
52+
compactComponentList: {
53+
label: 'Compact component list',
54+
id: 'compact-list',
55+
},
56+
detailedComponentList: {
57+
label: 'Detailed component list',
58+
id: 'detailed-list',
59+
},
60+
spdxYAML: { label: 'SPDX (yaml)', id: 'spdx-yaml' },
61+
spdxJSON: { label: 'SPDX (json)', id: 'spdx-json' },
62+
projectMetadata: { label: 'Project Metadata', id: 'project-metadata' },
63+
projectStatistics: {
64+
label: 'Project Statistics',
65+
id: 'project-statistics',
66+
},
67+
selectAll: { label: 'Select All', id: 'select-all' },
68+
searchAttributions: {
69+
label: 'Search Attributions',
70+
id: 'search-attributions',
71+
},
72+
searchSignals: { label: 'Search Signals', id: 'search-signals' },
73+
searchResourcesAll: {
74+
label: 'Search All Resources',
75+
id: 'search-resources-all',
76+
},
77+
searchResourceLinked: {
78+
label: 'Search Linked Resources',
79+
id: 'search-resources-linked',
80+
},
81+
};
82+
2883
export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
2984
const webContents = mainWindow.webContents;
3085
const qaMode = await UserSettings.get('qaMode');
@@ -49,13 +104,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
49104
'icons/save-white.png',
50105
'icons/save-black.png',
51106
),
52-
label: 'Save',
107+
label: INITIALLY_DISABLED_ITEMS_INFO.save.label,
53108
accelerator: 'CmdOrCtrl+S',
54109
click: () => {
55110
webContents.send(AllowedFrontendChannels.SaveFileRequest, {
56111
saveFile: true,
57112
});
58113
},
114+
id: INITIALLY_DISABLED_ITEMS_INFO.save.id,
115+
enabled: false,
59116
},
60117
{
61118
label: 'Export',
@@ -65,7 +122,7 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
65122
),
66123
submenu: [
67124
{
68-
label: 'Follow-Up',
125+
label: INITIALLY_DISABLED_ITEMS_INFO.followUp.label,
69126
icon: getIconBasedOnTheme(
70127
'icons/follow-up-white.png',
71128
'icons/follow-up-black.png',
@@ -78,13 +135,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
78135
ExportType.FollowUp,
79136
);
80137
},
138+
id: INITIALLY_DISABLED_ITEMS_INFO.followUp.id,
139+
enabled: false,
81140
},
82141
{
83142
icon: getIconBasedOnTheme(
84143
'icons/com-list-white.png',
85144
'icons/com-list-black.png',
86145
),
87-
label: 'Compact component list',
146+
label: INITIALLY_DISABLED_ITEMS_INFO.compactComponentList.label,
88147
click: () => {
89148
setLoadingState(mainWindow.webContents, true);
90149
logger.info('Preparing data for compact component list export');
@@ -93,13 +152,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
93152
ExportType.CompactBom,
94153
);
95154
},
155+
id: INITIALLY_DISABLED_ITEMS_INFO.compactComponentList.id,
156+
enabled: false,
96157
},
97158
{
98159
icon: getIconBasedOnTheme(
99160
'icons/det-list-white.png',
100161
'icons/det-list-black.png',
101162
),
102-
label: 'Detailed component list',
163+
label: INITIALLY_DISABLED_ITEMS_INFO.detailedComponentList.label,
103164
click: () => {
104165
setLoadingState(mainWindow.webContents, true);
105166
logger.info(
@@ -110,13 +171,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
110171
ExportType.DetailedBom,
111172
);
112173
},
174+
id: INITIALLY_DISABLED_ITEMS_INFO.detailedComponentList.id,
175+
enabled: false,
113176
},
114177
{
115178
icon: getIconBasedOnTheme(
116179
'icons/yaml-white.png',
117180
'icons/yaml-black.png',
118181
),
119-
label: 'SPDX (yaml)',
182+
label: INITIALLY_DISABLED_ITEMS_INFO.spdxYAML.label,
120183
click: () => {
121184
setLoadingState(mainWindow.webContents, true);
122185
logger.info('Preparing data for SPDX (yaml) export');
@@ -125,13 +188,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
125188
ExportType.SpdxDocumentYaml,
126189
);
127190
},
191+
id: INITIALLY_DISABLED_ITEMS_INFO.spdxYAML.id,
192+
enabled: false,
128193
},
129194
{
130195
icon: getIconBasedOnTheme(
131196
'icons/json-white.png',
132197
'icons/json-black.png',
133198
),
134-
label: 'SPDX (json)',
199+
label: INITIALLY_DISABLED_ITEMS_INFO.spdxJSON.label,
135200
click: () => {
136201
setLoadingState(mainWindow.webContents, true);
137202
logger.info('Preparing data for SPDX (json) export');
@@ -140,6 +205,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
140205
ExportType.SpdxDocumentJson,
141206
);
142207
},
208+
id: INITIALLY_DISABLED_ITEMS_INFO.spdxJSON.id,
209+
enabled: false,
143210
},
144211
],
145212
},
@@ -148,7 +215,7 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
148215
'icons/about-white.png',
149216
'icons/about-black.png',
150217
),
151-
label: 'Project Metadata',
218+
label: INITIALLY_DISABLED_ITEMS_INFO.projectMetadata.label,
152219
click: () => {
153220
if (isFileLoaded(getGlobalBackendState())) {
154221
webContents.send(
@@ -159,13 +226,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
159226
);
160227
}
161228
},
229+
id: INITIALLY_DISABLED_ITEMS_INFO.projectMetadata.id,
230+
enabled: false,
162231
},
163232
{
164233
icon: getIconBasedOnTheme(
165234
'icons/statictics-white.png',
166235
'icons/statictics-black.png',
167236
),
168-
label: 'Project Statistics',
237+
label: INITIALLY_DISABLED_ITEMS_INFO.projectStatistics.label,
169238
click: () => {
170239
if (isFileLoaded(getGlobalBackendState())) {
171240
webContents.send(
@@ -176,6 +245,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
176245
);
177246
}
178247
},
248+
id: INITIALLY_DISABLED_ITEMS_INFO.projectStatistics.id,
249+
enabled: false,
179250
},
180251
{
181252
icon: getIconBasedOnTheme(
@@ -254,62 +325,72 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
254325
'icons/select-all-white.png',
255326
'icons/select-all-black.png',
256327
),
257-
label: 'Select All',
328+
label: INITIALLY_DISABLED_ITEMS_INFO.selectAll.label,
258329
accelerator: 'CmdOrCtrl+A',
259330
role: 'selectAll',
331+
id: INITIALLY_DISABLED_ITEMS_INFO.selectAll.id,
332+
enabled: false,
260333
},
261334
{ type: 'separator' },
262335
{
263336
icon: getIconBasedOnTheme(
264337
'icons/magnifying-glass-white.png',
265338
'icons/magnifying-glass-black.png',
266339
),
267-
label: 'Search Attributions',
340+
label: INITIALLY_DISABLED_ITEMS_INFO.searchAttributions.label,
268341
accelerator: 'CmdOrCtrl+Shift+A',
269342
click: () => {
270343
if (isFileLoaded(getGlobalBackendState())) {
271344
webContents.send(AllowedFrontendChannels.SearchAttributions);
272345
}
273346
},
347+
id: INITIALLY_DISABLED_ITEMS_INFO.searchAttributions.id,
348+
enabled: false,
274349
},
275350
{
276351
icon: getIconBasedOnTheme(
277352
'icons/magnifying-glass-white.png',
278353
'icons/magnifying-glass-black.png',
279354
),
280-
label: 'Search Signals',
355+
label: INITIALLY_DISABLED_ITEMS_INFO.searchSignals.label,
281356
accelerator: 'CmdOrCtrl+Shift+S',
282357
click: () => {
283358
if (isFileLoaded(getGlobalBackendState())) {
284359
webContents.send(AllowedFrontendChannels.SearchSignals);
285360
}
286361
},
362+
id: INITIALLY_DISABLED_ITEMS_INFO.searchSignals.id,
363+
enabled: false,
287364
},
288365
{
289366
icon: getIconBasedOnTheme(
290367
'icons/search-white.png',
291368
'icons/search-black.png',
292369
),
293-
label: 'Search All Resources',
370+
label: INITIALLY_DISABLED_ITEMS_INFO.searchResourcesAll.label,
294371
accelerator: 'CmdOrCtrl+Shift+R',
295372
click: () => {
296373
if (isFileLoaded(getGlobalBackendState())) {
297374
webContents.send(AllowedFrontendChannels.SearchResources);
298375
}
299376
},
377+
id: INITIALLY_DISABLED_ITEMS_INFO.searchResourcesAll.id,
378+
enabled: false,
300379
},
301380
{
302381
icon: getIconBasedOnTheme(
303382
'icons/search-white.png',
304383
'icons/search-black.png',
305384
),
306-
label: 'Search Linked Resources',
385+
label: INITIALLY_DISABLED_ITEMS_INFO.searchResourceLinked.label,
307386
accelerator: 'CmdOrCtrl+Shift+L',
308387
click: () => {
309388
if (isFileLoaded(getGlobalBackendState())) {
310389
webContents.send(AllowedFrontendChannels.SearchLinkedResources);
311390
}
312391
},
392+
id: INITIALLY_DISABLED_ITEMS_INFO.searchResourceLinked.id,
393+
enabled: false,
313394
},
314395
],
315396
},
@@ -450,3 +531,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
450531
},
451532
]);
452533
}
534+
535+
export function activateMenuItems(): void {
536+
const menu = Menu.getApplicationMenu();
537+
INITIALLY_DISABLED_MENU_ITEMS.forEach((key) => {
538+
const menuItem = menu?.getMenuItemById(
539+
INITIALLY_DISABLED_ITEMS_INFO[key].id,
540+
);
541+
if (menuItem) {
542+
menuItem.enabled = true;
543+
}
544+
});
545+
}

0 commit comments

Comments
 (0)