Skip to content

Commit 23877f1

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
ATL-879: Avoid reopening the same sketch.
Instead of reopening it, focus the existing window. Signed-off-by: Akos Kitta <[email protected]>
1 parent 96f0722 commit 23877f1

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Diff for: arduino-ide-extension/src/electron-main/arduino-electron-main-module.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import { ContainerModule } from 'inversify';
22
import { JsonRpcConnectionHandler } from '@theia/core/lib/common/messaging/proxy-factory';
33
import { ElectronConnectionHandler } from '@theia/core/lib/electron-common/messaging/electron-connection-handler';
4+
import { ElectronMainWindowService } from '@theia/core/lib/electron-common/electron-main-window-service';
45
import { ElectronMainApplication as TheiaElectronMainApplication } from '@theia/core/lib/electron-main/electron-main-application';
56
import { SplashService, splashServicePath } from '../electron-common/splash-service';
67
import { SplashServiceImpl } from './splash/splash-service-impl';
78
import { ElectronMainApplication } from './theia/electron-main-application';
9+
import { ElectronMainWindowServiceImpl } from './theia/electron-main-window-service';
810

911
export default new ContainerModule((bind, unbind, isBound, rebind) => {
1012
bind(ElectronMainApplication).toSelf().inSingletonScope();
1113
rebind(TheiaElectronMainApplication).toService(ElectronMainApplication);
1214

15+
bind(ElectronMainWindowServiceImpl).toSelf().inSingletonScope();
16+
rebind(ElectronMainWindowService).toService(ElectronMainWindowServiceImpl);
17+
1318
bind(SplashServiceImpl).toSelf().inSingletonScope();
1419
bind(SplashService).toService(SplashServiceImpl);
1520
bind(ElectronConnectionHandler).toDynamicValue(context =>

Diff for: arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { SplashServiceImpl } from '../splash/splash-service-impl';
1313
@injectable()
1414
export class ElectronMainApplication extends TheiaElectronMainApplication {
1515

16-
protected windows: BrowserWindow[] = [];
16+
protected _windows: BrowserWindow[] = [];
1717

1818
@inject(SplashServiceImpl)
1919
protected readonly splashService: SplashServiceImpl;
@@ -34,7 +34,7 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
3434
async createWindow(asyncOptions: MaybePromise<TheiaBrowserWindowOptions> = this.getDefaultBrowserWindowOptions()): Promise<BrowserWindow> {
3535
const options = await asyncOptions;
3636
let electronWindow: BrowserWindow | undefined;
37-
if (this.windows.length) {
37+
if (this._windows.length) {
3838
electronWindow = new BrowserWindow(options);
3939
} else {
4040
const { bounds } = screen.getDisplayNearestPoint(screen.getCursorScreenPoint());
@@ -63,14 +63,14 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
6363
splashScreenOpts
6464
}, this.splashService.onCloseRequested);
6565
}
66-
this.windows.push(electronWindow);
66+
this._windows.push(electronWindow);
6767
electronWindow.on('closed', () => {
6868
if (electronWindow) {
69-
const index = this.windows.indexOf(electronWindow);
69+
const index = this._windows.indexOf(electronWindow);
7070
if (index === -1) {
7171
console.warn(`Could not dispose browser window: '${electronWindow.title}'.`);
7272
} else {
73-
this.windows.splice(index, 1);
73+
this._windows.splice(index, 1);
7474
electronWindow = undefined;
7575
}
7676
}
@@ -148,4 +148,8 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
148148
}
149149
}
150150

151+
get windows(): BrowserWindow[] {
152+
return this._windows.slice();
153+
}
154+
151155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { inject, injectable } from 'inversify';
2+
import { NewWindowOptions } from '@theia/core/lib/browser/window/window-service';
3+
import { ElectronMainWindowServiceImpl as TheiaElectronMainWindowService } from '@theia/core/lib/electron-main/electron-main-window-service-impl';
4+
import { ElectronMainApplication } from './electron-main-application';
5+
6+
@injectable()
7+
export class ElectronMainWindowServiceImpl extends TheiaElectronMainWindowService {
8+
9+
@inject(ElectronMainApplication)
10+
protected readonly app: ElectronMainApplication;
11+
12+
openNewWindow(url: string, { external }: NewWindowOptions): undefined {
13+
if (!external) {
14+
const existing = this.app.windows.find(window => window.webContents.getURL() === url);
15+
if (existing) {
16+
existing.focus();
17+
return;
18+
}
19+
}
20+
return super.openNewWindow(url, { external });
21+
}
22+
23+
24+
}

0 commit comments

Comments
 (0)