Skip to content

Commit 06b4d8f

Browse files
committed
Support for touch events
1 parent 7082d5e commit 06b4d8f

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

webview/js/customize.js

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,68 @@
4141
var initialX = 0;
4242
var initialY = 0;
4343

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;
4767
window.pywebview._jsApiCallback('pywebviewMoveWindow', [x, y], 'move');
68+
69+
if (ev.type.startsWith('touch')) {
70+
ev.preventDefault();
71+
}
4872
}
4973

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);
5379
}
5480

55-
function onMouseDown(ev) {
81+
function onStart(ev) {
5682
if (
5783
'%(drag_region_direct_target_only)s' === 'True' &&
5884
!ev.target.matches('%(drag_selector)s')
5985
) {
6086
return
6187
}
6288

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});
67102
}
68103

69-
function onBodyMouseDown(event) {
104+
// Unified body handler for drag selector
105+
function onBodyStart(event) {
70106
var target = event.target;
71107
var dragSelectorElements = document.querySelectorAll('%(drag_selector)s');
72108

@@ -75,7 +111,7 @@
75111
// Check if target matches the drag selector
76112
for (var i = 0; i < dragSelectorElements.length; i++) {
77113
if (dragSelectorElements[i] === target) {
78-
onMouseDown(event);
114+
onStart(event);
79115
return;
80116
}
81117
}
@@ -86,11 +122,13 @@
86122
}
87123
}
88124

89-
document.body.addEventListener('mousedown', onBodyMouseDown);
125+
document.body.addEventListener('mousedown', onBodyStart);
126+
document.body.addEventListener('touchstart', onBodyStart);
90127

91-
// easy drag for edge chromium
128+
// easy drag for edge chromium
92129
if ('%(easy_drag)s' === 'True') {
93-
window.addEventListener('mousedown', onMouseDown);
130+
window.addEventListener('mousedown', onStart);
131+
window.addEventListener('touchstart', onStart);
94132
}
95133

96134
if ('%(zoomable)s' === 'False') {

0 commit comments

Comments
 (0)