-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.tsx
171 lines (153 loc) · 5.32 KB
/
Provider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import type OpenFin from "@openfin/core";
import React, { useEffect, useState } from 'react';
import logo from '../logo.svg';
import { Dock, Home, Storefront, type App } from "@openfin/workspace";
import { CustomActionCallerType, init } from "@openfin/workspace-platform";
import * as Notifications from "@openfin/workspace/notifications";
import { register as registerDock } from "./dock";
import { register as registerHome } from "./home";
import { launchApp } from "./launch";
import { register as registerNotifications } from "./notifications";
import type { CustomSettings, PlatformSettings } from "./shapes";
import { register as registerStore } from "./store";
let isInitialized = false;
let logMessage: React.Dispatch<React.SetStateAction<string>>;
function Provider() {
const [message, setMessage] = useState("");
logMessage = setMessage;
useEffect(() => {
(async function () {
if (!isInitialized) {
isInitialized = true;
try {
setMessage("Workspace platform initializing");
// Load the settings from the manifest
const settings = await getManifestCustomSettings();
// When the platform api is ready we bootstrap the platform.
const platform = fin.Platform.getCurrentSync();
await platform.once("platform-api-ready", async () => {
await initializeWorkspaceComponents(settings.platformSettings, settings.customSettings)
setMessage("Workspace platform initialized");
});
// The DOM is ready so initialize the platform
// Provide default icons and default theme for the browser windows
await initializeWorkspacePlatform(settings.platformSettings);
} catch (err) {
setMessage(`Error Initializing Platform: ${err instanceof Error ? err.message : err}`)
}
}
})();
}, []);
return (
<div className="col fill gap20">
<header className="row spread middle">
<div className="col">
<h1>OpenFin Platform Window</h1>
<h1 className="tag">Workspace platform window</h1>
</div>
<div className="row middle gap10">
<img src={logo} alt="OpenFin" height="40px" />
</div>
</header>
<main className="col gap10">
<p>This is the platform window, which initializes the platform.</p>
<p>The window would usually be hidden, you can make it hidden on startup by setting the platform.autoShow flag to false in the manifest.fin.json</p>
<p>{message}</p>
</main>
</div>
);
}
/**
* Initialize the workspace platform.
* @param platformSettings The platform settings from the manifest.
*/
async function initializeWorkspacePlatform(platformSettings: PlatformSettings): Promise<void> {
logMessage("Initializing workspace platform");
await init({
browser: {
defaultWindowOptions: {
icon: platformSettings.icon,
workspacePlatform: {
pages: [],
favicon: platformSettings.icon
}
}
},
theme: [
{
label: "Default",
default: "dark",
palette: {
brandPrimary: "#0A76D3",
brandSecondary: "#383A40",
backgroundPrimary: "#1E1F23"
}
}
],
customActions: {
"launch-app": async (e): Promise<void> => {
if (
e.callerType === CustomActionCallerType.CustomButton ||
e.callerType === CustomActionCallerType.CustomDropdownItem
) {
await launchApp(e.customData as App);
}
}
}
});
}
/**
* Bring the platform to life.
* @param platformSettings The platform settings from the manifest.
* @param customSettings The custom settings from the manifest.
*/
async function initializeWorkspaceComponents(
platformSettings: PlatformSettings,
customSettings?: CustomSettings
): Promise<void> {
logMessage("Initializing the workspace components");
// Register with home and show it
logMessage("Initializing the workspace components: home");
await registerHome(platformSettings, customSettings?.apps);
await Home.show();
// Register with store
logMessage("Initializing the workspace components: store");
await registerStore(platformSettings, customSettings?.apps);
// Register with dock
logMessage("Initializing the workspace components: dock");
await registerDock(platformSettings, customSettings?.apps);
// Register with notifications
logMessage("Initializing the workspace components: notifications");
await registerNotifications(platformSettings);
// When the platform requests to be close we deregister from home and quit
const providerWindow = fin.Window.getCurrentSync();
await providerWindow.once("close-requested", async () => {
await Home.deregister(platformSettings.id);
await Storefront.deregister(platformSettings.id);
await Dock.deregister();
await Notifications.deregister(platformSettings.id);
await fin.Platform.getCurrentSync().quit();
});
}
/**
* Read the custom settings from the manifest.fin.json.
* @returns The custom settings from the manifest.
*/
async function getManifestCustomSettings(): Promise<{
platformSettings: PlatformSettings;
customSettings?: CustomSettings;
}> {
// Get the manifest for the current application
const app = await fin.Application.getCurrent();
// Extract the custom settings for this application
const manifest: OpenFin.Manifest & { customSettings?: CustomSettings } = await app.getManifest();
return {
platformSettings: {
id: manifest.platform?.uuid ?? "",
title: manifest.shortcut?.name ?? "",
icon: manifest.platform?.icon ?? ""
},
customSettings: manifest.customSettings
};
}
export default Provider;