Skip to content

Commit

Permalink
Add container.ready event (#239)
Browse files Browse the repository at this point in the history
Fixes #236
  • Loading branch information
bingenito authored May 9, 2019
1 parent 380393c commit 82fc24d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
54 changes: 29 additions & 25 deletions examples/web/assets/js/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var container = desktopJS.resolveContainer();
var container;
var snapAssist;

var hostName = document.getElementById('hostName');
Expand Down Expand Up @@ -59,38 +59,42 @@ desktopJS.Electron.ElectronContainer.prototype.showNotification = function (titl
document.addEventListener("DOMContentLoaded", function (event) {
updatefps();

hostName.innerHTML = container.hostType + " • " + container.uuid + " • "+ desktopJS.version;
container = desktopJS.resolveContainer();

$("#button-joingroup").prop("disabled", !container.getCurrentWindow().allowGrouping);
$("#button-leavegroup").prop("disabled", !container.getCurrentWindow().allowGrouping);
container.ready().then(() => {
hostName.innerHTML = container.hostType + " • " + container.uuid + " • " + desktopJS.version;

container.addListener("window-created", (e) => container.log("info", "Window created: " + e.window + ", " + e.windowId + ", " + e.windowName));
container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded"));
container.addListener("layout-saved", (e) => container.log("info", "Layout saved"));
$("#button-joingroup").prop("disabled", !container.getCurrentWindow().allowGrouping);
$("#button-leavegroup").prop("disabled", !container.getCurrentWindow().allowGrouping);

desktopJS.Container.addListener("window-created", (e) => container.log("info", "Window created - static (Container): " + e.windowId + ", " + e.windowName));
desktopJS.ContainerWindow.addListener("window-created", (e) => container.log("info", "Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName));
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));
container.addListener("window-created", (e) => container.log("info", "Window created: " + e.window + ", " + e.windowId + ", " + e.windowName));
container.addListener("layout-loaded", (e) => container.log("info", "Layout loaded"));
container.addListener("layout-saved", (e) => container.log("info", "Layout saved"));

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)));
desktopJS.Container.addListener("window-created", (e) => container.log("info", "Window created - static (Container): " + e.windowId + ", " + e.windowName));
desktopJS.ContainerWindow.addListener("window-created", (e) => container.log("info", "Window created - static (ContainerWindow): " + e.windowId + ", " + e.windowName));
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));

subscribe();
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)));

// If url is provided a hash, try to navigate to bootstrap tab if exists
var activeTab = $('[href="' + location.hash + '"]');
activeTab && activeTab.tab('show');
subscribe();

// Enable popovers
$('[data-toggle="popover"]').popover();
// If url is provided a hash, try to navigate to bootstrap tab if exists
var activeTab = $('[href="' + location.hash + '"]');
activeTab && activeTab.tab('show');

if (container.getCurrentWindow().id === "desktopJS") {
snapAssist = new desktopJS.SnapAssistWindowManager(container,
{
windowStateTracking: desktopJS.WindowStateTracking.Main | desktopJS.WindowStateTracking.Group
});
}
// Enable popovers
$('[data-toggle="popover"]').popover();

if (container.getCurrentWindow().id === "desktopJS") {
snapAssist = new desktopJS.SnapAssistWindowManager(container,
{
windowStateTracking: desktopJS.WindowStateTracking.Main | desktopJS.WindowStateTracking.Group
});
}
});
});

openWindowButton.onclick = function () {
Expand Down
4 changes: 4 additions & 0 deletions packages/desktopjs-openfin/src/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ export class OpenFinContainer extends WebContainerBase {
});
}

public ready(): Promise<void> {
return new Promise(resolve => this.desktop.main(resolve));
}

public getMainWindow(): ContainerWindow {
if (!this.mainWindow) {
this.mainWindow = this.wrapWindow(this.desktop.Application.getCurrent().getWindow());
Expand Down
8 changes: 8 additions & 0 deletions packages/desktopjs-openfin/tests/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MockDesktop {
}
}
}
main(callback): any { callback() };
GlobalHotkey: any = { };
Window: any = MockWindow;
Notification(): any { return {}; }
Expand Down Expand Up @@ -502,6 +503,13 @@ describe("OpenFinContainer", () => {
expect(container.hostType).toEqual("OpenFin");
});

it("ready invokes underlying main", (done) => {
spyOn(desktop, "main").and.callThrough();
container.ready().then(() => {
expect(desktop.main).toHaveBeenCalled();
}).then(done);
});

describe("ctor options", () => {
describe("registerUser", () => {
let desktop;
Expand Down
4 changes: 4 additions & 0 deletions packages/desktopjs/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export abstract class Container extends EventEmitter implements ContainerWindowM
*/
public abstract uuid: string;

public ready(): Promise<void> {
return Promise.resolve();
}

public abstract getMainWindow(): ContainerWindow;

public abstract getCurrentWindow(): ContainerWindow;
Expand Down
4 changes: 4 additions & 0 deletions packages/desktopjs/tests/unit/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ describe("container", () => {
expect(container.ipc).toBeDefined();
});

it("ready resolves", (done) => {
container.ready().then(done);
});

describe("Static events", () => {
it("addListener adds callback to listeners", () => {
expect(Container.listeners("TestEvent").length).toEqual(0);
Expand Down

0 comments on commit 82fc24d

Please sign in to comment.