-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example of using custom snap registration through an override
- Loading branch information
Showing
9 changed files
with
242 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...r/client/src/modules/platform-override/snap-window-selection-override/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
9 changes: 9 additions & 0 deletions
9
...form-starter/client/src/modules/platform-override/snap-window-selection-override/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; |
138 changes: 138 additions & 0 deletions
138
.../client/src/modules/platform-override/snap-window-selection-override/platform-override.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; | ||
}; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...orm-starter/client/src/modules/platform-override/snap-window-selection-override/shapes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters