From 78c24ce6c5b944351a8112fb7ffe153969a5aa77 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 17 Aug 2016 13:38:16 +0530 Subject: [PATCH] Start work on gesture support for viewer --- src/pyj/read_book/iframe.pyj | 2 + src/pyj/read_book/touch.pyj | 72 ++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/pyj/read_book/touch.pyj diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index 5053cfe0c5..7637078eb1 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -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) diff --git a/src/pyj/read_book/touch.pyj b/src/pyj/read_book/touch.pyj new file mode 100644 index 0000000000..6322de4f71 --- /dev/null +++ b/src/pyj/read_book/touch.pyj @@ -0,0 +1,72 @@ +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2016, Kovid Goyal +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)