-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
245 lines (216 loc) · 6.13 KB
/
Copy pathmain.ts
File metadata and controls
245 lines (216 loc) · 6.13 KB
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
import './sentry';
import {
BrowserWindow,
IpcMainEvent,
app,
nativeTheme,
powerMonitor,
systemPreferences,
} from 'electron';
import { setupAboutPanel } from './about-panel';
import { processCommandLine } from './command-line';
import { setupContent } from './content';
// import { setupDevTools } from './devtools'; // 已禁用 DevTools
import { setupDialogs } from './dialogs';
import { setupTypes } from './electron-types';
import { setupFiddleCore } from './fiddle-core';
import { onFirstRunMaybe } from './first-run';
import { ipcMainManager } from './ipc';
import { setupNpm } from './npm';
import { listenForProtocolHandler, setupProtocolHandler } from './protocol';
import { shouldQuit } from './squirrel';
import { setupTemplates } from './templates';
import { setupThemes } from './themes';
import { setupUpdates } from './update';
import { isDevMode } from './utils/devmode';
import { getProjectName } from './utils/get-project-name';
import { getUsername } from './utils/get-username';
import { setupVersions } from './versions';
import { getOrCreateMainWindow, mainIsReady } from './windows';
import { IpcEvents } from '../ipc-events';
import { TachybaseEngine } from './engine';
// import { setupDevTools } from './devtools'; // 已禁用 DevTools
let argv: string[] = [];
/**
* Handle the app's "ready" event. This is essentially
* the method that takes care of booting the application.
*/
export async function onReady() {
await onFirstRunMaybe();
if (!isDevMode()) process.env.NODE_ENV = 'production';
setupAboutPanel();
// const { setupMenu } = await import('./menu');
const { setupFileListeners } = await import('./files');
setupShowWindow();
// 禁用顶部菜单栏
// setupMenu();
setupMenuHandler();
setupProtocolHandler();
setupFileListeners();
setupUpdates();
setupDialogs();
// 禁用 DevTools
// setupDevTools();
setupTitleBarClickMac();
setupNativeTheme();
setupTemplates();
setupContent();
setupThemes();
setupIsDevMode();
setupNpm();
setupPowerMonitor();
const knownVersions = await setupVersions();
setupGetProjectName();
setupGetUsername();
setupTypes(knownVersions);
await setupFiddleCore(knownVersions);
// Do this after setting everything up to ensure that
// any IPC listeners are set up before they're used
mainIsReady();
await getOrCreateMainWindow();
new TachybaseEngine();
processCommandLine(argv);
}
const POWER_MONITOR_EVENTS = ['lock-screen', 'unlock-screen'];
export function setupPowerMonitor() {
POWER_MONITOR_EVENTS.forEach((event) => {
powerMonitor.on(event as any, () => {
ipcMainManager.send(IpcEvents.POWER_MONITOR, [event]);
});
});
}
/**
* Handle the "before-quit" event
*/
export function onBeforeQuit() {
ipcMainManager.send(IpcEvents.BEFORE_QUIT);
ipcMainManager.on(IpcEvents.CONFIRM_QUIT, app.quit);
}
export function setupShowWindow() {
ipcMainManager.on(IpcEvents.SHOW_WINDOW, (event: IpcMainEvent) => {
const win = BrowserWindow.fromWebContents(event.sender);
if (win) {
win.show();
}
});
}
export function setupMenuHandler() {
ipcMainManager.on(
IpcEvents.BLOCK_ACCELERATORS,
async (_, acceleratorsToBlock) => {
(await import('./menu')).setupMenu({
acceleratorsToBlock,
activeTemplate: null,
});
},
);
ipcMainManager.on(
IpcEvents.SET_SHOW_ME_TEMPLATE,
async (_, activeTemplate) => {
(await import('./menu')).setupMenu({
acceleratorsToBlock: [],
activeTemplate,
});
},
);
}
/**
* On macOS, set up the custom titlebar click handler.
*/
export function setupTitleBarClickMac() {
if (process.platform !== 'darwin') {
return;
}
ipcMainManager.on(IpcEvents.CLICK_TITLEBAR_MAC, (event: IpcMainEvent) => {
const doubleClickAction = systemPreferences.getUserDefault(
'AppleActionOnDoubleClick',
'string',
);
const win = BrowserWindow.fromWebContents(event.sender);
if (win) {
if (doubleClickAction === 'Minimize') {
win.minimize();
} else if (doubleClickAction === 'Maximize') {
if (!win.isMaximized()) {
win.maximize();
} else {
win.unmaximize();
}
}
}
});
}
function isNativeThemeSource(
val: unknown,
): val is typeof nativeTheme.themeSource {
return typeof val === 'string' && ['dark', 'light', 'system'].includes(val);
}
/**
* Handle theme changes.
*/
export function setupNativeTheme() {
ipcMainManager.on(IpcEvents.SET_NATIVE_THEME, async (_, source: string) => {
if (isNativeThemeSource(source)) {
nativeTheme.themeSource = source;
}
});
}
/**
* Handle isDevMode for renderer.
*/
export function setupIsDevMode() {
ipcMainManager.on(IpcEvents.IS_DEV_MODE, (event) => {
event.returnValue = isDevMode();
});
}
export function setupGetProjectName() {
ipcMainManager.handle(IpcEvents.GET_PROJECT_NAME, (_, localPath?: string) =>
getProjectName(localPath),
);
}
export function setupGetUsername() {
ipcMainManager.on(IpcEvents.GET_USERNAME, (event) => {
event.returnValue = getUsername();
});
}
/**
* All windows have been closed, quit on anything but
* macOS.
*/
export function onWindowsAllClosed() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
}
/**
* The main method - and the first function to run
* when Fiddle is launched.
*
* Exported for testing purposes.
*/
export function main(argv_in: string[]) {
argv = argv_in;
// Handle creating/removing shortcuts on Windows when
// installing/uninstalling.
if (shouldQuit()) {
app.quit();
return;
}
// Set the app's name
app.name = 'MH-F13';
// Ensure that there's only ever one Fiddle running
listenForProtocolHandler();
// Launch
app.whenReady().then(onReady);
app.on('before-quit', onBeforeQuit);
app.on('window-all-closed', onWindowsAllClosed);
app.on('activate', () => {
app.whenReady().then(getOrCreateMainWindow);
});
}
// only call main() if this is the main module
if (typeof module !== 'undefined' && require.main === module) {
main(process.argv);
}