Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/browser/web.factory.ts
3294 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { ITunnel, ITunnelOptions, IWorkbench, IWorkbenchConstructionOptions, Menu } from './web.api.js';
7
import { BrowserMain } from './web.main.js';
8
import { URI, UriComponents } from '../../base/common/uri.js';
9
import { IDisposable, toDisposable } from '../../base/common/lifecycle.js';
10
import { CommandsRegistry } from '../../platform/commands/common/commands.js';
11
import { mark, PerformanceMark } from '../../base/common/performance.js';
12
import { MenuId, MenuRegistry } from '../../platform/actions/common/actions.js';
13
import { DeferredPromise } from '../../base/common/async.js';
14
import { asArray } from '../../base/common/arrays.js';
15
import { IProgress, IProgressCompositeOptions, IProgressDialogOptions, IProgressNotificationOptions, IProgressOptions, IProgressStep, IProgressWindowOptions } from '../../platform/progress/common/progress.js';
16
import { LogLevel } from '../../platform/log/common/log.js';
17
import { IEmbedderTerminalOptions } from '../services/terminal/common/embedderTerminalService.js';
18
19
let created = false;
20
const workbenchPromise = new DeferredPromise<IWorkbench>();
21
22
/**
23
* Creates the workbench with the provided options in the provided container.
24
*
25
* @param domElement the container to create the workbench in
26
* @param options for setting up the workbench
27
*/
28
export function create(domElement: HTMLElement, options: IWorkbenchConstructionOptions): IDisposable {
29
30
// Mark start of workbench
31
mark('code/didLoadWorkbenchMain');
32
33
// Assert that the workbench is not created more than once. We currently
34
// do not support this and require a full context switch to clean-up.
35
if (created) {
36
throw new Error('Unable to create the VSCode workbench more than once.');
37
} else {
38
created = true;
39
}
40
41
// Register commands if any
42
if (Array.isArray(options.commands)) {
43
for (const command of options.commands) {
44
45
CommandsRegistry.registerCommand(command.id, (accessor, ...args) => {
46
// we currently only pass on the arguments but not the accessor
47
// to the command to reduce our exposure of internal API.
48
return command.handler(...args);
49
});
50
51
// Commands with labels appear in the command palette
52
if (command.label) {
53
for (const menu of asArray(command.menu ?? Menu.CommandPalette)) {
54
MenuRegistry.appendMenuItem(asMenuId(menu), { command: { id: command.id, title: command.label } });
55
}
56
}
57
}
58
}
59
60
// Startup workbench and resolve waiters
61
let instantiatedWorkbench: IWorkbench | undefined = undefined;
62
new BrowserMain(domElement, options).open().then(workbench => {
63
instantiatedWorkbench = workbench;
64
workbenchPromise.complete(workbench);
65
});
66
67
return toDisposable(() => {
68
if (instantiatedWorkbench) {
69
instantiatedWorkbench.shutdown();
70
} else {
71
workbenchPromise.p.then(instantiatedWorkbench => instantiatedWorkbench.shutdown());
72
}
73
});
74
}
75
76
function asMenuId(menu: Menu): MenuId {
77
switch (menu) {
78
case Menu.CommandPalette: return MenuId.CommandPalette;
79
case Menu.StatusBarWindowIndicatorMenu: return MenuId.StatusBarWindowIndicatorMenu;
80
}
81
}
82
83
export namespace commands {
84
85
/**
86
* {@linkcode IWorkbench.commands IWorkbench.commands.executeCommand}
87
*/
88
export async function executeCommand(command: string, ...args: any[]): Promise<unknown> {
89
const workbench = await workbenchPromise.p;
90
91
return workbench.commands.executeCommand(command, ...args);
92
}
93
}
94
95
export namespace logger {
96
97
/**
98
* {@linkcode IWorkbench.logger IWorkbench.logger.log}
99
*/
100
export function log(level: LogLevel, message: string) {
101
workbenchPromise.p.then(workbench => workbench.logger.log(level, message));
102
}
103
}
104
105
export namespace env {
106
107
/**
108
* {@linkcode IWorkbench.env IWorkbench.env.retrievePerformanceMarks}
109
*/
110
export async function retrievePerformanceMarks(): Promise<[string, readonly PerformanceMark[]][]> {
111
const workbench = await workbenchPromise.p;
112
113
return workbench.env.retrievePerformanceMarks();
114
}
115
116
/**
117
* {@linkcode IWorkbench.env IWorkbench.env.getUriScheme}
118
*/
119
export async function getUriScheme(): Promise<string> {
120
const workbench = await workbenchPromise.p;
121
122
return workbench.env.getUriScheme();
123
}
124
125
/**
126
* {@linkcode IWorkbench.env IWorkbench.env.openUri}
127
*/
128
export async function openUri(target: URI | UriComponents): Promise<boolean> {
129
const workbench = await workbenchPromise.p;
130
131
return workbench.env.openUri(URI.isUri(target) ? target : URI.from(target));
132
}
133
}
134
135
export namespace window {
136
137
/**
138
* {@linkcode IWorkbench.window IWorkbench.window.withProgress}
139
*/
140
export async function withProgress<R>(
141
options: IProgressOptions | IProgressDialogOptions | IProgressNotificationOptions | IProgressWindowOptions | IProgressCompositeOptions,
142
task: (progress: IProgress<IProgressStep>) => Promise<R>
143
): Promise<R> {
144
const workbench = await workbenchPromise.p;
145
146
return workbench.window.withProgress(options, task);
147
}
148
149
export async function createTerminal(options: IEmbedderTerminalOptions): Promise<void> {
150
const workbench = await workbenchPromise.p;
151
workbench.window.createTerminal(options);
152
}
153
154
export async function showInformationMessage<T extends string>(message: string, ...items: T[]): Promise<T | undefined> {
155
const workbench = await workbenchPromise.p;
156
return await workbench.window.showInformationMessage(message, ...items);
157
}
158
}
159
160
export namespace workspace {
161
162
/**
163
* {@linkcode IWorkbench.workspace IWorkbench.workspace.didResolveRemoteAuthority}
164
*/
165
export async function didResolveRemoteAuthority() {
166
const workbench = await workbenchPromise.p;
167
await workbench.workspace.didResolveRemoteAuthority();
168
}
169
170
/**
171
* {@linkcode IWorkbench.workspace IWorkbench.workspace.openTunnel}
172
*/
173
export async function openTunnel(tunnelOptions: ITunnelOptions): Promise<ITunnel> {
174
const workbench = await workbenchPromise.p;
175
176
return workbench.workspace.openTunnel(tunnelOptions);
177
}
178
}
179
180