-
Notifications
You must be signed in to change notification settings - Fork 27.8k
/
issueService.ts
383 lines (326 loc) · 12.7 KB
/
issueService.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import * as objects from 'vs/base/common/objects';
import { parseArgs } from 'vs/platform/environment/node/argv';
import { IIssueService, IssueReporterData, IssueReporterFeatures, ProcessExplorerData } from 'vs/platform/issue/common/issue';
import { BrowserWindow, ipcMain, screen, dialog } from 'electron';
import { ILaunchService } from 'vs/platform/launch/electron-main/launchService';
import { PerformanceInfo, IDiagnosticsService } from 'vs/platform/diagnostics/electron-main/diagnosticsService';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { isMacintosh, IProcessEnvironment } from 'vs/base/common/platform';
import { ILogService } from 'vs/platform/log/common/log';
import { IWindowsService } from 'vs/platform/windows/common/windows';
import { IWindowState } from 'vs/platform/windows/electron-main/windows';
import { listProcesses } from 'vs/base/node/ps';
import { isRemoteDiagnosticError } from 'vs/platform/diagnostics/common/diagnosticsService';
const DEFAULT_BACKGROUND_COLOR = '#1E1E1E';
export class IssueService implements IIssueService {
_serviceBrand: any;
_issueWindow: BrowserWindow | null;
_issueParentWindow: BrowserWindow | null;
_processExplorerWindow: BrowserWindow | null;
_processExplorerParentWindow: BrowserWindow | null;
constructor(
private machineId: string,
private userEnv: IProcessEnvironment,
@IEnvironmentService private readonly environmentService: IEnvironmentService,
@ILaunchService private readonly launchService: ILaunchService,
@ILogService private readonly logService: ILogService,
@IDiagnosticsService private readonly diagnosticsService: IDiagnosticsService,
@IWindowsService private readonly windowsService: IWindowsService
) {
this.registerListeners();
}
private registerListeners(): void {
ipcMain.on('vscode:issueSystemInfoRequest', (event: Electron.IpcMainEvent) => {
this.diagnosticsService.getSystemInfo(this.launchService).then(msg => {
event.sender.send('vscode:issueSystemInfoResponse', msg);
});
});
ipcMain.on('vscode:listProcesses', async (event: Electron.IpcMainEvent) => {
const processes = [];
try {
const mainPid = await this.launchService.getMainProcessId();
processes.push({ name: localize('local', "Local"), rootProcess: await listProcesses(mainPid) });
(await this.launchService.getRemoteDiagnostics({ includeProcesses: true }))
.forEach(data => {
if (isRemoteDiagnosticError(data)) {
processes.push({
name: data.hostName,
rootProcess: data
});
} else {
if (data.processes) {
processes.push({
name: data.hostName,
rootProcess: data.processes
});
}
}
});
} catch (e) {
this.logService.error(`Listing processes failed: ${e}`);
}
event.sender.send('vscode:listProcessesResponse', processes);
});
ipcMain.on('vscode:issueReporterClipboard', (event: Electron.IpcMainEvent) => {
const messageOptions = {
message: localize('issueReporterWriteToClipboard', "There is too much data to send to GitHub. Would you like to write the information to the clipboard so that it can be pasted?"),
type: 'warning',
buttons: [
localize('yes', "Yes"),
localize('cancel', "Cancel")
]
};
if (this._issueWindow) {
dialog.showMessageBox(this._issueWindow, messageOptions)
.then(result => {
event.sender.send('vscode:issueReporterClipboardResponse', result.response === 0);
});
}
});
ipcMain.on('vscode:issuePerformanceInfoRequest', (event: Electron.IpcMainEvent) => {
this.getPerformanceInfo().then(msg => {
event.sender.send('vscode:issuePerformanceInfoResponse', msg);
});
});
ipcMain.on('vscode:issueReporterConfirmClose', () => {
const messageOptions = {
message: localize('confirmCloseIssueReporter', "Your input will not be saved. Are you sure you want to close this window?"),
type: 'warning',
buttons: [
localize('yes', "Yes"),
localize('cancel', "Cancel")
]
};
if (this._issueWindow) {
dialog.showMessageBox(this._issueWindow, messageOptions)
.then(result => {
if (result.response === 0) {
if (this._issueWindow) {
this._issueWindow.destroy();
this._issueWindow = null;
}
}
});
}
});
ipcMain.on('vscode:workbenchCommand', (_: unknown, commandInfo: { id: any; from: any; args: any; }) => {
const { id, from, args } = commandInfo;
let parentWindow: BrowserWindow | null;
switch (from) {
case 'issueReporter':
parentWindow = this._issueParentWindow;
break;
case 'processExplorer':
parentWindow = this._processExplorerParentWindow;
break;
default:
throw new Error(`Unexpected command source: ${from}`);
}
if (parentWindow) {
parentWindow.webContents.send('vscode:runAction', { id, from, args });
}
});
ipcMain.on('vscode:openExternal', (_: unknown, arg: string) => {
this.windowsService.openExternal(arg);
});
ipcMain.on('vscode:closeIssueReporter', (event: Electron.IpcMainEvent) => {
if (this._issueWindow) {
this._issueWindow.close();
}
});
ipcMain.on('windowsInfoRequest', (event: Electron.IpcMainEvent) => {
this.launchService.getMainProcessInfo().then(info => {
event.sender.send('vscode:windowsInfoResponse', info.windows);
});
});
}
openReporter(data: IssueReporterData): Promise<void> {
return new Promise(_ => {
if (!this._issueWindow) {
this._issueParentWindow = BrowserWindow.getFocusedWindow();
if (this._issueParentWindow) {
const position = this.getWindowPosition(this._issueParentWindow, 700, 800);
this._issueWindow = new BrowserWindow({
fullscreen: false,
width: position.width,
height: position.height,
minWidth: 300,
minHeight: 200,
x: position.x,
y: position.y,
title: localize('issueReporter', "Issue Reporter"),
backgroundColor: data.styles.backgroundColor || DEFAULT_BACKGROUND_COLOR,
webPreferences: {
nodeIntegration: true
}
});
this._issueWindow.setMenuBarVisibility(false); // workaround for now, until a menu is implemented
// Modified when testing UI
const features: IssueReporterFeatures = {};
this.logService.trace('issueService#openReporter: opening issue reporter');
this._issueWindow.loadURL(this.getIssueReporterPath(data, features));
this._issueWindow.on('close', () => this._issueWindow = null);
this._issueParentWindow.on('closed', () => {
if (this._issueWindow) {
this._issueWindow.close();
this._issueWindow = null;
}
});
}
}
if (this._issueWindow) {
this._issueWindow.focus();
}
});
}
openProcessExplorer(data: ProcessExplorerData): Promise<void> {
return new Promise(_ => {
// Create as singleton
if (!this._processExplorerWindow) {
this._processExplorerParentWindow = BrowserWindow.getFocusedWindow();
if (this._processExplorerParentWindow) {
const position = this.getWindowPosition(this._processExplorerParentWindow, 800, 300);
this._processExplorerWindow = new BrowserWindow({
skipTaskbar: true,
resizable: true,
fullscreen: false,
width: position.width,
height: position.height,
minWidth: 300,
minHeight: 200,
x: position.x,
y: position.y,
backgroundColor: data.styles.backgroundColor,
title: localize('processExplorer', "Process Explorer"),
webPreferences: {
nodeIntegration: true
}
});
this._processExplorerWindow.setMenuBarVisibility(false);
const windowConfiguration = {
appRoot: this.environmentService.appRoot,
nodeCachedDataDir: this.environmentService.nodeCachedDataDir,
windowId: this._processExplorerWindow.id,
userEnv: this.userEnv,
machineId: this.machineId,
data
};
const environment = parseArgs(process.argv);
const config = objects.assign(environment, windowConfiguration);
for (let key in config) {
if (config[key] === undefined || config[key] === null || config[key] === '') {
delete config[key]; // only send over properties that have a true value
}
}
this._processExplorerWindow.loadURL(`${require.toUrl('vs/code/electron-browser/processExplorer/processExplorer.html')}?config=${encodeURIComponent(JSON.stringify(config))}`);
this._processExplorerWindow.on('close', () => this._processExplorerWindow = null);
this._processExplorerParentWindow.on('close', () => {
if (this._processExplorerWindow) {
this._processExplorerWindow.close();
this._processExplorerWindow = null;
}
});
}
}
// Focus
if (this._processExplorerWindow) {
this._processExplorerWindow.focus();
}
});
}
public getSystemStatus(): Promise<string> {
return this.diagnosticsService.getDiagnostics(this.launchService);
}
private getWindowPosition(parentWindow: BrowserWindow, defaultWidth: number, defaultHeight: number): IWindowState {
// We want the new window to open on the same display that the parent is in
let displayToUse: Electron.Display | undefined;
const displays = screen.getAllDisplays();
// Single Display
if (displays.length === 1) {
displayToUse = displays[0];
}
// Multi Display
else {
// on mac there is 1 menu per window so we need to use the monitor where the cursor currently is
if (isMacintosh) {
const cursorPoint = screen.getCursorScreenPoint();
displayToUse = screen.getDisplayNearestPoint(cursorPoint);
}
// if we have a last active window, use that display for the new window
if (!displayToUse && parentWindow) {
displayToUse = screen.getDisplayMatching(parentWindow.getBounds());
}
// fallback to primary display or first display
if (!displayToUse) {
displayToUse = screen.getPrimaryDisplay() || displays[0];
}
}
const state: IWindowState = {
width: defaultWidth,
height: defaultHeight
};
const displayBounds = displayToUse.bounds;
state.x = displayBounds.x + (displayBounds.width / 2) - (state.width! / 2);
state.y = displayBounds.y + (displayBounds.height / 2) - (state.height! / 2);
if (displayBounds.width > 0 && displayBounds.height > 0 /* Linux X11 sessions sometimes report wrong display bounds */) {
if (state.x < displayBounds.x) {
state.x = displayBounds.x; // prevent window from falling out of the screen to the left
}
if (state.y < displayBounds.y) {
state.y = displayBounds.y; // prevent window from falling out of the screen to the top
}
if (state.x > (displayBounds.x + displayBounds.width)) {
state.x = displayBounds.x; // prevent window from falling out of the screen to the right
}
if (state.y > (displayBounds.y + displayBounds.height)) {
state.y = displayBounds.y; // prevent window from falling out of the screen to the bottom
}
if (state.width! > displayBounds.width) {
state.width = displayBounds.width; // prevent window from exceeding display bounds width
}
if (state.height! > displayBounds.height) {
state.height = displayBounds.height; // prevent window from exceeding display bounds height
}
}
return state;
}
private getPerformanceInfo(): Promise<PerformanceInfo> {
return new Promise((resolve, reject) => {
this.diagnosticsService.getPerformanceInfo(this.launchService)
.then(diagnosticInfo => {
resolve(diagnosticInfo);
})
.catch(err => {
this.logService.warn('issueService#getPerformanceInfo ', err.message);
reject(err);
});
});
}
private getIssueReporterPath(data: IssueReporterData, features: IssueReporterFeatures): string {
if (!this._issueWindow) {
throw new Error('Issue window has been disposed');
}
const windowConfiguration = {
appRoot: this.environmentService.appRoot,
nodeCachedDataDir: this.environmentService.nodeCachedDataDir,
windowId: this._issueWindow.id,
machineId: this.machineId,
userEnv: this.userEnv,
data,
features
};
const environment = parseArgs(process.argv);
const config = objects.assign(environment, windowConfiguration);
for (let key in config) {
if (config[key] === undefined || config[key] === null || config[key] === '') {
delete config[key]; // only send over properties that have a true value
}
}
return `${require.toUrl('vs/code/electron-browser/issue/issueReporter.html')}?config=${encodeURIComponent(JSON.stringify(config))}`;
}
}