bitwarden-estensione-browser/electron/src/baseMenu.ts

230 lines
6.9 KiB
TypeScript
Raw Normal View History

2018-04-25 21:43:02 +02:00
import {
app,
clipboard,
dialog,
Menu,
MenuItemConstructorOptions,
} from 'electron';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
2018-04-25 21:43:02 +02:00
import { WindowMain } from './window.main';
export class BaseMenu {
protected editMenuItemOptions: MenuItemConstructorOptions;
protected viewSubMenuItemOptions: MenuItemConstructorOptions[];
protected windowMenuItemOptions: MenuItemConstructorOptions;
protected macAppMenuItemOptions: MenuItemConstructorOptions[];
protected macWindowSubmenuOptions: MenuItemConstructorOptions[];
2018-05-08 15:40:12 +02:00
constructor(protected i18nService: I18nService, protected windowMain: WindowMain) { }
2018-04-25 21:43:02 +02:00
protected initProperties() {
this.editMenuItemOptions = {
label: this.i18nService.t('edit'),
submenu: [
{
label: this.i18nService.t('undo'),
role: 'undo',
},
{
label: this.i18nService.t('redo'),
role: 'redo',
},
{ type: 'separator' },
{
label: this.i18nService.t('cut'),
role: 'cut',
},
{
label: this.i18nService.t('copy'),
role: 'copy',
},
{
label: this.i18nService.t('paste'),
role: 'paste',
},
{ type: 'separator' },
{
label: this.i18nService.t('selectAll'),
2020-01-27 15:46:42 +01:00
role: 'selectAll',
2018-04-25 21:43:02 +02:00
},
],
};
this.viewSubMenuItemOptions = [
{
label: this.i18nService.t('zoomIn'),
2020-01-27 15:46:42 +01:00
role: 'zoomIn',
accelerator: 'CmdOrCtrl+=',
2018-04-25 21:43:02 +02:00
},
{
label: this.i18nService.t('zoomOut'),
2020-01-27 15:46:42 +01:00
role: 'zoomOut',
accelerator: 'CmdOrCtrl+-',
2018-04-25 21:43:02 +02:00
},
{
label: this.i18nService.t('resetZoom'),
2020-01-27 15:46:42 +01:00
role: 'resetZoom',
accelerator: 'CmdOrCtrl+0',
2018-04-25 21:43:02 +02:00
},
{ type: 'separator' },
{
label: this.i18nService.t('toggleFullScreen'),
role: 'togglefullscreen',
},
{ type: 'separator' },
{
label: this.i18nService.t('reload'),
2020-01-27 15:46:42 +01:00
role: 'forceReload',
2018-04-25 21:43:02 +02:00
},
{
label: this.i18nService.t('toggleDevTools'),
2020-01-27 15:46:42 +01:00
role: 'toggleDevTools',
2018-04-25 21:43:02 +02:00
accelerator: 'F12',
},
];
this.windowMenuItemOptions = {
label: this.i18nService.t('window'),
role: 'window',
submenu: [
{
label: this.i18nService.t('minimize'),
role: 'minimize',
},
{
label: this.i18nService.t('close'),
2018-12-03 22:06:28 +01:00
role: 'close',
2018-04-25 21:43:02 +02:00
},
],
};
if (process.platform === 'darwin') {
this.macAppMenuItemOptions = [
{
label: this.i18nService.t('services'),
role: 'services', submenu: [],
},
{ type: 'separator' },
{
label: this.i18nService.t('hideBitwarden'),
role: 'hide',
},
{
label: this.i18nService.t('hideOthers'),
2020-01-27 15:46:42 +01:00
role: 'hideOthers',
2018-04-25 21:43:02 +02:00
},
{
label: this.i18nService.t('showAll'),
role: 'unhide',
},
{ type: 'separator' },
{
label: this.i18nService.t('quitBitwarden'),
role: 'quit',
},
];
this.macWindowSubmenuOptions = [
{
label: this.i18nService.t('minimize'),
role: 'minimize',
},
{
label: this.i18nService.t('zoom'),
role: 'zoom',
},
{ type: 'separator' },
{
label: this.i18nService.t('bringAllToFront'),
role: 'front',
},
2018-05-08 15:40:12 +02:00
{
label: this.i18nService.t('close'),
role: 'close',
2018-05-08 15:40:12 +02:00
},
2018-04-25 21:43:02 +02:00
];
}
}
protected initContextMenu() {
if (this.windowMain.win == null) {
return;
}
const selectionMenu = Menu.buildFromTemplate([
{
label: this.i18nService.t('copy'),
role: 'copy',
},
{ type: 'separator' },
{
label: this.i18nService.t('selectAll'),
2020-01-27 15:46:42 +01:00
role: 'selectAll',
2018-04-25 21:43:02 +02:00
},
]);
const inputMenu = Menu.buildFromTemplate([
{
label: this.i18nService.t('undo'),
role: 'undo',
},
{
label: this.i18nService.t('redo'),
role: 'redo',
},
{ type: 'separator' },
{
label: this.i18nService.t('cut'),
role: 'cut',
enabled: false,
},
{
label: this.i18nService.t('copy'),
role: 'copy',
enabled: false,
},
{
label: this.i18nService.t('paste'),
role: 'paste',
},
{ type: 'separator' },
{
label: this.i18nService.t('selectAll'),
2020-01-27 15:46:42 +01:00
role: 'selectAll',
2018-04-25 21:43:02 +02:00
},
]);
const inputSelectionMenu = Menu.buildFromTemplate([
{
label: this.i18nService.t('cut'),
role: 'cut',
},
{
label: this.i18nService.t('copy'),
role: 'copy',
},
{
label: this.i18nService.t('paste'),
role: 'paste',
},
{ type: 'separator' },
{
label: this.i18nService.t('selectAll'),
2020-01-27 15:46:42 +01:00
role: 'selectAll',
2018-04-25 21:43:02 +02:00
},
]);
this.windowMain.win.webContents.on('context-menu', (e, props) => {
const selected = props.selectionText && props.selectionText.trim() !== '';
if (props.isEditable && selected) {
2018-05-31 14:09:56 +02:00
inputSelectionMenu.popup({ window: this.windowMain.win });
2018-04-25 21:43:02 +02:00
} else if (props.isEditable) {
2018-05-31 14:09:56 +02:00
inputMenu.popup({ window: this.windowMain.win });
2018-04-25 21:43:02 +02:00
} else if (selected) {
2018-05-31 14:09:56 +02:00
selectionMenu.popup({ window: this.windowMain.win });
2018-04-25 21:43:02 +02:00
}
});
}
}