Atom Electron - 用javascript关闭窗口 [英] Atom Electron - Close the window with javascript

查看:94
本文介绍了Atom Electron - 用javascript关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Electron (以前称为atom-shell)并希望拥有一个简约的框架窗口,以便三个OSX窗口按钮(关闭,最大化,最小化)可以从HTML页面的 中看到。

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.

我将电子选项 frame 设置为定义 false browserwindowoptionsrel =nofollow noreferrer> BrowserWindow 有一个无边框的无框窗口。

I set the Electron option frame to false when defining the BrowserWindow to have a chromeless, frameless window.

我想我可以用这样的方式处理关闭按钮:

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?

推荐答案

您必须访问主进程创建的BrowserWindow对象并调用最小化最大化,以及关闭方法。您可以使用远程模块访问它。以下是绑定所有三个按钮的示例:

You must access the BrowserWindow object created by your main process and call the minimize, maximize, and close methods on that. You can access this using the remote module. 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();
  }); 

假设您的最小,最大,关闭按钮的ID为 min-btn max-btn close-btn

assuming your min, max, close buttons have ids of min-btn, max-btn, and close-btn, respectively.

您可以在此处查看BrowserWindow的完整文档以及您可能需要的其他功能: http://electron.atom.io/docs/v0.28.0/api/browser-window/

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/.

它也可以帮助你看一下我写的关于构建一个看起来像Visual Studio的无格式窗口的教程: http://www.mylifeforthecode.com/making-the-electron-壳作为-漂亮-AS-的视觉工作室壳。你的问题与一些css一起被覆盖,以正确定位按钮。

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. Your question is covered along with some css to properly position the buttons.

这篇关于Atom Electron - 用javascript关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆