From ce9445fcf53593123f1ec55437bbb8ef7bdb8353 Mon Sep 17 00:00:00 2001 From: bingenito Date: Tue, 4 Jun 2019 10:18:06 -0400 Subject: [PATCH] Fire state-changed upon setState of window Fixes #248 --- packages/desktopjs-electron/src/electron.ts | 13 ++++++++----- packages/desktopjs-openfin/src/openfin.ts | 3 +++ packages/desktopjs/src/Default/default.ts | 3 +++ packages/desktopjs/src/window.ts | 3 ++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/desktopjs-electron/src/electron.ts b/packages/desktopjs-electron/src/electron.ts index 0189e918..39226e9b 100644 --- a/packages/desktopjs-electron/src/electron.ts +++ b/packages/desktopjs-electron/src/electron.ts @@ -203,11 +203,14 @@ export class ElectronContainerWindow extends ContainerWindow { } public setState(state: any): Promise { - if (this.innerWindow && this.innerWindow.webContents) { - return this.innerWindow.webContents.executeJavaScript(`if (window.setState) { window.setState(JSON.parse(\`${JSON.stringify(state)}\`)); }`); - } else { - return Promise.resolve(); - } + const promise = (this.innerWindow && this.innerWindow.webContents) + ? this.innerWindow.webContents.executeJavaScript(`if (window.setState) { window.setState(JSON.parse(\`${JSON.stringify(state)}\`)); }`) + : Promise.resolve(); + + return promise.then(() => { + this.emit("state-changed", { name: "state-changed", sender: this }); + ContainerWindow.emit("state-changed", { name: "state-changed", windowId: this.id } ); + }); } protected attachListener(eventName: string, listener: (...args: any[]) => void): void { diff --git a/packages/desktopjs-openfin/src/openfin.ts b/packages/desktopjs-openfin/src/openfin.ts index f66b55f9..7a686517 100644 --- a/packages/desktopjs-openfin/src/openfin.ts +++ b/packages/desktopjs-openfin/src/openfin.ts @@ -187,6 +187,9 @@ export class OpenFinContainerWindow extends ContainerWindow { } resolve(); + }).then(() => { + this.emit("state-changed", { name: "state-changed", sender: this }); + ContainerWindow.emit("state-changed", { name: "state-changed", windowId: this.id } ); }); } diff --git a/packages/desktopjs/src/Default/default.ts b/packages/desktopjs/src/Default/default.ts index 29b2e937..0df845b0 100644 --- a/packages/desktopjs/src/Default/default.ts +++ b/packages/desktopjs/src/Default/default.ts @@ -125,6 +125,9 @@ export namespace Default { } resolve(); + }).then(() => { + this.emit("state-changed", { name: "state-changed", sender: this }); + ContainerWindow.emit("state-changed", { name: "state-changed", windowId: this.id } ); }); } diff --git a/packages/desktopjs/src/window.ts b/packages/desktopjs/src/window.ts index 7282f21a..04ebacb7 100644 --- a/packages/desktopjs/src/window.ts +++ b/packages/desktopjs/src/window.ts @@ -64,7 +64,8 @@ export type WindowEventType = "maximize" | "minimize" | "restore" | - "beforeunload"; + "beforeunload" | + "state-changed"; export class WindowEventArgs extends EventArgs { public readonly window?: ContainerWindow;