简体   繁体   English

如何制作一个 electron 反应应用程序,它有 2 个 windows - 一个通用的一个和一个用于托盘

[英]How to make an electron react app, which has 2 windows - a general one and one for the Tray

I want to make an Electronjs app with some of the react boilerplates.我想用一些反应样板制作一个 Electronjs 应用程序。 I guess the best one is the one with the most stars in github , but I am open to suggestions.我猜最好的是 github 中星星最多的那个,但我愿意接受建议。 My goal is to have one window, which is going to be the main one and another one that will be displayed only when the user clicks the Tray( https://github.com/sfatihk/electron-tray-window ).我的目标是拥有一个 window,它将成为主要的一个,另一个只有在用户单击托盘时才会显示( https://github.com/sfatihk/electron-tray-window )。 What is the best solution, without using a second html and if possible without ejecting.什么是最好的解决方案,不使用第二个 html 并且如果可能不弹出。

I recently researched methods of integrating Election + React & I highly recommend using electron-forge & adding React like this: https://blog.logrocket.com/electron-forge-vs-electron-react-boilerplate/我最近研究了集成 Election + React 的方法,我强烈推荐使用 electron-forge 并像这样添加 React: https://blog.logrocket.com/electron-forge-vs-electron-react-boilerplate/

That article gives you good context from an experienced Slack engineer, but essentially reccommends you follow this in the electron-forge docs: https://www.electronforge.io/guides/framework-integration/react-with-typescript那篇文章为您提供了一位经验丰富的 Slack 工程师的良好背景,但基本上建议您在电子锻造文档中遵循这一点: https://www.electronforge.io/guides/framework-integration/react-with-typescript

That is the simplest integration possible & you'll be on solid footing with electron-forge, instead of a GitHub boilerplate.这是最简单的集成,您将在电子锻造中站稳脚跟,而不是 GitHub 样板。

I however started with a create-react-app project.然而,我从一个 create-react-app 项目开始。 When combining 2 frameworks, each with large package.json & lots of build tooling I do not like to merge their package.json in a single project. When combining 2 frameworks, each with large package.json & lots of build tooling I do not like to merge their package.json in a single project. Instead I nested a CRA project inside of electron-forge, & I tell electron to load the /build folder out of CRA.相反,我在 electron-forge 中嵌套了一个 CRA 项目,并告诉 electron 从 CRA 中加载 /build 文件夹。 This works for packaging the app, but during development I use electron-is-dev to load the CRA dev server on port 3001 instead.这适用于打包应用程序,但在开发过程中,我使用 electron-is-dev 在端口 3001 上加载 CRA 开发服务器。 Use a.env config to change the port the CRA dev server uses to 3001, because both electron-forge & CRA want to use 3000 by default.使用 a.env config 将 CRA 开发服务器使用的端口更改为 3001,因为 electron-forge 和 CRA 都希望默认使用 3000。

To connect actions from your tray window with the main app window, use IPC messages to communicate between those 2 Renderers, with the Main process in between to relay the messages.要将托盘 window 中的操作与主应用程序 window 连接,请使用 IPC 消息在这 2 个渲染器之间进行通信,主进程在它们之间传递消息。

Your best solution given the existing parameters, would probably be to have a separate route in your application (since you don't want a separate html page), and when the tray icon is clicked, wire it to something like给定现有参数,您的最佳解决方案可能是在您的应用程序中有一个单独的路由(因为您不想要单独的 html 页面),当单击托盘图标时,将其连接到类似

const window = new BrowserWindow();
window.loadUrl('path/to/app/special/route');

You can use Tray in electron for this您可以为此使用 electron 中的托盘

Quote from the tray docuentation从托盘文档中引用

Add icons and context menus to the system's notification area.将图标和上下文菜单添加到系统的通知区域。

Example code:示例代码:

const { app, Menu, Tray } = require('electron')

let tray = null
app.whenReady().then(() => {
  tray = new Tray('/path/to/my/icon')
  const contextMenu = Menu.buildFromTemplate([
    { label: 'Item1', type: 'radio' },
    { label: 'Item2', type: 'radio' },
    { label: 'Item3', type: 'radio', checked: true },
    { label: 'Item4', type: 'radio' }
  ])
  tray.setToolTip('This is my application.')
  tray.setContextMenu(contextMenu)
})

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

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