简体   繁体   English

Electron - 将自定义事件从 Menu 发送到渲染器

[英]Electron - send a custom event from Menu to renderer

I stumbled across a problem that I had a lot of problem finding documentation for while creating an Electron app.我偶然发现了一个问题,在创建 Electron 应用程序时,我在查找文档时遇到了很多问题。 This is a Question / Answer on how to have a custom Electron menu item communicate with the application frontend.这是一个关于如何让自定义 Electron 菜单项与应用程序前端通信的问题/答案。

Please let me know if this kind of post is useful or if some parts need to be elaborated.请让我知道这种帖子是否有用或是否需要详细说明某些部分。


For one of my apps I wanted my frontend to display pictures inside a directory.对于我的一个应用程序,我希望我的前端在目录中显示图片。 When the user clicks on his right arrow key I want to go to the next picture.当用户单击他的右箭头键时,我想将 go 转到下一张图片。 I wanted this behavior to be handled by the inbuilt Electron Menu Accelerator .我希望这种行为由内置的Electron Menu Accelerator处理。

I have built my app separating my main.js from my menu templates like so:我已经构建了我的应用程序,将我的main.js与我的菜单模板分开,如下所示:

app
├── main.js
├── renderer.js
├── index.html
├── templates
        ├── menu.js => uses objects from picture.js and about.js to build & return a menu
        ├── picture.js
        ├── about.js
├── ... (rest of files)

My picture.js looked something like this:我的 picture.js 看起来像这样:

const picture = {
    label: 'Picture',
    role: 'help',
    submenu: [
      {
        label: 'Previous',
        accelerator: 'Left',
        click: function() {
            // this needs to be figured out
        },
      },
      {
        label: 'Next',
        accelerator: 'Right',
        click: function() {
            // this needs to be figured out
        },
      }
    ],
}

exports.picture = picture;

My gut-feeling told me to fiddle around with ipcMain , but this approach just didn't work.我的直觉告诉我要摆弄ipcMain ,但这种方法不起作用。 I got a lot of messages saying that ipc is not defined or that the method send of undefined would not work.我收到很多消息说ipc is not definedthe method send of undefined不起作用。

Here is how I managed to solve my problem:这是我设法解决问题的方法:

What we know: For communication between the Menu and the frontend, we need to create an event in main.js .我们知道:对于 Menu 和前端之间的通信,我们需要在main.js中创建一个事件。 And it is main.js that will send an event to renderer.js .它将向renderer.js发送事件的是main.js

What we found while searching: It is possible to send messages from the main process to the renderer process using webContents.send我们在搜索时发现:可以使用webContents.send将消息从主进程发送到渲染器进程

How to apply it: We need 'app' (the application object) to be called and 'emit' an event.如何应用它:我们需要调用“app”(应用程序对象)并“发出”一个事件。 This is why we had to change the 'picture' object to being a function taking one argument: app.这就是为什么我们必须将“图片” object 更改为 function 并采用一个参数:app。

const fileMenu = () => {
  return {
    label: 'Picture',
    role: 'help',
    submenu: [
      {
        label: 'Previous',
        accelerator: 'Left',
        click: () => app.emit('prevPicture'),
      },
      {
        label: 'Next',
        accelerator: 'Right',
        click: () => app.emit('nextPicture'),
      }
    ],
  }
}

exports.fileMenu = fileMenu;

And then we just need to add the argument in main:然后我们只需要在 main 中添加参数:

// LOTS OF CODE ...
// mainWindow is the name of the Electron BrowserWindow object that holds our menu and app

app.on('ready', function() {
  const template = new AppMenu([fileMenu(app), editMenu, windowMenu, aboutMenu]).getTemplate();
  const menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);
  createWindow();
});

// OTHER CODE ...
app.on('prevPicture', () => {mainWindow.webContents.send('prevPicture');});
app.on('nextPicture', () => {mainWindow.webContents.send('nextPicture');});

// REST OF CODE ...

This allows us to use a simple ipc.on('prevPicture', () => doWhatever) in our renderer.js file and create custom keyboard shortcuts that will affect the frontend in Electron.这允许我们在我们的renderer.js文件中使用一个简单ipc.on('prevPicture', () => doWhatever)并创建自定义键盘快捷键,这将影响 Electron 中的前端。

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

相关问题 从主进程向渲染进程发送 IPC 消息 Electron - Send IPC message from main process to renderer process Electron 将数据从渲染过程发送到Electron中的渲染器过程 - Send Data from render process to renderer process in Electron 不需要Electron中的渲染器自定义模块 - Can't require custom module from renderer in Electron 在电子应用程序中从主向渲染器发出自定义事件 - Emit custom events in electron app from main to renderer Electron:我可以将 http 请求从渲染器发送到 electron 中的主进程吗? - Electron: Can i send http request from renderer to main process in electron? 从渲染器到主的电子调用 - Electron call from renderer to main 将JavaScript对象从主进程发送到渲染器进程以填充HTML Table Electron - Send JavaScript Object from main process to renderer process to populate HTML Table Electron 列出来自特定目录的所有图像并使用 Electron 发送到渲染器进程的最佳方法 - Best approach to list all images from a specific directory and send to renderer process using Electron 我们如何将消息从主进程发送到 Electron 中的渲染器进程 - How can we send messages from the main process to renderer process in Electron 如何在Electron渲染器窗口中从一个JavaScript类/文件向另一个JavaScript类/文件发送消息? - How to send a message in an Electron renderer window from one JavaScript class/file to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM