简体   繁体   English

如何从远程切换到 ipcMain 和 ipcRenderer?

[英]How to switch from remote to ipcMain and ipcRenderer?

I am making electron app with react and I have titlebar controls done with remote module but in console I am getting warning that remote module is deprecated.我正在使用 react 制作 electron 应用程序,并且我使用远程模块完成了标题栏控制,但在控制台中我收到警告说remote模块已被弃用。 I want to change my code to use ipcMain and ipcRenderer instead remote but I can't get it working.我想更改我的代码以使用ipcMainipcRenderer而不是远程,但我无法让它工作。

This is my code that is working 100% atm but with remote module.这是我的代码,它可以 100% atm 工作,但使用远程模块。 Can someone show on at least one function for example app.minimize() how to use ipcMain and ipcRenderer ?有人可以显示至少一个 function 例如app.minimize()如何使用ipcMainipcRenderer

import { useState, useEffect } from "react";
import { remote } from "electron";
import {
  VscChromeMinimize,
  VscChromeMaximize,
  VscChromeRestore,
  VscChromeClose,
} from "react-icons/vsc";

const TitlebarControls = ({ minimize, maximize, close }) => {
  const app = remote.getCurrentWindow();
  const [maximizeIcon, setMaximizeIcon] = useState(
    app.isMaximized() ? <VscChromeRestore /> : <VscChromeMaximize />
  );

  useEffect(() => {
    app.on("maximize", () => setMaximizeIcon(<VscChromeRestore />));
    app.on("unmaximize", () => setMaximizeIcon(<VscChromeMaximize />));
  }, [app]);

  return (
    <ul className="titlebar-controls">
      {minimize && (
        <li className="minimize" onClick={() => app.minimize()}>
          <VscChromeMinimize />
        </li>
      )}
      {maximize && (
        <li
          className="maximize"
          onClick={() =>
            !app.isMaximized() ? app.maximize() : app.unmaximize()
          }
        >
          {maximizeIcon}
        </li>
      )}
      {close && (
        <li className="close" onClick={() => app.close()}>
          <VscChromeClose />
        </li>
      )}
    </ul>
  );
};

export default TitlebarControls;

The remote module will be removed in Electron 14 but instead you can use @electron/remote module if you only care about deprecation. remote模块将在 Electron 14 中删除,但如果您只关心弃用,则可以使用@electron/remote模块。 The docs give the following way to use it: 文档提供了以下使用方法:

// Deprecated way
// const { BrowserWindow } = require('electron').remote
// Replace with:
const { BrowserWindow } = require('@electron/remote')

// In the main process:
require('@electron/remote/main').initialize()

If you want the ipc method to achieve the same, do the following:如果您希望 ipc 方法达到相同的效果,请执行以下操作:

In renderer.js:在 renderer.js 中:

const { ipcRenderer } = require('electron')
//to minimize
ipcRenderer.send('minimize', data);//data contains any extra info you may need to send

In main.js:在 main.js 中:

const { ipcMain, app } = require('electron')
ipcMain.on('minimize', (event, data) => {
    //Minimize logic
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM