@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
152 lines • 6.07 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2018 TypeFox 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 { __decorate } from "tslib";
import { inject, optional, postConstruct, injectable } from 'inversify';
import { Emitter } from '../common/index.js';
import { ExtractableWidget } from './widgets/index.js';
import { ApplicationShell } from './shell/index.js';
import { WidgetManager } from './widget-manager.js';
import { extractSecondaryWindow } from './secondary-window-handler.js';
import { WindowFocusService } from './window/window-focus-service.js';
/**
* Generic base class for {@link OpenHandler}s that are opening a widget for a given {@link URI}.
*/
let WidgetOpenHandler = class WidgetOpenHandler {
constructor() {
this.onCreatedEmitter = new Emitter();
/**
* Emit when a new widget is created.
*/
this.onCreated = this.onCreatedEmitter.event;
}
init() {
this.widgetManager.onDidCreateWidget(({ factoryId, widget }) => {
if (factoryId === this.id) {
this.onCreatedEmitter.fire(widget);
}
});
}
/**
* Open a widget for the given uri and options.
* Reject if the given options are not widget options or a widget cannot be opened.
* @param uri the uri of the resource that should be opened.
* @param options the widget opener options.
*
* @returns promise of the widget that resolves when the widget has been opened.
*/
async open(uri, options) {
const widget = await this.getOrCreateWidget(uri, options);
await this.doOpen(widget, uri, options);
return widget;
}
async doOpen(widget, uri, options) {
const op = {
mode: 'activate',
...options
};
if (!widget.isAttached) {
await this.shell.addWidget(widget, op.widgetOptions || this.getDefaultWidgetOptions(widget));
}
if (op.mode === 'activate') {
await this.shell.activateWidget(widget.id);
}
else if (op.mode === 'reveal') {
await this.shell.revealWidget(widget.id);
}
}
/**
* The options to add a widget with if the opener options don't specify any: widgets are added to the main area,
* unless the user works in a secondary window. Then widgets that can be moved to secondary windows are added there,
* next to the current widget, like editors are opened in the active editor group in VS Code.
*
* @param widget the widget to add, if known already
*/
getDefaultWidgetOptions(widget) {
const current = this.shell.currentWidget;
const secondaryWindow = current && extractSecondaryWindow(current);
if (secondaryWindow && secondaryWindow === this.windowFocusService?.focusedWindow && (!widget || ExtractableWidget.is(widget))) {
return { area: 'secondaryWindow', ref: current };
}
return { area: 'main' };
}
/**
* Tries to get an existing widget for the given uri.
* @param uri the uri of the widget.
*
* @returns a promise that resolves to the existing widget or `undefined` if no widget for the given uri exists.
*/
getByUri(uri) {
return this.getWidget(uri);
}
/**
* Return an existing widget for the given uri or creates a new one.
*
* It does not open a widget, use {@link WidgetOpenHandler#open} instead.
* @param uri uri of the widget.
*
* @returns a promise of the existing or newly created widget.
*/
getOrCreateByUri(uri) {
return this.getOrCreateWidget(uri);
}
/**
* Retrieves all open widgets that have been opened by this handler.
*
* @returns all open widgets for this open handler.
*/
get all() {
return this.widgetManager.getWidgets(this.id);
}
tryGetPendingWidget(uri, options) {
const factoryOptions = this.createWidgetOptions(uri, options);
return this.widgetManager.tryGetPendingWidget(this.id, factoryOptions);
}
getWidget(uri, options) {
const widgetOptions = this.createWidgetOptions(uri, options);
return this.widgetManager.getWidget(this.id, widgetOptions);
}
getOrCreateWidget(uri, options) {
const widgetOptions = this.createWidgetOptions(uri, options);
return this.widgetManager.getOrCreateWidget(this.id, widgetOptions);
}
/**
* Closes all widgets that have been opened by this open handler.
* @param options the close options that should be applied to all widgets.
*
* @returns a promise of all closed widgets that resolves after they have been closed.
*/
async closeAll(options) {
return this.shell.closeMany(this.all, options);
}
};
__decorate([
inject(ApplicationShell)
], WidgetOpenHandler.prototype, "shell", void 0);
__decorate([
inject(WidgetManager)
], WidgetOpenHandler.prototype, "widgetManager", void 0);
__decorate([
inject(WindowFocusService),
optional()
], WidgetOpenHandler.prototype, "windowFocusService", void 0);
__decorate([
postConstruct()
], WidgetOpenHandler.prototype, "init", null);
WidgetOpenHandler = __decorate([
injectable()
], WidgetOpenHandler);
export { WidgetOpenHandler };
//# sourceMappingURL=widget-open-handler.js.map