From 6229c0683ded3736ea08fb3f7790e0c9ee62decf Mon Sep 17 00:00:00 2001 From: Brian Ingenito Date: Fri, 4 May 2018 09:51:48 -0400 Subject: [PATCH] Add minimize, maximize and restore window instance methods to ContainerWindow (#152) --- src/Default/default.ts | 21 ++++++++++++++++ src/Electron/electron.ts | 21 ++++++++++++++++ src/OpenFin/openfin.ts | 18 ++++++++++++++ src/window.ts | 17 +++++++++---- tests/unit/Default/default.spec.ts | 21 ++++++++++++++++ tests/unit/Electron/electron.spec.ts | 21 ++++++++++++++++ tests/unit/OpenFin/openfin.spec.ts | 36 ++++++++++++++++++++++++++++ 7 files changed, 151 insertions(+), 4 deletions(-) diff --git a/src/Default/default.ts b/src/Default/default.ts index 38a21968..ca73c68a 100644 --- a/src/Default/default.ts +++ b/src/Default/default.ts @@ -46,6 +46,27 @@ export class DefaultContainerWindow extends ContainerWindow { return Promise.resolve(); } + public minimize(): Promise { + return new Promise(resolve => { + this.innerWindow.minimize(); + resolve(); + }); + } + + public maximize(): Promise { + return new Promise(resolve => { + this.innerWindow.maximize(); + resolve(); + }); + } + + public restore(): Promise { + return new Promise(resolve => { + this.innerWindow.restore(); + resolve(); + }); + } + public isShowing(): Promise { // https://github.com/ai/visibilityjs ? return Promise.resolve(true); diff --git a/src/Electron/electron.ts b/src/Electron/electron.ts index d1233fe9..dea38d96 100644 --- a/src/Electron/electron.ts +++ b/src/Electron/electron.ts @@ -72,6 +72,27 @@ export class ElectronContainerWindow extends ContainerWindow { return Promise.resolve(); } + public maximize(): Promise { + return new Promise(resolve => { + this.innerWindow.maximize(); + resolve(); + }); + } + + public minimize(): Promise { + return new Promise(resolve => { + this.innerWindow.minimize(); + resolve(); + }); + } + + public restore(): Promise { + return new Promise(resolve => { + this.innerWindow.restore(); + resolve(); + }); + } + public isShowing(): Promise { return new Promise((resolve, reject) => { resolve(this.innerWindow.isVisible()); diff --git a/src/OpenFin/openfin.ts b/src/OpenFin/openfin.ts index 9341e022..c73ef057 100644 --- a/src/OpenFin/openfin.ts +++ b/src/OpenFin/openfin.ts @@ -65,6 +65,24 @@ export class OpenFinContainerWindow extends ContainerWindow { }); } + public minimize(): Promise { + return new Promise((resolve, reject) => { + this.innerWindow.minimize(resolve, reject); + }); + } + + public maximize(): Promise { + return new Promise((resolve, reject) => { + this.innerWindow.maximize(resolve, reject); + }); + } + + public restore(): Promise { + return new Promise((resolve, reject) => { + this.innerWindow.restore(resolve, reject); + }); + } + public isShowing(): Promise { return new Promise((resolve, reject) => { this.innerWindow.isShowing(resolve, reject); diff --git a/src/window.ts b/src/window.ts index 66986db5..f9f4a61a 100644 --- a/src/window.ts +++ b/src/window.ts @@ -97,6 +97,15 @@ export abstract class ContainerWindow extends EventEmitter { /** Try to close the window. This has the same effect as clicking the close button on the window. */ public abstract close(): Promise; + /** Minimizes the window. */ + public abstract minimize(): Promise; + + /** Maximizes the window. */ + public abstract maximize(): Promise; + + /** Restores the window to its previous state. */ + public abstract restore(): Promise; + /** Determines whether the window is currently showing. */ public abstract isShowing(): Promise; @@ -292,13 +301,13 @@ export class GroupWindowManager { win.addListener((typeof fin !== "undefined") ? "minimized" : "minimize", (e) => { if ((this.windowStateTracking & WindowStateTracking.Main) && this.container.getMainWindow().id === e.sender.id) { this.container.getAllWindows().then(windows => { - windows.forEach(window => window.innerWindow.minimize()); + windows.forEach(window => window.minimize()); }); } if (this.windowStateTracking & WindowStateTracking.Group) { e.sender.getGroup().then(windows => { - windows.forEach(window => window.innerWindow.minimize()); + windows.forEach(window => window.minimize()); }); } }); @@ -306,13 +315,13 @@ export class GroupWindowManager { win.addListener((typeof fin !== "undefined") ? "restored" : "restore", (e) => { if ((this.windowStateTracking & WindowStateTracking.Main) && this.container.getMainWindow().id === e.sender.id) { this.container.getAllWindows().then(windows => { - windows.forEach(window => window.innerWindow.restore()); + windows.forEach(window => window.restore()); }); } if (this.windowStateTracking & WindowStateTracking.Group) { e.sender.getGroup().then(windows => { - windows.forEach(window => window.innerWindow.restore()); + windows.forEach(window => window.restore()); }); } }); diff --git a/tests/unit/Default/default.spec.ts b/tests/unit/Default/default.spec.ts index 6a04d18b..6f4d2099 100644 --- a/tests/unit/Default/default.spec.ts +++ b/tests/unit/Default/default.spec.ts @@ -78,6 +78,27 @@ describe("DefaultContainerWindow", () => { }).then(done); }); + it("minimize", (done) => { + const innerWindow = jasmine.createSpyObj("BrowserWindow", ["minimize"]); + new DefaultContainerWindow(innerWindow).minimize().then(() => { + expect(innerWindow.minimize).toHaveBeenCalledTimes(1); + }).then(done); + }); + + it("maximize", (done) => { + const innerWindow = jasmine.createSpyObj("BrowserWindow", ["maximize"]); + new DefaultContainerWindow(innerWindow).maximize().then(() => { + expect(innerWindow.maximize).toHaveBeenCalledTimes(1); + }).then(done); + }); + + it("restore", (done) => { + const innerWindow = jasmine.createSpyObj("BrowserWindow", ["restore"]); + new DefaultContainerWindow(innerWindow).restore().then(() => { + expect(innerWindow.restore).toHaveBeenCalledTimes(1); + }).then(done); + }); + it("getBounds retrieves underlying window position", (done) => { win.getBounds().then(bounds => { expect(bounds).toBeDefined(); diff --git a/tests/unit/Electron/electron.spec.ts b/tests/unit/Electron/electron.spec.ts index 8f3e4e4a..da117355 100644 --- a/tests/unit/Electron/electron.spec.ts +++ b/tests/unit/Electron/electron.spec.ts @@ -139,6 +139,27 @@ describe("ElectronContainerWindow", () => { }).then(done); }); + it("minimize", (done) => { + const browserWindow = jasmine.createSpyObj("BrowserWindow", ["minimize"]); + new ElectronContainerWindow(browserWindow, null).minimize().then(() => { + expect(browserWindow.minimize).toHaveBeenCalledTimes(1); + }).then(done); + }); + + it("maximize", (done) => { + const browserWindow = jasmine.createSpyObj("BrowserWindow", ["maximize"]); + new ElectronContainerWindow(browserWindow, null).maximize().then(() => { + expect(browserWindow.maximize).toHaveBeenCalledTimes(1); + }).then(done); + }); + + it("restore", (done) => { + const browserWindow = jasmine.createSpyObj("BrowserWindow", ["restore"]); + new ElectronContainerWindow(browserWindow, null).restore().then(() => { + expect(browserWindow.restore).toHaveBeenCalledTimes(1); + }).then(done); + }); + it("isShowing", (done) => { spyOn(innerWin, "isVisible").and.callThrough(); let success: boolean = false; diff --git a/tests/unit/OpenFin/openfin.spec.ts b/tests/unit/OpenFin/openfin.spec.ts index a4560fea..9a2d5bec 100644 --- a/tests/unit/OpenFin/openfin.spec.ts +++ b/tests/unit/OpenFin/openfin.spec.ts @@ -78,6 +78,21 @@ class MockWindow { return {}; } + minimize(callback: () => void, error: (reason) => void): any { + callback(); + return {}; + } + + maximize(callback: () => void, error: (reason) => void): any { + callback(); + return {}; + } + + restore(callback: () => void, error: (reason) => void): any { + callback(); + return {}; + } + isShowing(callback: (showing: boolean) => void, error: (reason) => void): any { callback(true); return {}; @@ -176,6 +191,27 @@ describe("OpenFinContainerWindow", () => { }).then(done); }); + it("minimize", (done) => { + spyOn(innerWin, "minimize").and.callThrough(); + win.minimize().then(() => { + expect(innerWin.minimize).toHaveBeenCalled(); + }).then(done); + }); + + it("maximize", (done) => { + spyOn(innerWin, "maximize").and.callThrough(); + win.maximize().then(() => { + expect(innerWin.maximize).toHaveBeenCalled(); + }).then(done); + }); + + it("restore", (done) => { + spyOn(innerWin, "restore").and.callThrough(); + win.restore().then(() => { + expect(innerWin.restore).toHaveBeenCalled(); + }).then(done); + }); + it("isShowing", (done) => { spyOn(innerWin, "isShowing").and.callThrough(); let success: boolean = false;