From eeca0a8489c6473fcc107b01ed371baededa912d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 27 May 2017 21:32:43 +0530 Subject: [PATCH] Abstract out window.scrollTo --- src/pyj/read_book/cfi.pyj | 2 +- src/pyj/read_book/flow_mode.pyj | 10 +++++----- src/pyj/read_book/iframe.pyj | 33 +++++++++++++++++--------------- src/pyj/read_book/paged_mode.pyj | 6 +++--- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/pyj/read_book/cfi.pyj b/src/pyj/read_book/cfi.pyj index 4b4bfb5f5b..dd61d28e8f 100644 --- a/src/pyj/read_book/cfi.pyj +++ b/src/pyj/read_book/cfi.pyj @@ -491,7 +491,7 @@ def scroll_to(cfi, callback, doc): # {{{ x += (point_.x*node.offsetWidth)/100 if jstype(point_.y) is 'number' and node.offsetHeight: y += (point_.y*node.offsetHeight)/100 - window.scrollTo(x, y) + scroll_viewport.scroll_to(x, y) if callback: callback(x, y) diff --git a/src/pyj/read_book/flow_mode.pyj b/src/pyj/read_book/flow_mode.pyj index cd51ea5c3c..2ae68e08c7 100644 --- a/src/pyj/read_book/flow_mode.pyj +++ b/src/pyj/read_book/flow_mode.pyj @@ -8,7 +8,7 @@ from keycodes import get_key from utils import document_height, document_width, viewport_to_document def flow_to_scroll_fraction(frac): - window.scrollTo(0, document_height() * frac) + scroll_viewport.scroll_to(0, document_height() * frac) def check_for_scroll_end(func): return def (): @@ -59,7 +59,7 @@ def smooth_y_scroll(up): @check_for_scroll_end def goto_boundary(y): - window.scrollTo(window.pageXOffset, 0) + scroll_viewport.scroll_to(window.pageXOffset, 0) @check_for_scroll_end def scroll_by_page(backward): @@ -78,7 +78,7 @@ def flow_onkeydown(evt): elif (key is 'left' or key is 'right') and not evt.altKey: handled = True if evt.ctrlKey: - window.scrollTo(0 if key is 'left' else document_width(), window.pageYOffset) + scroll_viewport.scroll_to(0 if key is 'left' else document_width(), window.pageYOffset) else: window.scrollBy(-15 if key is 'left' else 15, 0) elif key is 'home' or key is 'end': @@ -87,9 +87,9 @@ def flow_onkeydown(evt): get_boss().send_message('goto_doc_boundary', start=key is 'home') else: if key is 'home': - window.scrollTo(window.pageXOffset, 0) + scroll_viewport.scroll_to(window.pageXOffset, 0) else: - window.scrollTo(window.pageXOffset, document_height()) + scroll_viewport.scroll_to(window.pageXOffset, document_height()) elif key is 'pageup' or key is 'pagedown' or key is 'space': handled = True scroll_by_page(key is 'pageup') diff --git a/src/pyj/read_book/iframe.pyj b/src/pyj/read_book/iframe.pyj index a6911ea549..19dc220db8 100644 --- a/src/pyj/read_book/iframe.pyj +++ b/src/pyj/read_book/iframe.pyj @@ -4,28 +4,31 @@ from __python__ import bound_methods, hash_literals import traceback from aes import GCM -from gettext import install, gettext as _ -from utils import html_escape +from gettext import gettext as _, install from read_book.cfi import at_current, scroll_to as scroll_to_cfi -from read_book.globals import set_boss, set_current_spine_item, current_layout_mode, current_spine_item, set_layout_mode, current_book, window_width, window_height -from read_book.mathjax import apply_mathjax -from read_book.toc import update_visible_toc_anchors -from read_book.resources import finalize_resources, unserialize_html from read_book.flow_mode import ( - flow_to_scroll_fraction, flow_onwheel, flow_onkeydown, layout as flow_layout, handle_gesture as flow_handle_gesture, - scroll_by_page as flow_scroll_by_page, anchor_funcs as flow_anchor_funcs + anchor_funcs as flow_anchor_funcs, flow_onkeydown, flow_onwheel, + flow_to_scroll_fraction, handle_gesture as flow_handle_gesture, + layout as flow_layout, scroll_by_page as flow_scroll_by_page ) +from read_book.globals import ( + current_book, current_layout_mode, current_spine_item, scroll_viewport, set_boss, + set_current_spine_item, set_layout_mode, window_height, window_width +) +from read_book.mathjax import apply_mathjax from read_book.paged_mode import ( - layout as paged_layout, scroll_to_fraction as paged_scroll_to_fraction, - onwheel as paged_onwheel, onkeydown as paged_onkeydown, scroll_to_elem, - jump_to_cfi as paged_jump_to_cfi, handle_gesture as paged_handle_gesture, - scroll_by_page as paged_scroll_by_page, anchor_funcs as paged_anchor_funcs, - snap_to_selection, reset_paged_mode_globals, progress_frac + anchor_funcs as paged_anchor_funcs, handle_gesture as paged_handle_gesture, + jump_to_cfi as paged_jump_to_cfi, layout as paged_layout, + onkeydown as paged_onkeydown, onwheel as paged_onwheel, progress_frac, + reset_paged_mode_globals, scroll_by_page as paged_scroll_by_page, scroll_to_elem, + scroll_to_fraction as paged_scroll_to_fraction, snap_to_selection ) +from read_book.resources import finalize_resources, unserialize_html from read_book.settings import apply_settings, opts +from read_book.toc import update_visible_toc_anchors from read_book.touch import create_handlers as create_touch_handlers -from utils import debounce, is_ios +from utils import debounce, html_escape, is_ios FORCE_FLOW_MODE = False CALIBRE_VERSION = '__CALIBRE_VERSION__' @@ -379,7 +382,7 @@ class IframeBoss: if elem: scroll_to_elem(elem) else: - window.scrollTo(0, 0) + scroll_viewport.scroll_to(0, 0) def find(self, data, from_load): if data.searched_in_spine: diff --git a/src/pyj/read_book/paged_mode.pyj b/src/pyj/read_book/paged_mode.pyj index 76e51987e5..77fdfaeb48 100644 --- a/src/pyj/read_book/paged_mode.pyj +++ b/src/pyj/read_book/paged_mode.pyj @@ -223,11 +223,11 @@ def layout(is_single_page): return gap def current_scroll_offset(): - return window.pageXOffset + return scroll_viewport.x() def scroll_to_offset(x): - window.scrollTo(x, 0) + scroll_viewport.scroll_to(x, 0) def scroll_to_column(number, notify=False, duration=1000): @@ -381,7 +381,7 @@ def jump_to_cfi(cfi): if in_paged_mode: scroll_to_xpos(x) else: - window.scrollTo(0, y) + scroll_viewport.scroll_to(0, y) ) def current_cfi():