mirror of https://github.com/Fabio286/antares.git
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
|
import { BrowserWindow, globalShortcut } from 'electron';
|
||
|
import * as Store from 'electron-store';
|
||
|
import { ShortcutRecord, shortcuts as defaultShortcuts } from 'common/shortcuts';
|
||
|
const shortcutsStore = new Store({ name: 'shortcuts' });
|
||
|
const isDevelopment = process.env.NODE_ENV !== 'production';
|
||
|
|
||
|
export class ShortcutRegister {
|
||
|
shortcuts: ShortcutRecord[];
|
||
|
private _mainWindow: BrowserWindow;
|
||
|
|
||
|
constructor (args: { mainWindow: BrowserWindow }) {
|
||
|
this._mainWindow = args.mainWindow;
|
||
|
this.shortcuts = shortcutsStore.get('shortcuts', defaultShortcuts) as ShortcutRecord[];
|
||
|
}
|
||
|
|
||
|
init () {
|
||
|
for (const shortcut of this.shortcuts) {
|
||
|
if (shortcut.os.includes(process.platform)) {
|
||
|
for (const key of shortcut.keys) {
|
||
|
try {
|
||
|
globalShortcut.register(key, () => {
|
||
|
this._mainWindow.webContents.send(shortcut.event);
|
||
|
if (isDevelopment) console.log('EVENT:', shortcut);
|
||
|
});
|
||
|
}
|
||
|
catch (error) {
|
||
|
this.restoreDefaults();
|
||
|
throw error;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
reload () {
|
||
|
this.unregister();
|
||
|
this.init();
|
||
|
}
|
||
|
|
||
|
updateShortcuts (shortcuts: ShortcutRecord[]) {
|
||
|
this.shortcuts = shortcuts;
|
||
|
}
|
||
|
|
||
|
restoreDefaults () {
|
||
|
shortcutsStore.set('shortcuts', defaultShortcuts);
|
||
|
this.shortcuts = defaultShortcuts;
|
||
|
this.reload();
|
||
|
}
|
||
|
|
||
|
unregister () {
|
||
|
globalShortcut.unregisterAll();
|
||
|
}
|
||
|
}
|