Skip to content

Commit

Permalink
Add window.bringToFront() for raising z-order of a window without act…
Browse files Browse the repository at this point in the history
…ivation. Default implementation will fallback to invoking focus() for containers in which it is not supported. (#210)
  • Loading branch information
bingenito authored Jan 3, 2019
1 parent bf2d896 commit 8bafe24
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/desktopjs-electron/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export class ElectronContainerWindow extends ContainerWindow {
return Promise.resolve();
}

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

public getOptions(): Promise<any> {
return new Promise<any>((resolve, reject) => {
const options = (this.isRemote)
Expand Down
11 changes: 10 additions & 1 deletion packages/desktopjs-electron/tests/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class MockWindow extends MockEventEmitter {
this.emit("move", null);
}

public flashFrame(enable: boolean): void { }
public flashFrame(enable: boolean): void { }

public moveTop(): void { }
}

class MockCapture {
Expand Down Expand Up @@ -408,6 +410,13 @@ describe("ElectronContainerWindow", () => {
expect((<any>container).windowManager.ungroupWindows).toHaveBeenCalled();
});
});

it ("bringToFront invokes underlying moveTop", (done) => {
spyOn(win.innerWindow, "moveTop").and.callThrough()
win.bringToFront().then(() => {
expect(innerWin.moveTop).toHaveBeenCalled();
}).then(done);
});
});

describe("ElectronContainer", () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/desktopjs-openfin/src/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ export class OpenFinContainerWindow extends ContainerWindow {
});
}

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

public getOptions(): Promise<any> {
return new Promise<any>((resolve, reject) => {
this.innerWindow.getOptions(options => resolve(options.customData ? JSON.parse(options.customData) : undefined), reject);
Expand Down
11 changes: 11 additions & 0 deletions packages/desktopjs-openfin/tests/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ class MockWindow {
joinGroup(target: any, callback: any, errorCallback: any) { }
leaveGroup(callback: any, errorCallback: any) { }

bringToFront(callback:any, errorCallback: any) {
callback();
}

addEventListener(eventName: string, listener: any): void { }

removeEventListener(eventName: string, listener: any): void { }
Expand Down Expand Up @@ -474,6 +478,13 @@ describe("OpenFinContainerWindow", () => {
}).then(done);
});
});

it("bringToFront invokes underlying bringToFront", (done) => {
spyOn(innerWin, "bringToFront").and.callThrough();
win.bringToFront().then(() => {
expect(innerWin.bringToFront).toHaveBeenCalled();
}).then(done);
});
});

describe("OpenFinContainer", () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/desktopjs/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ export abstract class ContainerWindow extends EventEmitter {
return Promise.resolve();
}

public bringToFront(): Promise<void> {
// Provide simple delegation to focus/activate by default
return this.focus();
}

public abstract getOptions(): Promise<any>;

/** Retrieves custom window state from underlying native window by invoking 'window.getState()' if defined. */
Expand Down
9 changes: 9 additions & 0 deletions packages/desktopjs/tests/unit/window.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MockWindow extends ContainerWindow {
return;
}

public focus(): Promise<void> { return Promise.resolve(); }
public minimize(): Promise<void> { return Promise.resolve(); }
public restore(): Promise<void> { return Promise.resolve(); }
}
Expand All @@ -29,6 +30,14 @@ describe ("ContainerWindow", () => {
expect(true);
}).then(done);
});

it("bringToFront invokes focus by default", (done) => {
const win = new MockWindow(undefined)
spyOn(win, "focus").and.callThrough();
win.bringToFront().then(() => {
expect(win.focus).toHaveBeenCalled();
}).then(done);
});
});

describe ("static events", () => {
Expand Down

0 comments on commit 8bafe24

Please sign in to comment.