diff --git a/src/common/utils/dragWindow.ts b/src/common/utils/dragWindow.ts new file mode 100644 index 0000000..bfc7cd0 --- /dev/null +++ b/src/common/utils/dragWindow.ts @@ -0,0 +1,36 @@ +import { ipcRenderer } from 'electron'; + +const useDrag = () => { + let animationId: number; + let mouseX: number; + let mouseY: number; + let draggable = true; + + const onMouseDown = (e) => { + draggable = true; + mouseX = e.clientX; + mouseY = e.clientY; + document.addEventListener('mouseup', onMouseUp); + animationId = requestAnimationFrame(moveWindow); + }; + + const onMouseUp = () => { + draggable = false; + document.removeEventListener('mouseup', onMouseUp); + cancelAnimationFrame(animationId); + }; + + const moveWindow = () => { + ipcRenderer.send('msg-trigger', { + type: 'windowMoving', + data: { mouseX, mouseY }, + }); + if (draggable) animationId = requestAnimationFrame(moveWindow); + }; + + return { + onMouseDown, + }; +}; + +export default useDrag; diff --git a/src/main/common/api.ts b/src/main/common/api.ts index 3d72a6a..139e4ed 100644 --- a/src/main/common/api.ts +++ b/src/main/common/api.ts @@ -6,6 +6,7 @@ import { Notification, nativeImage, clipboard, + screen, shell, } from 'electron'; import { runner, detach } from '../browsers'; @@ -55,6 +56,13 @@ class API { } }; + public windowMoving({ data: { mouseX, mouseY, width, height } }, window, e) { + const { x, y } = screen.getCursorScreenPoint(); + const originWindow = this.getCurrentWindow(window, e); + if (!originWindow) return; + originWindow.setBounds({ x: x - mouseX, y: y - mouseY }); + } + public loadPlugin({ data: plugin }, window) { window.webContents.executeJavaScript( `window.loadPlugin(${JSON.stringify(plugin)})` diff --git a/src/renderer/App.vue b/src/renderer/App.vue index 092b6bd..6aecbdf 100644 --- a/src/renderer/App.vue +++ b/src/renderer/App.vue @@ -1,30 +1,21 @@