Fix tapping causing multiple page turns

Only respond to taps once they end. Also recognize double-tap and pinch
gestures.
This commit is contained in:
Kovid Goyal 2016-09-28 08:20:45 +05:30
parent ec86b6287e
commit 9310a3c76e

View File

@ -7,7 +7,7 @@ from read_book.globals import get_boss
HOLD_THRESHOLD = 750 # milliseconds
TAP_THRESHOLD = 5 # pixels
TAP_LINK_THRESHOLD = 5 # pixels
PINCH_THRESHOLD = 10 # pixels
gesture_id = 0
@ -58,6 +58,28 @@ def interpret_single_gesture(touch, gesture_id):
return ans
return ans
def interpret_double_gesture(touch1, touch2, gesture_id):
ans = {'active':touch1.active or touch2.active, 'is_held':touch1.is_held or touch2.is_held, 'id':gesture_id}
max_x_displacement1 = max_displacement(touch1.viewport_x)
max_x_displacement2 = max_displacement(touch2.viewport_x)
max_y_displacement1 = max_displacement(touch1.viewport_y)
max_y_displacement2 = max_displacement(touch2.viewport_y)
if max(max_x_displacement1, max_y_displacement1) < TAP_THRESHOLD and max(max_x_displacement2, max_y_displacement2) < TAP_THRESHOLD:
ans.type = 'double-tap'
ans.viewport_x1 = touch1.viewport_x[0]
ans.viewport_y1 = touch1.viewport_y[0]
ans.viewport_x2 = touch2.viewport_x[0]
ans.viewport_y2 = touch2.viewport_y[0]
return ans
initial_distance = Math.sqrt((touch1.viewport_x[0] - touch2.viewport_x[0])**2 + (touch1.viewport_y[0] - touch2.viewport_y[0])**2)
final_distance = Math.sqrt((touch1.viewport_x[-1] - touch2.viewport_x[-1])**2 + (touch1.viewport_y[-1] - touch2.viewport_y[-1])**2)
distance = abs(final_distance - initial_distance)
if distance > PINCH_THRESHOLD:
ans.type = 'pinch'
ans.direction = 'in' if final_distance < initial_distance else 'out'
ans.distance = distance
return ans
return ans
def element_from_point(x, y):
# This does not currently support detecting links inside iframes, but since
@ -188,8 +210,12 @@ class TouchHandler:
gesture = {}
if num is 1:
gesture = interpret_single_gesture(touches[Object.keys(touches)[0]], self.gesture_id)
elif num is 2:
t = Object.keys(touches)
gesture = interpret_double_gesture(touches[t[0]], touches[t[1]], self.gesture_id)
if not gesture?.type:
return
if gesture.type is 'tap':
if gesture.is_held:
if not self.handled_tap_hold:
@ -202,14 +228,21 @@ class TouchHandler:
if elem:
elem.dispatchEvent(fake_click)
return
if not tap_on_link(gesture):
if gesture.viewport_y < min(100, window.innerHeight / 4):
gesture.type = 'show-chrome'
else:
if gesture.viewport_x < min(100, window.innerWidth / 4):
gesture.type = 'prev-page'
if not gesture.active:
if not tap_on_link(gesture):
if gesture.viewport_y < min(100, window.innerHeight / 4):
gesture.type = 'show-chrome'
else:
gesture.type = 'next-page'
if gesture.viewport_x < min(100, window.innerWidth / 4):
gesture.type = 'prev-page'
else:
gesture.type = 'next-page'
if gesture.type is 'pinch':
if gesture.active:
return
if gesture.type is 'double-tap':
if gesture.active:
return
get_boss().handle_gesture(gesture)
touch_handler = TouchHandler()