-
Notifications
You must be signed in to change notification settings - Fork 722
/
Copy pathwindows.ts
133 lines (114 loc) · 3.57 KB
/
windows.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
import * as path from 'node:path';
import { BrowserWindow, app, shell } from 'electron';
import { IpcEvents } from '../ipc-events';
import { createContextMenu } from './context-menu';
import { ipcMainManager } from './ipc';
// Keep a global reference of the window objects, if we don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
export let browserWindows: Array<BrowserWindow | null> = [];
// Global variables exposed by forge/webpack-plugin to reference
// the entry point of preload and index.html over http://
declare const MAIN_WINDOW_WEBPACK_ENTRY: string;
declare const MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY: string;
/**
* Gets default options for the main window
*
* @returns {Electron.BrowserWindowConstructorOptions}
*/
export function getMainWindowOptions(): Electron.BrowserWindowConstructorOptions {
const HEADER_COMMANDS_HEIGHT = 50;
const MACOS_TRAFFIC_LIGHTS_HEIGHT = 16;
return {
width: 1400,
height: 900,
minHeight: 600,
minWidth: 600,
titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : undefined,
titleBarOverlay: process.platform === 'darwin',
trafficLightPosition: {
x: 20,
y: HEADER_COMMANDS_HEIGHT / 2 - MACOS_TRAFFIC_LIGHTS_HEIGHT / 2,
},
acceptFirstMouse: true,
backgroundColor: '#1d2427',
show: false,
webPreferences: {
webviewTag: false,
nodeIntegration: true,
nodeIntegrationInWorker: true,
contextIsolation: false,
preload: !!process.env.JEST
? path.join(process.cwd(), './.webpack/renderer/main_window/preload.js')
: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY,
},
};
}
/**
* Creates a new main window.
*
* @export
* @returns {Electron.BrowserWindow}
*/
export function createMainWindow(): Electron.BrowserWindow {
console.log(`Creating main window`);
let browserWindow: BrowserWindow | null;
browserWindow = new BrowserWindow(getMainWindowOptions());
browserWindow.loadURL(
!!process.env.JEST
? path.join(process.cwd(), './.webpack/renderer/main_window/index.html')
: MAIN_WINDOW_WEBPACK_ENTRY,
);
browserWindow.webContents.once('dom-ready', () => {
if (browserWindow) {
browserWindow.show();
createContextMenu(browserWindow);
}
});
browserWindow.on('focus', () => {
if (browserWindow) {
ipcMainManager.send(IpcEvents.SET_SHOW_ME_TEMPLATE);
}
});
browserWindow.on('closed', () => {
browserWindows = browserWindows.filter((bw) => browserWindow !== bw);
browserWindow = null;
});
browserWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url);
return { action: 'deny' };
});
browserWindow.webContents.on('will-navigate', (event, url) => {
event.preventDefault();
shell.openExternal(url);
});
ipcMainManager.on(IpcEvents.RELOAD_WINDOW, () => {
browserWindow?.reload();
});
ipcMainManager.handle(IpcEvents.GET_APP_PATHS, () => {
const pathsToQuery = [
'home',
'appData',
'userData',
'temp',
'downloads',
'desktop',
] as const;
const paths = {} as Record<typeof pathsToQuery[number], string>;
for (const path of pathsToQuery) {
paths[path] = app.getPath(path);
}
return paths;
});
browserWindows.push(browserWindow);
return browserWindow;
}
/**
* Gets or creates the main window, returning it in both cases.
*
* @returns {Electron.BrowserWindow}
*/
export function getOrCreateMainWindow(): Electron.BrowserWindow {
return (
BrowserWindow.getFocusedWindow() || browserWindows[0] || createMainWindow()
);
}