forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstartPage.ts
More file actions
311 lines (280 loc) · 12.3 KB
/
Copy pathstartPage.ts
File metadata and controls
311 lines (280 loc) · 12.3 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import { ConfigurationTarget, EventEmitter, UIKind, Uri, ViewColumn } from 'vscode';
import { IExtensionSingleActivationService } from '../../activation/types';
import { EXTENSION_ROOT_DIR } from '../../constants';
import { sendTelemetryEvent } from '../../telemetry';
import {
CommandSource,
IApplicationEnvironment,
IApplicationShell,
ICommandManager,
IDocumentManager,
IWebviewPanelProvider,
IWorkspaceService,
} from '../application/types';
import { IFileSystem } from '../platform/types';
import { IConfigurationService, IExtensionContext, Resource } from '../types';
import * as localize from '../utils/localize';
import { StopWatch } from '../utils/stopWatch';
import { Telemetry } from './constants';
import { StartPageMessageListener } from './startPageMessageListener';
import { ICodeCssGenerator, IStartPage, IStartPageMapping, IThemeFinder, StartPageMessages } from './types';
import { WebviewPanelHost } from './webviewPanelHost';
const startPageDir = path.join(EXTENSION_ROOT_DIR, 'out', 'startPage-ui', 'viewers');
// Class that opens, disposes and handles messages and actions for the Python Extension Start Page.
// It also runs when the extension activates.
@injectable()
export class StartPage extends WebviewPanelHost<IStartPageMapping>
implements IStartPage, IExtensionSingleActivationService {
protected closedEvent: EventEmitter<IStartPage> = new EventEmitter<IStartPage>();
private timer: StopWatch;
private actionTaken = false;
private actionTakenOnFirstTime = false;
private firstTime = false;
private webviewDidLoad = false;
constructor(
@inject(IWebviewPanelProvider) provider: IWebviewPanelProvider,
@inject(ICodeCssGenerator) cssGenerator: ICodeCssGenerator,
@inject(IThemeFinder) themeFinder: IThemeFinder,
@inject(IConfigurationService) protected configuration: IConfigurationService,
@inject(IWorkspaceService) workspaceService: IWorkspaceService,
@inject(IFileSystem) private file: IFileSystem,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IDocumentManager) private readonly documentManager: IDocumentManager,
@inject(IApplicationShell) private appShell: IApplicationShell,
@inject(IExtensionContext) private readonly context: IExtensionContext,
@inject(IApplicationEnvironment) private appEnvironment: IApplicationEnvironment,
) {
super(
configuration,
provider,
cssGenerator,
themeFinder,
workspaceService,
(c, v, d) => new StartPageMessageListener(c, v, d),
startPageDir,
[path.join(startPageDir, 'commons.initial.bundle.js'), path.join(startPageDir, 'startPage.js')],
localize.StartPage.getStarted(),
ViewColumn.One,
false,
);
this.timer = new StopWatch();
}
public async activate(): Promise<void> {
if (this.appEnvironment.uiKind === UIKind.Web) {
// We're running in Codespaces browser-based editor, do not open start page.
return;
}
this.activateBackground().ignoreErrors();
}
public dispose(): Promise<void> {
if (!this.isDisposed) {
super.dispose();
}
return this.close();
}
public async open(): Promise<void> {
sendTelemetryEvent(Telemetry.StartPageViewed);
setTimeout(async () => {
await this.loadWebPanel(process.cwd());
// open webview
await super.show(true);
setTimeout(() => {
if (!this.webviewDidLoad) {
sendTelemetryEvent(Telemetry.StartPageWebViewError);
}
}, 5000);
}, 3000);
}
public get owningResource(): Resource {
return undefined;
}
public async close(): Promise<void> {
if (!this.actionTaken) {
sendTelemetryEvent(Telemetry.StartPageClosedWithoutAction);
}
if (this.actionTakenOnFirstTime) {
sendTelemetryEvent(Telemetry.StartPageUsedAnActionOnFirstTime);
}
sendTelemetryEvent(Telemetry.StartPageTime, this.timer.elapsedTime);
// Fire our event
this.closedEvent.fire(this);
}
public async onMessage(message: string, payload: any) {
switch (message) {
case StartPageMessages.Started:
this.webviewDidLoad = true;
break;
case StartPageMessages.RequestShowAgainSetting:
const settings = this.configuration.getSettings();
await this.postMessage(StartPageMessages.SendSetting, {
showAgainSetting: settings.showStartPage,
});
break;
case StartPageMessages.OpenBlankNotebook:
sendTelemetryEvent(Telemetry.StartPageOpenBlankNotebook);
this.setTelemetryFlags();
const savedVersion: string | undefined = this.context.globalState.get('extensionVersion');
if (savedVersion) {
await this.commandManager.executeCommand(
'jupyter.opennotebook',
undefined,
CommandSource.commandPalette,
);
} else {
this.openSampleNotebook().ignoreErrors();
}
break;
case StartPageMessages.OpenBlankPythonFile:
sendTelemetryEvent(Telemetry.StartPageOpenBlankPythonFile);
this.setTelemetryFlags();
const doc = await this.documentManager.openTextDocument({
language: 'python',
content: `print("${localize.StartPage.helloWorld()}")`,
});
await this.documentManager.showTextDocument(doc, 1, true);
break;
case StartPageMessages.OpenInteractiveWindow:
sendTelemetryEvent(Telemetry.StartPageOpenInteractiveWindow);
this.setTelemetryFlags();
const doc2 = await this.documentManager.openTextDocument({
language: 'python',
content: `#%%\nprint("${localize.StartPage.helloWorld()}")`,
});
await this.documentManager.showTextDocument(doc2, 1, true);
await this.commandManager.executeCommand('jupyter.runallcells', Uri.parse(''));
break;
case StartPageMessages.OpenCommandPalette:
sendTelemetryEvent(Telemetry.StartPageOpenCommandPalette);
this.setTelemetryFlags();
await this.commandManager.executeCommand('workbench.action.showCommands');
break;
case StartPageMessages.OpenCommandPaletteWithOpenNBSelected:
sendTelemetryEvent(Telemetry.StartPageOpenCommandPaletteWithOpenNBSelected);
this.setTelemetryFlags();
await this.commandManager.executeCommand('workbench.action.quickOpen', '>Create New Blank Notebook');
break;
case StartPageMessages.OpenSampleNotebook:
sendTelemetryEvent(Telemetry.StartPageOpenSampleNotebook);
this.setTelemetryFlags();
this.openSampleNotebook().ignoreErrors();
break;
case StartPageMessages.OpenFileBrowser:
sendTelemetryEvent(Telemetry.StartPageOpenFileBrowser);
this.setTelemetryFlags();
const uri = await this.appShell.showOpenDialog({
filters: {
Python: ['py', 'ipynb'],
},
canSelectMany: false,
});
if (uri) {
const doc3 = await this.documentManager.openTextDocument(uri[0]);
await this.documentManager.showTextDocument(doc3);
}
break;
case StartPageMessages.OpenFolder:
sendTelemetryEvent(Telemetry.StartPageOpenFolder);
this.setTelemetryFlags();
this.commandManager.executeCommand('workbench.action.files.openFolder');
break;
case StartPageMessages.OpenWorkspace:
sendTelemetryEvent(Telemetry.StartPageOpenWorkspace);
this.setTelemetryFlags();
this.commandManager.executeCommand('workbench.action.openWorkspace');
break;
case StartPageMessages.UpdateSettings:
if (payload === false) {
sendTelemetryEvent(Telemetry.StartPageClickedDontShowAgain);
}
await this.configuration.updateSetting('showStartPage', payload, undefined, ConfigurationTarget.Global);
break;
default:
break;
}
super.onMessage(message, payload);
}
// Public for testing
public async extensionVersionChanged(): Promise<boolean> {
const savedVersion: string | undefined = this.context.globalState.get('extensionVersion');
const version: string = this.appEnvironment.packageJson.version;
let shouldShowStartPage: boolean;
if (savedVersion) {
if (savedVersion === version || this.savedVersionisOlder(savedVersion, version)) {
// There has not been an update
shouldShowStartPage = false;
} else {
sendTelemetryEvent(Telemetry.StartPageOpenedFromNewUpdate);
shouldShowStartPage = true;
}
} else {
sendTelemetryEvent(Telemetry.StartPageOpenedFromNewInstall);
shouldShowStartPage = true;
}
// savedVersion being undefined means this is the first time the user activates the extension.
// if savedVersion != version, there was an update
await this.context.globalState.update('extensionVersion', version);
return shouldShowStartPage;
}
private async activateBackground(): Promise<void> {
const settings = this.configuration.getSettings();
if (settings.showStartPage && this.appEnvironment.extensionChannel === 'stable') {
// extesionVersionChanged() reads CHANGELOG.md
// So we use separate if's to try and avoid reading a file every time
const firstTimeOrUpdate = await this.extensionVersionChanged();
if (firstTimeOrUpdate) {
this.firstTime = true;
this.open().ignoreErrors();
}
}
}
private savedVersionisOlder(savedVersion: string, actualVersion: string): boolean {
const saved = savedVersion.split('.');
const actual = actualVersion.split('.');
switch (true) {
case Number(actual[0]) > Number(saved[0]):
return false;
case Number(actual[0]) < Number(saved[0]):
return true;
case Number(actual[1]) > Number(saved[1]):
return false;
case Number(actual[1]) < Number(saved[1]):
return true;
case Number(actual[2][0]) > Number(saved[2][0]):
return false;
case Number(actual[2][0]) < Number(saved[2][0]):
return true;
default:
return false;
}
}
private async openSampleNotebook(): Promise<void> {
const ipynb = '.ipynb';
const localizedFilePath = path.join(
EXTENSION_ROOT_DIR,
'pythonFiles',
localize.StartPage.sampleNotebook() + ipynb,
);
let sampleNotebookPath: string;
if (await this.file.fileExists(localizedFilePath)) {
sampleNotebookPath = localizedFilePath;
} else {
sampleNotebookPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'Notebooks intro.ipynb');
}
await this.commandManager.executeCommand(
'jupyter.opennotebook',
Uri.file(sampleNotebookPath),
CommandSource.commandPalette,
);
}
private setTelemetryFlags() {
if (this.firstTime) {
this.actionTakenOnFirstTime = true;
}
this.actionTaken = true;
}
}