UNPKG

@icenet/electron-window

Version:

主要针对使用 electron-vite 初始化的项目做的插件,点击[这里](https://cn-evite.netlify.app/),必在 electron-vite 的基础上使用,针对 electron-vite 中打开新的无边框窗口缓慢、传参困难等问题做的优化,安装方式简单、使用简单

72 lines (68 loc) 3.23 kB
"use strict"; const { BrowserWindow, ipcMain } = require("electron"); // 窗口移动 位置刷新定时器 let movingInterval; /** * @desc 拖动当前窗口 * @param {"dragWindow"} type 拖动当前窗口的事件key */ ipcMain.handle("dragWindow", (_, { type, opts }) => { switch (type) { // 监听渲染进程发送的拖动窗口事件 case "dragWindow": let winStartPosition = { x: 0, y: 0 }; let mouseStartPosition = { x: 0, y: 0 }; // 获取当前聚焦的窗口 const currentWindow = BrowserWindow.getFocusedWindow(); if (currentWindow) { const currentWindowSize = opts.size ? opts.size : currentWindow.getSize(); if (opts.isMove) { // 读取原位置 const winPosition = currentWindow.getPosition(); winStartPosition = { x: winPosition[0], y: winPosition[1] }; mouseStartPosition = screen.getCursorScreenPoint(); // 清除旧的定时器 if (movingInterval) { clearInterval(movingInterval); } // 创建定时器,每10毫秒更新一次窗口位置,保证一致 movingInterval = setInterval(() => { // 窗口销毁判断,高频率的更新有可能窗口已销毁,定时器还没结束,此时就会出现执行销毁窗口方法的错误 if (!currentWindow.isDestroyed()) { // 如果窗口失去焦点,则停止移动 if (!currentWindow.isFocused()) { clearInterval(movingInterval); movingInterval = null; } // 实时更新位置 const cursorPosition = screen.getCursorScreenPoint(); const x = winStartPosition.x + cursorPosition.x - mouseStartPosition.x; const y = winStartPosition.y + cursorPosition.y - mouseStartPosition.y; // 更新位置的同时设置窗口原大小, windows上设置=>显示设置=>文本缩放 不是100%时,窗口会拖拽放大 currentWindow.setBounds({ x: x, y: y, width: currentWindowSize[0], height: currentWindowSize[1], }); } }, 10); } else { clearInterval(movingInterval); movingInterval = null; } } return; default: return; } });