mirror of
https://github.com/rubickCenter/rubick
synced 2025-06-09 12:24:09 +08:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { ipcRenderer } from 'electron';
|
|
|
|
const useDrag = () => {
|
|
let animationId: number;
|
|
let mouseX: number;
|
|
let mouseY: number;
|
|
let clientWidth = 0;
|
|
let clientHeight = 0;
|
|
let draggable = true;
|
|
|
|
const onMouseDown = (e) => {
|
|
// if (commonConst.macOS()) return;
|
|
draggable = true;
|
|
mouseX = e.clientX;
|
|
mouseY = e.clientY;
|
|
if (Math.abs(document.body.clientWidth - clientWidth) > 5) {
|
|
clientWidth = document.body.clientWidth;
|
|
}
|
|
if (Math.abs(document.body.clientHeight - clientHeight) > 5) {
|
|
clientHeight = document.body.clientHeight;
|
|
}
|
|
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, width: clientWidth, height: clientHeight },
|
|
});
|
|
if (draggable) animationId = requestAnimationFrame(moveWindow);
|
|
};
|
|
|
|
return {
|
|
onMouseDown,
|
|
};
|
|
};
|
|
|
|
export default useDrag;
|