renderer: Use ipcRenderer.sendTo to communicate with other WebContents.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-04-25 19:34:34 -07:00
parent 3b3fa88c89
commit 994c412bd2
5 changed files with 26 additions and 23 deletions

View File

@@ -335,6 +335,6 @@ export default class WebView {
async send(channel: string, ...parameters: unknown[]): Promise<void> { async send(channel: string, ...parameters: unknown[]): Promise<void> {
await this.domReady; await this.domReady;
await this.$el!.send(channel, ...parameters); ipcRenderer.sendTo(this.$el!.getWebContentsId(), channel, ...parameters);
} }
} }

View File

@@ -783,10 +783,8 @@ class ServerManagerView {
updateGeneralSettings(setting: string, value: unknown): void { updateGeneralSettings(setting: string, value: unknown): void {
if (this.getActiveWebview()) { if (this.getActiveWebview()) {
const webContents = remote.webContents.fromId( const webContentsId = this.getActiveWebview().getWebContentsId();
this.getActiveWebview().getWebContentsId(), ipcRenderer.sendTo(webContentsId, setting, value);
);
webContents.send(setting, value);
} }
} }
@@ -1065,10 +1063,8 @@ class ServerManagerView {
"toggle-silent", "toggle-silent",
newSettings.silent, newSettings.silent,
); );
const webContents = remote.webContents.fromId( const webContentsId = this.getActiveWebview().getWebContentsId();
this.getActiveWebview().getWebContentsId(), ipcRenderer.sendTo(webContentsId, "toggle-dnd", state, newSettings);
);
webContents.send("toggle-dnd", state, newSettings);
}, },
); );
@@ -1197,22 +1193,22 @@ class ServerManagerView {
await this.openSettings("AddServer"); await this.openSettings("AddServer");
}); });
ipcRenderer.on("set-active", async () => { ipcRenderer.on("set-active", () => {
const webviews: NodeListOf<Electron.WebviewTag> = document.querySelectorAll( const webviews: NodeListOf<Electron.WebviewTag> = document.querySelectorAll(
"webview", "webview",
); );
await Promise.all( for (const webview of webviews) {
[...webviews].map(async (webview) => webview.send("set-active")), ipcRenderer.sendTo(webview.getWebContentsId(), "set-active");
); }
}); });
ipcRenderer.on("set-idle", async () => { ipcRenderer.on("set-idle", () => {
const webviews: NodeListOf<Electron.WebviewTag> = document.querySelectorAll( const webviews: NodeListOf<Electron.WebviewTag> = document.querySelectorAll(
"webview", "webview",
); );
await Promise.all( for (const webview of webviews) {
[...webviews].map(async (webview) => webview.send("set-idle")), ipcRenderer.sendTo(webview.getWebContentsId(), "set-idle");
); }
}); });
ipcRenderer.on("open-network-settings", async () => { ipcRenderer.on("open-network-settings", async () => {

View File

@@ -1,4 +1,4 @@
import {remote} from "electron"; import {ipcRenderer, remote} from "electron";
// Do not change this // Do not change this
export const appId = "org.zulip.zulip-electron"; export const appId = "org.zulip.zulip-electron";
@@ -10,5 +10,9 @@ const webContentsId = webContents.id;
// This function will focus the server that sent // This function will focus the server that sent
// the notification. Main function implemented in main.js // the notification. Main function implemented in main.js
export function focusCurrentServer(): void { export function focusCurrentServer(): void {
currentWindow.webContents.send("focus-webview-with-id", webContentsId); ipcRenderer.sendTo(
currentWindow.webContents.id,
"focus-webview-with-id",
webContentsId,
);
} }

View File

@@ -352,7 +352,11 @@ export function initGeneralSection(props: GeneralSectionProps): void {
const newValue = !ConfigUtil.getConfigItem("silent", true); const newValue = !ConfigUtil.getConfigItem("silent", true);
ConfigUtil.setConfigItem("silent", newValue); ConfigUtil.setConfigItem("silent", newValue);
updateSilentOption(); updateSilentOption();
currentBrowserWindow.webContents.send("toggle-silent", newValue); ipcRenderer.sendTo(
currentBrowserWindow.webContents.id,
"toggle-silent",
newValue,
);
}, },
}); });
} }

View File

@@ -125,7 +125,7 @@ function sendAction(action: string): void {
win.restore(); win.restore();
} }
win.webContents.send(action); ipcRenderer.sendTo(win.webContents.id, action);
} }
const createTray = function (): void { const createTray = function (): void {
@@ -219,8 +219,7 @@ function toggleTray(): void {
const selector = "webview:not([class*=disabled])"; const selector = "webview:not([class*=disabled])";
const webview: WebviewTag = document.querySelector(selector)!; const webview: WebviewTag = document.querySelector(selector)!;
const webContents = remote.webContents.fromId(webview.getWebContentsId()); ipcRenderer.sendTo(webview.getWebContentsId(), "toggletray", state);
webContents.send("toggletray", state);
} }
ipcRenderer.on("toggletray", toggleTray); ipcRenderer.on("toggletray", toggleTray);