From c1dacae5affd947efce59925acf9350be0e24e35 Mon Sep 17 00:00:00 2001 From: Ilze Date: Thu, 16 Feb 2023 18:18:03 +0000 Subject: [PATCH] Remove deprecated apis not available in OpenFin v22 (#635) --- examples/web/assets/js/app.js | 9 ++- packages/desktopjs-openfin/src/openfin.ts | 75 +++---------------- .../desktopjs-openfin/tests/openfin.spec.ts | 64 ++-------------- 3 files changed, 22 insertions(+), 126 deletions(-) diff --git a/examples/web/assets/js/app.js b/examples/web/assets/js/app.js index 3ec29c3c..3ee522d8 100644 --- a/examples/web/assets/js/app.js +++ b/examples/web/assets/js/app.js @@ -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")); @@ -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 @@ -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); +} diff --git a/packages/desktopjs-openfin/src/openfin.ts b/packages/desktopjs-openfin/src/openfin.ts index 17c17882..ec31199d 100644 --- a/packages/desktopjs-openfin/src/openfin.ts +++ b/packages/desktopjs-openfin/src/openfin.ts @@ -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"; @@ -152,40 +152,6 @@ export class OpenFinContainerWindow extends ContainerWindow { }); } - public get allowGrouping() { - return true; - } - - public getGroup(): Promise { - return new Promise((resolve, reject) => { - this.innerWindow.getGroup(windows => { - resolve(windows.map(window => new OpenFinContainerWindow(window))); - }, reject); - }); - } - - public joinGroup(target: ContainerWindow): Promise { - if (!target || target.id === this.id) { - return Promise.resolve(); - } - - return new Promise((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 { - return new Promise((resolve, reject) => { - this.innerWindow.leaveGroup(() => { - ContainerWindow.emit("window-leaveGroup", { name: "window-leaveGroup", windowId: this.id } ); - resolve(); - }, reject); - }); - } - public bringToFront(): Promise { return new Promise((resolve, reject) => { this.innerWindow.bringToFront(resolve, reject); @@ -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} @@ -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 = ` @@ -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 { + throw new Error("Not supported"); } }; } @@ -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; } @@ -714,7 +660,7 @@ export class OpenFinContainer extends WebContainerBase { const mainWindow = this.getMainWindow(); const promises: Promise[] = []; - 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(async (innerResolve, innerReject) => { @@ -728,7 +674,6 @@ export class OpenFinContainer extends WebContainerBase { innerResolve(); } else { delete (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, @@ -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); @@ -838,4 +781,4 @@ class OpenFinGlobalShortcutManager extends GlobalShortcutManager { this.desktop.GlobalHotkey.unregisterAll(resolve, reject); }); } -} \ No newline at end of file +} diff --git a/packages/desktopjs-openfin/tests/openfin.spec.ts b/packages/desktopjs-openfin/tests/openfin.spec.ts index eb825cfb..58ead9c7 100644 --- a/packages/desktopjs-openfin/tests/openfin.spec.ts +++ b/packages/desktopjs-openfin/tests/openfin.spec.ts @@ -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() { @@ -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(); } @@ -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(); @@ -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"); }); }); @@ -961,4 +907,4 @@ describe("OpenfinGlobalShortcutManager", () => { expect(desktop.GlobalHotkey.unregisterAll).toHaveBeenCalledWith(jasmine.any(Function), jasmine.any(Function)); }); }); -}); \ No newline at end of file +});