@@ -10,6 +10,7 @@ export const ElectronDraggableWindow = ({ children }) => {
1010 const mouseDownEvent = useRef < MouseEvent | null > ( null )
1111 const mouseUpEvent = useRef < MouseEvent | null > ( null )
1212 const target = useRef < EventTarget | null > ( null )
13+ const lastMouseDownTime = useRef < number > ( 0 )
1314
1415 const handleMouseClick = ( e : MouseEvent ) => {
1516 if ( e [ 'pointerId' ] !== - 1 ) {
@@ -28,6 +29,12 @@ export const ElectronDraggableWindow = ({ children }) => {
2829 } ;
2930
3031 const handleMouseDown = ( e : MouseEvent ) => {
32+ const now = Date . now ( ) ;
33+ if ( now - lastMouseDownTime . current < 100 ) {
34+ return ;
35+ }
36+ lastMouseDownTime . current = now ;
37+
3138 if ( ! isDrag . current && ! mouseDownEvent . current ) {
3239 target . current = e . target
3340 passthroughEvent . current = false
@@ -45,16 +52,28 @@ export const ElectronDraggableWindow = ({ children }) => {
4552 mouseUpEvent . current = e
4653 } ;
4754
55+ const handleMouseLeave = ( ) => {
56+ if ( isDrag . current ) {
57+ window . electron . send ( IPC_EVENTS . STOP_DRAG ) ;
58+ isDrag . current = false ;
59+ mouseDownEvent . current = null ;
60+ }
61+ } ;
62+
63+ const handleWindowBlur = ( ) => {
64+ if ( isDrag . current ) {
65+ isDrag . current = false ;
66+ mouseDownEvent . current = null ;
67+ window . electron . send ( IPC_EVENTS . STOP_DRAG ) ;
68+ }
69+ } ;
70+
4871 useEffect ( ( ) => {
49- document . addEventListener ( 'click' , handleMouseClick , {
50- capture : true ,
51- } ) ;
52- document . addEventListener ( 'mousedown' , handleMouseDown , {
53- capture : true ,
54- } ) ;
55- document . addEventListener ( 'mouseup' , handleMouseUp , {
56- capture : true ,
57- } ) ;
72+ document . addEventListener ( 'click' , handleMouseClick , { capture : true } ) ;
73+ document . addEventListener ( 'mousedown' , handleMouseDown , { capture : true } ) ;
74+ document . addEventListener ( 'mouseup' , handleMouseUp , { capture : true } ) ;
75+ window . addEventListener ( 'mouseleave' , handleMouseLeave ) ;
76+ window . addEventListener ( 'blur' , handleWindowBlur ) ;
5877
5978 window . electron . receive ( IPC_EVENTS . ENABLE_CLICK , ( ) => {
6079 if ( target . current && ! passthroughEvent . current ) {
@@ -64,18 +83,19 @@ export const ElectronDraggableWindow = ({ children }) => {
6483 cancelable : true ,
6584 view : window ,
6685 } ) ;
67- //we create an untrusted event, with this property we ensure that this event was created by us
6886 newEvent [ 'nethlink' ] = true
6987 target . current . dispatchEvent ( newEvent ) ;
7088 }
71- } )
89+ } ) ;
7290
7391 return ( ) => {
7492 document . removeEventListener ( 'click' , handleMouseClick ) ;
7593 document . removeEventListener ( 'mousedown' , handleMouseDown ) ;
7694 document . removeEventListener ( 'mouseup' , handleMouseUp ) ;
95+ window . removeEventListener ( 'mouseleave' , handleMouseLeave ) ;
96+ window . removeEventListener ( 'blur' , handleWindowBlur ) ;
7797 } ;
78- } , [ handleMouseUp ] ) ;
98+ } , [ ] ) ;
7999
80100 return (
81101 < div
@@ -84,5 +104,4 @@ export const ElectronDraggableWindow = ({ children }) => {
84104 { children }
85105 </ div >
86106 ) ;
87- } ;
88-
107+ } ;
0 commit comments