Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Add SupApp.SendMessage to allow direct communication between two windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bilou84 committed Oct 19, 2017
1 parent 50f9f21 commit a2328c8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SupApp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ namespace SupApp {
export function onMessage(messageType: string, callback: Function) {
electron.ipcRenderer.addListener(`sup-app-message-${messageType}`, (event, ...args) => { callback(...args); });
}
export function sendMessage(windowId: number, message: string) {
electron.ipcRenderer.send("send-message", windowId, message);
}

export function getCurrentWindow() { return currentWindow; }
export function showMainWindow() { electron.ipcRenderer.send("show-main-window"); }
Expand Down
8 changes: 8 additions & 0 deletions src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ electron.ipcMain.on("choose-folder", onChooseFolder);
electron.ipcMain.on("choose-file", onChooseFile);
electron.ipcMain.on("authorize-folder", onAuthorizeFolder);
electron.ipcMain.on("check-path-authorization", onCheckPathAuthorization);
electron.ipcMain.on("send-message", onSendMessage);

const secretKeys = new Map<Electron.WebContents, string>();

Expand Down Expand Up @@ -96,3 +97,10 @@ function onCheckPathAuthorization(event: Electron.IpcMainEvent, secretKey: strin
const authorization = canReadWrite ? "readWrite" : (canExecute ? "execute" : null);
event.sender.send("check-path-authorization-callback", ipcId, normalizedPath, authorization);
}

function onSendMessage(event: Electron.IpcMainEvent, windowId: number, message: string, args: any[] = []) {
const window = electron.BrowserWindow.fromId(windowId);
if (window == null) return;

window.webContents.send(`sup-app-message-${message}`, ...args);
}

0 comments on commit a2328c8

Please sign in to comment.