UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

119 lines • 5.18 kB
// ***************************************************************************** // Copyright (C) 2017 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 { injectable, inject, postConstruct } from 'inversify'; import { DefaultWindowService } from '../../browser/window/default-window-service.js'; import { ElectronMainWindowService } from '../../electron-common/electron-main-window-service.js'; import { ElectronWindowPreferences, PREF_WINDOW_ZOOM_LEVEL } from '../../electron-common/electron-window-preferences.js'; import { ConnectionCloseService } from '../../common/messaging/connection-management.js'; import { FrontendIdProvider } from '../../browser/messaging/frontend-id-provider.js'; import { Listener, ListenerList } from '../../common/listener.js'; let ElectronWindowService = class ElectronWindowService extends DefaultWindowService { constructor() { super(...arguments); /** * Lock to prevent multiple parallel executions of the `beforeunload` listener. */ this.isUnloading = false; /** * Close the window right away when `true`, else check if we can unload. */ this.closeOnUnload = false; this.onWillShutDownListeners = new ListenerList(); this.onWillShutDown = this.onWillShutDownListeners.registration; } openNewWindow(url, { external } = {}) { this.delegate.openNewWindow(url, { external }); return undefined; } async openNewDefaultWindow(params) { return this.delegate.openNewDefaultWindow(params?.search); } closeWindow(windowId) { this.delegate.closeWindow(windowId); } focus() { window.electronTheiaCore.focusWindow(); } init() { // Update the default zoom level on startup when the preferences event is fired. this.electronWindowPreferences.onPreferenceChanged(e => { if (e.preferenceName === PREF_WINDOW_ZOOM_LEVEL) { this.updateWindowZoomLevel(); } }); window.electronTheiaCore.onAboutToClose(() => { this.connectionCloseService.markForClose(this.frontendIdProvider.getId()); }); } registerUnloadListeners() { window.electronTheiaCore.setCloseRequestHandler(async (reason) => { const willShutDown = await this.isSafeToShutDown(reason); if (willShutDown) { await Listener.awaitAll(undefined, this.onWillShutDownListeners); } return willShutDown; }); // Intentionally not calling `super`: in Electron the close request handler above takes the role of // `beforeunload`, and there is no back/forward cache, so only the `pagehide` listener is shared. this.registerPageHideListener(); } /** * Updates the window zoom level based on the preference value. */ async updateWindowZoomLevel() { const preferredZoomLevel = this.electronWindowPreferences[PREF_WINDOW_ZOOM_LEVEL]; if (await window.electronTheiaCore.getZoomLevel() !== preferredZoomLevel) { window.electronTheiaCore.setZoomLevel(preferredZoomLevel); } } reload(params) { if (params) { const newLocation = new URL(location.href); if (params.search) { const query = Object.entries(params.search).map(([name, value]) => `${name}=${value}`).join('&'); newLocation.search = query; } if (params.hash) { newLocation.hash = '#' + params.hash; } window.electronTheiaCore.requestReload(newLocation.toString()); } else { window.electronTheiaCore.requestReload(); } } }; __decorate([ inject(FrontendIdProvider) ], ElectronWindowService.prototype, "frontendIdProvider", void 0); __decorate([ inject(ElectronMainWindowService) ], ElectronWindowService.prototype, "delegate", void 0); __decorate([ inject(ElectronWindowPreferences) ], ElectronWindowService.prototype, "electronWindowPreferences", void 0); __decorate([ inject(ConnectionCloseService) ], ElectronWindowService.prototype, "connectionCloseService", void 0); __decorate([ postConstruct() ], ElectronWindowService.prototype, "init", null); ElectronWindowService = __decorate([ injectable() ], ElectronWindowService); export { ElectronWindowService }; //# sourceMappingURL=electron-window-service.js.map