Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated apis not available in OpenFin v22 #635

Merged
merged 3 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
65 changes: 14 additions & 51 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 @@ -153,37 +153,19 @@ export class OpenFinContainerWindow extends ContainerWindow {
}

public get allowGrouping() {
bingenito marked this conversation as resolved.
Show resolved Hide resolved
return true;
return false;
}

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

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);
});
return Promise.reject(new Error("Not supported"));
}

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);
});
return Promise.reject(new Error("Not supported"));
}

public bringToFront(): Promise<void> {
Expand Down Expand Up @@ -332,8 +314,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 +333,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 +452,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 @@ -573,7 +539,7 @@ export class OpenFinContainer extends WebContainerBase {
}

public showNotification(title: string, options?: NotificationOptions) {
bingenito marked this conversation as resolved.
Show resolved Hide resolved
const msg = new this.desktop.Notification(ObjectTransform.transformProperties(options, this.notificationOptionsMap));
throw new Error("Not supported");
}

protected getMenuHtml() {
Expand Down Expand Up @@ -714,7 +680,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 +694,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 +702,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 +801,4 @@ class OpenFinGlobalShortcutManager extends GlobalShortcutManager {
this.desktop.GlobalHotkey.unregisterAll(resolve, reject);
});
}
}
}
60 changes: 14 additions & 46 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 @@ -434,37 +426,19 @@ describe("OpenFinContainerWindow", () => {

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

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 ("getGroup not supported", async () => {
await expectAsync(win.getGroup()).toBeRejectedWithError("Not supported");
});

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 not supported", async () => {
await expectAsync(win.joinGroup(null)).toBeRejectedWithError("Not supported");
});

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 ("leaveGroup not supported", async () => {
await expectAsync(win.leaveGroup()).toBeRejectedWithError("Not supported");
});
});

Expand Down Expand Up @@ -738,22 +712,16 @@ 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("showNotification not supported", () => {
expect(() => container.showNotification("title", { body: "Test message", url: "notification.html" })).toThrowError("Not supported");
});

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

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 +929,4 @@ describe("OpenfinGlobalShortcutManager", () => {
expect(desktop.GlobalHotkey.unregisterAll).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function));
});
});
});
});