Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce open recent #2857

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions src/ElectronBackend/main/__tests__/iconHelpers.test.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/ElectronBackend/main/__tests__/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock('electron', () => ({
},
Menu: {
buildFromTemplate: jest.fn(),
setApplicationMenu: jest.fn(),
},
}));

Expand Down
17 changes: 1 addition & 16 deletions src/ElectronBackend/main/iconHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
import electron, { app, Menu } from 'electron';
import electron, { app } from 'electron';
import path from 'path';
import upath from 'upath';

Expand All @@ -23,18 +23,3 @@ export function getIconBasedOnTheme(
? path.join(getBasePathOfAssets(), white_icon)
: path.join(getBasePathOfAssets(), black_icon);
}

export function makeFirstIconVisibleAndSecondHidden(
firstItemId: string,
secondItemId: string,
): void {
const itemToMakeVisible =
Menu.getApplicationMenu()?.getMenuItemById(firstItemId);
if (itemToMakeVisible) {
itemToMakeVisible.visible = true;
}
const itemToHide = Menu.getApplicationMenu()?.getMenuItemById(secondItemId);
if (itemToHide) {
itemToHide.visible = false;
}
}
21 changes: 21 additions & 0 deletions src/ElectronBackend/main/listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// SPDX-License-Identifier: Apache-2.0
import { BrowserWindow, shell, WebContents } from 'electron';
import fs from 'fs';
import { uniq } from 'lodash';
import path from 'path';
import upath from 'upath';

Expand Down Expand Up @@ -53,6 +54,10 @@ import {
setGlobalBackendState,
} from './globalBackendState';
import logger from './logger';
import { createMenu } from './menu';
import { UserSettings } from './user-settings';

const MAX_NUMBER_OF_RECENTLY_OPENED_PATHS = 10;

export const saveFileListener =
(mainWindow: BrowserWindow) =>
Expand Down Expand Up @@ -130,6 +135,10 @@ export async function handleOpeningFile(

await openFile(mainWindow, filePath, onOpen);

await updateRecentlyOpenedPaths(filePath);

await createMenu(mainWindow);

setLoadingState(mainWindow.webContents, false);
}

Expand Down Expand Up @@ -351,6 +360,18 @@ export async function openFile(
onOpen();
}

async function updateRecentlyOpenedPaths(filePath: string): Promise<void> {
const recentlyOpenedPaths = await UserSettings.get('recentlyOpenedPaths');
await UserSettings.set(
'recentlyOpenedPaths',
uniq([filePath, ...(recentlyOpenedPaths ?? [])]).slice(
0,
MAX_NUMBER_OF_RECENTLY_OPENED_PATHS,
),
{ skipNotification: true },
);
}

function setTitle(mainWindow: BrowserWindow, filePath: string): void {
const defaultTitle = 'OpossumUI';

Expand Down
4 changes: 2 additions & 2 deletions src/ElectronBackend/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
import { dialog, ipcMain, Menu, systemPreferences } from 'electron';
import { dialog, ipcMain, systemPreferences } from 'electron';
import os from 'os';

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

await UserSettings.init();
Menu.setApplicationMenu(await createMenu(mainWindow));
await createMenu(mainWindow);

mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => {
Expand Down
24 changes: 14 additions & 10 deletions src/ElectronBackend/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-FileCopyrightText: Nico Carl <nicocarl@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import { BrowserWindow, Menu, MenuItem } from 'electron';
import { BrowserWindow, Menu, MenuItemConstructorOptions } from 'electron';
import os from 'os';

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

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

return Menu.buildFromTemplate([
...(os.platform() === 'darwin' ? [{ role: 'appMenu' } as MenuItem] : []),
getFileMenu(mainWindow),
getEditMenu(webContents),
await getViewMenu(),
getAboutMenu(),
getHelpMenu(webContents),
]);
return Menu.setApplicationMenu(
Menu.buildFromTemplate([
...(os.platform() === 'darwin'
? [{ role: 'appMenu' } satisfies MenuItemConstructorOptions]
: []),
await getFileMenu(mainWindow),
getEditMenu(webContents),
await getViewMenu(mainWindow),
getAboutMenu(),
getHelpMenu(webContents),
]),
);
}
10 changes: 5 additions & 5 deletions src/ElectronBackend/main/menu/aboutMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-FileCopyrightText: Nico Carl <nicocarl@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import { shell } from 'electron';
import { MenuItemConstructorOptions, shell } from 'electron';

import { text } from '../../../shared/text';
import { getIconBasedOnTheme } from '../iconHelpers';
Expand All @@ -12,7 +12,7 @@ import {
getPathOfNoticeDocument,
} from '../notice-document-helpers';

function getOpenOnGithub() {
function getOpenOnGithub(): MenuItemConstructorOptions {
return {
icon: getIconBasedOnTheme(
'icons/github-white.png',
Expand All @@ -24,7 +24,7 @@ function getOpenOnGithub() {
};
}

function getOpossumUiNotices() {
function getOpossumUiNotices(): MenuItemConstructorOptions {
return {
icon: getIconBasedOnTheme(
'icons/notice-white.png',
Expand All @@ -35,7 +35,7 @@ function getOpossumUiNotices() {
};
}

function getChromiumNotices() {
function getChromiumNotices(): MenuItemConstructorOptions {
return {
icon: getIconBasedOnTheme(
'icons/chromium-white.png',
Expand All @@ -46,7 +46,7 @@ function getChromiumNotices() {
};
}

export function getAboutMenu() {
export function getAboutMenu(): MenuItemConstructorOptions {
return {
label: text.menu.about,
submenu: [getOpenOnGithub(), getOpossumUiNotices(), getChromiumNotices()],
Expand Down
16 changes: 12 additions & 4 deletions src/ElectronBackend/main/menu/editMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ function getSelectAll(): MenuItemConstructorOptions {
};
}

function getSearchAttributions(webContents: Electron.WebContents) {
function getSearchAttributions(
webContents: Electron.WebContents,
): MenuItemConstructorOptions {
return {
icon: getIconBasedOnTheme(
'icons/magnifying-glass-white.png',
Expand All @@ -89,7 +91,9 @@ function getSearchAttributions(webContents: Electron.WebContents) {
};
}

function getSearchSignals(webContents: Electron.WebContents) {
function getSearchSignals(
webContents: Electron.WebContents,
): MenuItemConstructorOptions {
return {
icon: getIconBasedOnTheme(
'icons/magnifying-glass-white.png',
Expand All @@ -107,7 +111,9 @@ function getSearchSignals(webContents: Electron.WebContents) {
};
}

function getSearchResources(webContents: Electron.WebContents) {
function getSearchResources(
webContents: Electron.WebContents,
): MenuItemConstructorOptions {
return {
icon: getIconBasedOnTheme(
'icons/search-white.png',
Expand All @@ -125,7 +131,9 @@ function getSearchResources(webContents: Electron.WebContents) {
};
}

function getSearchLinkedResources(webContents: Electron.WebContents) {
function getSearchLinkedResources(
webContents: Electron.WebContents,
): MenuItemConstructorOptions {
return {
icon: getIconBasedOnTheme(
'icons/search-white.png',
Expand Down
Loading
Loading