2022-04-18 23:06:44 +00:00
|
|
|
import {
|
|
|
|
BrowserWindow,
|
|
|
|
ContextMenuParams,
|
2023-08-25 13:10:05 +00:00
|
|
|
Event as ElectronEvent,
|
2022-04-18 23:06:44 +00:00
|
|
|
} from 'electron';
|
2021-06-26 13:59:28 +00:00
|
|
|
import contextMenu from 'electron-context-menu';
|
2022-04-21 02:03:49 +00:00
|
|
|
|
2021-06-02 19:18:32 +00:00
|
|
|
import { nativeTabsSupported, openExternal } from '../helpers/helpers';
|
2022-04-21 02:03:49 +00:00
|
|
|
import * as log from '../helpers/loggingHelper';
|
2021-06-02 19:18:32 +00:00
|
|
|
import { setupNativefierWindow } from '../helpers/windowEvents';
|
2022-04-18 23:06:44 +00:00
|
|
|
import { createNewWindow } from '../helpers/windowHelpers';
|
2021-06-26 13:59:28 +00:00
|
|
|
import {
|
|
|
|
OutputOptions,
|
|
|
|
outputOptionsToWindowOptions,
|
|
|
|
} from '../../../shared/src/options/model';
|
2021-06-02 19:18:32 +00:00
|
|
|
|
2021-06-26 13:59:28 +00:00
|
|
|
export function initContextMenu(
|
|
|
|
options: OutputOptions,
|
|
|
|
window?: BrowserWindow,
|
|
|
|
): void {
|
2022-09-18 02:22:19 +00:00
|
|
|
log.debug('initContextMenu');
|
2016-01-25 07:56:33 +00:00
|
|
|
|
2021-06-26 13:59:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
2018-04-22 23:48:56 +00:00
|
|
|
contextMenu({
|
2021-06-26 13:59:28 +00:00
|
|
|
prepend: (actions: contextMenu.Actions, params: ContextMenuParams) => {
|
2021-06-02 19:18:32 +00:00
|
|
|
log.debug('contextMenu.prepend', { actions, params });
|
2018-04-22 23:48:56 +00:00
|
|
|
const items = [];
|
2023-08-25 13:10:05 +00:00
|
|
|
if (params.linkURL && window) {
|
2018-04-22 23:48:56 +00:00
|
|
|
items.push({
|
|
|
|
label: 'Open Link in Default Browser',
|
|
|
|
click: () => {
|
2021-06-02 19:18:32 +00:00
|
|
|
openExternal(params.linkURL).catch((err) =>
|
|
|
|
log.error('contextMenu Open Link in Default Browser ERROR', err),
|
|
|
|
);
|
2018-04-22 23:48:56 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
items.push({
|
|
|
|
label: 'Open Link in New Window',
|
2021-06-02 19:18:32 +00:00
|
|
|
click: () =>
|
|
|
|
createNewWindow(
|
2023-08-25 13:10:05 +00:00
|
|
|
outputOptionsToWindowOptions(options, nativeTabsSupported()),
|
2021-06-02 19:18:32 +00:00
|
|
|
setupNativefierWindow,
|
|
|
|
params.linkURL,
|
2023-08-25 13:10:05 +00:00
|
|
|
// window,
|
2021-06-02 19:18:32 +00:00
|
|
|
),
|
2018-04-22 23:48:56 +00:00
|
|
|
});
|
2021-06-02 19:18:32 +00:00
|
|
|
if (nativeTabsSupported()) {
|
2018-05-01 23:24:35 +00:00
|
|
|
items.push({
|
|
|
|
label: 'Open Link in New Tab',
|
2021-06-02 19:18:32 +00:00
|
|
|
click: () =>
|
2023-08-25 13:10:05 +00:00
|
|
|
// // Fire a new window event for a foreground tab
|
|
|
|
// // Previously we called createNewTab directly, but it had incosistent and buggy behavior
|
|
|
|
// // as it was mostly designed for running off of events. So this will create a new event
|
|
|
|
// // for a foreground-tab for the event handler to grab and take care of instead.
|
|
|
|
// (window as BrowserWindow).webContents.emit(
|
|
|
|
// // event name
|
|
|
|
// 'new-window',
|
|
|
|
// // event object
|
|
|
|
// {
|
|
|
|
// // Leave to the default for a NewWindowWebContentsEvent
|
|
|
|
// newGuest: undefined,
|
|
|
|
// ...new Event('new-window'),
|
|
|
|
// }, // as NewWindowWebContentsEvent,
|
|
|
|
// // url
|
|
|
|
// params.linkURL,
|
|
|
|
// // frameName
|
|
|
|
// window?.webContents.mainFrame.name ?? '',
|
|
|
|
// // disposition
|
|
|
|
// 'foreground-tab',
|
|
|
|
// ),
|
|
|
|
window.emit('new-window-for-tab', {
|
|
|
|
...new Event('new-window-for-tab'),
|
|
|
|
url: params.linkURL,
|
|
|
|
} as ElectronEvent<{ url: string }>),
|
2018-05-01 23:24:35 +00:00
|
|
|
});
|
|
|
|
}
|
2018-04-22 23:48:56 +00:00
|
|
|
}
|
|
|
|
return items;
|
|
|
|
},
|
2021-07-10 14:55:00 +00:00
|
|
|
showCopyImage: true,
|
|
|
|
showCopyImageAddress: true,
|
|
|
|
showSaveImage: true,
|
2017-04-29 14:52:12 +00:00
|
|
|
});
|
2016-01-25 07:56:33 +00:00
|
|
|
}
|