|
41 | 41 | var initialX = 0; |
42 | 42 | var initialY = 0; |
43 | 43 |
|
44 | | - function onMouseMove(ev) { |
45 | | - var x = ev.screenX - initialX; |
46 | | - var y = ev.screenY - initialY; |
| 44 | + // Extract coordinates from mouse or touch event |
| 45 | + function getEventCoords(ev) { |
| 46 | + if (ev.type.startsWith('touch')) { |
| 47 | + var touch = ev.touches[0] || ev.changedTouches[0]; |
| 48 | + return { |
| 49 | + clientX: touch.clientX, |
| 50 | + clientY: touch.clientY, |
| 51 | + screenX: touch.screenX, |
| 52 | + screenY: touch.screenY |
| 53 | + }; |
| 54 | + } |
| 55 | + return { |
| 56 | + clientX: ev.clientX, |
| 57 | + clientY: ev.clientY, |
| 58 | + screenX: ev.screenX, |
| 59 | + screenY: ev.screenY |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + function onMove(ev) { |
| 64 | + var coords = getEventCoords(ev); |
| 65 | + var x = coords.screenX - initialX; |
| 66 | + var y = coords.screenY - initialY; |
47 | 67 | window.pywebview._jsApiCallback('pywebviewMoveWindow', [x, y], 'move'); |
| 68 | + |
| 69 | + if (ev.type.startsWith('touch')) { |
| 70 | + ev.preventDefault(); |
| 71 | + } |
48 | 72 | } |
49 | 73 |
|
50 | | - function onMouseUp() { |
51 | | - window.removeEventListener('mousemove', onMouseMove); |
52 | | - window.removeEventListener('mouseup', onMouseUp); |
| 74 | + function onEnd() { |
| 75 | + window.removeEventListener('mousemove', onMove); |
| 76 | + window.removeEventListener('mouseup', onEnd); |
| 77 | + window.removeEventListener('touchmove', onMove); |
| 78 | + window.removeEventListener('touchend', onEnd); |
53 | 79 | } |
54 | 80 |
|
55 | | - function onMouseDown(ev) { |
| 81 | + function onStart(ev) { |
56 | 82 | if ( |
57 | 83 | '%(drag_region_direct_target_only)s' === 'True' && |
58 | 84 | !ev.target.matches('%(drag_selector)s') |
59 | 85 | ) { |
60 | 86 | return |
61 | 87 | } |
62 | 88 |
|
63 | | - initialX = ev.clientX; |
64 | | - initialY = ev.clientY; |
65 | | - window.addEventListener('mouseup', onMouseUp); |
66 | | - window.addEventListener('mousemove', onMouseMove); |
| 89 | + // Only handle single-touch events |
| 90 | + if (ev.type.startsWith('touch') && ev.touches.length !== 1) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + var coords = getEventCoords(ev); |
| 95 | + initialX = coords.clientX; |
| 96 | + initialY = coords.clientY; |
| 97 | + |
| 98 | + window.addEventListener('mouseup', onEnd); |
| 99 | + window.addEventListener('mousemove', onMove); |
| 100 | + window.addEventListener('touchend', onEnd); |
| 101 | + window.addEventListener('touchmove', onMove, {passive: false}); |
67 | 102 | } |
68 | 103 |
|
69 | | - function onBodyMouseDown(event) { |
| 104 | + // Unified body handler for drag selector |
| 105 | + function onBodyStart(event) { |
70 | 106 | var target = event.target; |
71 | 107 | var dragSelectorElements = document.querySelectorAll('%(drag_selector)s'); |
72 | 108 |
|
|
75 | 111 | // Check if target matches the drag selector |
76 | 112 | for (var i = 0; i < dragSelectorElements.length; i++) { |
77 | 113 | if (dragSelectorElements[i] === target) { |
78 | | - onMouseDown(event); |
| 114 | + onStart(event); |
79 | 115 | return; |
80 | 116 | } |
81 | 117 | } |
|
86 | 122 | } |
87 | 123 | } |
88 | 124 |
|
89 | | - document.body.addEventListener('mousedown', onBodyMouseDown); |
| 125 | + document.body.addEventListener('mousedown', onBodyStart); |
| 126 | + document.body.addEventListener('touchstart', onBodyStart); |
90 | 127 |
|
91 | | - // easy drag for edge chromium |
| 128 | + // easy drag for edge chromium |
92 | 129 | if ('%(easy_drag)s' === 'True') { |
93 | | - window.addEventListener('mousedown', onMouseDown); |
| 130 | + window.addEventListener('mousedown', onStart); |
| 131 | + window.addEventListener('touchstart', onStart); |
94 | 132 | } |
95 | 133 |
|
96 | 134 | if ('%(zoomable)s' === 'False') { |
|
0 commit comments