refactor: cleanup initIPCHandlers

This commit is contained in:
tiddlygit-test 2021-02-06 23:15:12 +08:00
parent 5a94266d3a
commit 762e1bb596
7 changed files with 32 additions and 78 deletions

View file

@ -50,6 +50,7 @@ export enum WikiGitWorkspaceChannel {
}
export enum WorkspaceChannel {
name = 'WorkspaceChannel',
focusWorkspace = 'focus-workspace',
}
export enum WorkspaceViewChannel {
name = 'WorkspaceViewChannel',
@ -68,6 +69,7 @@ export enum WindowChannel {
updateTitle = 'update-title',
updateCanGoBack = 'update-can-go-back',
updateCanGoForward = 'update-can-go-forward',
showDisplayMediaWindow = 'show-display-media-window',
}
export enum ThemeChannel {

View file

@ -1,13 +1,12 @@
import { ipcRenderer, remote, webFrame } from 'electron';
import { enable as enableDarkMode, disable as disableDarkMode } from 'darkreader';
import ContextMenuBuilder from '@services/libs/context-menu-builder';
import { ContextChannel, NotificationChannel } from '@/constants/channels';
import { WindowChannel, NotificationChannel, WorkspaceChannel } from '@/constants/channels';
import i18next from '@services/libs/i18n';
import './wiki-operation';
import { preference, theme } from './common/services';
import { preference, theme, workspace, workspaceView, menu } from './common/services';
const { MenuItem, shell } = remote;
// @ts-expect-error ts-migrate(2322) FIXME: Type '{}' is not assignable to type 'Global & type... Remove this comment to see the full error message
window.global = {};
let handled = false;
const handleLoaded = async (event: string): Promise<void> => {
@ -67,7 +66,6 @@ const handleLoaded = async (event: string): Promise<void> => {
console.log(error); // eslint-disable-line no-console
}
}
// @ts-expect-error ts-migrate(2554) FIXME: Expected 1-2 arguments, but got 0.
(window as any).contextMenuBuilder = new ContextMenuBuilder();
remote.getCurrentWebContents().on('context-menu', (e, info) => {
// eslint-disable-next-line promise/catch-or-return
@ -121,30 +119,30 @@ const handleLoaded = async (event: string): Promise<void> => {
submenu: [
{
label: i18next.t('ContextMenu.About'),
click: () => ipcRenderer.invoke('request-show-about-window'),
click: async () => await ipcRenderer.invoke('request-show-about-window'),
},
{ type: 'separator' },
{
label: i18next.t('ContextMenu.CheckForUpdates'),
click: () => ipcRenderer.invoke('request-check-for-updates'),
click: async () => await ipcRenderer.invoke('request-check-for-updates'),
},
{
label: i18next.t('ContextMenu.Preferences'),
click: () => ipcRenderer.invoke('request-show-preferences-window'),
click: async () => await ipcRenderer.invoke('request-show-preferences-window'),
},
{ type: 'separator' },
{
label: i18next.t('ContextMenu.TiddlyGitSupport'),
click: () => shell.openExternal('https://github.com/tiddly-gittly/TiddlyGit-Desktop/issues/new/choose'),
click: async () => await shell.openExternal('https://github.com/tiddly-gittly/TiddlyGit-Desktop/issues/new/choose'),
},
{
label: i18next.t('ContextMenu.TiddlyGitWebsite'),
click: () => shell.openExternal('https://github.com/tiddly-gittly/TiddlyGit-Desktop'),
click: async () => await shell.openExternal('https://github.com/tiddly-gittly/TiddlyGit-Desktop'),
},
{ type: 'separator' },
{
label: i18next.t('ContextMenu.Quit'),
click: () => ipcRenderer.invoke('request-quit'),
click: async () => await ipcRenderer.invoke('request-quit'),
},
],
}),
@ -170,31 +168,32 @@ const handleLoaded = async (event: string): Promise<void> => {
};
// try to load as soon as dom is loaded
document.addEventListener('DOMContentLoaded', () => handleLoaded('document.on("DOMContentLoaded")'));
document.addEventListener('DOMContentLoaded', async () => await handleLoaded('document.on("DOMContentLoaded")'));
// if user navigates between the same website
// DOMContentLoaded might not be triggered so double check with 'onload'
// https://github.com/atomery/webcatalog/issues/797
window.addEventListener('load', () => handleLoaded('window.on("onload")'));
window.addEventListener('load', async () => await handleLoaded('window.on("onload")'));
// Communicate with the frame
// Have to use this weird trick because contextIsolation: true
ipcRenderer.on(NotificationChannel.shouldPauseNotificationsChanged, (e, value) => {
// @ts-expect-error ts-migrate(2554) FIXME: Expected 2-3 arguments, but got 1.
window.postMessage({ type: NotificationChannel.shouldPauseNotificationsChanged, val: value });
});
ipcRenderer.on('display-media-id-received', (e, value) => {
// @ts-expect-error ts-migrate(2554) FIXME: Expected 2-3 arguments, but got 1.
window.postMessage({ type: 'return-display-media-id', val: value });
});
window.addEventListener('message', (e) => {
if (!e.data) {
window.addEventListener('message', (event) => {
if (!event.data) {
return;
}
if (e.data.type === 'get-display-media-id') {
ipcRenderer.invoke('request-show-display-media-window');
if (event.data.type === 'get-display-media-id') {
ipcRenderer.invoke(WindowChannel.showDisplayMediaWindow);
}
// set workspace to active when its notification is clicked
if (e.data.type === 'focus-workspace') {
ipcRenderer.invoke('request-set-active-workspace', e.data.workspaceId);
if (event.data.type === WorkspaceChannel.focusWorkspace) {
const id = event.data.workspaceId;
if (workspace.get(id) !== undefined) {
void workspaceView.setActiveWorkspaceView(id).then(async () => await menu.buildMenu());
}
}
});
// Fix Can't show file list of Google Drive
@ -202,7 +201,6 @@ window.addEventListener('message', (e) => {
// Fix chrome.runtime.sendMessage is undefined for FastMail
// https://github.com/atomery/singlebox/issues/21
const initialShouldPauseNotifications = ipcRenderer.invoke('get-pause-notifications-info') != undefined;
// @ts-expect-error ts-migrate(2339) FIXME: Property 'workspaceId' does not exist on type 'Web... Remove this comment to see the full error message
const { workspaceId } = remote.getCurrentWebContents();
void webFrame.executeJavaScript(`
(function() {
@ -243,7 +241,7 @@ void webFrame.executeJavaScript(`
if (!shouldPauseNotifications) {
const notif = new oldNotification(...arguments);
notif.addEventListener('click', () => {
window.postMessage({ type: 'focus-workspace', workspaceId: "${workspaceId}" });
window.postMessage({ type: '${WorkspaceChannel.focusWorkspace}', workspaceId: "${workspaceId}" });
});
return notif;
}

View file

@ -1,6 +1,6 @@
import { injectable } from 'inversify';
import getDecorators from 'inversify-inject-decorators';
import { app, App, remote, dialog } from 'electron';
import { app, App, remote, dialog, nativeTheme } from 'electron';
import path from 'path';
import semver from 'semver';
import settings from 'electron-settings';
@ -13,6 +13,7 @@ import { PreferenceChannel } from '@/constants/channels';
import { container } from '@services/container';
import i18n from '@services/libs/i18n';
import { IPreferences, IPreferenceService } from './interface';
import { IViewService } from '@services/view/interface';
const { lazyInject } = getDecorators(container);
@ -84,6 +85,7 @@ const defaultPreferences: IPreferences = {
@injectable()
export class Preference implements IPreferenceService {
@lazyInject(serviceIdentifier.Window) private readonly windowService!: IWindowService;
@lazyInject(serviceIdentifier.View) private readonly viewService!: IViewService;
@lazyInject(serviceIdentifier.NotificationService) private readonly notificationService!: INotificationService;
cachedPreferences: IPreferences;
@ -155,17 +157,16 @@ export class Preference implements IPreferenceService {
* @param preference new preference settings
*/
private reactWhenPreferencesChanged<K extends keyof IPreferences>(key: K, value: IPreferences[K]): void {
// TODO: call ThemeService
// if (key.startsWith('darkReader')) {
// ipcMain.emit('request-reload-views-dark-reader');
// }
if (key.startsWith('darkReader')) {
this.viewService.reloadViewsDarkReader();
}
// maybe pauseNotificationsBySchedule or pauseNotifications or ...
if (key.startsWith('pauseNotifications')) {
this.notificationService.updatePauseNotificationsInfo();
}
// if (key === 'themeSource') {
// nativeTheme.themeSource = value;
// }
if (key === 'themeSource') {
nativeTheme.themeSource = value as IPreferences['themeSource'];
}
}
/**

View file

@ -46,9 +46,6 @@ export class View implements IViewService {
this.reloadViewsWebContentsIfDidFailLoad();
}
});
ipcMain.handle('request-reload-views-dark-reader', () => {
this.reloadViewsDarkReader();
});
}
private registerMenu(): void {

View file

@ -39,7 +39,7 @@ export class Window implements IWindowService {
}
initIPCHandlers(): void {
ipcMain.handle('request-show-display-media-window', (_event: Electron.IpcMainInvokeEvent) => {
ipcMain.handle(WindowChannel.showDisplayMediaWindow, (_event: Electron.IpcMainInvokeEvent) => {
const viewID = BrowserWindow.fromWebContents(_event.sender)?.id;
if (viewID !== undefined) {
return this.open(WindowNames.displayMedia, { displayMediaRequestedViewID: viewID }, (windowMeta: WindowMeta[WindowNames.displayMedia]) => {

View file

@ -43,34 +43,9 @@ export class Workspace implements IWorkspaceService {
constructor() {
this.workspaces = this.getInitWorkspacesForCache();
this.initIPCHandlers();
this.registerMenu();
}
initIPCHandlers(): void {
ipcMain.handle('get-workspace-meta', (_event, id) => {
return this.getMetaData(id);
});
ipcMain.handle('get-workspace-metas', (_event) => {
return this.getAllMetaData();
});
ipcMain.handle('count-workspace', (_event) => {
return this.countWorkspaces();
});
ipcMain.handle('get-workspace', (_event, id) => {
return this.get(id);
});
ipcMain.handle('get-workspaces', (_event) => {
return this.getWorkspaces();
});
ipcMain.handle('request-set-workspace-picture', async (_event, id, picturePath) => {
await this.setWorkspacePicture(id, picturePath);
});
ipcMain.handle('request-remove-workspace-picture', async (_event, id) => {
await this.removeWorkspacePicture(id);
});
}
private registerMenu(): void {
this.menuService.insertMenu('Workspaces', [
{

View file

@ -27,28 +27,9 @@ export class WorkspaceView implements IWorkspaceViewService {
@inject(serviceIdentifier.Preference) private readonly preferenceService: IPreferenceService,
@inject(serviceIdentifier.MenuService) private readonly menuService: IMenuService,
) {
this.initIPCHandlers();
this.registerMenu();
}
private initIPCHandlers(): void {
ipcMain.handle('request-set-active-workspace', async (_event, id) => {
if (this.workspaceService.get(id) !== undefined) {
await this.setActiveWorkspaceView(id);
this.menuService.buildMenu();
}
});
ipcMain.handle('request-set-workspace', async (_event, id, options) => {
await this.setWorkspaceView(id, options);
this.menuService.buildMenu();
});
ipcMain.handle('request-set-workspaces', async (_event, workspaces) => {
await this.setWorkspaceViews(workspaces);
this.menuService.buildMenu();
});
}
/**
* Prepare workspaces on startup
*/