Electron系统托盘,托盘右键菜单、托盘图标闪烁 点击右上角关闭按钮隐藏到托盘

ipcmain.js

var {Menu,Tray,BrowserWindow,app}=require('electron');

var path=require('path');


var iconTray=new Tray(path.join(__dirname,'../static/lover.png'));



//绑定右键菜单

var trayMenu=Menu.buildFromTemplate([

    {

      label:'设置',
      click:function(){

        console.log('setting')
      }
    },
    {

        label:'升级',
        click:function(){
  
          console.log('update')
        }
      },
    {

        label:'退出',
        click:function(){
            if (process.platform !== 'darwin') {
                app.quit();
              }
        }
    }
]);

iconTray.setContextMenu(trayMenu);

iconTray.setToolTip('electron应用');
//实现点击关闭按钮让应用保存在托盘里面 ,双击托盘打开应用


var win=BrowserWindow.getFocusedWindow();


win.on('close',(e)=>{


        console.log(win.isFocused());

        if(!win.isFocused()){
                win=null;
        }else{
            e.preventDefault();  //阻止窗口的关闭事件
            win.hide();

        }
    
})




//监听任务栏图标的点击事件
iconTray.on('double-click',function(){
   
    win.show();
})





//闪烁图标

var count=0;

var timer=setInterval(function(){
    count++;

    if(count%2==0){

         iconTray.setImage(path.join(__dirname,'../static/lover.png'))

    }else{

        iconTray.setImage(path.join(__dirname,'../static/empty.ico'))
    }

},500)

 

posted on 2020-12-31 14:31  LoaderMan  阅读(1483)  评论(0编辑  收藏  举报

导航