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> {
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 {
if (this.getActiveWebview()) {
const webContents = remote.webContents.fromId(
this.getActiveWebview().getWebContentsId(),
);
webContents.send(setting, value);
const webContentsId = this.getActiveWebview().getWebContentsId();
ipcRenderer.sendTo(webContentsId, setting, value);
}
}
@@ -1065,10 +1063,8 @@ class ServerManagerView {
"toggle-silent",
newSettings.silent,
);
const webContents = remote.webContents.fromId(
this.getActiveWebview().getWebContentsId(),
);
webContents.send("toggle-dnd", state, newSettings);
const webContentsId = this.getActiveWebview().getWebContentsId();
ipcRenderer.sendTo(webContentsId, "toggle-dnd", state, newSettings);
},
);
@@ -1197,22 +1193,22 @@ class ServerManagerView {
await this.openSettings("AddServer");
});
ipcRenderer.on("set-active", async () => {
ipcRenderer.on("set-active", () => {
const webviews: NodeListOf<Electron.WebviewTag> = document.querySelectorAll(
"webview",
);
await Promise.all(
[...webviews].map(async (webview) => webview.send("set-active")),
);
for (const webview of webviews) {
ipcRenderer.sendTo(webview.getWebContentsId(), "set-active");
}
});
ipcRenderer.on("set-idle", async () => {
ipcRenderer.on("set-idle", () => {
const webviews: NodeListOf<Electron.WebviewTag> = document.querySelectorAll(
"webview",
);
await Promise.all(
[...webviews].map(async (webview) => webview.send("set-idle")),
);
for (const webview of webviews) {
ipcRenderer.sendTo(webview.getWebContentsId(), "set-idle");
}
});
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
export const appId = "org.zulip.zulip-electron";
@@ -10,5 +10,9 @@ const webContentsId = webContents.id;
// This function will focus the server that sent
// the notification. Main function implemented in main.js
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);
ConfigUtil.setConfigItem("silent", newValue);
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.webContents.send(action);
ipcRenderer.sendTo(win.webContents.id, action);
}
const createTray = function (): void {
@@ -219,8 +219,7 @@ function toggleTray(): void {
const selector = "webview:not([class*=disabled])";
const webview: WebviewTag = document.querySelector(selector)!;
const webContents = remote.webContents.fromId(webview.getWebContentsId());
webContents.send("toggletray", state);
ipcRenderer.sendTo(webview.getWebContentsId(), "toggletray", state);
}
ipcRenderer.on("toggletray", toggleTray);