Skip to content

Commit

Permalink
Added example of using custom snap registration through an override
Browse files Browse the repository at this point in the history
  • Loading branch information
johnman committed Oct 21, 2024
1 parent bfe71ad commit 3843c3f
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 15 deletions.
5 changes: 3 additions & 2 deletions how-to/workspace-platform-starter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
- Updated example-notification-service-app to show raising a notification that has a call to action that passes data back to itself (or a new instance if it is closed) though a context listener.
- Updated the WPS Platform Override Module so that our workspace platform override will log an error and then throw an exception if saving, updating or deleting a page or a workspace fails. The exception is required so that the default workspace platform code knows that the indicator should show failure instead of success.
- Updated integration template that is used when you use npm run generate-module integrations "Your Integration" so that it includes example results and examples of handling result clicks so that you have an easier starting point.
- Updated example-notification-source so that you can specify a url to post notifications to (it will wrap the request in a message property and send it to the server you specify via post: { url: "" } settings. Our example node-starter supports messages being posted to <http://localhost:6060/api/messages>.
- Updated example-notification-source so that you can specify a url to post notifications to. It will wrap the request in a message property and send it to the server you specify via post: { url: "" } settings. Our example node-starter supports messages being posted to <http://localhost:6060/api/messages>.
- Updated the notification service example app so that if you select the endpoint example action it will post a notification to the notification source create endpoint.
- BUGFIX - Updated Snapshot launching logic as there was a bug when updating a page's layout (to ensure view names are regenerated when using app naming conventions) in a multi page window which resulted in the page layout being assigned at the wrong level.
- Enhancement - Previously if you were generating a log module you needed to avoid using the injected createLogger function as you would end up calling yourself and ending up in a loop. The module logic has been updated to pass an alternative createLogger function to log modules that only logs to the console (so that it can still be used for debugging) but doesn't call itself or other log sinks (log modules).
- Enhancement - You may want to enable snap but not have auto window registration. This may be because you only want snap for your native windows or you might want to have your own rules (via a custom platform provider module) on which windows should be tracked (outside of the default rules snap has such as includeInSnapshots false meaning don't track).
- Enhancement - You may want to enable snap but not have auto window registration. This may be because you only want snap for your native windows or you might want to have your own rules (via a custom platform provider module) on which windows should be tracked (outside of the default rules snap has such as includeInSnapshots false meaning don't track). The snapProvider config now lets you specify "enableAutoWindowRegistration": false to disable auto tracking.
- Example Added - Added an additional [Platform Override Example](./client/src/modules/platform-override/snap-window-selection-override/README.md) that shows how you can enable Snap but disable auto window tracking so that you can plug in your own logic to determine which windows should be tracked by the snap server.

## v19.0.0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Snap Url Validator

## What is this?

This is another example of how you can plug in custom platform logic through a platform override module.

This example is related to the following scenario:

- I (Platform Owner) want to use Snap SDK to snap windows together
- I don't want every type of window to be tracked by the Snap Server and I need more granular control over the default behavior of excluding windows that are not included in snapshots (includeInSnapshots false in window options). In this scenario I (the platform owner) want to specify urls that should not be tracked (rather than it being driven by Window Options).

## How do I do it?

First you need to [enable Snap SDK](../../../../../docs/how-to-configure-snap.md). Then you need to specify that you want to disable the `"enableAutoWindowRegistration": true,` setting by setting it to false. Snap will now start up but not enable auto window registration (as you want your custom logic to drive this decision).

You then use this module (or something similar, remember this is an example) to track everything except urls that have been specified in the module's custom data definition.

## How is it configured?

This example module is defined as a platform override module in a manifest or settings service:

```json
{
"id": "snap-window-selection-override",
"icon": "http://localhost:8080/favicon.ico",
"title": "Snap Window Selection Override",
"description": "Snap Window Selection Override",
"enabled": true,
"url": "http://localhost:8080/js/modules/platform-override/snap-window-selection-override.bundle.js",
"data": {
"excludeUrls": ["*/platform/*", "*/common/windows/*", "*/common/views/*"]
}
}
```

The example above is specifying that it doesn't want platform windows or common windows or views tracked (for cases where a window is inside the view folder as an additional host for a specific view).

## How can I test this?

- You would add this module in the manifest.fin.json file in the public folder.
- You would make sure that snap is enabled and auto window registration is off.
- You would launch the platform using npm run client
- You would then launch the call app and some other apps through home (or store).
- You would see that they snap (if you hold down Ctrl when you click on a window to drag which is the new default for the snapProvider in the main manifest).
- You would launch the IRS RFQ app through Home or Store. You will notice that it doesn't snap when you hold down Ctrl and click and drag the RFQ Window (by selecting the title) and moving it to a Browser window.
- Quit the platform.
- Disable this module by setting enabled to false.
- Go to the snapProvider settings and set window registration to true: `"enableAutoWindowRegistration": true,`
- Relaunch the platform and after startup launch the call app and the IRS RFQ app. They will now snap when you hold Ctrl and drag a window.

[How To Customize Your Platform Override](../../../../../docs/how-to-customize-your-platform-override.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ModuleImplementation, ModuleTypes } from "workspace-platform-starter/shapes/module-shapes";
import { SnapWindowSelectionOverride } from "./platform-override";

/**
* Define the entry points for the module.
*/
export const entryPoints: { [type in ModuleTypes]?: ModuleImplementation } = {
platformOverride: new SnapWindowSelectionOverride()
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// eslint-disable-next-line max-classes-per-file
import type OpenFin from "@openfin/core";
import type { WorkspacePlatformProvider } from "@openfin/workspace-platform";
import type { Logger, LoggerCreator } from "workspace-platform-starter/shapes/logger-shapes";
import type { ModuleDefinition } from "workspace-platform-starter/shapes/module-shapes";
import type {
PlatformOverride,
PlatformOverrideHelpers,
PlatformOverrideOptions
} from "workspace-platform-starter/shapes/platform-shapes";
import type { SnapWindowSelectionOverrideOptions } from "./shapes";

/**
* Implementation for the snap window selection override platform override.
*/
export class SnapWindowSelectionOverride implements PlatformOverride<SnapWindowSelectionOverrideOptions> {
/**
* The module definition including settings.
* @internal
*/
private _definition: ModuleDefinition<SnapWindowSelectionOverrideOptions> | undefined;

/**
* The logger for displaying information from the module.
* @internal
*/
private _logger?: Logger;

/**
* Helper methods for the module.
* @internal
*/
private _helpers: PlatformOverrideHelpers | undefined;

/**
* Initialize the module.
* @param definition The definition of the module from configuration include custom options.
* @param loggerCreator For logging entries.
* @param helpers Helper methods for the module to interact with the application core.
* @returns Nothing.
*/
public async initialize(
definition: ModuleDefinition<SnapWindowSelectionOverrideOptions>,
loggerCreator: LoggerCreator,
helpers: PlatformOverrideHelpers
): Promise<void> {
this._definition = definition;
this._logger = loggerCreator("SnapWindowSelectionOverride");
this._helpers = helpers;

this._logger.info("Initializing");

// TODO: Add code here to allocate any module resources
// You can access the configured options e.g. definition.data?.exampleProp
}

/**
* Close down any resources being used by the module.
* @returns Nothing.
*/
public async closedown(): Promise<void> {
this._logger?.info("Closedown");

// TODO: Add code here to free up any module resources
}

/**
* Get the override constructor for the platform override (useful if you wish this implementation to be layered with other implementations and passed to the platform's initialization object as part of an array).
* @param options The options for the platform override defined as part of the platform.
* @returns The override constructor to be used in an array.
*/
public async getConstructorOverride(
options: PlatformOverrideOptions
): Promise<OpenFin.ConstructorOverride<WorkspacePlatformProvider>> {
return (Base: OpenFin.Constructor<WorkspacePlatformProvider>) => {
// use settings passed through the module definition in your override or the default options passed with the function call
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const moduleData = this._definition?.data ?? {};
const logger = this._logger;
const helpers = this._helpers;
/**
* Extend the Platform Override.
*/
return class CustomPlatformOverride extends Base {
/**
* Constructor for the interop override.
*/
constructor() {
super();
// this is just an example to show a reference to the options, module data and local reference to the passed helpers.
logger?.info(
`Options passed: ${JSON.stringify(options)} and module data: ${JSON.stringify(moduleData)} with session id: ${helpers?.sessionId}`
);
}

/**
* Creates a window with the given options.
* @param windowOptions The options for the window.
* @param identity The identity of the window.
* @returns The created window.
*/
public async createWindow(
windowOptions: OpenFin.PlatformWindowCreationOptions,
identity?: OpenFin.Identity
): Promise<OpenFin.Window> {
const createdWindow = await super.createWindow(windowOptions, identity);
// This example is for cases where snap autoWindowRegistration is disabled and you want to have custom logic to determine if a window should be tracked or not by snap.
// This function is only called for the creation of platform windows and not native applications.
const getSnapClient = await helpers?.getSnapClient();
const snapEnabled = await getSnapClient?.isEnabled();
const snapServer = await getSnapClient?.getSnapServer();
if (!snapEnabled || !snapServer) {
return createdWindow;
}
let track = true;
// Check if the window should be excluded from snap tracking
if (
Array.isArray(moduleData?.excludeUrls) &&
moduleData.excludeUrls.length > 0 &&
windowOptions.url
) {
// Check if the entry in the array is part of the current url of windowOptions.url
const url = windowOptions.url;
track = !moduleData.excludeUrls.some((pattern) => {
const regex = new RegExp(pattern.replace(/\*/g, ".*"));
return regex.test(url);
});
}
if (track) {
const nativeId = await createdWindow.getNativeId();
await snapServer.registerWindow(createdWindow.identity.name, nativeId);
}
return createdWindow;
}
};
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Options for the snap window selection override platform override.
*/
export interface SnapWindowSelectionOverrideOptions {
/**
* A list of urls that would result in a window not being tracked by the snap server.
*/
excludeUrls?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,28 @@ const configs = [
experiments: {
outputModule: true
}
},
{
entry: './client/src/modules/platform-override/snap-window-selection-override/index.ts',
devtool: 'source-map',
module: {
rules: [loaderRule]
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
alias
},
externals: { fin: 'fin' },
output: {
filename: 'snap-window-selection-override.bundle.js',
library: {
type: 'module'
},
path: path.resolve(__dirname, '..', 'public', 'js', 'modules', 'platform-override')
},
experiments: {
outputModule: true
}
}
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ To enable snap support in your platform you can add the following in your manife
"snapProvider": {
"enabled": true,
"id": "workspace-platform-starter",
"enableAutoWindowRegistration": true,
"serverAssetInfo": {
"src": "https://cdn.openfin.co/release/snap/0.5.0/snap.zip",
"alias": "openfin-snap",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@ This now opens up the capability to add your own logic to the platform (you want

- [Default Workspace Starter Platform Override Module](../client/src/modules/platform-override/wps-platform-override/)
- [Example Workspace Platform Override That Validates App Urls and Access](../client/src/modules/platform-override/application-url-and-access-validator/)
- [Example Workspace Platform Override That Has Custom Snap SDK Logic To Determine Which Windows Can Snap](../client/src/modules/platform-override/snap-window-selection-override/)

[<- Back to Table Of Contents](../README.md)
21 changes: 8 additions & 13 deletions how-to/workspace-platform-starter/public/manifest.fin.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,6 @@
"initialLanguage": "en-US"
},
"modules": [
{
"id": "application-url-and-access-validator",
"icon": "http://localhost:8080/favicon.ico",
"title": "Application Url and Access Validator",
"description": "This is an example platform override module that shows how you could validate a saved page, workspace or an application snapshot (if it combines apps) to ensure that it is using the latest url for the applications used and that the user still has access to that app.",
"enabled": false,
"url": "http://localhost:8080/js/modules/platform-override/application-url-and-access-validator.bundle.js",
"data": {
"deniedAccessUrl": "http://localhost:8080/common/views/platform/no-access/no-access.html"
}
},
{
"id": "default-wps-platform",
"icon": "http://localhost:8080/favicon.ico",
Expand Down Expand Up @@ -298,8 +287,13 @@
"enabled": true,
"url": "http://localhost:8080/js/modules/endpoint/example-notification-source.bundle.js",
"data": {
"websocket": { "url": "" },
"longpoll": { "url": "", "intervalInSeconds": 5 },
"websocket": {
"url": ""
},
"longpoll": {
"url": "",
"intervalInSeconds": 5
},
"post": {
"url": ""
},
Expand Down Expand Up @@ -1811,6 +1805,7 @@
"snapProvider": {
"enabled": false,
"id": "workspace-platform-starter",
"enableAutoWindowRegistration": true,
"serverAssetInfo": {
"src": "https://cdn.openfin.co/release/snap/0.5.0/snap.zip",
"alias": "openfin-snap",
Expand Down

0 comments on commit 3843c3f

Please sign in to comment.