-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
getting-started-widget.tsx
625 lines (575 loc) · 23.9 KB
/
getting-started-widget.tsx
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// *****************************************************************************
// Copyright (C) 2018 Ericsson and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { codicon, CommonCommands, Key, KeyCode, LabelProvider, Message, PreferenceService, ReactWidget } from '@theia/core/lib/browser';
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
import { WindowService } from '@theia/core/lib/browser/window/window-service';
import { CommandRegistry, environment, isOSX, Path } from '@theia/core/lib/common';
import { ApplicationInfo, ApplicationServer } from '@theia/core/lib/common/application-protocol';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { nls } from '@theia/core/lib/common/nls';
import URI from '@theia/core/lib/common/uri';
import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
import * as React from '@theia/core/shared/react';
import { KeymapsCommands } from '@theia/keymaps/lib/browser';
import { WorkspaceCommands, WorkspaceService } from '@theia/workspace/lib/browser';
/**
* Default implementation of the `GettingStartedWidget`.
* The widget is displayed when there are currently no workspaces present.
* Some of the features displayed include:
* - `open` commands.
* - `recently used workspaces`.
* - `settings` commands.
* - `help` commands.
* - helpful links.
*/
@injectable()
export class GettingStartedWidget extends ReactWidget {
/**
* The widget `id`.
*/
static readonly ID = 'getting.started.widget';
/**
* The widget `label` which is used for display purposes.
*/
static readonly LABEL = nls.localizeByDefault('Welcome');
/**
* The `ApplicationInfo` for the application if available.
* Used in order to obtain the version number of the application.
*/
protected applicationInfo: ApplicationInfo | undefined;
/**
* The application name which is used for display purposes.
*/
protected applicationName = FrontendApplicationConfigProvider.get().applicationName;
protected home: string | undefined;
/**
* The recently used workspaces limit.
* Used in order to limit the number of recently used workspaces to display.
*/
protected recentLimit = 5;
/**
* The list of recently used workspaces.
*/
protected recentWorkspaces: string[] = [];
/**
* Indicates whether the "ai-core" extension is available.
*/
protected aiIsIncluded: boolean;
/**
* Collection of useful links to display for end users.
*/
protected readonly documentationUrl = 'https://www.theia-ide.org/docs/';
protected readonly compatibilityUrl = 'https://eclipse-theia.github.io/vscode-theia-comparator/status.html';
protected readonly extensionUrl = 'https://www.theia-ide.org/docs/authoring_extensions';
protected readonly pluginUrl = 'https://www.theia-ide.org/docs/authoring_plugins';
protected readonly theiaAIDocUrl = 'https://theia-ide.org/docs/user_ai/';
protected readonly ghProjectUrl = 'https://github.com/eclipse-theia/theia/issues/new/choose';
@inject(ApplicationServer)
protected readonly appServer: ApplicationServer;
@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;
@inject(EnvVariablesServer)
protected readonly environments: EnvVariablesServer;
@inject(LabelProvider)
protected readonly labelProvider: LabelProvider;
@inject(WindowService)
protected readonly windowService: WindowService;
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;
@postConstruct()
protected init(): void {
this.doInit();
}
protected async doInit(): Promise<void> {
this.id = GettingStartedWidget.ID;
this.title.label = GettingStartedWidget.LABEL;
this.title.caption = GettingStartedWidget.LABEL;
this.title.closable = true;
this.applicationInfo = await this.appServer.getApplicationInfo();
this.recentWorkspaces = await this.workspaceService.recentWorkspaces();
this.home = new URI(await this.environments.getHomeDirUri()).path.toString();
const extensions = await this.appServer.getExtensionsInfos();
this.aiIsIncluded = extensions.find(ext => ext.name === '@theia/ai-core') !== undefined;
this.update();
}
protected override onActivateRequest(msg: Message): void {
super.onActivateRequest(msg);
const elArr = this.node.getElementsByTagName('a');
if (elArr && elArr.length > 0) {
(elArr[0] as HTMLElement).focus();
}
}
/**
* Render the content of the widget.
*/
protected render(): React.ReactNode {
return <div className='gs-container'>
<div className='gs-content-container'>
{this.aiIsIncluded &&
<div className='gs-float shadow-pulse'>
{this.renderAIBanner()}
</div>
}
{this.renderHeader()}
<hr className='gs-hr' />
<div className='flex-grid'>
<div className='col'>
{this.renderStart()}
</div>
</div>
<div className='flex-grid'>
<div className='col'>
{this.renderRecentWorkspaces()}
</div>
</div>
<div className='flex-grid'>
<div className='col'>
{this.renderSettings()}
</div>
</div>
<div className='flex-grid'>
<div className='col'>
{this.renderHelp()}
</div>
</div>
<div className='flex-grid'>
<div className='col'>
{this.renderVersion()}
</div>
</div>
</div>
<div className='gs-preference-container'>
{this.renderPreferences()}
</div>
</div>;
}
/**
* Render the widget header.
* Renders the title `{applicationName} Getting Started`.
*/
protected renderHeader(): React.ReactNode {
return <div className='gs-header'>
<h1>{this.applicationName}<span className='gs-sub-header'>{' ' + GettingStartedWidget.LABEL}</span></h1>
</div>;
}
/**
* Render the `Start` section.
* Displays a collection of "start-to-work" related commands like `open` commands and some other.
*/
protected renderStart(): React.ReactNode {
const requireSingleOpen = isOSX || !environment.electron.is();
const createFile = <div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={this.doCreateFile}
onKeyDown={this.doCreateFileEnter}>
{CommonCommands.NEW_UNTITLED_FILE.label ?? nls.localizeByDefault('New File...')}
</a>
</div>;
const open = requireSingleOpen && <div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={this.doOpen}
onKeyDown={this.doOpenEnter}>
{nls.localizeByDefault('Open')}
</a>
</div>;
const openFile = !requireSingleOpen && <div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={this.doOpenFile}
onKeyDown={this.doOpenFileEnter}>
{nls.localizeByDefault('Open File')}
</a>
</div>;
const openFolder = !requireSingleOpen && <div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={this.doOpenFolder}
onKeyDown={this.doOpenFolderEnter}>
{nls.localizeByDefault('Open Folder')}
</a>
</div>;
const openWorkspace = (
<a
role={'button'}
tabIndex={0}
onClick={this.doOpenWorkspace}
onKeyDown={this.doOpenWorkspaceEnter}>
{nls.localizeByDefault('Open Workspace')}
</a>
);
return <div className='gs-section'>
<h3 className='gs-section-header'><i className={codicon('folder-opened')}></i>{nls.localizeByDefault('Start')}</h3>
{createFile}
{open}
{openFile}
{openFolder}
{openWorkspace}
</div>;
}
/**
* Render the recently used workspaces section.
*/
protected renderRecentWorkspaces(): React.ReactNode {
const items = this.recentWorkspaces;
const paths = this.buildPaths(items);
const content = paths.slice(0, this.recentLimit).map((item, index) =>
<div className='gs-action-container' key={index}>
<a
role={'button'}
tabIndex={0}
onClick={() => this.open(new URI(items[index]))}
onKeyDown={(e: React.KeyboardEvent) => this.openEnter(e, new URI(items[index]))}>
{new URI(items[index]).path.base}
</a>
<span className='gs-action-details'>
{item}
</span>
</div>
);
// If the recently used workspaces list exceeds the limit, display `More...` which triggers the recently used workspaces quick-open menu upon selection.
const more = paths.length > this.recentLimit && <div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={this.doOpenRecentWorkspace}
onKeyDown={this.doOpenRecentWorkspaceEnter}>
{nls.localizeByDefault('More...')}
</a>
</div>;
return <div className='gs-section'>
<h3 className='gs-section-header'>
<i className={codicon('history')}></i>{nls.localizeByDefault('Recent')}
</h3>
{items.length > 0 ? content : <p className='gs-no-recent'>
{nls.localizeByDefault('You have no recent folders,') + ' '}
<a
role={'button'}
tabIndex={0}
onClick={this.doOpenFolder}
onKeyDown={this.doOpenFolderEnter}>
{nls.localizeByDefault('open a folder')}
</a>
{' ' + nls.localizeByDefault('to start.')}
</p>}
{more}
</div>;
}
/**
* Render the settings section.
* Generally used to display useful links.
*/
protected renderSettings(): React.ReactNode {
return <div className='gs-section'>
<h3 className='gs-section-header'>
<i className={codicon('settings-gear')}></i>
{nls.localizeByDefault('Settings')}
</h3>
<div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={this.doOpenPreferences}
onKeyDown={this.doOpenPreferencesEnter}>
{nls.localizeByDefault('Open Settings')}
</a>
</div>
<div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={this.doOpenKeyboardShortcuts}
onKeyDown={this.doOpenKeyboardShortcutsEnter}>
{nls.localizeByDefault('Open Keyboard Shortcuts')}
</a>
</div>
</div>;
}
/**
* Render the help section.
*/
protected renderHelp(): React.ReactNode {
return <div className='gs-section'>
<h3 className='gs-section-header'>
<i className={codicon('question')}></i>
{nls.localizeByDefault('Help')}
</h3>
<div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={() => this.doOpenExternalLink(this.documentationUrl)}
onKeyDown={(e: React.KeyboardEvent) => this.doOpenExternalLinkEnter(e, this.documentationUrl)}>
{nls.localizeByDefault('Documentation')}
</a>
</div>
<div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={() => this.doOpenExternalLink(this.compatibilityUrl)}
onKeyDown={(e: React.KeyboardEvent) => this.doOpenExternalLinkEnter(e, this.compatibilityUrl)}>
{nls.localize('theia/getting-started/apiComparator', '{0} API Compatibility', 'VS Code')}
</a>
</div>
<div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={() => this.doOpenExternalLink(this.extensionUrl)}
onKeyDown={(e: React.KeyboardEvent) => this.doOpenExternalLinkEnter(e, this.extensionUrl)}>
{nls.localize('theia/getting-started/newExtension', 'Building a New Extension')}
</a>
</div>
<div className='gs-action-container'>
<a
role={'button'}
tabIndex={0}
onClick={() => this.doOpenExternalLink(this.pluginUrl)}
onKeyDown={(e: React.KeyboardEvent) => this.doOpenExternalLinkEnter(e, this.pluginUrl)}>
{nls.localize('theia/getting-started/newPlugin', 'Building a New Plugin')}
</a>
</div>
</div>;
}
/**
* Render the version section.
*/
protected renderVersion(): React.ReactNode {
return <div className='gs-section'>
<div className='gs-action-container'>
<p className='gs-sub-header' >
{this.applicationInfo ? nls.localizeByDefault('Version: {0}', this.applicationInfo.version) : ''}
</p>
</div>
</div>;
}
protected renderPreferences(): React.ReactNode {
return <WelcomePreferences preferenceService={this.preferenceService}></WelcomePreferences>;
}
protected renderAIBanner(): React.ReactNode {
return <div className='gs-container gs-experimental-container'>
<div className='flex-grid'>
<div className='col'>
<h3 className='gs-section-header'> 🚀 AI Support in the Theia IDE is available! [Experimental] ✨</h3>
<br />
<div className='gs-action-container'>
Theia IDE now contains experimental AI support, which offers early access to cutting-edge AI capabilities within your IDE.
<br />
<br />
Please note that these features are disabled by default, ensuring that users can opt-in at their discretion.
For those who choose to enable AI support, it is important to be aware that these experimental features may generate continuous
requests to the language models (LLMs) you provide access to. This might incur costs that you need to monitor closely.
<br />
For more details, please visit
<a
role={'button'}
tabIndex={0}
onClick={() => this.doOpenExternalLink(this.theiaAIDocUrl)}
onKeyDown={(e: React.KeyboardEvent) => this.doOpenExternalLinkEnter(e, this.theiaAIDocUrl)}>
{'the documentation'}
</a>.
<br />
<br />
🚧 Please note that this feature is currently in development and may undergo frequent changes.
We welcome your feedback, contributions, and sponsorship! To support the ongoing development of the AI capabilities please visit the
<a
role={'button'}
tabIndex={0}
onClick={() => this.doOpenExternalLink(this.ghProjectUrl)}
onKeyDown={(e: React.KeyboardEvent) => this.doOpenExternalLinkEnter(e, this.ghProjectUrl)}>
{'Github Project'}
</a>.
Thank you for being part of our community!
</div>
<br />
<div className='gs-action-container'>
<a
role={'button'}
style={{ fontSize: 'var(--theia-ui-font-size2)' }}
tabIndex={0}
onClick={() => this.doOpenAIChatView()}
onKeyDown={(e: React.KeyboardEvent) => this.doOpenAIChatViewEnter(e)}>
{'Open the AI Chat View now to learn how to start! ✨'}
</a>
</div>
<br />
<br />
</div>
</div>
</div>;
}
protected doOpenAIChatView = () => this.commandRegistry.executeCommand('aiChat:toggle');
protected doOpenAIChatViewEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpenAIChatView();
}
};
/**
* Build the list of workspace paths.
* @param workspaces {string[]} the list of workspaces.
* @returns {string[]} the list of workspace paths.
*/
protected buildPaths(workspaces: string[]): string[] {
const paths: string[] = [];
workspaces.forEach(workspace => {
const uri = new URI(workspace);
const pathLabel = this.labelProvider.getLongName(uri);
const path = this.home ? Path.tildify(pathLabel, this.home) : pathLabel;
paths.push(path);
});
return paths;
}
/**
* Trigger the create file command.
*/
protected doCreateFile = () => this.commandRegistry.executeCommand(CommonCommands.NEW_UNTITLED_FILE.id);
protected doCreateFileEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doCreateFile();
}
};
/**
* Trigger the open command.
*/
protected doOpen = () => this.commandRegistry.executeCommand(WorkspaceCommands.OPEN.id);
protected doOpenEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpen();
}
};
/**
* Trigger the open file command.
*/
protected doOpenFile = () => this.commandRegistry.executeCommand(WorkspaceCommands.OPEN_FILE.id);
protected doOpenFileEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpenFile();
}
};
/**
* Trigger the open folder command.
*/
protected doOpenFolder = () => this.commandRegistry.executeCommand(WorkspaceCommands.OPEN_FOLDER.id);
protected doOpenFolderEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpenFolder();
}
};
/**
* Trigger the open workspace command.
*/
protected doOpenWorkspace = () => this.commandRegistry.executeCommand(WorkspaceCommands.OPEN_WORKSPACE.id);
protected doOpenWorkspaceEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpenWorkspace();
}
};
/**
* Trigger the open recent workspace command.
*/
protected doOpenRecentWorkspace = () => this.commandRegistry.executeCommand(WorkspaceCommands.OPEN_RECENT_WORKSPACE.id);
protected doOpenRecentWorkspaceEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpenRecentWorkspace();
}
};
/**
* Trigger the open preferences command.
* Used to open the preferences widget.
*/
protected doOpenPreferences = () => this.commandRegistry.executeCommand(CommonCommands.OPEN_PREFERENCES.id);
protected doOpenPreferencesEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpenPreferences();
}
};
/**
* Trigger the open keyboard shortcuts command.
* Used to open the keyboard shortcuts widget.
*/
protected doOpenKeyboardShortcuts = () => this.commandRegistry.executeCommand(KeymapsCommands.OPEN_KEYMAPS.id);
protected doOpenKeyboardShortcutsEnter = (e: React.KeyboardEvent) => {
if (this.isEnterKey(e)) {
this.doOpenKeyboardShortcuts();
}
};
/**
* Open a workspace given its uri.
* @param uri {URI} the workspace uri.
*/
protected open = (uri: URI) => this.workspaceService.open(uri);
protected openEnter = (e: React.KeyboardEvent, uri: URI) => {
if (this.isEnterKey(e)) {
this.open(uri);
}
};
/**
* Open a link in an external window.
* @param url the link.
*/
protected doOpenExternalLink = (url: string) => this.windowService.openNewWindow(url, { external: true });
protected doOpenExternalLinkEnter = (e: React.KeyboardEvent, url: string) => {
if (this.isEnterKey(e)) {
this.doOpenExternalLink(url);
}
};
protected isEnterKey(e: React.KeyboardEvent): boolean {
return Key.ENTER.keyCode === KeyCode.createKeyCode(e.nativeEvent).key?.keyCode;
}
}
export interface PreferencesProps {
preferenceService: PreferenceService;
}
function WelcomePreferences(props: PreferencesProps): JSX.Element {
const [startupEditor, setStartupEditor] = React.useState<string>(
props.preferenceService.get('workbench.startupEditor', 'welcomePage')
);
React.useEffect(() => {
const prefListener = props.preferenceService.onPreferenceChanged(change => {
if (change.preferenceName === 'workbench.startupEditor') {
const prefValue = change.newValue;
setStartupEditor(prefValue);
}
});
return () => prefListener.dispose();
}, [props.preferenceService]);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.checked ? 'welcomePage' : 'none';
props.preferenceService.updateValue('workbench.startupEditor', newValue);
};
return (
<div className='gs-preference'>
<input
type="checkbox"
className="theia-input"
id="startupEditor"
onChange={handleChange}
checked={startupEditor === 'welcomePage' || startupEditor === 'welcomePageInEmptyWorkbench'}
/>
<label htmlFor="startupEditor">
{nls.localizeByDefault('Show welcome page on startup')}
</label>
</div>
);
}