electron鼠标经过托盘显示自定义菜单

发布时间 2023-04-24 14:52:09作者: zhang_you_wu

1.自定义菜单

  // 自定义菜单
  var templateMenu = [{
      label: "首页",
      submenu: [{ label: "111" }, { label: '9089' }]
  }, , {
      label: "编辑",
      submenu: [{ label: '123' }]
  }]
  testmenu = Menu.buildFromTemplate(templateMenu);

2. 在鼠标经过托盘事件中展示自定义菜单

//isLeave 是自定义是否鼠标离开字段,默认为true,   tray是托盘
tray.on('mouse-move', () => {
      if (isLeave) {
          //触发mouse-enter
          isLeave = false;
          checkTrayLeave();//检查是否离开
          testmenu.popup();//打开
      }
  })

 3.检查方法

//检查是否离开
function checkTrayLeave(){
    clearInterval(leaveInter)
    leaveInter = setInterval(function(){
        trayBounds = tray.getBounds();
        point = screen.getCursorScreenPoint();
        if(!(trayBounds.x < point.x && trayBounds.y < point.y && point.x < (trayBounds.x + trayBounds.width) && point.y < (trayBounds.y  + trayBounds.height))){//触发mouse-leave
            clearInterval(leaveInter);
            isLeave = true;
            testmenu.closePopup();
        }
    }, 100)
}