Skip to content

Commit

Permalink
Propagate Electron and OpenFin application level window created event…
Browse files Browse the repository at this point in the history
…s instead of emitting our own (#156)

Fixes #155
  • Loading branch information
bingenito authored May 15, 2018
1 parent 3552924 commit 96f9998
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/Electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down
12 changes: 7 additions & 5 deletions src/OpenFin/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -408,11 +414,7 @@ export class OpenFinContainer extends WebContainerBase {

return new Promise<ContainerWindow>((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);
});
}
Expand Down
7 changes: 1 addition & 6 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/Electron/electron.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string, any> = new Map();
Expand Down Expand Up @@ -322,7 +323,7 @@ describe("ElectronContainer", () => {
windows = [new MockWindow(), new MockWindow("Name")];

electron = {
app: {},
app: new MockEventEmitter(),
BrowserWindow: (options: any) => {
return {
loadURL: (url: string) => { },
Expand Down Expand Up @@ -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) => {
(<any>container).isRemote = false;
container.windowManager = new ElectronWindowManager({}, new MockMainIpc(), { fromId(): any {}, getAllWindows(): any {} })
Expand Down
22 changes: 17 additions & 5 deletions tests/unit/OpenFin/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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 });
Expand Down

0 comments on commit 96f9998

Please sign in to comment.