Skip to content

Commit

Permalink
Merge branch 'main' into workspace/v18.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
johnman committed May 29, 2024
2 parents 2a3e2eb + 2ec2873 commit 2a0f248
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions how-to/integrate-with-snap/client/src/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type OpenFin from "@openfin/core";
import * as Snap from "@openfin/snap-sdk";
import type { SnapProviderOptions } from "./shapes";

if (window === window.top) {
console.log("Adding snap support through a preload.");

window.addEventListener("DOMContentLoaded", async () => {
await initialize({ platformId: fin.me.identity.uuid, showDebugWindow: true });
});
}

/**
* Initialize the snap components.
* @param options The options for initializing the snap provider.
*/
export async function initialize(options: SnapProviderOptions): Promise<void> {
try {
if (options.platformId) {
console.log("Registering Snap with platformId", options.platformId);
const server = new Snap.SnapServer(options.platformId);
console.log("Enabling debug window:", options.showDebugWindow ?? false);
await server.start({ showDebug: options.showDebugWindow ?? false });
const app = fin.Application.getCurrentSync();
await app.on("window-created", async (e) => {
const win = fin.Window.wrapSync(e);
const winOptions = await win.getOptions();
if (!winOptions.includeInSnapshots) {
console.log("Window is not registered with Snap because includeInSnapshots is disabled.");
} else {
const nativeId = await win.getNativeId();
console.log("Registering window with NativeID with Snap", nativeId);
await server.registerWindow(win.identity.name, nativeId);
}
});
const hostOptions = (await fin.me.getOptions()) as OpenFin.WindowOptions;
if (hostOptions.autoShow) {
console.log("Registering current window with snap");
await server.registerWindow(fin.me.identity.name, await fin.Window.getCurrentSync().getNativeId());
} else {
console.log("Current window is not registered with Snap because autoShow is disabled.");
}
}
} catch (err) {
console.error("Error initializing Snap", err);
}
}

0 comments on commit 2a0f248

Please sign in to comment.