Skip to content

Commit

Permalink
Save and restore window options in persisted window layouts (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingenito authored Jul 12, 2018
1 parent 2bffa55 commit 0eff39e
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class DefaultContainer extends WebContainerBase {
}

const window = this.globalWindow.open(url, target, features);
window[Container.windowOptionsPropertyKey] = options; // Store the original options

// Set name as a unique desktopJS name property instead of overloading window.name
window[DefaultContainer.windowNamePropertyKey] = newOptions.name;
Expand Down Expand Up @@ -342,7 +343,7 @@ export class DefaultContainer extends WebContainerBase {
for (const key in windows) {
const win = windows[key];
if (this.globalWindow !== win) {
layout.windows.push({ name: win.name, url: win.location.toString(), bounds: { x: win.screenX, y: win.screenY, width: win.outerWidth, height: win.outerHeight } });
layout.windows.push({ name: win.name, url: win.location.toString(), bounds: { x: win.screenX, y: win.screenY, width: win.outerWidth, height: win.outerHeight }, options: win[Container.windowOptionsPropertyKey] });
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class ElectronContainer extends WebContainerBase {

public getMainWindow(): ContainerWindow {
for (const window of this.browserWindow.getAllWindows()) {
if (window.options && window.options.main) {
if (window[Container.windowOptionsPropertyKey] && window[Container.windowOptionsPropertyKey].main) {
return this.wrapWindow(window);
}
}
Expand Down Expand Up @@ -359,6 +359,7 @@ export class ElectronContainer extends WebContainerBase {
*/
if (this.isRemote) {
electronWindow["name"] = (<any>this.internalIpc).sendSync(InternalMessageType.initialize, { id: electronWindow.id, name: windowName, options: newOptions });
electronWindow[Container.windowOptionsPropertyKey] = options;
} else {
this.windowManager.initializeWindow(electronWindow, windowName, newOptions);
}
Expand Down Expand Up @@ -444,6 +445,7 @@ export class ElectronContainer extends WebContainerBase {
name: window.name,
url: window.innerWindow.webContents.getURL(),
main: (mainWindow === window.innerWindow),
options: window.innerWindow[Container.windowOptionsPropertyKey],
bounds: window.innerWindow.getBounds(),
group: group.map(win => win.id)
});
Expand Down Expand Up @@ -502,7 +504,7 @@ export class ElectronWindowManager {

public initializeWindow(win: any, name: string, options: any) {
win.name = name;
win.options = options;
win[Container.windowOptionsPropertyKey] = options;

if (options && options.main && (!("quitOnClose" in options) || options.quitOnClose)) {
win.on("closed", () => {
Expand Down
2 changes: 2 additions & 0 deletions src/OpenFin/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,15 @@ export class OpenFinContainer extends WebContainerBase {
promises.push(new Promise<void>((innerResolve, innerReject) => {
window.getBounds(bounds => {
window.getOptions(options => {
delete (<any>options).show; // show is an undocumented option that interferes with the createWindow mapping of show -> autoShow
window.getGroup(group => {
layout.windows.push(
{
name: window.name,
id: window.name,
url: window.getNativeWindow() ? window.getNativeWindow().location.toString() : options.url,
main: (mainWindow.name === window.name),
options: options,
bounds: { x: bounds.left, y: bounds.top, width: bounds.width, height: bounds.height },
group: group.map(win => win.name)
});
Expand Down
4 changes: 3 additions & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export abstract class Container extends EventEmitter implements ContainerWindowM

private static _ipc: MessageBus; // tslint:disable-line

public static readonly windowOptionsPropertyKey: string = "desktopJS-options";

/**
* Display type of current Container.
* @type {string}
Expand Down Expand Up @@ -176,7 +178,7 @@ export abstract class ContainerBase extends Container {
if (layout && layout.windows) {
const promises: Promise<ContainerWindow>[] = [];
for (const window of layout.windows) {
const options: any = Object.assign({}, window.bounds);
const options: any = Object.assign(window.options || {}, window.bounds);
options.name = window.name;
if (window.main) {
this.getMainWindow().setBounds(window.bounds);
Expand Down
1 change: 1 addition & 0 deletions src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export class PersistedWindow {
public name: string;
public id: string;
public bounds: any;
public options?: any;
public url?: string;
public main?: boolean;
public group?: string[];
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/Electron/electron.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ElectronContainer, ElectronContainerWindow, ElectronMessageBus, ElectronWindowManager } from "../../../src/Electron/electron";
import { MessageBusSubscription } from "../../../src/ipc";
import { ContainerWindow } from "../../../src/window";
import { Container } from "../../../src/container";

class MockEventEmitter {
private eventListeners: Map<string, any> = new Map();
Expand Down Expand Up @@ -377,7 +378,7 @@ describe("ElectronContainer", () => {
fromId(): any { };
};

windows[1]["options"] = { main: true };
windows[1][Container.windowOptionsPropertyKey] = { main: true };

spyOn(container.browserWindow, "fromId").and.returnValue(innerWin);

Expand Down

0 comments on commit 0eff39e

Please sign in to comment.