electron BrowserWindow对象在mac平台下实现moved事件

25

问题:

mac下的moved事件等同于move,窗体没有一个”拖动完成“的事件钩子。

解决思路:

1.当窗体的will-move发生后,在render中监听鼠标抬起的动作,通过ipc传递回来。(很麻烦)

2.治标不治本的方法,使用位置判断,x毫秒内鼠标位置没变化,则视为抬起,moved事件完成

    if(process.platform === 'win32'){
      //win下窗口移动时
      newWindow.on('moved',movedHandler)
    }else if(process.platform === 'darwin'){
       //mac下窗口移动时
       newWindow.on('will-move',()=>{
        let moveEndTimer: NodeJS.Timeout | null = null;
        let lastPosition = { x: 0, y: 0 };
        moveEndTimer = setInterval(() => {
          const currentBounds = newWindow.getBounds();
          if (lastPosition.x === currentBounds.x && lastPosition.y === currentBounds.y) {
            //位置没变化
            clearInterval(moveEndTimer);
            moveEndTimer = null;
            movedHandler()
          }
          lastPosition = { x: currentBounds.x, y: currentBounds.y };
        }, 200);
      })
    }