diff --git a/src/Electron/electron.ts b/src/Electron/electron.ts index 22ce1968..51a3093c 100644 --- a/src/Electron/electron.ts +++ b/src/Electron/electron.ts @@ -279,8 +279,13 @@ export class ElectronContainer extends WebContainerBase { this.internalIpc = ipc || ((this.isRemote) ? require("electron").ipcRenderer : this.electron.ipcMain); this.ipc = new ElectronMessageBus(this.internalIpc, this.browserWindow); - if (!this.isRemote) { + if (!this.isRemote || (options && typeof options.isRemote !== "undefined" && !options.isRemote)) { this.windowManager = new ElectronWindowManager(this.app, this.internalIpc, this.browserWindow); + + this.app.on("browser-window-created", (event, window) => { + Container.emit("window-created", { name: "window-created", windowId: window.webContents.id }); + ContainerWindow.emit("window-created", { name: "window-created", windowId: window.webContents.id }); + }); } } catch (e) { console.error(e); @@ -362,8 +367,6 @@ export class ElectronContainer extends WebContainerBase { const newWindow = this.wrapWindow(electronWindow); this.emit("window-created", { sender: this, name: "window-created", window: newWindow, windowId: electronWindow.id, windowName: windowName }); - Container.emit("window-created", { name: "window-created", windowId: electronWindow.id, windowName: windowName }); - ContainerWindow.emit("window-created", { name: "window-created", windowId: electronWindow.id, windowName: windowName }); return Promise.resolve(newWindow); } diff --git a/src/OpenFin/openfin.ts b/src/OpenFin/openfin.ts index 880b431d..0c2e3f80 100644 --- a/src/OpenFin/openfin.ts +++ b/src/OpenFin/openfin.ts @@ -337,6 +337,12 @@ export class OpenFinContainer extends WebContainerBase { this.registerNotificationsApi(); } + this.desktop.Application.getCurrent().addEventListener("window-created", (event: any) => { + this.emit("window-created", { sender: this, name: "window-created", windowId: event.name, windowName: event.name }); + Container.emit("window-created", { name: "window-created", windowId: event.name, windowName: event.name }); + ContainerWindow.emit("window-created", { name: "window-created", windowId: event.name, windowName: event.name }); + }); + this.screen = new OpenFinDisplayManager(this.desktop); } @@ -408,11 +414,7 @@ export class OpenFinContainer extends WebContainerBase { return new Promise((resolve, reject) => { const ofWin = new this.desktop.Window(newOptions, win => { - const newWin = this.wrapWindow(ofWin); - this.emit("window-created", { sender: this, name: "window-created", window: newWin, windowId: newOptions.name, windowName: newOptions.name }); - Container.emit("window-created", { name: "window-created", windowId: newOptions.name, windowName: newOptions.name }); - ContainerWindow.emit("window-created", { name: "window-created", windowId: newOptions.name, windowName: newOptions.name }); - resolve(newWin); + resolve(this.wrapWindow(ofWin)); }, reject); }); } diff --git a/src/container.ts b/src/container.ts index 2abe3c7d..79906587 100644 --- a/src/container.ts +++ b/src/container.ts @@ -267,12 +267,7 @@ export abstract class WebContainerBase extends ContainerBase { } protected onOpen(open: (...args: any[]) => Window, ...args: any[]): Window { - const wrap = this.wrapWindow(open.apply(this.globalWindow, args)); - - Container.emit("window-created", { name: "window-created", windowId: wrap.id }); - ContainerWindow.emit("window-created", { name: "window-created", windowId: wrap.id }); - - return wrap.innerWindow; + return open.apply(this.globalWindow, args); } public abstract wrapWindow(window: any): ContainerWindow; diff --git a/tests/unit/Electron/electron.spec.ts b/tests/unit/Electron/electron.spec.ts index da117355..ba1f4451 100644 --- a/tests/unit/Electron/electron.spec.ts +++ b/tests/unit/Electron/electron.spec.ts @@ -1,5 +1,6 @@ import { ElectronContainer, ElectronContainerWindow, ElectronMessageBus, ElectronWindowManager } from "../../../src/Electron/electron"; import { MessageBusSubscription } from "../../../src/ipc"; +import { ContainerWindow } from "../../../src/window"; class MockEventEmitter { private eventListeners: Map = new Map(); @@ -322,7 +323,7 @@ describe("ElectronContainer", () => { windows = [new MockWindow(), new MockWindow("Name")]; electron = { - app: {}, + app: new MockEventEmitter(), BrowserWindow: (options: any) => { return { loadURL: (url: string) => { }, @@ -418,6 +419,12 @@ describe("ElectronContainer", () => { container.createWindow("url"); }); + it ("app browser-window-created fires Container window-created", (done) => { + new ElectronContainer(electron, new MockMainIpc(), globalWindow, { isRemote: false }); + ContainerWindow.addListener("window-created", () => done()); + electron.app.emit("browser-window-created", {}, { webContents: {id: "id"}}); + }); + it("createWindow on main process invokes ElectronWindowManager.initializeWindow", (done) => { (container).isRemote = false; container.windowManager = new ElectronWindowManager({}, new MockMainIpc(), { fromId(): any {}, getAllWindows(): any {} }) diff --git a/tests/unit/OpenFin/openfin.spec.ts b/tests/unit/OpenFin/openfin.spec.ts index 9a2d5bec..a9bc6180 100644 --- a/tests/unit/OpenFin/openfin.spec.ts +++ b/tests/unit/OpenFin/openfin.spec.ts @@ -4,10 +4,22 @@ import { MenuItem } from "../../../src/menu"; class MockDesktop { public static application: any = { + eventListeners: new Map(), uuid: "uuid", getChildWindows(callback) { callback([MockWindow.singleton]); }, setTrayIcon() { }, - getWindow() { return MockWindow.singleton; } + getWindow() { return MockWindow.singleton; }, + addEventListener(eventName, listener) { + (this.eventListeners[eventName] = this.eventListeners[eventName] || []).push(listener); + }, + listeners(eventName: string): ((event: any) => void)[] { + return (this.eventListeners[eventName] || []); + }, + emit(eventName: string, ...eventArgs: any[]) { + for (const listener of this.listeners(eventName)) { + listener(...eventArgs); + } + } } Window: any = MockWindow; @@ -379,7 +391,7 @@ describe("OpenFinContainer", () => { beforeEach(() => { desktop = jasmine.createSpyObj("desktop", ["Application"]); - app = jasmine.createSpyObj("application", ["getCurrent", "registerUser"]); + app = jasmine.createSpyObj("application", ["getCurrent", "registerUser", "addEventListener"]); Object.defineProperty(desktop, "Application", { value: app }); app.getCurrent.and.returnValue(app); }); @@ -467,9 +479,9 @@ describe("OpenFinContainer", () => { }); }); - it("createWindow fires window-created", (done) => { + it("application window-created fires container window-created", (done) => { container.addListener("window-created", () => done()); - container.createWindow("url"); + MockDesktop.application.emit('window-created', { name: "name" }); }); describe("window management", () => { @@ -652,7 +664,7 @@ describe("OpenFinDisplayManager", () => { beforeEach(() => { desktop = jasmine.createSpyObj("desktop", ["Application", "System"]); - app = jasmine.createSpyObj("application", ["getCurrent"]); + app = jasmine.createSpyObj("application", ["getCurrent", "addEventListener"]); system = jasmine.createSpyObj("system", ["getMonitorInfo"]); Object.defineProperty(desktop, "Application", { value: app }); Object.defineProperty(desktop, "System", { value: system });