Skip to content

Commit

Permalink
Add minimize, maximize and restore window instance methods to Contain…
Browse files Browse the repository at this point in the history
…erWindow (#152)
  • Loading branch information
bingenito authored May 4, 2018
1 parent 631e19d commit 6229c06
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/Default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ export class DefaultContainerWindow extends ContainerWindow {
return Promise.resolve();
}

public minimize(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.minimize();
resolve();
});
}

public maximize(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.maximize();
resolve();
});
}

public restore(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.restore();
resolve();
});
}

public isShowing(): Promise<boolean> {
// https://github.com/ai/visibilityjs ?
return Promise.resolve(true);
Expand Down
21 changes: 21 additions & 0 deletions src/Electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ export class ElectronContainerWindow extends ContainerWindow {
return Promise.resolve();
}

public maximize(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.maximize();
resolve();
});
}

public minimize(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.minimize();
resolve();
});
}

public restore(): Promise<void> {
return new Promise<void>(resolve => {
this.innerWindow.restore();
resolve();
});
}

public isShowing(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
resolve(this.innerWindow.isVisible());
Expand Down
18 changes: 18 additions & 0 deletions src/OpenFin/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ export class OpenFinContainerWindow extends ContainerWindow {
});
}

public minimize(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.innerWindow.minimize(resolve, reject);
});
}

public maximize(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.innerWindow.maximize(resolve, reject);
});
}

public restore(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.innerWindow.restore(resolve, reject);
});
}

public isShowing(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
this.innerWindow.isShowing(resolve, reject);
Expand Down
17 changes: 13 additions & 4 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;

/** Minimizes the window. */
public abstract minimize(): Promise<void>;

/** Maximizes the window. */
public abstract maximize(): Promise<void>;

/** Restores the window to its previous state. */
public abstract restore(): Promise<void>;

/** Determines whether the window is currently showing. */
public abstract isShowing(): Promise<boolean>;

Expand Down Expand Up @@ -292,27 +301,27 @@ export class GroupWindowManager {
win.addListener((typeof fin !== "undefined") ? <WindowEventType> "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());
});
}
});

win.addListener((typeof fin !== "undefined") ? <WindowEventType> "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());
});
}
});
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Default/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Electron/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/OpenFin/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6229c06

Please sign in to comment.