Start work on gesture support for viewer

This commit is contained in:
Kovid Goyal 2016-08-17 13:38:16 +05:30
parent b1bd22b20e
commit 78c24ce6c5
2 changed files with 74 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from read_book.paged_mode import (
jump_to_cfi as paged_jump_to_cfi
)
from read_book.settings import apply_settings
from read_book.touch import create_handlers as create_touch_handlers
from utils import debounce
FORCE_FLOW_MODE = False
@ -70,6 +71,7 @@ class IframeBoss:
if data.translations:
install(data.translations)
window.onerror = self.onerror
create_touch_handlers()
def onerror(self, msg, script_url, line_number, column_number, error_object):
console.log(error_object)

View File

@ -0,0 +1,72 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from __python__ import hash_literals, bound_methods
def copy_touch(t):
return {'identifier':t.identifier, 'page_x':v'[t.pageX]', 'page_y':v'[t.pageY]', 'viewport_x':v'[t.clientX]', 'viewport_y':v'[t.clientY]',
'active':True, 'mtime':window.performance.now()}
class TouchHandler:
def __init__(self):
self.ongoing_touches = {}
@property
def has_active_touches(self):
now = window.performance.now()
expired = v'[]'
ans = False
for t in self.ongoing_touches:
if t.active:
if now - t.mtime > 3000:
expired.push(t.identifier)
ans = True
break
for tid in expired:
v'delete self.ongoing_touches[tid]'
return ans
def handle_touchstart(self, ev):
ev.preventDefault(), ev.stopPropagation()
for touch in ev.changedTouches:
self.ongoing_touches[touch.identifier] = copy_touch(touch)
def update_touch(self, t, touch):
t.mtime = window.performance.now()
t.page_x.push(touch.pageX), t.page_y.push(touch.pageY)
t.viewport_x.push(touch.clientX), t.viewport_y.push(touch.clientY)
def handle_touchmove(self, ev):
ev.preventDefault(), ev.stopPropagation()
for touch in ev.changedTouches:
t = self.ongoing_touches[touch.identifier]
if t:
self.update_touch(t, touch)
def handle_touchend(self, ev):
ev.preventDefault(), ev.stopPropagation()
for touch in ev.changedTouches:
t = self.ongoing_touches[touch.identifier]
if t:
t.active = False
self.update_touch(t, touch)
if not self.has_active_touches:
touches, self.ongoing_touches = self.ongoing_touches, {}
self.interpret_gesture(touches)
def handle_touchcancel(self, ev):
ev.preventDefault(), ev.stopPropagation()
for touch in ev.changedTouches:
v'delete self.ongoing_touches[touch.identifier]'
def interpret_gesture(self, touches):
print(1111111, touches)
touch_handler = TouchHandler()
def create_handlers():
document.body.addEventListener('touchstart', touch_handler.handle_touchstart, True)
document.body.addEventListener('touchmove', touch_handler.handle_touchmove, True)
document.body.addEventListener('touchend', touch_handler.handle_touchend, True)
document.body.addEventListener('touchcancel', touch_handler.handle_touchcancel, True)