ATL-879: Avoid reopening the same sketch.

Instead of reopening it, focus the existing window.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2021-02-03 14:14:34 +01:00 committed by Akos Kitta
parent 96f0722d56
commit 23877f162c
3 changed files with 38 additions and 5 deletions

View File

@ -1,15 +1,20 @@
import { ContainerModule } from 'inversify'; import { ContainerModule } from 'inversify';
import { JsonRpcConnectionHandler } from '@theia/core/lib/common/messaging/proxy-factory'; import { JsonRpcConnectionHandler } from '@theia/core/lib/common/messaging/proxy-factory';
import { ElectronConnectionHandler } from '@theia/core/lib/electron-common/messaging/electron-connection-handler'; import { ElectronConnectionHandler } from '@theia/core/lib/electron-common/messaging/electron-connection-handler';
import { ElectronMainWindowService } from '@theia/core/lib/electron-common/electron-main-window-service';
import { ElectronMainApplication as TheiaElectronMainApplication } from '@theia/core/lib/electron-main/electron-main-application'; import { ElectronMainApplication as TheiaElectronMainApplication } from '@theia/core/lib/electron-main/electron-main-application';
import { SplashService, splashServicePath } from '../electron-common/splash-service'; import { SplashService, splashServicePath } from '../electron-common/splash-service';
import { SplashServiceImpl } from './splash/splash-service-impl'; import { SplashServiceImpl } from './splash/splash-service-impl';
import { ElectronMainApplication } from './theia/electron-main-application'; import { ElectronMainApplication } from './theia/electron-main-application';
import { ElectronMainWindowServiceImpl } from './theia/electron-main-window-service';
export default new ContainerModule((bind, unbind, isBound, rebind) => { export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(ElectronMainApplication).toSelf().inSingletonScope(); bind(ElectronMainApplication).toSelf().inSingletonScope();
rebind(TheiaElectronMainApplication).toService(ElectronMainApplication); rebind(TheiaElectronMainApplication).toService(ElectronMainApplication);
bind(ElectronMainWindowServiceImpl).toSelf().inSingletonScope();
rebind(ElectronMainWindowService).toService(ElectronMainWindowServiceImpl);
bind(SplashServiceImpl).toSelf().inSingletonScope(); bind(SplashServiceImpl).toSelf().inSingletonScope();
bind(SplashService).toService(SplashServiceImpl); bind(SplashService).toService(SplashServiceImpl);
bind(ElectronConnectionHandler).toDynamicValue(context => bind(ElectronConnectionHandler).toDynamicValue(context =>

View File

@ -13,7 +13,7 @@ import { SplashServiceImpl } from '../splash/splash-service-impl';
@injectable() @injectable()
export class ElectronMainApplication extends TheiaElectronMainApplication { export class ElectronMainApplication extends TheiaElectronMainApplication {
protected windows: BrowserWindow[] = []; protected _windows: BrowserWindow[] = [];
@inject(SplashServiceImpl) @inject(SplashServiceImpl)
protected readonly splashService: SplashServiceImpl; protected readonly splashService: SplashServiceImpl;
@ -34,7 +34,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
async createWindow(asyncOptions: MaybePromise<TheiaBrowserWindowOptions> = this.getDefaultBrowserWindowOptions()): Promise<BrowserWindow> { async createWindow(asyncOptions: MaybePromise<TheiaBrowserWindowOptions> = this.getDefaultBrowserWindowOptions()): Promise<BrowserWindow> {
const options = await asyncOptions; const options = await asyncOptions;
let electronWindow: BrowserWindow | undefined; let electronWindow: BrowserWindow | undefined;
if (this.windows.length) { if (this._windows.length) {
electronWindow = new BrowserWindow(options); electronWindow = new BrowserWindow(options);
} else { } else {
const { bounds } = screen.getDisplayNearestPoint(screen.getCursorScreenPoint()); const { bounds } = screen.getDisplayNearestPoint(screen.getCursorScreenPoint());
@ -63,14 +63,14 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
splashScreenOpts splashScreenOpts
}, this.splashService.onCloseRequested); }, this.splashService.onCloseRequested);
} }
this.windows.push(electronWindow); this._windows.push(electronWindow);
electronWindow.on('closed', () => { electronWindow.on('closed', () => {
if (electronWindow) { if (electronWindow) {
const index = this.windows.indexOf(electronWindow); const index = this._windows.indexOf(electronWindow);
if (index === -1) { if (index === -1) {
console.warn(`Could not dispose browser window: '${electronWindow.title}'.`); console.warn(`Could not dispose browser window: '${electronWindow.title}'.`);
} else { } else {
this.windows.splice(index, 1); this._windows.splice(index, 1);
electronWindow = undefined; electronWindow = undefined;
} }
} }
@ -148,4 +148,8 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
} }
} }
get windows(): BrowserWindow[] {
return this._windows.slice();
}
} }

View File

@ -0,0 +1,24 @@
import { inject, injectable } from 'inversify';
import { NewWindowOptions } from '@theia/core/lib/browser/window/window-service';
import { ElectronMainWindowServiceImpl as TheiaElectronMainWindowService } from '@theia/core/lib/electron-main/electron-main-window-service-impl';
import { ElectronMainApplication } from './electron-main-application';
@injectable()
export class ElectronMainWindowServiceImpl extends TheiaElectronMainWindowService {
@inject(ElectronMainApplication)
protected readonly app: ElectronMainApplication;
openNewWindow(url: string, { external }: NewWindowOptions): undefined {
if (!external) {
const existing = this.app.windows.find(window => window.webContents.getURL() === url);
if (existing) {
existing.focus();
return;
}
}
return super.openNewWindow(url, { external });
}
}