/
index.ts
572 lines (530 loc) · 21.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable @typescript-eslint/no-misused-promises */
/* eslint-disable @typescript-eslint/consistent-type-assertions */
import mergeDeep from 'lodash/merge';
import { BrowserWindow, ipcMain, dialog, app, clipboard, BrowserWindowConstructorOptions, Tray, nativeImage, Menu } from 'electron';
import { injectable } from 'inversify';
import { menubar, Menubar } from 'menubar';
import windowStateKeeper, { State as windowStateKeeperState } from 'electron-window-state';
import { IBrowserViewMetaData, WindowNames, windowDimension, WindowMeta } from '@services/windows/WindowProperties';
import serviceIdentifier from '@services/serviceIdentifier';
import type { IPreferenceService } from '@services/preferences/interface';
import type { IWorkspaceService } from '@services/workspaces/interface';
import type { IWorkspaceViewService } from '@services/workspacesView/interface';
import type { IMenuService } from '@services/menu/interface';
import { Channels, WindowChannel, MetaDataChannel, ViewChannel } from '@/constants/channels';
import i18n from '@services/libs/i18n';
import getViewBounds from '@services/libs/getViewBounds';
import getFromRenderer from '@services/libs/getFromRenderer';
import { lazyInject } from '@services/container';
import { IWindowService } from './interface';
import { isDevelopmentOrTest, isTest } from '@/constants/environment';
import { MENUBAR_ICON_PATH } from '@/constants/paths';
import { getLocalHostUrlWithActualIP } from '@services/libs/url';
@injectable()
export class Window implements IWindowService {
private windows = {} as Partial<Record<WindowNames, BrowserWindow | undefined>>;
private windowMeta = {} as Partial<WindowMeta>;
/** menubar version of main window, if user set openInMenubar to true in preferences */
private mainWindowMenuBar?: Menubar;
@lazyInject(serviceIdentifier.Preference) private readonly preferenceService!: IPreferenceService;
@lazyInject(serviceIdentifier.Workspace) private readonly workspaceService!: IWorkspaceService;
@lazyInject(serviceIdentifier.WorkspaceView) private readonly workspaceViewService!: IWorkspaceViewService;
@lazyInject(serviceIdentifier.MenuService) private readonly menuService!: IMenuService;
constructor() {
void this.registerMenu();
}
public async findInPage(text: string, forward?: boolean, windowName: WindowNames = WindowNames.main): Promise<void> {
const mainWindow = this.get(windowName);
const contents = mainWindow?.getBrowserView()?.webContents;
if (contents !== undefined) {
contents.findInPage(text, {
forward,
});
}
}
public async stopFindInPage(close?: boolean, windowName: WindowNames = WindowNames.main): Promise<void> {
const mainWindow = this.get(windowName);
const view = mainWindow?.getBrowserView();
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (view) {
const contents = view.webContents;
if (contents !== undefined) {
contents.stopFindInPage('clearSelection');
contents.send(ViewChannel.updateFindInPageMatches, 0, 0);
// adjust bounds to hide the gap for find in page
if (close === true && mainWindow !== undefined) {
const contentSize = mainWindow.getContentSize();
view.setBounds(await getViewBounds(contentSize as [number, number]));
}
}
}
}
public async requestRestart(): Promise<void> {
app.relaunch();
app.quit();
}
public get(windowName: WindowNames = WindowNames.main): BrowserWindow | undefined {
if (windowName === WindowNames.main && this.mainWindowMenuBar?.window !== undefined) {
return this.mainWindowMenuBar.window;
}
return this.windows[windowName];
}
public async close(windowName: WindowNames): Promise<void> {
this.get(windowName)?.close();
}
public async open<N extends WindowNames>(
windowName: N,
meta: WindowMeta[N] = {} as WindowMeta[N],
recreate?: boolean | ((windowMeta: WindowMeta[N]) => boolean),
): Promise<void> {
const existedWindow = this.get(windowName);
// update window meta
await this.setWindowMeta(windowName, meta);
const existedWindowMeta = await this.getWindowMeta(windowName);
const attachToMenubar: boolean = await this.preferenceService.get('attachToMenubar');
const titleBar: boolean = await this.preferenceService.get('titleBar');
const isMainWindow = windowName === WindowNames.main;
// handle existed window, bring existed window to the front and return.
if (existedWindow !== undefined) {
if (recreate === true || (typeof recreate === 'function' && existedWindowMeta !== undefined && recreate(existedWindowMeta))) {
existedWindow.close();
} else {
return existedWindow.show();
}
}
// create new window
let mainWindowConfig: Partial<BrowserWindowConstructorOptions> = {};
let mainWindowState: windowStateKeeperState | undefined;
if (isMainWindow) {
mainWindowState = windowStateKeeper({
defaultWidth: windowDimension[WindowNames.main].width,
defaultHeight: windowDimension[WindowNames.main].height,
});
mainWindowConfig = {
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
alwaysOnTop: await this.preferenceService.get('alwaysOnTop'),
};
}
const windowConfig: BrowserWindowConstructorOptions = {
...windowDimension[windowName],
...mainWindowConfig,
resizable: true,
maximizable: true,
minimizable: true,
fullscreenable: true,
autoHideMenuBar: false,
titleBarStyle: titleBar ? 'default' : 'hidden',
webPreferences: {
devTools: !isTest,
nodeIntegration: false,
webSecurity: !isDevelopmentOrTest,
allowRunningInsecureContent: false,
contextIsolation: true,
preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
additionalArguments: [windowName, JSON.stringify(meta)],
},
parent: isMainWindow || attachToMenubar ? undefined : this.get(WindowNames.main),
};
if (isMainWindow && attachToMenubar) {
this.mainWindowMenuBar = await this.handleAttachToMenuBar(windowConfig);
return;
}
const newWindow = new BrowserWindow(windowConfig);
this.windows[windowName] = newWindow;
if (isMainWindow) {
mainWindowState?.manage(newWindow);
this.registerMainWindowListeners(newWindow);
} else {
newWindow.setMenuBarVisibility(false);
}
const unregisterContextMenu = await this.menuService.initContextMenuForWindowWebContents(newWindow.webContents);
newWindow.on('closed', () => {
this.windows[windowName] = undefined;
unregisterContextMenu();
});
let webContentLoadingPromise: Promise<void> | undefined;
if (isMainWindow) {
// handle window show and Webview/browserView show
webContentLoadingPromise = new Promise<void>((resolve) => {
newWindow.once('ready-to-show', async () => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow === undefined) return;
const { wasOpenedAsHidden } = app.getLoginItemSettings();
if (!wasOpenedAsHidden) {
mainWindow.show();
}
// calling this to redundantly setBounds BrowserView
// after the UI is fully loaded
// if not, BrowserView mouseover event won't work correctly
// https://github.com/atomery/webcatalog/issues/812
await this.workspaceViewService.realignActiveWorkspace();
// ensure redux is loaded first
// if not, redux might not be able catch changes sent from ipcMain
if (!mainWindow.webContents.isLoading()) {
return resolve();
}
mainWindow.webContents.once('did-stop-loading', () => {
resolve();
});
});
});
}
// This loading will wait for a while
await newWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
await webContentLoadingPromise;
}
private registerMainWindowListeners(newWindow: BrowserWindow): void {
// Enable swipe to navigate
void this.preferenceService.get('swipeToNavigate').then((swipeToNavigate) => {
if (swipeToNavigate) {
const mainWindow = this.get(WindowNames.main);
if (mainWindow === undefined) return;
mainWindow.on('swipe', (_event, direction) => {
const view = mainWindow?.getBrowserView();
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
if (view) {
if (direction === 'left') {
view.webContents.goBack();
} else if (direction === 'right') {
view.webContents.goForward();
}
}
});
}
});
// Hide window instead closing on macos
newWindow.on('close', async (event) => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow === undefined) return;
if (process.platform === 'darwin' && (await this.getWindowMeta(WindowNames.main))?.forceClose !== true) {
event.preventDefault();
// https://github.com/electron/electron/issues/6033#issuecomment-242023295
if (mainWindow.isFullScreen()) {
mainWindow.once('leave-full-screen', () => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow !== undefined) {
mainWindow.hide();
}
});
mainWindow.setFullScreen(false);
} else {
mainWindow.hide();
}
}
});
newWindow.on('focus', () => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow === undefined) return;
const view = mainWindow?.getBrowserView();
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
view?.webContents?.focus();
});
newWindow.on('enter-full-screen', async () => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow === undefined) return;
mainWindow?.webContents.send('is-fullscreen-updated', true);
await this.workspaceViewService.realignActiveWorkspace();
});
newWindow.on('leave-full-screen', async () => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow === undefined) return;
mainWindow?.webContents.send('is-fullscreen-updated', false);
await this.workspaceViewService.realignActiveWorkspace();
});
}
public async isFullScreen(windowName = WindowNames.main): Promise<boolean | undefined> {
return this.windows[windowName]?.isFullScreen();
}
public async setWindowMeta<N extends WindowNames>(windowName: N, meta: WindowMeta[N]): Promise<void> {
this.windowMeta[windowName] = meta;
}
public async updateWindowMeta<N extends WindowNames>(windowName: N, meta: WindowMeta[N]): Promise<void> {
this.windowMeta[windowName] = { ...this.windowMeta[windowName], ...meta };
}
public async getWindowMeta<N extends WindowNames>(windowName: N): Promise<WindowMeta[N] | undefined> {
return this.windowMeta[windowName] as WindowMeta[N];
}
/**
* BroadCast message to all opened windows, so we can sync state to redux and make them take effect immediately
* @param channel ipc channel to send
* @param arguments_ any messages
*/
public sendToAllWindows = async (channel: Channels, ...arguments_: unknown[]): Promise<void> => {
const wins = BrowserWindow.getAllWindows();
wins.forEach((win) => {
win.webContents.send(channel, ...arguments_);
});
};
public async goHome(windowName: WindowNames = WindowNames.main): Promise<void> {
const win = this.get(windowName);
const contents = win?.getBrowserView()?.webContents;
const activeWorkspace = await this.workspaceService.getActiveWorkspace();
if (contents !== undefined && activeWorkspace !== undefined && win !== undefined) {
await contents.loadURL(getLocalHostUrlWithActualIP(activeWorkspace.homeUrl));
contents.send(WindowChannel.updateCanGoBack, contents.canGoBack());
contents.send(WindowChannel.updateCanGoForward, contents.canGoForward());
}
}
public async goBack(windowName: WindowNames = WindowNames.main): Promise<void> {
const win = this.get(windowName);
const contents = win?.getBrowserView()?.webContents;
if (contents?.canGoBack() === true) {
contents.goBack();
contents.send(WindowChannel.updateCanGoBack, contents.canGoBack());
contents.send(WindowChannel.updateCanGoForward, contents.canGoForward());
}
}
public async goForward(windowName: WindowNames = WindowNames.main): Promise<void> {
const win = this.get(windowName);
const contents = win?.getBrowserView()?.webContents;
if (contents?.canGoForward() === true) {
contents.goForward();
contents.send(WindowChannel.updateCanGoBack, contents.canGoBack());
contents.send(WindowChannel.updateCanGoForward, contents.canGoForward());
}
}
public async reload(windowName: WindowNames = WindowNames.main): Promise<void> {
const win = this.get(windowName);
win?.getBrowserView()?.webContents?.reload();
}
async loadURL(windowName: WindowNames, newUrl: string): Promise<void> {
const win = this.get(windowName);
await win?.loadURL(newUrl);
}
public async clearStorageData(windowName: WindowNames = WindowNames.main): Promise<void> {
const win = this.get(windowName);
const session = win?.getBrowserView()?.webContents?.session;
if (session !== undefined) {
await session.clearStorageData();
await session.clearAuthCache();
}
}
private async registerMenu(): Promise<void> {
await this.menuService.insertMenu('View', [
{ role: 'reload' },
{ role: 'forceReload' },
// `role: 'zoom'` is only supported on macOS
process.platform === 'darwin'
? {
role: 'zoom',
}
: {
label: 'Zoom',
click: () => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow !== undefined) {
mainWindow.maximize();
}
},
},
{ role: 'resetZoom' },
{ role: 'togglefullscreen' },
{ role: 'close' },
]);
await this.menuService.insertMenu(
'View',
[
{
label: () => i18n.t('Menu.Find'),
accelerator: 'CmdOrCtrl+F',
click: async () => {
const mainWindow = this.get(WindowNames.main);
if (mainWindow !== undefined) {
mainWindow.webContents.focus();
mainWindow.webContents.send(WindowChannel.openFindInPage);
const contentSize = mainWindow.getContentSize();
const view = mainWindow.getBrowserView();
view?.setBounds(await getViewBounds(contentSize as [number, number], true));
}
},
enabled: async () => (await this.workspaceService.countWorkspaces()) > 0,
},
{
label: () => i18n.t('Menu.FindNext'),
accelerator: 'CmdOrCtrl+G',
click: () => {
const mainWindow = this.get(WindowNames.main);
mainWindow?.webContents?.send('request-back-find-in-page', true);
},
enabled: async () => (await this.workspaceService.countWorkspaces()) > 0,
},
{
label: () => i18n.t('Menu.FindPrevious'),
accelerator: 'Shift+CmdOrCtrl+G',
click: () => {
const mainWindow = this.get(WindowNames.main);
mainWindow?.webContents?.send('request-back-find-in-page', false);
},
enabled: async () => (await this.workspaceService.countWorkspaces()) > 0,
},
],
// eslint-disable-next-line unicorn/no-null
null,
true,
);
await this.menuService.insertMenu('History', [
{
label: () => i18n.t('Menu.Home'),
accelerator: 'Shift+CmdOrCtrl+H',
click: async () => await this.goHome(),
enabled: async () => (await this.workspaceService.countWorkspaces()) > 0,
},
{
label: () => i18n.t('ContextMenu.Back'),
accelerator: 'CmdOrCtrl+[',
click: async (_menuItem, browserWindow) => {
// if back is called in popup window
// navigate in the popup window instead
if (browserWindow !== undefined) {
// TODO: test if we really can get this isPopup value
const { isPopup } = await getFromRenderer<IBrowserViewMetaData>(MetaDataChannel.getViewMetaData, browserWindow);
if (isPopup === true) {
browserWindow.webContents.goBack();
return;
}
}
ipcMain.emit('request-go-back');
},
enabled: async () => (await this.workspaceService.countWorkspaces()) > 0,
},
{
label: () => i18n.t('ContextMenu.Forward'),
accelerator: 'CmdOrCtrl+]',
click: async (_menuItem, browserWindow) => {
// if back is called in popup window
// navigate in the popup window instead
if (browserWindow !== undefined) {
const { isPopup } = await getFromRenderer<IBrowserViewMetaData>(MetaDataChannel.getViewMetaData, browserWindow);
if (isPopup === true) {
browserWindow.webContents.goBack();
return;
}
}
ipcMain.emit('request-go-forward');
},
enabled: async () => (await this.workspaceService.countWorkspaces()) > 0,
},
{ type: 'separator' },
{
label: () => i18n.t('ContextMenu.CopyLink'),
accelerator: 'CmdOrCtrl+L',
click: async (_menuItem, browserWindow) => {
// if back is called in popup window
// copy the popup window URL instead
if (browserWindow !== undefined) {
const { isPopup } = await getFromRenderer<IBrowserViewMetaData>(MetaDataChannel.getViewMetaData, browserWindow);
if (isPopup === true) {
const url = browserWindow.webContents.getURL();
clipboard.writeText(url);
return;
}
}
const mainWindow = this.get(WindowNames.main);
const url = mainWindow?.getBrowserView()?.webContents?.getURL();
if (typeof url === 'string') {
clipboard.writeText(url);
}
},
enabled: async () => (await this.workspaceService.countWorkspaces()) > 0,
},
]);
}
private async handleAttachToMenuBar(windowConfig: BrowserWindowConstructorOptions): Promise<Menubar> {
const menubarWindowState = windowStateKeeper({
file: 'window-state-menubar.json',
defaultWidth: 400,
defaultHeight: 400,
});
// setImage after Tray instance is created to avoid
// "Segmentation fault (core dumped)" bug on Linux
// https://github.com/electron/electron/issues/22137#issuecomment-586105622
// https://github.com/atomery/translatium/issues/164
const tray = new Tray(nativeImage.createEmpty());
// icon template is not supported on Windows & Linux
tray.setImage(MENUBAR_ICON_PATH);
const menuBar = menubar({
index: MAIN_WINDOW_WEBPACK_ENTRY,
tray,
preloadWindow: true,
tooltip: i18n.t('Menu.TiddlyGit'),
browserWindow: mergeDeep(windowConfig, {
show: false,
x: menubarWindowState.x,
y: menubarWindowState.y,
width: menubarWindowState.width,
height: menubarWindowState.height,
minHeight: 100,
minWidth: 250,
}),
});
menuBar.on('after-create-window', () => {
if (menuBar.window !== undefined) {
menubarWindowState.manage(menuBar.window);
menuBar.window.on('focus', () => {
const view = menuBar.window?.getBrowserView();
if (view?.webContents !== undefined) {
view.webContents.focus();
}
});
}
});
return await new Promise<Menubar>((resolve) => {
menuBar.on('ready', async () => {
// right on tray icon
menuBar.tray.on('right-click', () => {
// TODO: restore updater options here
const contextMenu = Menu.buildFromTemplate([
{
label: i18n.t('ContextMenu.OpenTiddlyGit'),
click: async () => await menuBar.showWindow(),
},
{
label: i18n.t('Preference.NoAttach'),
click: async () => {
await this.preferenceService.set('attachToMenubar', false);
app.relaunch();
app.quit();
},
},
{
type: 'separator',
},
{
label: i18n.t('ContextMenu.About'),
click: async () => await this.open(WindowNames.about),
},
{ type: 'separator' },
{
label: i18n.t('ContextMenu.Preferences'),
click: async () => await this.open(WindowNames.preferences),
},
{
label: i18n.t('ContextMenu.Notifications'),
click: async () => await this.open(WindowNames.notifications),
},
{ type: 'separator' },
{
label: i18n.t('ContextMenu.Quit'),
click: () => {
menuBar.app.quit();
},
},
]);
menuBar.tray.popUpContextMenu(contextMenu);
});
// right click on window content
if (menuBar.window?.webContents !== undefined) {
const unregisterContextMenu = await this.menuService.initContextMenuForWindowWebContents(menuBar.window.webContents);
menuBar.on('after-close', () => {
unregisterContextMenu();
});
}
resolve(menuBar);
});
});
}
}