简体   繁体   English

Electron 中的多个 Windows

[英]Multiple Windows in Electron

I've been looking for hours, and I cant find a way to build a working setup to use multiple windows in my Electron app.我一直在寻找几个小时,但我无法找到一种方法来构建工作设置以在我的 Electron 应用程序中使用多个 windows。

I'm using electron-forge with plain vanilla JS.我正在使用带有普通 JS electron-forge No react or vue.没有反应或Vue。

What I'm trying to achieve is popups that I can trigger from inside the main application Dashboard using functions.我想要实现的是可以使用函数从主应用程序仪表板内部触发的弹出窗口。

Here's what I have for HTML:这是我为 HTML 提供的:

<i onclick="newBot()" class="fas fa-plus new-bot-button bar-icon"></i>
<i onclick="selectBot()" class="fas fa-box-open select-bot-button bar-icon"></i>
<i onclick="startBot()" class="fas fa-play start-bot-button bar-icon"></i>
<i onclick="stopBot()" class="fas fa-stop stop-bot-button bar-icon"></i>
<i onclick="about()" class="fas fa-info about-button bar-icon"></i>

I'm completely clueless on how to implement this in JavaScript, so help would be greatly appreciated.我完全不知道如何在 JavaScript 中实现这一点,因此将不胜感激。

Note, I have a preload script mainPreload.js and the main script is main.js .注意,我有一个预加载脚本mainPreload.js ,主脚本是main.js The window is also frameless as well, and have a custom title bar that I coded. window 也是无框的,并且有一个我编写的自定义标题栏。 It looks like this.它看起来像这样。 The icons referenced above are the set on the far right.上面引用的图标是最右边的集合。

标题栏

Here's my main.js :这是我的main.js

const { ipcMain, app, BrowserWindow } = require('electron')

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    resizable: false,
    webPreferences: {
      preload: path.join(__dirname, 'mainPreload.js'),
      nodeIntegration: true,
      contextIsolation: false
    },
    frame: false,
    icon: './assets/toggle-01.png'
  })
  
  win.loadFile('index.html');
  ipcMain.on('minimize', () => {
    win.minimize()
  });
}



app.whenReady().then(() => {
  createWindow()
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

Here's my mainPreload.js :这是我的mainPreload.js

function scrollDown() {
  document.getElementById('console-text').scrollTop = document.getElementById('console-text').scrollHeight
}

window.addEventListener('DOMContentLoaded', () => {
  const container = document.getElementById('console-text');
  console.log = function(message) {
    const p = container.appendChild(document.createElement('p'));
    p.textContent = message;
    p.className = 'console-output';
    scrollDown();
  };
  window.addEventListener('error', (error) => {
    const p = container.appendChild(document.createElement('p'));
    p.textContent = error.message;
    p.className = 'console-output';
    scrollDown();
  });
  console.log('Ready')
})

Thanks!谢谢!

To show new windows you just need to create new BrowserWindow instances.要显示新的 windows,您只需创建新的 BrowserWindow 实例。 You can refer to ElectronJS samples on GitHub您可以参考GitHub 上的 ElectronJS 示例

Below is a code based on the Create Window demo.下面是基于 Create Window 演示的代码。

Add an ID to your button (not the best idea to use <i> tag for this btw):为您的按钮添加一个 ID(使用<i>标签不是最好的办法):

    <i id="id-about" class="fas fa-info about-button bar-icon"></i>

Then create a click event handler.然后创建一个单击事件处理程序。 Put this code into your renderer process (in your case a JS running in your index.html ).将此代码放入您的渲染器进程(在您的情况下,JS 在您的index.html中运行)。

const {BrowserWindow} = require('electron').remote
const path = require('path')

const newWindowBtn = document.getElementById('id-about')

newWindowBtn.addEventListener('click', (event) => {
  const modalPath = path.join('file://', __dirname, '../../sections/windows/about.html')
  let win = new BrowserWindow({ width: 400, height: 320 })

  win.on('close', () => { win = null })
  win.loadURL(modalPath)
  win.show()
})

Hope this helps.希望这可以帮助。 You can find more info in the ElectronJS Docs ( BrowserWindow in particular)您可以在ElectronJS 文档(特别是BrowserWindow )中找到更多信息

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

相关问题 如何在 Electron 中制作多个 Svelte windows? - How to make multiple Svelte windows in Electron? 具有连接到https站点的多个选项卡式浏览器窗口的电子应用程序 - Electron app with multiple tabbed browser windows connecting to https sites 如何在电子的多个窗口之间共享状态(反应)? - How to share state (react)between electron's multiple windows? Electron 应用程序打开多个 windows 或 Spectron 测试中的进程 - Electron app opening multiple windows or processes in spectron test 当使用 webpack 模板使用 electron forge 时,如何为不同的 Electron windows 使用多个预加载脚本? - How can I use multiple preload scripts for different Electron windows when using electron forge with the webpack template? Windows 10上的电子 - Electron on windows 10 Linux 上的透明 Windows (Electron) - Transparent Windows on Linux (Electron) 电子上有多个http页面 - Multiple http pages on electron 当您有多个窗口时,Electron ApplicationMenu 仅适用于最后一个窗口 - Electron ApplicationMenu only works for last window when you have multiple windows 是否可以使用 electron 调整特定外部窗口的大小(当多个 windows 使用相同的 Exe 打开时)? - Is it Possible to resize the specific external window(When multiple windows open with the same Exe) using an electron?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM