Electron.js如何最小化/关闭系统托盘的窗口并从托盘恢复窗口? [英] Electron.js How to minimize/close window to system tray and restore window back from tray?

查看:2124
本文介绍了Electron.js如何最小化/关闭系统托盘的窗口并从托盘恢复窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的 Electron.js 应用程序驻留在系统任务栏上,并且只要用户希望执行某些操作即可他们可以从系统托盘恢复某些操作,并将其最小化/关闭,使其重新回到系统托盘.我该怎么办?

I want my Electron.js application to live on system tray and whenever the user wants to do something they can restore from the system tray do something and minimize/close it back to system tray. How do i do that?

我已经从文档中看到了tray部分,但是对实现我想要的内容并没有太大帮助.

I've seen the tray section from the documentation but doesn't help much to achieve what i want.

这是我到目前为止在main.js文件上得到的内容

Here is what i got so far on the main.js file

var application = require('app'),
    BrowserWindow = require('browser-window'),
    Menu = require('menu'), 
    Tray = require('tray'); 
application.on('ready', function () {
    var mainWindow = new BrowserWindow({
        width: 650,
        height: 450,
        'min-width': 500,
        'min-height': 200,
        'accept-first-mouse': true,
        // 'title-bar-style': 'hidden',
        icon:'./icon.png'
    });
    mainWindow.loadUrl('file://' + __dirname + '/src/index.html');
    mainWindow.on('closed', function () {
        mainWindow = null;
    });
    mainWindow.setMenu(null);

    var appIcon = null;
    appIcon = new Tray('./icon-resized.png');
    var contextMenu = Menu.buildFromTemplate([
        { label: 'Restore', type: 'radio' }
    ]);
    appIcon.setToolTip('Electron.js App');
    appIcon.setContextMenu(contextMenu);
});

更新:

我发现了这个菜单回购,但是在Linux上无法正常工作.

I found this menubar repo, but it won't work as expected on linux.

推荐答案

我很久以前就已经弄清楚了,但是对于遇到相同问题的人来说,这是一种实现最小化到tray并从<恢复的方法c0>.诀窍是捕获closeminimize事件.

I actually figured it out a long time ago but for folks who encounter the same problem here is one way you could achieve minimizing to tray and restoring from tray. The trick is to catch the close and minimize events.

var BrowserWindow = require('browser-window'),

var mainWindow = new BrowserWindow({
    width: 850,
    height: 450,
    title: "TEST",
    icon:'./icon.png'
});

mainWindow.on('minimize',function(event){
    event.preventDefault();
    mainWindow.hide();
});

mainWindow.on('close', function (event) {
    if(!application.isQuiting){
        event.preventDefault();
        mainWindow.hide();
    }

    return false;
});

并从Tray

var contextMenu = Menu.buildFromTemplate([
    { label: 'Show App', click:  function(){
        mainWindow.show();
    } },
    { label: 'Quit', click:  function(){
        application.isQuiting = true;
        application.quit();
    } }
]);

这篇关于Electron.js如何最小化/关闭系统托盘的窗口并从托盘恢复窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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