Skip to content

Commit 23e8708

Browse files
committed
feat: introduce open recent
This PR allows users to chose from up to 10 recently opened files. It also creates a central method for creating/rebuilding the electron menu and uses it to simplify some code around QA mode. Signed-off-by: Maxim Stykow <maxim.stykow@tngtech.com>
1 parent 9fcf137 commit 23e8708

20 files changed

+315
-215
lines changed

src/ElectronBackend/main/__tests__/iconHelpers.test.ts

-47
This file was deleted.

src/ElectronBackend/main/__tests__/menu.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jest.mock('electron', () => ({
1414
},
1515
Menu: {
1616
buildFromTemplate: jest.fn(),
17+
setApplicationMenu: jest.fn(),
1718
},
1819
}));
1920

src/ElectronBackend/main/iconHelpers.ts

+1-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
33
//
44
// SPDX-License-Identifier: Apache-2.0
5-
import electron, { app, Menu } from 'electron';
5+
import electron, { app } from 'electron';
66
import path from 'path';
77
import upath from 'upath';
88

@@ -23,18 +23,3 @@ export function getIconBasedOnTheme(
2323
? path.join(getBasePathOfAssets(), white_icon)
2424
: path.join(getBasePathOfAssets(), black_icon);
2525
}
26-
27-
export function makeFirstIconVisibleAndSecondHidden(
28-
firstItemId: string,
29-
secondItemId: string,
30-
): void {
31-
const itemToMakeVisible =
32-
Menu.getApplicationMenu()?.getMenuItemById(firstItemId);
33-
if (itemToMakeVisible) {
34-
itemToMakeVisible.visible = true;
35-
}
36-
const itemToHide = Menu.getApplicationMenu()?.getMenuItemById(secondItemId);
37-
if (itemToHide) {
38-
itemToHide.visible = false;
39-
}
40-
}

src/ElectronBackend/main/listeners.ts

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// SPDX-License-Identifier: Apache-2.0
66
import { BrowserWindow, shell, WebContents } from 'electron';
77
import fs from 'fs';
8+
import { uniq } from 'lodash';
89
import path from 'path';
910
import upath from 'upath';
1011

@@ -53,6 +54,10 @@ import {
5354
setGlobalBackendState,
5455
} from './globalBackendState';
5556
import logger from './logger';
57+
import { createMenu } from './menu';
58+
import { UserSettings } from './user-settings';
59+
60+
const MAX_NUMBER_OF_RECENTLY_OPENED_PATHS = 10;
5661

5762
export const saveFileListener =
5863
(mainWindow: BrowserWindow) =>
@@ -130,6 +135,10 @@ export async function handleOpeningFile(
130135

131136
await openFile(mainWindow, filePath, onOpen);
132137

138+
await updateRecentlyOpenedPaths(filePath);
139+
140+
await createMenu(mainWindow);
141+
133142
setLoadingState(mainWindow.webContents, false);
134143
}
135144

@@ -351,6 +360,18 @@ export async function openFile(
351360
onOpen();
352361
}
353362

363+
async function updateRecentlyOpenedPaths(filePath: string): Promise<void> {
364+
const recentlyOpenedPaths = await UserSettings.get('recentlyOpenedPaths');
365+
await UserSettings.set(
366+
'recentlyOpenedPaths',
367+
uniq([filePath, ...(recentlyOpenedPaths ?? [])]).slice(
368+
0,
369+
MAX_NUMBER_OF_RECENTLY_OPENED_PATHS,
370+
),
371+
{ skipNotification: true },
372+
);
373+
}
374+
354375
function setTitle(mainWindow: BrowserWindow, filePath: string): void {
355376
const defaultTitle = 'OpossumUI';
356377

src/ElectronBackend/main/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
33
//
44
// SPDX-License-Identifier: Apache-2.0
5-
import { dialog, ipcMain, Menu, systemPreferences } from 'electron';
5+
import { dialog, ipcMain, systemPreferences } from 'electron';
66
import os from 'os';
77

88
import { AllowedFrontendChannels, IpcChannel } from '../../shared/ipc-channels';
@@ -36,7 +36,7 @@ export async function main(): Promise<void> {
3636
const mainWindow = await createWindow();
3737

3838
await UserSettings.init();
39-
Menu.setApplicationMenu(await createMenu(mainWindow));
39+
await createMenu(mainWindow);
4040

4141
mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
4242
(details, callback) => {

src/ElectronBackend/main/menu.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-FileCopyrightText: Nico Carl <nicocarl@protonmail.com>
44
//
55
// SPDX-License-Identifier: Apache-2.0
6-
import { BrowserWindow, Menu, MenuItem } from 'electron';
6+
import { BrowserWindow, Menu, MenuItemConstructorOptions } from 'electron';
77
import os from 'os';
88

99
import { getAboutMenu } from './menu/aboutMenu';
@@ -12,15 +12,19 @@ import { getFileMenu } from './menu/fileMenu';
1212
import { getHelpMenu } from './menu/helpMenu';
1313
import { getViewMenu } from './menu/viewMenu';
1414

15-
export async function createMenu(mainWindow: BrowserWindow): Promise<Menu> {
15+
export async function createMenu(mainWindow: BrowserWindow): Promise<void> {
1616
const webContents = mainWindow.webContents;
1717

18-
return Menu.buildFromTemplate([
19-
...(os.platform() === 'darwin' ? [{ role: 'appMenu' } as MenuItem] : []),
20-
getFileMenu(mainWindow),
21-
getEditMenu(webContents),
22-
await getViewMenu(),
23-
getAboutMenu(),
24-
getHelpMenu(webContents),
25-
]);
18+
return Menu.setApplicationMenu(
19+
Menu.buildFromTemplate([
20+
...(os.platform() === 'darwin'
21+
? [{ role: 'appMenu' } satisfies MenuItemConstructorOptions]
22+
: []),
23+
await getFileMenu(mainWindow),
24+
getEditMenu(webContents),
25+
await getViewMenu(mainWindow),
26+
getAboutMenu(),
27+
getHelpMenu(webContents),
28+
]),
29+
);
2630
}

src/ElectronBackend/main/menu/aboutMenu.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-FileCopyrightText: Nico Carl <nicocarl@protonmail.com>
44
//
55
// SPDX-License-Identifier: Apache-2.0
6-
import { shell } from 'electron';
6+
import { MenuItemConstructorOptions, shell } from 'electron';
77

88
import { text } from '../../../shared/text';
99
import { getIconBasedOnTheme } from '../iconHelpers';
@@ -12,7 +12,7 @@ import {
1212
getPathOfNoticeDocument,
1313
} from '../notice-document-helpers';
1414

15-
function getOpenOnGithub() {
15+
function getOpenOnGithub(): MenuItemConstructorOptions {
1616
return {
1717
icon: getIconBasedOnTheme(
1818
'icons/github-white.png',
@@ -24,7 +24,7 @@ function getOpenOnGithub() {
2424
};
2525
}
2626

27-
function getOpossumUiNotices() {
27+
function getOpossumUiNotices(): MenuItemConstructorOptions {
2828
return {
2929
icon: getIconBasedOnTheme(
3030
'icons/notice-white.png',
@@ -35,7 +35,7 @@ function getOpossumUiNotices() {
3535
};
3636
}
3737

38-
function getChromiumNotices() {
38+
function getChromiumNotices(): MenuItemConstructorOptions {
3939
return {
4040
icon: getIconBasedOnTheme(
4141
'icons/chromium-white.png',
@@ -46,7 +46,7 @@ function getChromiumNotices() {
4646
};
4747
}
4848

49-
export function getAboutMenu() {
49+
export function getAboutMenu(): MenuItemConstructorOptions {
5050
return {
5151
label: text.menu.about,
5252
submenu: [getOpenOnGithub(), getOpossumUiNotices(), getChromiumNotices()],

src/ElectronBackend/main/menu/editMenu.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ function getSelectAll(): MenuItemConstructorOptions {
7171
};
7272
}
7373

74-
function getSearchAttributions(webContents: Electron.WebContents) {
74+
function getSearchAttributions(
75+
webContents: Electron.WebContents,
76+
): MenuItemConstructorOptions {
7577
return {
7678
icon: getIconBasedOnTheme(
7779
'icons/magnifying-glass-white.png',
@@ -89,7 +91,9 @@ function getSearchAttributions(webContents: Electron.WebContents) {
8991
};
9092
}
9193

92-
function getSearchSignals(webContents: Electron.WebContents) {
94+
function getSearchSignals(
95+
webContents: Electron.WebContents,
96+
): MenuItemConstructorOptions {
9397
return {
9498
icon: getIconBasedOnTheme(
9599
'icons/magnifying-glass-white.png',
@@ -107,7 +111,9 @@ function getSearchSignals(webContents: Electron.WebContents) {
107111
};
108112
}
109113

110-
function getSearchResources(webContents: Electron.WebContents) {
114+
function getSearchResources(
115+
webContents: Electron.WebContents,
116+
): MenuItemConstructorOptions {
111117
return {
112118
icon: getIconBasedOnTheme(
113119
'icons/search-white.png',
@@ -125,7 +131,9 @@ function getSearchResources(webContents: Electron.WebContents) {
125131
};
126132
}
127133

128-
function getSearchLinkedResources(webContents: Electron.WebContents) {
134+
function getSearchLinkedResources(
135+
webContents: Electron.WebContents,
136+
): MenuItemConstructorOptions {
129137
return {
130138
icon: getIconBasedOnTheme(
131139
'icons/search-white.png',

0 commit comments

Comments
 (0)