Skip to content

Commit

Permalink
Add static grouping events to ContainerWindow (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingenito authored Feb 1, 2019
1 parent 8bafe24 commit d4ee23d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 2 additions & 0 deletions examples/electron/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function createWindow() {
let container = desktopJS.resolveContainer();

desktopJS.ContainerWindow.addListener("window-created", (e) => container.log("info", "Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName));
desktopJS.ContainerWindow.addListener("window-joinGroup", (e) => container.log("info", "grouped " + JSON.stringify(e)));
desktopJS.ContainerWindow.addListener("window-leaveGroup", (e) => container.log("info", "ungrouped" + JSON.stringify(e)));

snapAssist = new desktopJS.SnapAssistWindowManager(container,
{
Expand Down
3 changes: 3 additions & 0 deletions examples/web/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ document.addEventListener("DOMContentLoaded", function (event) {
desktopJS.Container.addListener("layout-saved", (e) => container.log("info", "Layout saved - static: " + e.layoutName));
desktopJS.Container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded - static: " + e.layoutName));

desktopJS.ContainerWindow.addListener("window-joinGroup", (e) => container.log("info", "grouped " + JSON.stringify(e)));
desktopJS.ContainerWindow.addListener("window-leaveGroup", (e) => container.log("info", "ungrouped" + JSON.stringify(e)));

subscribe();

// If url is provided a hash, try to navigate to bootstrap tab if exists
Expand Down
3 changes: 3 additions & 0 deletions packages/desktopjs-electron/src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,8 @@ export class ElectronWindowManager {
for (const win of windows) {
win.group = target.group || (target.group = Guid.newGuid());
this.registerWindowEvents(win);

ContainerWindow.emit("window-joinGroup", { name: "window-joinGroup", windowId: win.id, targetWindowId: target.id });
}

this.registerWindowEvents(target);
Expand All @@ -621,6 +623,7 @@ export class ElectronWindowManager {
for (const win of windows) {
this.unregisterWindowEvents(win);
win.group = null;
ContainerWindow.emit("window-leaveGroup", { name: "window-leaveGroup", windowId: win.id });
}

// Group all windows by group and for any group consisting of one window unhook and clear the group
Expand Down
10 changes: 8 additions & 2 deletions packages/desktopjs-openfin/src/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,19 @@ export class OpenFinContainerWindow extends ContainerWindow {
}

return new Promise<void>((resolve, reject) => {
this.innerWindow.joinGroup(target.innerWindow, resolve, reject);
this.innerWindow.joinGroup(target.innerWindow, () => {
ContainerWindow.emit("window-joinGroup", { name: "window-joinGroup", windowId: this.id, targetWindowId: target.id } );
resolve();
}, reject);
});
}

public leaveGroup(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.innerWindow.leaveGroup(resolve, reject);
this.innerWindow.leaveGroup(() => {
ContainerWindow.emit("window-leaveGroup", { name: "window-leaveGroup", windowId: this.id } );
resolve();
}, reject);
});
}

Expand Down
15 changes: 10 additions & 5 deletions packages/desktopjs/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export class Rectangle { // tslint:disable-line

export type WindowEventType =
"window-created" |
"window-joinGroup" |
"window-leaveGroup" |
"move" |
"resize" |
"close" |
Expand All @@ -64,15 +66,18 @@ export type WindowEventType =
"maximize" |
"minimize" |
"restore" |
"beforeunload"
;
"beforeunload";

export class WindowEventArgs extends EventArgs {
public readonly window?: ContainerWindow;
public readonly windowId: string;
public readonly windowName?: string;
}

export class WindowGroupEventArgs extends WindowEventArgs {
public readonly targetWindowId?: string;
}

/** Represents a container window. */
export abstract class ContainerWindow extends EventEmitter {
private static readonly staticEventScopePrefix: string = "containerwindow-";
Expand Down Expand Up @@ -201,15 +206,15 @@ export abstract class ContainerWindow extends EventEmitter {
return super.removeListener(eventName, callback);
}

public static addListener(eventName: WindowEventType, listener: (event: WindowEventArgs) => void): void { // tslint:disable-line
public static addListener(eventName: WindowEventType, listener: (event: WindowEventArgs | WindowGroupEventArgs) => void): void { // tslint:disable-line
EventEmitter.addListener(ContainerWindow.staticEventScopePrefix + eventName, listener);
}

public static removeListener(eventName: WindowEventType, listener: (event: WindowEventArgs) => void): void { // tslint:disable-line
public static removeListener(eventName: WindowEventType, listener: (event: WindowEventArgs | WindowGroupEventArgs) => void): void { // tslint:disable-line
EventEmitter.removeListener(ContainerWindow.staticEventScopePrefix + eventName, listener);
}

public static emit(eventName: WindowEventType, eventArgs: WindowEventArgs): void { // tslint:disable-line
public static emit(eventName: WindowEventType, eventArgs: WindowEventArgs | WindowGroupEventArgs): void { // tslint:disable-line
EventEmitter.emit(ContainerWindow.staticEventScopePrefix + eventName, eventArgs, Container.ipc);
}

Expand Down

0 comments on commit d4ee23d

Please sign in to comment.