Skip to content

Commit

Permalink
Remove deprecated apis not available in OpenFin v22 (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilzep authored Feb 16, 2023
1 parent f0c1fdc commit c1dacae
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 126 deletions.
9 changes: 8 additions & 1 deletion examples/web/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ document.addEventListener("DOMContentLoaded", function (event) {
$("#button-joingroup").prop("disabled", !container.getCurrentWindow().allowGrouping);
$("#button-leavegroup").prop("disabled", !container.getCurrentWindow().allowGrouping);

$("#button-notification").prop("disabled", isOpenFin());

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"));
Expand All @@ -91,7 +93,7 @@ document.addEventListener("DOMContentLoaded", function (event) {
// Enable popovers
$('[data-toggle="popover"]').popover();

if (container.getCurrentWindow().id === "desktopJS") {
if (!isOpenFin() && container.getCurrentWindow().id === "desktopJS") {
snapAssist = new desktopJS.SnapAssistWindowManager(container,
{
windowStateTracking: desktopJS.WindowStateTracking.Main | desktopJS.WindowStateTracking.Group
Expand Down Expand Up @@ -223,3 +225,8 @@ function getState() {
function setState(state) {
this.container.log("info", state);
}

function isOpenFin() {
const pattern = /OpenFin/;
return pattern.test(window.navigator.userAgent);
}
75 changes: 9 additions & 66 deletions packages/desktopjs-openfin/src/openfin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import {
registerContainer, ContainerWindow, PersistedWindowLayout, Rectangle, Container, WebContainerBase,
ScreenManager, Display, Point, ObjectTransform, PropertyMap, NotificationOptions, ContainerNotification,
ScreenManager, Display, Point, ObjectTransform, PropertyMap, NotificationOptions,
TrayIconDetails, MenuItem, Guid, MessageBus, MessageBusSubscription, MessageBusOptions, EventArgs,
GlobalShortcutManager, WindowEventArgs
} from "@morgan-stanley/desktopjs";
Expand Down Expand Up @@ -152,40 +152,6 @@ export class OpenFinContainerWindow extends ContainerWindow {
});
}

public get allowGrouping() {
return true;
}

public getGroup(): Promise<ContainerWindow[]> {
return new Promise<ContainerWindow[]>((resolve, reject) => {
this.innerWindow.getGroup(windows => {
resolve(windows.map(window => new OpenFinContainerWindow(window)));
}, reject);
});
}

public joinGroup(target: ContainerWindow): Promise<void> {
if (!target || target.id === this.id) {
return Promise.resolve();
}

return new Promise<void>((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(() => {
ContainerWindow.emit("window-leaveGroup", { name: "window-leaveGroup", windowId: this.id } );
resolve();
}, reject);
});
}

public bringToFront(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.innerWindow.bringToFront(resolve, reject);
Expand Down Expand Up @@ -332,8 +298,6 @@ export class OpenFinContainer extends WebContainerBase {
private static readonly trayIconMenuLeftOffset: number = 4;
private static readonly trayIconMenuTopOffset: number = 23;

private static readonly notificationGuid: string = "A21B62E0-16B1-4B10-8BE3-BBB6B489D862";

/**
* Gets or sets whether to replace the native web Notification API with OpenFin notifications.
* @type {boolean}
Expand All @@ -353,12 +317,6 @@ export class OpenFinContainer extends WebContainerBase {

public windowOptionsMap: PropertyMap = OpenFinContainer.windowOptionsMap;

public static readonly notificationOptionsMap: PropertyMap = {
body: { target: "message" }
};

public notificationOptionsMap: PropertyMap = OpenFinContainer.notificationOptionsMap;

public static menuHtml =
`<html>
<head>
Expand Down Expand Up @@ -478,21 +436,13 @@ export class OpenFinContainer extends WebContainerBase {

protected registerNotificationsApi() {
if (typeof this.globalWindow !== "undefined" && this.globalWindow) {
// Define owningContainer for closure to inner class
// eslint-disable-next-line @typescript-eslint/no-this-alias
const owningContainer: OpenFinContainer = this;

this.globalWindow["Notification"] = class OpenFinNotification extends ContainerNotification {
this.globalWindow["Notification"] = class OpenFinNotification {
constructor(title: string, options?: NotificationOptions) {
super(title, options);

options["notification"] = this;

// Forward OpenFin notification events back to Notification API
options["onClick"] = (event) => { if (this.onclick) { this.onclick(event); } };
options["onError"] = (event) => { if (this.onerror) { this.onerror(event); } };
throw new Error("Not supported");
}

owningContainer.showNotification(this.title, this.options);
static async requestPermission(callback?: NotificationPermissionCallback): Promise<string> {
throw new Error("Not supported");
}
};
}
Expand Down Expand Up @@ -572,10 +522,6 @@ export class OpenFinContainer extends WebContainerBase {
});
}

public showNotification(title: string, options?: NotificationOptions) {
const msg = new this.desktop.Notification(ObjectTransform.transformProperties(options, this.notificationOptionsMap));
}

protected getMenuHtml() {
return OpenFinContainer.menuHtml;
}
Expand Down Expand Up @@ -714,7 +660,7 @@ export class OpenFinContainer extends WebContainerBase {
const mainWindow = this.getMainWindow();
const promises: Promise<void>[] = [];

windows.filter(window => window.name !== "queueCounter" && !window.name.startsWith(OpenFinContainer.notificationGuid))
windows.filter(window => window.name !== "queueCounter")
.forEach(djsWindow => {
// eslint-disable-next-line no-async-promise-executor
promises.push(new Promise<void>(async (innerResolve, innerReject) => {
Expand All @@ -728,7 +674,6 @@ export class OpenFinContainer extends WebContainerBase {
innerResolve();
} else {
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,
Expand All @@ -737,11 +682,9 @@ export class OpenFinContainer extends WebContainerBase {
main: (mainWindow && (mainWindow.name === window.name)),
options: options,
state: state,
bounds: { x: bounds.left, y: bounds.top, width: bounds.width, height: bounds.height },
group: group.map(win => win.name)
bounds: { x: bounds.left, y: bounds.top, width: bounds.width, height: bounds.height }
});
innerResolve();
}, innerReject);
}
}, innerReject);
}, innerReject);
Expand Down Expand Up @@ -838,4 +781,4 @@ class OpenFinGlobalShortcutManager extends GlobalShortcutManager {
this.desktop.GlobalHotkey.unregisterAll(resolve, reject);
});
}
}
}
64 changes: 5 additions & 59 deletions packages/desktopjs-openfin/tests/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class MockDesktop {
main(callback): any { callback(); }
GlobalHotkey: any = { };
Window: any = MockWindow;
Notification(): any { return {}; }
InterApplicationBus: any = new MockInterApplicationBus();
Application: any = {
getCurrent() {
Expand Down Expand Up @@ -164,13 +163,6 @@ class MockWindow {
return {};
}

getGroup(callback: any, errorCallback: any) {
callback([ MockWindow.singleton]);
}

joinGroup(target: any, callback: any, errorCallback: any) { }
leaveGroup(callback: any, errorCallback: any) { }

bringToFront(callback:any, errorCallback: any) {
callback();
}
Expand Down Expand Up @@ -432,42 +424,6 @@ describe("OpenFinContainerWindow", () => {
});
});

describe("window grouping", () => {
it("allowGrouping is true", () => {
expect(win.allowGrouping).toEqual(true);
});

it ("getGroup invokes underlying getGroup", async () => {
spyOn(innerWin, "getGroup").and.callFake(resolve => {
resolve([ win ] );
});

const windows = await win.getGroup();
expect(innerWin.getGroup).toHaveBeenCalled();
expect(windows).toBeDefined();
expect(windows.length).toEqual(1);
});

it ("joinGroup invokes underlying joinGroup", async () => {
spyOn(innerWin, "joinGroup").and.callFake((target, resolve) => resolve());
const window = new OpenFinContainerWindow(new MockWindow("Fake"));
await win.joinGroup(window);
expect(innerWin.joinGroup).toHaveBeenCalledWith(window.innerWindow, jasmine.any(Function), jasmine.any(Function));
});

it ("joinGroup with source == target does not invoke joinGroup", async () => {
spyOn(innerWin, "joinGroup").and.callFake((target, resolve) => resolve());
await win.joinGroup(win);
expect(innerWin.joinGroup).toHaveBeenCalledTimes(0);
});

it ("leaveGroup invokes underlying leaveGroup", async () => {
spyOn(innerWin, "leaveGroup").and.callFake(resolve => resolve());
await win.leaveGroup();
expect(innerWin.leaveGroup).toHaveBeenCalled();
});
});

it("bringToFront invokes underlying bringToFront", async () => {
spyOn(innerWin, "bringToFront").and.callThrough();
await win.bringToFront();
Expand Down Expand Up @@ -738,22 +694,12 @@ describe("OpenFinContainer", () => {
});

describe("notifications", () => {
it("showNotification passes message and invokes underlying notification api", () => {
spyOn(desktop, "Notification").and.stub();
container.showNotification("title", { body: "Test message", url: "notification.html" });
expect(desktop.Notification).toHaveBeenCalledWith({ url: "notification.html", message: "Test message" });
it("requestPermission not supported", async () => {
await expectAsync(globalWindow["Notification"].requestPermission()).toBeRejectedWithError("Not supported");
});

it("requestPermission granted", async () => {
await globalWindow["Notification"].requestPermission((permission) => {
expect(permission).toEqual("granted");
});
});

it("notification api delegates to showNotification", () => {
spyOn(container, "showNotification");
new globalWindow["Notification"]("title", { body: "Test message" });
expect(container.showNotification).toHaveBeenCalled();
it("notification api not supported", () => {
expect(() => new globalWindow["Notification"]("title", { body: "Test message" })).toThrowError("Not supported");
});
});

Expand Down Expand Up @@ -961,4 +907,4 @@ describe("OpenfinGlobalShortcutManager", () => {
expect(desktop.GlobalHotkey.unregisterAll).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function));
});
});
});
});

0 comments on commit c1dacae

Please sign in to comment.