Skip to content

Commit d31767d

Browse files
committed
chore: dynamically activate menu options when file is opened
Signed-off-by: alexzurbonsen <alexander.zur.bonsen@tngtech.com>
1 parent f83b6b3 commit d31767d

File tree

2 files changed

+86
-43
lines changed

2 files changed

+86
-43
lines changed

src/ElectronBackend/main/listeners.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import {
4646
setGlobalBackendState,
4747
} from './globalBackendState';
4848
import logger from './logger';
49-
import {activateMenuOptions} from "./menu";
49+
import { activateMenuOptions } from './menu';
5050

5151
const outputFileEnding = '_attributions.json';
5252
const jsonGzipFileExtension = '.json.gz';

src/ElectronBackend/main/menu.ts

+85-42
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,66 @@ import {
2525
} from './notice-document-helpers';
2626
import { UserSettings } from './user-settings';
2727

28-
// type Item = {label: string, id: string}
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;
2943

30-
const MENU_ITEMS = {
31-
file: {label: 'File', id: 'file'},
32-
open: {label: 'Open File', id: 'open'},
33-
save: {label: 'Save', id: 'save'},
34-
export: {label: 'Export', id: 'export'},
35-
followUp: {label: 'Follow-Up', id: 'follow-up'},
36-
compactComponentList: {label: 'Compact component list', id: 'compact-list'},
37-
detailedComponentList: {label: 'Detailed component list', id: 'detailed-list'},
38-
spdxYAML: {label: 'SPDX (yaml)', id: 'spdx-yaml'},
39-
spdxJSON: {label: 'SPDX (json)', id: 'spdx-json'},
40-
projectMetadata: {label: 'Project Metadata', id: 'project-metadata'},
41-
projectStatistics: {label: 'Project Statistics', id: 'project-statistics'},
42-
pathToSources: {label: 'Set Path to Sources', id: 'path-to-sources'},
43-
quit: {label: 'Quit', id: 'quit'},
44-
}
44+
type Item = { label: string; id: string };
4545

46-
const INITIALLY_DISABLED_MENU_ITEM_IDS: Array<string> = [MENU_ITEMS.save.id, MENU_ITEMS.export.id, MENU_ITEMS.projectMetadata.id, MENU_ITEMS.projectStatistics.id]
46+
const MENU_ITEMS: Record<(typeof INITIALLY_DISABLED_MENU_ITEMS)[number], Item> =
47+
{
48+
save: { label: 'Save', id: 'save' },
49+
followUp: { label: 'Follow-Up', id: 'follow-up' },
50+
compactComponentList: {
51+
label: 'Compact component list',
52+
id: 'compact-list',
53+
},
54+
detailedComponentList: {
55+
label: 'Detailed component list',
56+
id: 'detailed-list',
57+
},
58+
spdxYAML: { label: 'SPDX (yaml)', id: 'spdx-yaml' },
59+
spdxJSON: { label: 'SPDX (json)', id: 'spdx-json' },
60+
projectMetadata: { label: 'Project Metadata', id: 'project-metadata' },
61+
projectStatistics: {
62+
label: 'Project Statistics',
63+
id: 'project-statistics',
64+
},
65+
selectAll: { label: 'Select All', id: 'select-all' },
66+
searchAttributions: {
67+
label: 'Search Attributions',
68+
id: 'search-attributions',
69+
},
70+
searchSignals: { label: 'Search Signals', id: 'search-signals' },
71+
searchResourcesAll: {
72+
label: 'Search All Resources',
73+
id: 'search-resources-all',
74+
},
75+
searchResourceLinked: {
76+
label: 'Search Linked Resources',
77+
id: 'search-resources-linked',
78+
},
79+
};
4780

4881
export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
4982
const webContents = mainWindow.webContents;
5083
const qaMode = await UserSettings.get('qaMode');
5184

5285
return Menu.buildFromTemplate([
5386
{
54-
label: MENU_ITEMS.file.label,
87+
label: 'File',
5588
submenu: [
5689
{
5790
icon: getIconBasedOnTheme(
@@ -80,13 +113,11 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
80113
enabled: false,
81114
},
82115
{
83-
label: MENU_ITEMS.export.label,
116+
label: 'Export',
84117
icon: getIconBasedOnTheme(
85118
'icons/export-white.png',
86119
'icons/export-black.png',
87120
),
88-
id: MENU_ITEMS.export.id,
89-
enabled: false,
90121
submenu: [
91122
{
92123
label: MENU_ITEMS.followUp.label,
@@ -102,6 +133,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
102133
ExportType.FollowUp,
103134
);
104135
},
136+
id: MENU_ITEMS.followUp.id,
137+
enabled: false,
105138
},
106139
{
107140
icon: getIconBasedOnTheme(
@@ -117,7 +150,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
117150
ExportType.CompactBom,
118151
);
119152
},
120-
153+
id: MENU_ITEMS.compactComponentList.id,
154+
enabled: false,
121155
},
122156
{
123157
icon: getIconBasedOnTheme(
@@ -135,6 +169,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
135169
ExportType.DetailedBom,
136170
);
137171
},
172+
id: MENU_ITEMS.detailedComponentList.id,
173+
enabled: false,
138174
},
139175
{
140176
icon: getIconBasedOnTheme(
@@ -150,6 +186,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
150186
ExportType.SpdxDocumentYaml,
151187
);
152188
},
189+
id: MENU_ITEMS.spdxYAML.id,
190+
enabled: false,
153191
},
154192
{
155193
icon: getIconBasedOnTheme(
@@ -165,6 +203,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
165203
ExportType.SpdxDocumentJson,
166204
);
167205
},
206+
id: MENU_ITEMS.spdxJSON.id,
207+
enabled: false,
168208
},
169209
],
170210
},
@@ -211,7 +251,7 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
211251
'icons/restore-white.png',
212252
'icons/restore-black.png',
213253
),
214-
label: MENU_ITEMS.pathToSources.label,
254+
label: 'Set Path to Sources',
215255
click: () => {
216256
getSelectBaseURLListener(mainWindow)();
217257
},
@@ -221,7 +261,7 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
221261
'icons/quit-white.png',
222262
'icons/quit-black.png',
223263
),
224-
label: MENU_ITEMS.quit.label,
264+
label: 'Quit',
225265
accelerator: 'CmdOrCtrl+Q',
226266
click: () => {
227267
app.quit();
@@ -283,62 +323,72 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
283323
'icons/select-all-white.png',
284324
'icons/select-all-black.png',
285325
),
286-
label: 'Select All',
326+
label: MENU_ITEMS.selectAll.label,
287327
accelerator: 'CmdOrCtrl+A',
288328
role: 'selectAll',
329+
id: MENU_ITEMS.selectAll.id,
330+
enabled: false,
289331
},
290332
{ type: 'separator' },
291333
{
292334
icon: getIconBasedOnTheme(
293335
'icons/magnifying-glass-white.png',
294336
'icons/magnifying-glass-black.png',
295337
),
296-
label: 'Search Attributions',
338+
label: MENU_ITEMS.searchAttributions.label,
297339
accelerator: 'CmdOrCtrl+Shift+A',
298340
click: () => {
299341
if (isFileLoaded(getGlobalBackendState())) {
300342
webContents.send(AllowedFrontendChannels.SearchAttributions);
301343
}
302344
},
345+
id: MENU_ITEMS.searchAttributions.id,
346+
enabled: false,
303347
},
304348
{
305349
icon: getIconBasedOnTheme(
306350
'icons/magnifying-glass-white.png',
307351
'icons/magnifying-glass-black.png',
308352
),
309-
label: 'Search Signals',
353+
label: MENU_ITEMS.searchSignals.label,
310354
accelerator: 'CmdOrCtrl+Shift+S',
311355
click: () => {
312356
if (isFileLoaded(getGlobalBackendState())) {
313357
webContents.send(AllowedFrontendChannels.SearchSignals);
314358
}
315359
},
360+
id: MENU_ITEMS.searchSignals.id,
361+
enabled: false,
316362
},
317363
{
318364
icon: getIconBasedOnTheme(
319365
'icons/search-white.png',
320366
'icons/search-black.png',
321367
),
322-
label: 'Search All Resources',
368+
label: MENU_ITEMS.searchResourcesAll.label,
323369
accelerator: 'CmdOrCtrl+Shift+R',
324370
click: () => {
325371
if (isFileLoaded(getGlobalBackendState())) {
326372
webContents.send(AllowedFrontendChannels.SearchResources);
327373
}
328374
},
375+
id: MENU_ITEMS.searchResourcesAll.id,
376+
enabled: false,
329377
},
330378
{
331379
icon: getIconBasedOnTheme(
332380
'icons/search-white.png',
333381
'icons/search-black.png',
334382
),
335-
label: 'Search Linked Resources',
383+
label: MENU_ITEMS.searchResourceLinked.label,
336384
accelerator: 'CmdOrCtrl+Shift+L',
337385
click: () => {
338386
if (isFileLoaded(getGlobalBackendState())) {
339387
webContents.send(AllowedFrontendChannels.SearchLinkedResources);
340388
}
341389
},
390+
id: MENU_ITEMS.searchResourceLinked.id,
391+
enabled: false,
342392
},
343393
],
344394
},
@@ -482,17 +532,10 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
482532

483533
export function activateMenuOptions(): void {
484534
const menu = Menu.getApplicationMenu();
485-
// INITIALLY_DISABLED_MENU_ITEM_IDS.forEach((id) => {
486-
// console.log("key: ", id)
487-
// const item = menu?.getMenuItemById(id)
488-
// if (item) {
489-
// console.log("enable: ", item.label)
490-
// item.enabled = true;
491-
// }
492-
// })
493-
const save = menu?.getMenuItemById(MENU_ITEMS['save'].id)
494-
if (save) {
495-
console.log("heeereeeee");
496-
save.enabled = true;
497-
}
535+
INITIALLY_DISABLED_MENU_ITEMS.forEach((key) => {
536+
const menuItem = menu?.getMenuItemById(MENU_ITEMS[key].id);
537+
if (menuItem) {
538+
menuItem.enabled = true;
539+
}
540+
});
498541
}

0 commit comments

Comments
 (0)