简体   繁体   English

Atom Electron - 使用 javascript 关闭窗口

[英]Atom Electron - Close the window with javascript

I'm using Electron (formerly atom-shell) and would like to have a minimalist frame window so that the three OSX window buttons (close, maximize, minimize) are visible from within the HTML page.我使用的是Electron (原原子壳层),并希望能有一个最低限度的框架窗口使三个OSX窗口按钮(关闭,最大化,最小化)从HTML页面可见。

I set the Electron option frame to false when defining the BrowserWindow to have a chromeless, frameless window.在将BrowserWindow定义为具有无边框、无框架窗口时,我将 Electron 选项frame设置为false

And I thought I could handle the close button with something like this:我想我可以用这样的东西来处理关闭按钮:

<a btn href="#" id="close" onclick="window.top.close(); return false"></a>

With no luck, sadly.没有运气,可悲。 Any idea how to achieve this?知道如何实现这一目标吗?

You must access the BrowserWindow object created by your main process and call the minimize , maximize , and close methods on that.您必须访问由主进程创建的 BrowserWindow 对象,并在其上调用minimizemaximizeclose方法。 You can access this using the remote module.您可以使用remote模块访问它。 Here is an example of binding all three buttons:这是绑定所有三个按钮的示例:

  const remote = require('electron').remote;

  document.getElementById("min-btn").addEventListener("click", function (e) {
       var window = remote.getCurrentWindow();
       window.minimize(); 
  });

  document.getElementById("max-btn").addEventListener("click", function (e) {
       var window = remote.getCurrentWindow();
       if (!window.isMaximized()) {
           window.maximize();          
       } else {
           window.unmaximize();
       }
  });

  document.getElementById("close-btn").addEventListener("click", function (e) {
       var window = remote.getCurrentWindow();
       window.close();
  }); 

assuming your min, max, close buttons have ids of min-btn , max-btn , and close-btn , respectively.假设您的 min、max、close 按钮​​的 ID 分别为min-btnmax-btnclose-btn

You can view the full documentation for the BrowserWindow along with other functionality you might need here: http://electron.atom.io/docs/v0.28.0/api/browser-window/ .您可以在此处查看 BrowserWindow 的完整文档以及您可能需要的其他功能: http ://electron.atom.io/docs/v0.28.0/api/browser-window/。

It might also help you to take a look at a tutorial I wrote about building a chromeless window that looks like Visual Studio here: http://www.mylifeforthecode.com/making-the-electron-shell-as-pretty-as-the-visual-studio-shell .它还可以帮助您查看我写的关于构建一个看起来像 Visual Studio 的无边框窗口的教程: http : //www.mylifeforthecode.com/making-the-electron-shell-as-pretty-as-视觉工作室外壳 Your question is covered along with some css to properly position the buttons.您的问题与一些 css 一起涵盖,以正确定位按钮。

I have declarate my Window:我已经声明了我的窗口:

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

const notifyBtn = document.getElementById('notifyBtn')

notifyBtn.addEventListener('click',function(event){

    const modalPath = path.join('file://', __dirname,'add.html')
    let win = new BrowserWindow({ webPreferences: {nodeIntegration: true}, frame: false, transparent: true, alwaysOnTop:true, width: 400, height: 200 })
    win.on('close',function(){win = null})
    win.loadURL(modalPath)
    win.show()

})

and for close this:并关闭此:

const electron = require('electron')
const path = require('path')
const remote = electron.remote

const closeBtn = document.getElementById('closeBtn')

closeBtn.addEventListener('click', function (event) {
    var window = remote.getCurrentWindow();
    window.close();
})

To answer this question with an alternative technology.用另一种技术来回答这个问题。

What you want to do is trivially easy in NW.js , compared to Electron (as is always the case).与 Electron 相比,您想要在NW.js 中做的事情非常简单(总是如此)。

<a href="#" onclick="nw.Window.get().close()"></a>

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

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