electron关闭最小化,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?

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

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);

});

UPDATE:

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

解决方案

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;

});

and restoring from Tray

var contextMenu = Menu.buildFromTemplate([

{ label: 'Show App', click: function(){

mainWindow.show();

} },

{ label: 'Quit', click: function(){

application.isQuiting = true;

application.quit();

} }

]);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值