Skip to content

Commit

Permalink
feat: cleaner startup for electron #2942 #1678
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Dec 28, 2023
1 parent 64ab128 commit e841b6e
Show file tree
Hide file tree
Showing 3 changed files with 472 additions and 479 deletions.
271 changes: 133 additions & 138 deletions electron/ipc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
// FRONTEND EVENTS
// ---------------
import {
app,
dialog,
globalShortcut,
ipcMain,
IpcMainEvent,
nativeTheme,
shell,
systemPreferences,
} from 'electron';
import { app, dialog, globalShortcut, ipcMain, IpcMainEvent, shell } from 'electron';
import { IPC } from './shared-with-frontend/ipc-events.const';
import { lockscreen } from './lockscreen';
import { errorHandlerWithFrontendInform } from './error-handler-with-frontend-inform';
Expand All @@ -22,148 +13,152 @@ import { getWin } from './main-window';
import { quitApp, showOrFocus } from './various-shared';
import { loadSimpleStoreAll, saveSimpleStore } from './simple-store';

// HANDLER
// -------
ipcMain.handle(IPC.GET_PATH, (ev, name: string) => {
return app.getPath(name as any);
});

// BACKEND EVENTS
// --------------
// ...

// ON EVENTS
// ---------
ipcMain.on(IPC.SHUTDOWN_NOW, quitApp);
ipcMain.on(IPC.EXIT, (ev, exitCode: number) => app.exit(exitCode));
ipcMain.on(IPC.RELAUNCH, () => app.relaunch());
ipcMain.on(IPC.OPEN_DEV_TOOLS, () => getWin().webContents.openDevTools());
ipcMain.on(IPC.RELOAD_MAIN_WIN, () => getWin().reload());
ipcMain.on(IPC.OPEN_PATH, (ev, path: string) => shell.openPath(path));
ipcMain.on(IPC.OPEN_EXTERNAL, (ev, url: string) => shell.openExternal(url));

ipcMain.on(IPC.LOCK_SCREEN, () => {
if ((app as any).isLocked) {
return;
}

try {
lockscreen();
} catch (e) {
errorHandlerWithFrontendInform(e);
}
});
export const initIpcInterfaces = (): void => {
// HANDLER
// -------
ipcMain.handle(IPC.GET_PATH, (ev, name: string) => {
return app.getPath(name as any);
});

// BACKEND EVENTS
// --------------
// ...

// ON EVENTS
// ---------
ipcMain.on(IPC.SHUTDOWN_NOW, quitApp);
ipcMain.on(IPC.EXIT, (ev, exitCode: number) => app.exit(exitCode));
ipcMain.on(IPC.RELAUNCH, () => app.relaunch());
ipcMain.on(IPC.OPEN_DEV_TOOLS, () => getWin().webContents.openDevTools());
ipcMain.on(IPC.RELOAD_MAIN_WIN, () => getWin().reload());
ipcMain.on(IPC.OPEN_PATH, (ev, path: string) => shell.openPath(path));
ipcMain.on(IPC.OPEN_EXTERNAL, (ev, url: string) => shell.openExternal(url));

ipcMain.on(IPC.LOCK_SCREEN, () => {
if ((app as any).isLocked) {
return;
}

ipcMain.on(IPC.SET_PROGRESS_BAR, (ev, { progress, mode }) => {
const mainWin = getWin();
if (mainWin) {
mainWin.setProgressBar(Math.min(Math.max(progress, 0), 1), { mode });
}
});
try {
lockscreen();
} catch (e) {
errorHandlerWithFrontendInform(e);
}
});

ipcMain.on(IPC.FLASH_FRAME, (ev) => {
const mainWin = getWin();
if (mainWin) {
mainWin.flashFrame(false);
mainWin.flashFrame(true);
ipcMain.on(IPC.SET_PROGRESS_BAR, (ev, { progress, mode }) => {
const mainWin = getWin();
if (mainWin) {
mainWin.setProgressBar(Math.min(Math.max(progress, 0), 1), { mode });
}
});

mainWin.once('focus', () => {
ipcMain.on(IPC.FLASH_FRAME, (ev) => {
const mainWin = getWin();
if (mainWin) {
mainWin.flashFrame(false);
});
}
});

ipcMain.on(IPC.REGISTER_GLOBAL_SHORTCUTS_EVENT, (ev, cfg) => {
registerShowAppShortCuts(cfg);
});
mainWin.flashFrame(true);

ipcMain.on(
IPC.JIRA_SETUP_IMG_HEADERS,
(ev, { jiraCfg, wonkyCookie }: { jiraCfg: JiraCfg; wonkyCookie?: string }) => {
setupRequestHeadersForImages(jiraCfg, wonkyCookie);
},
);
mainWin.once('focus', () => {
mainWin.flashFrame(false);
});
}
});

ipcMain.on(IPC.JIRA_MAKE_REQUEST_EVENT, (ev, request) => {
sendJiraRequest(request);
});
ipcMain.on(IPC.REGISTER_GLOBAL_SHORTCUTS_EVENT, (ev, cfg) => {
registerShowAppShortCuts(cfg);
});

ipcMain.on(IPC.SHOW_OR_FOCUS, () => {
const mainWin = getWin();
showOrFocus(mainWin);
});
ipcMain.on(
IPC.JIRA_SETUP_IMG_HEADERS,
(ev, { jiraCfg, wonkyCookie }: { jiraCfg: JiraCfg; wonkyCookie?: string }) => {
setupRequestHeadersForImages(jiraCfg, wonkyCookie);
},
);

ipcMain.on(IPC.EXEC, execWithFrontendErrorHandlerInform);
ipcMain.on(IPC.JIRA_MAKE_REQUEST_EVENT, (ev, request) => {
sendJiraRequest(request);
});

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function registerShowAppShortCuts(cfg: KeyboardConfig): void {
// unregister all previous
globalShortcut.unregisterAll();
const GLOBAL_KEY_CFG_KEYS: (keyof KeyboardConfig)[] = [
'globalShowHide',
'globalToggleTaskStart',
'globalAddNote',
'globalAddTask',
];

if (cfg) {
ipcMain.on(IPC.SHOW_OR_FOCUS, () => {
const mainWin = getWin();
Object.keys(cfg)
.filter((key: string) => GLOBAL_KEY_CFG_KEYS.includes(key as keyof KeyboardConfig))
.forEach((key: string) => {
let actionFn: () => void;
const shortcut = cfg[key as keyof KeyboardConfig];

switch (key) {
case 'globalShowHide':
actionFn = () => {
if (mainWin.isFocused()) {
// we need to blur the window for windows
mainWin.blur();
mainWin.hide();
} else {
showOrFocus(mainWin);
});

ipcMain.on(IPC.EXEC, execWithFrontendErrorHandlerInform);

// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function registerShowAppShortCuts(cfg: KeyboardConfig): void {
// unregister all previous
globalShortcut.unregisterAll();
const GLOBAL_KEY_CFG_KEYS: (keyof KeyboardConfig)[] = [
'globalShowHide',
'globalToggleTaskStart',
'globalAddNote',
'globalAddTask',
];

if (cfg) {
const mainWin = getWin();
Object.keys(cfg)
.filter((key: string) =>
GLOBAL_KEY_CFG_KEYS.includes(key as keyof KeyboardConfig),
)
.forEach((key: string) => {
let actionFn: () => void;
const shortcut = cfg[key as keyof KeyboardConfig];

switch (key) {
case 'globalShowHide':
actionFn = () => {
if (mainWin.isFocused()) {
// we need to blur the window for windows
mainWin.blur();
mainWin.hide();
} else {
showOrFocus(mainWin);
}
};
break;

case 'globalToggleTaskStart':
actionFn = () => {
mainWin.webContents.send(IPC.TASK_TOGGLE_START);
};
break;

case 'globalAddNote':
actionFn = () => {
showOrFocus(mainWin);
}
};
break;

case 'globalToggleTaskStart':
actionFn = () => {
mainWin.webContents.send(IPC.TASK_TOGGLE_START);
};
break;

case 'globalAddNote':
actionFn = () => {
showOrFocus(mainWin);
mainWin.webContents.send(IPC.ADD_NOTE);
};
break;

case 'globalAddTask':
actionFn = () => {
showOrFocus(mainWin);
// NOTE: delay slightly to make sure app is ready
mainWin.webContents.send(IPC.ADD_TASK);
};
break;

default:
actionFn = () => undefined;
}
mainWin.webContents.send(IPC.ADD_NOTE);
};
break;

case 'globalAddTask':
actionFn = () => {
showOrFocus(mainWin);
// NOTE: delay slightly to make sure app is ready
mainWin.webContents.send(IPC.ADD_TASK);
};
break;

if (shortcut && shortcut.length > 0) {
const ret = globalShortcut.register(shortcut, actionFn) as unknown;
if (!ret) {
errorHandlerWithFrontendInform(
'Global Shortcut registration failed: ' + shortcut,
shortcut,
);
default:
actionFn = () => undefined;
}
}
});

if (shortcut && shortcut.length > 0) {
const ret = globalShortcut.register(shortcut, actionFn) as unknown;
if (!ret) {
errorHandlerWithFrontendInform(
'Global Shortcut registration failed: ' + shortcut,
shortcut,
);
}
}
});
}
}
}
};

const COMMAND_MAP_PROP = 'allowedCommands';

Expand Down
Loading

0 comments on commit e841b6e

Please sign in to comment.