Skip to content

Commit

Permalink
Merge pull request #64 from nkolba/handlers
Browse files Browse the repository at this point in the history
Handlers Refactor
  • Loading branch information
nkolba authored Oct 6, 2022
2 parents 34f8b7f + 73f8959 commit 5c6311e
Show file tree
Hide file tree
Showing 35 changed files with 2,502 additions and 1,784 deletions.
2 changes: 1 addition & 1 deletion .browserslistrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Chrome 98
Chrome 106
1,880 changes: 1,225 additions & 655 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@finos/electron-fdc3",
"name": "electron-fdc3",
"private": true,
"engines": {
"node": ">=v16.13",
Expand Down Expand Up @@ -31,6 +31,7 @@
"update-versions": "node scripts/update-electron-vendors.js"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^14.1.0",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@types/node": "^17.0.6",
Expand All @@ -40,7 +41,7 @@
"@vitejs/plugin-react": "^1.1.3",
"cross-env": "^7.0.3",
"dts-for-context-bridge": "^0.7.1",
"electron": "^20.1.4",
"electron": "^21.0.1",
"electron-builder": "^23.5.1",
"electron-devtools-installer": "^3.2.0",
"eslint": "^8.6.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/main/src/IntentResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { BrowserWindow } from 'electron';
import { FDC3App, IntentInstance, ResolverDetail } from './types/FDC3Data';
import { Context } from '@finos/fdc3';
import { join } from 'path';
import { TOPICS } from './constants';
import { Workspace } from './workspace';
import { randomUUID } from 'crypto';
import { RUNTIME_TOPICS } from './handlers/runtime/topics';

const RESOLVER_PRELOAD = join(
__dirname,
Expand Down Expand Up @@ -48,7 +48,7 @@ export class IntentResolver {
preload: RESOLVER_PRELOAD,
webSecurity: true,
nodeIntegration: true,
contextIsolation: false,
contextIsolation: true,
devTools: devTools,
},
});
Expand Down Expand Up @@ -108,7 +108,7 @@ export class IntentResolver {

console.log('startObject options', JSON.stringify(options));

this.window.webContents.send(TOPICS.WINDOW_START, startObject);
this.window.webContents.send(RUNTIME_TOPICS.WINDOW_START, startObject);

console.log(
'intent resolver create',
Expand Down
12 changes: 12 additions & 0 deletions packages/main/src/handlers/fdc3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getRuntime } from '../../index';
import { resolveIntent } from './resolveIntent';

export const FDC3_TOPICS = {
RESOLVE_INTENT: 'fdc3:resolveIntent',
};

export const register = () => {
const runtime = getRuntime();

runtime.addHandler(FDC3_TOPICS.RESOLVE_INTENT, resolveIntent);
};
47 changes: 47 additions & 0 deletions packages/main/src/handlers/fdc3/resolveIntent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getRuntime } from '../../index';
import { RuntimeMessage } from '../runtimeMessage';
import { DirectoryApp } from '../../types/FDC3Data';
import { TOPICS } from '../../constants';

export const resolveIntent = async (message: RuntimeMessage) => {
const runtime = getRuntime();

//TODO: autojoin the new app to the channel which the 'open' call is sourced from

if (!message.data.selected.instanceId) {
const data: DirectoryApp = message.data.selected?.directoryData;

//launch window
const runtime = getRuntime();
if (runtime) {
const win = runtime.createWorkspace();
const view = win.createView(data.start_url, {
directoryData: data as DirectoryApp,
});

//set pending intent and context
view.setPendingIntent(
message.data.intent,
message.data.context,
message.data.id,
);
}
} else {
const view = runtime.getView(message.data.selected?.instanceId);
//send new intent
if (view && view.parent) {
view.content.webContents.send(TOPICS.FDC3_INTENT, {
topic: 'intent',
data: { intent: message.data.intent, context: message.data.context },
source: message.data.id,
});
view.parent.setSelectedTab(view.id);
}
}

//close the resolver
const resolver = runtime.getResolver();
if (resolver) {
resolver.close();
}
};
29 changes: 29 additions & 0 deletions packages/main/src/handlers/fdc3Message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { FDC3App } from '../types/FDC3Data';
import { Context, TargetApp } from '@finos/fdc3';
import { RuntimeMessage } from './runtimeMessage';

export interface FDC3Message extends RuntimeMessage {
name?: string;
intent?: string;
selected?: FDC3App;
context?: Context;
}

export interface FDC3MessageData {
id?: string;
eventId?: string;
context?: Context;
name?: string;
intent?: string;
source?: string; //the viewId (internal instance identifier) of the sender of the message
channel?: string; //name of source/related channel
contextType?: string;
instanceId?: string;
ts?: number; //timestamp (for pending contexts/intents)
target?: TargetApp;
channelId?: string; //to do : refactor with channel prop
type?: string;
restoreOnly?: boolean;
selectedIntent?: string;
selected?: FDC3App;
}
29 changes: 29 additions & 0 deletions packages/main/src/handlers/runtime/channelPicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getRuntime } from '../../index';
import { RuntimeMessage } from '../runtimeMessage';

export const hideChannelWindow = async (message: RuntimeMessage) => {
const runtime = getRuntime();
//bring selected browserview to front
const workspace = runtime?.getWorkspace(message.source);
workspace?.hideChannelWindow();
};

export const pickChannel = async (message: RuntimeMessage) => {
const runtime = getRuntime();
console.log('pickChannel', message.source, message.data);
const sourceWS = runtime.getWorkspace(message.source);
const mouseX: number = message.data.mouseX || 0;
const mouseY: number = message.data.mouseY || 0;
if (sourceWS) {
sourceWS.showChannelWindow(mouseX, mouseY);
}
};

export const joinChannel = async (message: RuntimeMessage) => {
console.log('joinChannel', message);
const runtime = getRuntime();
const sourceWS = runtime.getWorkspace(message.source);
if (sourceWS) {
sourceWS.setChannel(message.data.channel);
}
};
41 changes: 41 additions & 0 deletions packages/main/src/handlers/runtime/directory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getRuntime } from '../../index';
import { RuntimeMessage } from '../runtimeMessage';
import { WebContents } from 'electron';
import utils from '../../utils';
import fetch from 'electron-fetch';
import { RUNTIME_TOPICS } from './topics';

export const fetchFromDirectory = async (message: RuntimeMessage) => {
const runtime = getRuntime();
const directoryUrl = await utils.getDirectoryUrl();

const response = await fetch(`${directoryUrl}${message.data.query}`);
const result = await response.json();

//request can come frome 2 types of (priviledged) sources: the workspace UI and views
//if the sourceType is View. We need to check that the view is a 'system' view and can access the directory
//through this API. Today, this is only the 'home' view.
let wContents: WebContents | undefined = undefined;

if (message.data.sourceType && message.data.sourceType === 'view') {
//ensure this is a view that has permissions for this api
const sourceView = runtime.getView(message.source);
if (sourceView && sourceView.isSystemView() && sourceView.content) {
wContents = sourceView.content.webContents;
}
} else {
const sourceWS = runtime.getWorkspace(message.source);
if (sourceWS && sourceWS.window) {
wContents = sourceWS.window.webContents;
}
}

if (wContents) {
wContents.send(
`${RUNTIME_TOPICS.FETCH_FROM_DIRECTORY}-${message.data.query}`,
{
data: result,
},
);
}
};
33 changes: 33 additions & 0 deletions packages/main/src/handlers/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getRuntime } from '../../index';
import { RUNTIME_TOPICS } from './topics';

import {
tabSelected,
tabDragStart,
newTab,
tearOutTab,
closeTab,
dropTab,
} from './tabs';
import { openToolsMenu } from './toolbar';
import { fetchFromDirectory } from './directory';
import { pickChannel, joinChannel } from './channelPicker';
import { loadSearchResults } from './search';
import { resolveIntent } from '../fdc3/resolveIntent';

export const register = () => {
const runtime = getRuntime();

runtime.addHandler(RUNTIME_TOPICS.TAB_SELECTED, tabSelected);
runtime.addHandler(RUNTIME_TOPICS.TAB_DRAG_START, tabDragStart);
runtime.addHandler(RUNTIME_TOPICS.DROP_TAB, dropTab);
runtime.addHandler(RUNTIME_TOPICS.NEW_TAB, newTab);
runtime.addHandler(RUNTIME_TOPICS.TEAR_OUT_TAB, tearOutTab);
runtime.addHandler(RUNTIME_TOPICS.CLOSE_TAB, closeTab);
runtime.addHandler(RUNTIME_TOPICS.FETCH_FROM_DIRECTORY, fetchFromDirectory);
runtime.addHandler(RUNTIME_TOPICS.OPEN_TOOLS_MENU, openToolsMenu);
runtime.addHandler(RUNTIME_TOPICS.OPEN_CHANNEL_PICKER, pickChannel);
runtime.addHandler(RUNTIME_TOPICS.JOIN_WORKSPACE_TO_CHANNEL, joinChannel);
runtime.addHandler(RUNTIME_TOPICS.SEARCH_LOAD_RESULTS, loadSearchResults);
runtime.addHandler(RUNTIME_TOPICS.RES_RESOLVE_INTENT, resolveIntent);
};
47 changes: 47 additions & 0 deletions packages/main/src/handlers/runtime/resolveIntent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getRuntime } from '../../index';
import { RuntimeMessage } from '../runtimeMessage';
import { DirectoryApp } from '../../types/FDC3Data';
import { TOPICS } from '../../constants';

export const resolveIntent = async (message: RuntimeMessage) => {
const runtime = getRuntime();

//TODO: autojoin the new app to the channel which the 'open' call is sourced from

if (!message.data.selected.instanceId) {
const data: DirectoryApp = message.data.selected?.directoryData;

//launch window
const runtime = getRuntime();
if (runtime) {
const win = runtime.createWorkspace();
const view = win.createView(data.start_url, {
directoryData: data as DirectoryApp,
});

//set pending intent and context
view.setPendingIntent(
message.data.intent,
message.data.context,
message.data.id,
);
}
} else {
const view = runtime.getView(message.data.selected?.instanceId);
//send new intent
if (view && view.parent) {
view.content.webContents.send(TOPICS.FDC3_INTENT, {
topic: 'intent',
data: { intent: message.data.intent, context: message.data.context },
source: message.data.id,
});
view.parent.setSelectedTab(view.id);
}
}

//close the resolver
const resolver = runtime.getResolver();
if (resolver) {
resolver.close();
}
};
17 changes: 17 additions & 0 deletions packages/main/src/handlers/runtime/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { getRuntime } from '../../index';
import { RuntimeMessage } from '../runtimeMessage';

export const hideSearchResults = async (message: RuntimeMessage) => {
const runtime = getRuntime();
//bring selected browserview to front
const workspace = runtime?.getWorkspace(message.source);
workspace?.hideSearchResults();
};

export const loadSearchResults = async (message: RuntimeMessage) => {
const runtime = getRuntime();
//bring selected browserview to front
console.log('loadSearchResults', message.data.results);
const workspace = runtime?.getWorkspace(message.source);
workspace?.loadSearchResults(message.data.results);
};
Loading

0 comments on commit 5c6311e

Please sign in to comment.