Skip to content

Commit 2be9cac

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 2be9cac

File tree

2 files changed

+103
-56
lines changed

2 files changed

+103
-56
lines changed

src/ElectronBackend/main/listeners.ts

+2-2
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 { activateMenuItems } from './menu';
5050

5151
const outputFileEnding = '_attributions.json';
5252
const jsonGzipFileExtension = '.json.gz';
@@ -140,7 +140,7 @@ export async function handleOpeningFile(
140140
}
141141

142142
await openFile(mainWindow, filePath);
143-
activateMenuOptions();
143+
activateMenuItems();
144144
}
145145

146146
function initializeGlobalBackendState(

src/ElectronBackend/main/menu.ts

+101-54
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,68 @@ 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 INITIALLY_DISABLED_ITEM_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+
};
4782

4883
export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
4984
const webContents = mainWindow.webContents;
5085
const qaMode = await UserSettings.get('qaMode');
5186

5287
return Menu.buildFromTemplate([
5388
{
54-
label: MENU_ITEMS.file.label,
89+
label: 'File',
5590
submenu: [
5691
{
5792
icon: getIconBasedOnTheme(
@@ -69,27 +104,25 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
69104
'icons/save-white.png',
70105
'icons/save-black.png',
71106
),
72-
label: MENU_ITEMS.save.label,
107+
label: INITIALLY_DISABLED_ITEM_INFO.save.label,
73108
accelerator: 'CmdOrCtrl+S',
74109
click: () => {
75110
webContents.send(AllowedFrontendChannels.SaveFileRequest, {
76111
saveFile: true,
77112
});
78113
},
79-
id: MENU_ITEMS.save.id,
114+
id: INITIALLY_DISABLED_ITEM_INFO.save.id,
80115
enabled: false,
81116
},
82117
{
83-
label: MENU_ITEMS.export.label,
118+
label: 'Export',
84119
icon: getIconBasedOnTheme(
85120
'icons/export-white.png',
86121
'icons/export-black.png',
87122
),
88-
id: MENU_ITEMS.export.id,
89-
enabled: false,
90123
submenu: [
91124
{
92-
label: MENU_ITEMS.followUp.label,
125+
label: INITIALLY_DISABLED_ITEM_INFO.followUp.label,
93126
icon: getIconBasedOnTheme(
94127
'icons/follow-up-white.png',
95128
'icons/follow-up-black.png',
@@ -102,13 +135,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
102135
ExportType.FollowUp,
103136
);
104137
},
138+
id: INITIALLY_DISABLED_ITEM_INFO.followUp.id,
139+
enabled: false,
105140
},
106141
{
107142
icon: getIconBasedOnTheme(
108143
'icons/com-list-white.png',
109144
'icons/com-list-black.png',
110145
),
111-
label: MENU_ITEMS.compactComponentList.label,
146+
label: INITIALLY_DISABLED_ITEM_INFO.compactComponentList.label,
112147
click: () => {
113148
setLoadingState(mainWindow.webContents, true);
114149
logger.info('Preparing data for compact component list export');
@@ -117,14 +152,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
117152
ExportType.CompactBom,
118153
);
119154
},
120-
155+
id: INITIALLY_DISABLED_ITEM_INFO.compactComponentList.id,
156+
enabled: false,
121157
},
122158
{
123159
icon: getIconBasedOnTheme(
124160
'icons/det-list-white.png',
125161
'icons/det-list-black.png',
126162
),
127-
label: MENU_ITEMS.detailedComponentList.label,
163+
label: INITIALLY_DISABLED_ITEM_INFO.detailedComponentList.label,
128164
click: () => {
129165
setLoadingState(mainWindow.webContents, true);
130166
logger.info(
@@ -135,13 +171,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
135171
ExportType.DetailedBom,
136172
);
137173
},
174+
id: INITIALLY_DISABLED_ITEM_INFO.detailedComponentList.id,
175+
enabled: false,
138176
},
139177
{
140178
icon: getIconBasedOnTheme(
141179
'icons/yaml-white.png',
142180
'icons/yaml-black.png',
143181
),
144-
label: MENU_ITEMS.spdxYAML.label,
182+
label: INITIALLY_DISABLED_ITEM_INFO.spdxYAML.label,
145183
click: () => {
146184
setLoadingState(mainWindow.webContents, true);
147185
logger.info('Preparing data for SPDX (yaml) export');
@@ -150,13 +188,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
150188
ExportType.SpdxDocumentYaml,
151189
);
152190
},
191+
id: INITIALLY_DISABLED_ITEM_INFO.spdxYAML.id,
192+
enabled: false,
153193
},
154194
{
155195
icon: getIconBasedOnTheme(
156196
'icons/json-white.png',
157197
'icons/json-black.png',
158198
),
159-
label: MENU_ITEMS.spdxJSON.label,
199+
label: INITIALLY_DISABLED_ITEM_INFO.spdxJSON.label,
160200
click: () => {
161201
setLoadingState(mainWindow.webContents, true);
162202
logger.info('Preparing data for SPDX (json) export');
@@ -165,6 +205,8 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
165205
ExportType.SpdxDocumentJson,
166206
);
167207
},
208+
id: INITIALLY_DISABLED_ITEM_INFO.spdxJSON.id,
209+
enabled: false,
168210
},
169211
],
170212
},
@@ -173,7 +215,7 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
173215
'icons/about-white.png',
174216
'icons/about-black.png',
175217
),
176-
label: MENU_ITEMS.projectMetadata.label,
218+
label: INITIALLY_DISABLED_ITEM_INFO.projectMetadata.label,
177219
click: () => {
178220
if (isFileLoaded(getGlobalBackendState())) {
179221
webContents.send(
@@ -184,15 +226,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
184226
);
185227
}
186228
},
187-
id: MENU_ITEMS.projectMetadata.id,
229+
id: INITIALLY_DISABLED_ITEM_INFO.projectMetadata.id,
188230
enabled: false,
189231
},
190232
{
191233
icon: getIconBasedOnTheme(
192234
'icons/statictics-white.png',
193235
'icons/statictics-black.png',
194236
),
195-
label: MENU_ITEMS.projectStatistics.label,
237+
label: INITIALLY_DISABLED_ITEM_INFO.projectStatistics.label,
196238
click: () => {
197239
if (isFileLoaded(getGlobalBackendState())) {
198240
webContents.send(
@@ -203,15 +245,15 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
203245
);
204246
}
205247
},
206-
id: MENU_ITEMS.projectStatistics.id,
248+
id: INITIALLY_DISABLED_ITEM_INFO.projectStatistics.id,
207249
enabled: false,
208250
},
209251
{
210252
icon: getIconBasedOnTheme(
211253
'icons/restore-white.png',
212254
'icons/restore-black.png',
213255
),
214-
label: MENU_ITEMS.pathToSources.label,
256+
label: 'Set Path to Sources',
215257
click: () => {
216258
getSelectBaseURLListener(mainWindow)();
217259
},
@@ -221,7 +263,7 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
221263
'icons/quit-white.png',
222264
'icons/quit-black.png',
223265
),
224-
label: MENU_ITEMS.quit.label,
266+
label: 'Quit',
225267
accelerator: 'CmdOrCtrl+Q',
226268
click: () => {
227269
app.quit();
@@ -283,62 +325,72 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
283325
'icons/select-all-white.png',
284326
'icons/select-all-black.png',
285327
),
286-
label: 'Select All',
328+
label: INITIALLY_DISABLED_ITEM_INFO.selectAll.label,
287329
accelerator: 'CmdOrCtrl+A',
288330
role: 'selectAll',
331+
id: INITIALLY_DISABLED_ITEM_INFO.selectAll.id,
332+
enabled: false,
289333
},
290334
{ type: 'separator' },
291335
{
292336
icon: getIconBasedOnTheme(
293337
'icons/magnifying-glass-white.png',
294338
'icons/magnifying-glass-black.png',
295339
),
296-
label: 'Search Attributions',
340+
label: INITIALLY_DISABLED_ITEM_INFO.searchAttributions.label,
297341
accelerator: 'CmdOrCtrl+Shift+A',
298342
click: () => {
299343
if (isFileLoaded(getGlobalBackendState())) {
300344
webContents.send(AllowedFrontendChannels.SearchAttributions);
301345
}
302346
},
347+
id: INITIALLY_DISABLED_ITEM_INFO.searchAttributions.id,
348+
enabled: false,
303349
},
304350
{
305351
icon: getIconBasedOnTheme(
306352
'icons/magnifying-glass-white.png',
307353
'icons/magnifying-glass-black.png',
308354
),
309-
label: 'Search Signals',
355+
label: INITIALLY_DISABLED_ITEM_INFO.searchSignals.label,
310356
accelerator: 'CmdOrCtrl+Shift+S',
311357
click: () => {
312358
if (isFileLoaded(getGlobalBackendState())) {
313359
webContents.send(AllowedFrontendChannels.SearchSignals);
314360
}
315361
},
362+
id: INITIALLY_DISABLED_ITEM_INFO.searchSignals.id,
363+
enabled: false,
316364
},
317365
{
318366
icon: getIconBasedOnTheme(
319367
'icons/search-white.png',
320368
'icons/search-black.png',
321369
),
322-
label: 'Search All Resources',
370+
label: INITIALLY_DISABLED_ITEM_INFO.searchResourcesAll.label,
323371
accelerator: 'CmdOrCtrl+Shift+R',
324372
click: () => {
325373
if (isFileLoaded(getGlobalBackendState())) {
326374
webContents.send(AllowedFrontendChannels.SearchResources);
327375
}
328376
},
377+
id: INITIALLY_DISABLED_ITEM_INFO.searchResourcesAll.id,
378+
enabled: false,
329379
},
330380
{
331381
icon: getIconBasedOnTheme(
332382
'icons/search-white.png',
333383
'icons/search-black.png',
334384
),
335-
label: 'Search Linked Resources',
385+
label: INITIALLY_DISABLED_ITEM_INFO.searchResourceLinked.label,
336386
accelerator: 'CmdOrCtrl+Shift+L',
337387
click: () => {
338388
if (isFileLoaded(getGlobalBackendState())) {
339389
webContents.send(AllowedFrontendChannels.SearchLinkedResources);
340390
}
341391
},
392+
id: INITIALLY_DISABLED_ITEM_INFO.searchResourceLinked.id,
393+
enabled: false,
342394
},
343395
],
344396
},
@@ -480,19 +532,14 @@ export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
480532
]);
481533
}
482534

483-
export function activateMenuOptions(): void {
535+
export function activateMenuItems(): void {
484536
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-
}
537+
INITIALLY_DISABLED_MENU_ITEMS.forEach((key) => {
538+
const menuItem = menu?.getMenuItemById(
539+
INITIALLY_DISABLED_ITEM_INFO[key].id,
540+
);
541+
if (menuItem) {
542+
menuItem.enabled = true;
543+
}
544+
});
498545
}

0 commit comments

Comments
 (0)