@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
212 lines • 9.57 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2022 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 { __decorate } from "tslib";
import { StopReason } from '../common/frontend-application-state.js';
import { BrowserWindow } from '../../electron-shared/electron/index.cjs';
import { inject, injectable, postConstruct } from '../../shared/inversify/index.js';
import { ElectronMainApplicationGlobals } from './electron-main-constants.js';
import { DisposableCollection, Emitter } from '../common/index.js';
import { createDisposableListener } from './event-utils.js';
import { URI } from '../common/uri.js';
import { FileUri } from '../common/file-uri.js';
import { TheiaRendererAPI } from './electron-api-main.js';
import { LaunchArgsStore } from './launch-args-store.js';
export const TheiaBrowserWindowOptions = Symbol('TheiaBrowserWindowOptions');
export const WindowApplicationConfig = Symbol('WindowApplicationConfig');
var ClosingState;
(function (ClosingState) {
ClosingState[ClosingState["initial"] = 0] = "initial";
ClosingState[ClosingState["inProgress"] = 1] = "inProgress";
ClosingState[ClosingState["readyToClose"] = 2] = "readyToClose";
})(ClosingState || (ClosingState = {}));
let TheiaElectronWindow = class TheiaElectronWindow {
constructor() {
this.onDidCloseEmitter = new Emitter();
this.toDispose = new DisposableCollection(this.onDidCloseEmitter);
this.closeIsConfirmed = false;
this.applicationState = 'init';
}
get onDidClose() {
return this.onDidCloseEmitter.event;
}
get window() {
return this._window;
}
init() {
this._window = new BrowserWindow(this.options);
this._window.setMenuBarVisibility(false);
if (!this.options.preventAutomaticShow) {
this.attachReadyToShow();
}
this.restoreMaximizedState();
this.attachCloseListeners();
this.trackApplicationState();
this.attachReloadListener();
this.attachSecondaryWindowListener();
}
attachSecondaryWindowListener() {
createDisposableListener(this._window.webContents, 'did-create-window', (newWindow) => {
let closingState = ClosingState.initial;
newWindow.on('close', event => {
if (closingState === ClosingState.initial) {
closingState = ClosingState.inProgress;
event.preventDefault();
TheiaRendererAPI.requestSecondaryClose(this._window.webContents, newWindow.webContents).then(shouldClose => {
if (shouldClose) {
closingState = ClosingState.readyToClose;
newWindow.close();
}
else {
closingState = ClosingState.initial;
}
});
}
else if (closingState === ClosingState.inProgress) {
// When the extracted widget is disposed programmatically, a dispose listener on it will try to close the window.
// if we dispose the widget because of closing the window, we'll get a recursive call to window.close()
event.preventDefault();
}
});
});
}
/**
* Only show the window when the content is ready.
*/
attachReadyToShow() {
this._window.once('ready-to-show', () => this._window.show());
}
attachCloseListeners() {
createDisposableListener(this._window, 'closed', () => {
this.onDidCloseEmitter.fire();
this.dispose();
}, this.toDispose);
createDisposableListener(this._window, 'close', async (event) => {
// User has already indicated that it is OK to close this window, or the window is being closed before it's ready.
if (this.closeIsConfirmed || this.applicationState !== 'ready') {
return;
}
event.preventDefault();
this.handleStopRequest(() => this.doCloseWindow(), StopReason.Close);
}, this.toDispose);
}
async doCloseWindow() {
this.closeIsConfirmed = true;
// Hide the window immediately so the user perceives an instant close.
// This is done after veto checks have passed to ensure save dialogs remain visible.
this._window.hide();
await TheiaRendererAPI.sendAboutToClose(this._window.webContents);
this._window.close();
}
close(reason = StopReason.Close) {
return this.handleStopRequest(() => this.doCloseWindow(), reason);
}
reload(newUrl) {
this.handleStopRequest(async () => {
this.applicationState = 'init';
if (newUrl) {
// The window is being redirected to a *different* frontend, e.g. into a dev container
// after a CLI attach. The forwarded launch arguments belong to the launch that created
// this window and have been acted upon by now, so they must not be redeemed again by
// the frontend loaded from `newUrl`: that would re-run the attach inside the container
// and re-apply the launch's preferences to the remote backend. A plain reload (no
// `newUrl`) keeps them, so `Reload Window` still observes the arguments.
this.launchArgsStore.delete(this._window.webContents.id);
this._window.loadURL(newUrl);
}
else {
this._window.reload();
}
}, StopReason.Reload);
}
async handleStopRequest(onSafeCallback, reason) {
// Only confirm close to windows that have loaded our frontend.
// Both the windows's URL and the FS path of the `index.html` should be converted to the "same" format to be able to compare them. (#11226)
// Notes:
// - Windows: file:///C:/path/to/somewhere vs file:///c%3A/path/to/somewhere
// - macOS: file:///Applications/App%20Name.app/Contents vs /Applications/App Name.app/Contents
// This URL string comes from electron, we can expect that this is properly encoded URL. For example, a space is `%20`
const currentUrl = new URI(this.window.webContents.getURL()).toString();
// THEIA_FRONTEND_HTML_PATH is an FS path, we have to covert to an encoded URI string.
const frontendUri = FileUri.create(this.globals.THEIA_FRONTEND_HTML_PATH).toString();
const safeToClose = !currentUrl.includes(frontendUri) || await this.checkSafeToStop(reason);
if (safeToClose) {
try {
await onSafeCallback();
return true;
}
catch (e) {
console.warn(`Request ${StopReason[reason]} failed.`, e);
}
}
return false;
}
checkSafeToStop(reason) {
return TheiaRendererAPI.requestClose(this.window.webContents, reason);
}
restoreMaximizedState() {
const restore = () => {
if (this.options.isMaximized) {
this._window.maximize();
}
else {
this._window.unmaximize();
}
};
if (this._window.isVisible()) {
restore();
}
else {
this._window.once('show', () => restore());
}
}
trackApplicationState() {
this.toDispose.push(TheiaRendererAPI.onApplicationStateChanged(this.window.webContents, state => {
this.applicationState = state;
}));
}
attachReloadListener() {
this.window.webContents.removeAllListeners('devtools-reload-page');
this.window.webContents.on('devtools-reload-page', () => this.reload());
this.toDispose.push(TheiaRendererAPI.onRequestReload(this.window.webContents, (newUrl) => this.reload(newUrl)));
}
openUrl(url) {
return TheiaRendererAPI.openUrl(this.window.webContents, url);
}
dispose() {
this.toDispose.dispose();
}
};
__decorate([
inject(TheiaBrowserWindowOptions)
], TheiaElectronWindow.prototype, "options", void 0);
__decorate([
inject(WindowApplicationConfig)
], TheiaElectronWindow.prototype, "config", void 0);
__decorate([
inject(ElectronMainApplicationGlobals)
], TheiaElectronWindow.prototype, "globals", void 0);
__decorate([
inject(LaunchArgsStore)
], TheiaElectronWindow.prototype, "launchArgsStore", void 0);
__decorate([
postConstruct()
], TheiaElectronWindow.prototype, "init", null);
TheiaElectronWindow = __decorate([
injectable()
], TheiaElectronWindow);
export { TheiaElectronWindow };
export const TheiaElectronWindowFactory = Symbol('TheiaElectronWindowFactory');
//# sourceMappingURL=theia-electron-window.js.map