Skip to content

Commit 52ab00e

Browse files
committed
Got rid of _windows field, rely on superclass.
Signed-off-by: Akos Kitta <[email protected]>
1 parent 3ece882 commit 52ab00e

File tree

1 file changed

+78
-93
lines changed

1 file changed

+78
-93
lines changed

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

+78-93
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
} from '@theia/core/lib/electron-main/electron-main-application';
1515
import { SplashServiceImpl } from '../splash/splash-service-impl';
1616
import { URI } from '@theia/core/shared/vscode-uri';
17-
import * as electronRemoteMain from '@theia/core/electron-shared/@electron/remote/main';
1817
import { Deferred } from '@theia/core/lib/common/promise-util';
1918
import * as os from '@theia/core/lib/common/os';
2019
import { Restart } from '@theia/core/lib/electron-common/messaging/electron-messages';
@@ -37,7 +36,6 @@ const WORKSPACES = 'workspaces';
3736

3837
@injectable()
3938
export class ElectronMainApplication extends TheiaElectronMainApplication {
40-
protected _windows: BrowserWindow[] = [];
4139
protected startup = false;
4240
protected openFilePromise = new Deferred();
4341

@@ -176,102 +174,89 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
176174
*
177175
* @param options
178176
*/
179-
async createWindow(
177+
async createWindow(
180178
asyncOptions: MaybePromise<TheiaBrowserWindowOptions> = this.getDefaultTheiaWindowOptions()
181179
): Promise<BrowserWindow> {
182-
let options = await asyncOptions;
183-
options = this.avoidOverlap(options);
184-
let electronWindow: BrowserWindow | undefined;
185-
if (this._windows.length) {
186-
electronWindow = await super.createWindow(options);
187-
} else {
188-
const { bounds } = screen.getDisplayNearestPoint(
189-
screen.getCursorScreenPoint()
190-
);
191-
const splashHeight = 450;
192-
const splashWidth = 600;
193-
const splashY = Math.floor(bounds.y + (bounds.height - splashHeight) / 2);
194-
const splashX = Math.floor(bounds.x + (bounds.width - splashWidth) / 2);
195-
const splashScreenOpts: BrowserWindowConstructorOptions = {
196-
height: splashHeight,
197-
width: splashWidth,
198-
x: splashX,
199-
y: splashY,
200-
transparent: true,
201-
alwaysOnTop: true,
202-
focusable: false,
203-
minimizable: false,
204-
maximizable: false,
205-
hasShadow: false,
206-
resizable: false,
207-
};
208-
electronWindow = initSplashScreen(
209-
{
210-
windowOpts: options,
211-
templateUrl: join(
212-
__dirname,
213-
'..',
214-
'..',
215-
'..',
216-
'src',
217-
'electron-main',
218-
'splash',
219-
'static',
220-
'splash.html'
221-
),
222-
delay: 0,
223-
minVisible: 2000,
224-
splashScreenOpts,
225-
},
226-
this.splashService.onCloseRequested
180+
const createRegularWindow = !this.windows.size;
181+
const electronWindow = await (createRegularWindow
182+
? super.createWindow(asyncOptions)
183+
: this.createSplashScreenWindow(asyncOptions));
184+
if (createRegularWindow) {
185+
electronWindow.webContents.on(
186+
'new-window',
187+
(event, url, frameName, disposition, options) => {
188+
if (frameName === 'serialPlotter') {
189+
event.preventDefault();
190+
Object.assign(options, {
191+
width: 800,
192+
minWidth: 620,
193+
height: 500,
194+
minHeight: 320,
195+
x: 100,
196+
y: 100,
197+
webPreferences: {
198+
devTools: true,
199+
nativeWindowOpen: true,
200+
openerId: electronWindow?.webContents.id,
201+
},
202+
});
203+
event.newGuest = new BrowserWindow(options);
204+
event.newGuest.setMenu(null);
205+
event.newGuest?.on('closed', (e: any) => {
206+
electronWindow?.webContents.send('CLOSE_CHILD_WINDOW');
207+
});
208+
event.newGuest?.loadURL(url);
209+
}
210+
}
227211
);
228212
}
213+
return electronWindow;
214+
}
229215

230-
electronWindow.webContents.on(
231-
'new-window',
232-
(event, url, frameName, disposition, options) => {
233-
if (frameName === 'serialPlotter') {
234-
event.preventDefault();
235-
Object.assign(options, {
236-
width: 800,
237-
minWidth: 620,
238-
height: 500,
239-
minHeight: 320,
240-
x: 100,
241-
y: 100,
242-
webPreferences: {
243-
devTools: true,
244-
nativeWindowOpen: true,
245-
openerId: electronWindow?.webContents.id,
246-
},
247-
});
248-
event.newGuest = new BrowserWindow(options);
249-
event.newGuest.setMenu(null);
250-
event.newGuest?.on('closed', (e: any) => {
251-
electronWindow?.webContents.send('CLOSE_CHILD_WINDOW');
252-
});
253-
event.newGuest?.loadURL(url);
254-
}
255-
}
216+
private async createSplashScreenWindow(
217+
asyncOptions: MaybePromise<TheiaBrowserWindowOptions>
218+
): Promise<BrowserWindow> {
219+
const options = await asyncOptions;
220+
const { bounds } = screen.getDisplayNearestPoint(
221+
screen.getCursorScreenPoint()
222+
);
223+
const splashHeight = 450;
224+
const splashWidth = 600;
225+
const splashY = Math.floor(bounds.y + (bounds.height - splashHeight) / 2);
226+
const splashX = Math.floor(bounds.x + (bounds.width - splashWidth) / 2);
227+
const splashScreenOpts: BrowserWindowConstructorOptions = {
228+
height: splashHeight,
229+
width: splashWidth,
230+
x: splashX,
231+
y: splashY,
232+
transparent: true,
233+
alwaysOnTop: true,
234+
focusable: false,
235+
minimizable: false,
236+
maximizable: false,
237+
hasShadow: false,
238+
resizable: false,
239+
};
240+
return initSplashScreen(
241+
{
242+
windowOpts: options,
243+
templateUrl: join(
244+
__dirname,
245+
'..',
246+
'..',
247+
'..',
248+
'src',
249+
'electron-main',
250+
'splash',
251+
'static',
252+
'splash.html'
253+
),
254+
delay: 0,
255+
minVisible: 2000,
256+
splashScreenOpts,
257+
},
258+
this.splashService.onCloseRequested
256259
);
257-
258-
this._windows.push(electronWindow);
259-
electronWindow.on('closed', () => {
260-
if (electronWindow) {
261-
const index = this._windows.indexOf(electronWindow);
262-
if (index === -1) {
263-
console.warn(
264-
`Could not dispose browser window: '${electronWindow.title}'.`
265-
);
266-
} else {
267-
this._windows.splice(index, 1);
268-
electronWindow = undefined;
269-
}
270-
}
271-
});
272-
this.attachClosedWorkspace(electronWindow);
273-
electronRemoteMain.enable(electronWindow.webContents);
274-
return electronWindow;
275260
}
276261

277262
protected async startBackend(): Promise<number> {
@@ -376,6 +361,6 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
376361
}
377362

378363
get browserWindows(): BrowserWindow[] {
379-
return this._windows;
364+
return Array.from(this.windows.values()).map(({ window }) => window);
380365
}
381366
}

0 commit comments

Comments
 (0)