11const { contextBridge, ipcRenderer } = require ( 'electron' ) ;
22const { createMouseEvent, sendMouseEvent } = require ( '../../lib/utils.js' )
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' )
35
4- const LONG_PRESS_DELAY = 500 ; // longpress duration in ms
5- var longPressTimer = null ;
6+ class TouchUtils_preload extends require ( '../../lib/BasePreload.js' )
7+ {
8+ LONG_PRESS_DELAY = 500 ; // longpress duration in ms
9+ longPressTimer = null ;
610
7- var lastTouchedObject = null ; // necessary for doubletap
8- var touchTime = 0 ;
11+ lastTouchedObject = null ; // necessary for doubletap
12+ touchTime = 0 ;
913
10- const fs = require ( 'fs' ) ;
11- const path = require ( 'path' )
12- const bubbleStyle = fs . readFileSync ( path . join ( __dirname , './bubble.html' ) ) ;
13- console . log ( "Bubble style:" , bubbleStyle ) ;
14+ start ( )
15+ {
16+ bubbleStyle = fs . readFileSync ( path . join ( __dirname , './bubble.html' ) ) ;
17+ console . log ( "Bubble style:" , bubbleStyle ) ;
1418
15- // detect long press (trigger left-click + contextmenu) and
16- // detect touched item for events sent from main
17- window . addEventListener ( 'pointerdown' , ( e , input ) => {
18- // Start timer on mouse down
19- if ( ! longPressTimer )
20- longPressTimer = setTimeout ( ( ) => {
21- // Trigger long press event after delay
22- console . log ( "long pressss!!!" ) ;
23- sendMouseEvent ( 'contextmenu' , e ) ;
24- sendMouseEvent ( 'pointerdown' , e ) ;
25- } , LONG_PRESS_DELAY ) ;
19+ // detect long press (trigger left-click + contextmenu) and
20+ // detect touched item for events sent from main
21+ window . addEventListener ( 'pointerdown' , ( e , input ) => {
22+ // Start timer on mouse down
23+ if ( ! longPressTimer )
24+ longPressTimer = setTimeout ( ( ) => {
25+ // Trigger long press event after delay
26+ console . log ( "long pressss!!!" ) ;
27+ sendMouseEvent ( 'contextmenu' , e ) ;
28+ sendMouseEvent ( 'pointerdown' , e ) ;
29+ } , LONG_PRESS_DELAY ) ;
2630
27- lastTouchedObject = e . target ;
28- touchTime = Date . now ( ) ;
29-
31+ lastTouchedObject = e . target ;
32+ touchTime = Date . now ( ) ;
33+
3034
31- // Summon Bubble when screen is touched
32- const bubble = document . createElement ( "span" ) ;
33- bubble . classList . add ( "bubble" ) ;
34- bubble . style . left = `${ e . clientX - bubble . style . width } px` ;
35- bubble . style . top = `${ e . clientY - bubble . style . height } px` ;
35+ // Summon Bubble when screen is touched
36+ const bubble = document . createElement ( "span" ) ;
37+ bubble . classList . add ( "bubble" ) ;
38+ bubble . style . left = `${ e . clientX - bubble . style . width } px` ;
39+ bubble . style . top = `${ e . clientY - bubble . style . height } px` ;
3640
37- const style = document . createElement ( "style" ) ;
38- style . textContent = bubbleStyle ;
39- document . head . appendChild ( style ) ;
40- document . body . appendChild ( bubble ) ;
41+ const style = document . createElement ( "style" ) ;
42+ style . textContent = bubbleStyle ;
43+ document . head . appendChild ( style ) ;
44+ document . body . appendChild ( bubble ) ;
4145
42- // remove after animation ends
43- bubble . addEventListener ( "animationend" , ( ) => {
44- bubble . remove ( ) ;
45- } ) ;
46- } ) ;
46+ // remove after animation ends
47+ bubble . addEventListener ( "animationend" , ( ) => {
48+ bubble . remove ( ) ;
49+ } ) ;
50+ } ) ;
4751
48- // same
49- window . addEventListener ( 'pointerup' , ( event ) => {
50- // Cancel timer on mouseup
51- if ( longPressTimer ) {
52- clearTimeout ( longPressTimer ) ;
53- longPressTimer = null ;
52+ // same
53+ window . addEventListener ( 'pointerup' , ( event ) => {
54+ // Cancel timer on mouseup
55+ if ( longPressTimer ) {
56+ clearTimeout ( longPressTimer ) ;
57+ longPressTimer = null ;
58+ }
59+ } ) ;
60+
61+ // radial double-click signal forward
62+ ipcRenderer . on ( 'double-click2' , function ( e , pos )
63+ {
64+ var dbc_event = e ;
65+ dbc_event . target = lastTouchedObject ;
66+ dbc_event . clientX = pos . x ;
67+ dbc_event . clientY = pos . x ;
68+ const dblClickEvent = createMouseEvent ( 'dblclick' , dbc_event ) ;
69+ // console.log("###lastItem:", lastTouchedObject);
70+ if ( lastTouchedObject && Date . now ( ) < touchTime + 1000 )
71+ lastTouchedObject . dispatchEvent ( dblClickEvent ) ;
72+ else
73+ document . dispatchEvent ( dblClickEvent ) ;
74+ } ) ;
5475 }
55- } ) ;
56-
57- // radial double-click signal forward
58- ipcRenderer . on ( 'double-click2' , function ( e , pos )
59- {
60- var dbc_event = e ;
61- dbc_event . target = lastTouchedObject ;
62- dbc_event . clientX = pos . x ;
63- dbc_event . clientY = pos . x ;
64- const dblClickEvent = createMouseEvent ( 'dblclick' , dbc_event ) ;
65- // console.log("###lastItem:", lastTouchedObject);
66- if ( lastTouchedObject && Date . now ( ) < touchTime + 1000 )
67- lastTouchedObject . dispatchEvent ( dblClickEvent ) ;
68- else
69- document . dispatchEvent ( dblClickEvent ) ;
70- } ) ;
76+ }
77+
78+ module . exports = TouchUtils_preload
0 commit comments