| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from dom import set_css |
| 6 |
from range_utils import wrap_range, unwrap |
| 7 |
from read_book.cfi import scroll_to as cfi_scroll_to |
| 8 |
from read_book.globals import current_spine_item, get_boss, rtl_page_progression, ltr_page_progression |
| 9 |
from read_book.settings import opts |
| 10 |
from read_book.viewport import line_height, rem_size, scroll_viewport |
| 11 |
|
| 12 |
|
| 13 |
def flow_to_scroll_fraction(frac, on_initial_load): |
| 14 |
scroll_viewport.scroll_to_in_block_direction(scroll_viewport.document_block_size() * frac) |
| 15 |
|
| 16 |
|
| 17 |
small_scroll_events = [] |
| 18 |
|
| 19 |
|
| 20 |
def clear_small_scrolls(): |
| 21 |
nonlocal small_scroll_events |
| 22 |
small_scroll_events = [] |
| 23 |
|
| 24 |
|
| 25 |
def dispatch_small_scrolls(): |
| 26 |
if small_scroll_events.length: |
| 27 |
now = window.performance.now() |
| 28 |
if now - small_scroll_events[-1].time <= 2000: |
| 29 |
window.setTimeout(dispatch_small_scrolls, 100) |
| 30 |
return |
| 31 |
amt = 0 |
| 32 |
for x in small_scroll_events: |
| 33 |
amt += x.amt |
| 34 |
clear_small_scrolls() |
| 35 |
get_boss().report_human_scroll(amt / scroll_viewport.document_block_size()) |
| 36 |
|
| 37 |
|
| 38 |
def add_small_scroll(amt): |
| 39 |
small_scroll_events.push({'amt': amt, 'time': window.performance.now()}) |
| 40 |
window.setTimeout(dispatch_small_scrolls, 100) |
| 41 |
|
| 42 |
|
| 43 |
def report_human_scroll(amt): |
| 44 |
h = scroll_viewport.height() |
| 45 |
is_large_scroll = (abs(amt) / h) >= 0.5 |
| 46 |
if amt > 0: |
| 47 |
if is_large_scroll: |
| 48 |
clear_small_scrolls() |
| 49 |
get_boss().report_human_scroll(amt / scroll_viewport.document_block_size()) |
| 50 |
else: |
| 51 |
add_small_scroll(amt) |
| 52 |
elif amt is 0 or is_large_scroll: |
| 53 |
clear_small_scrolls() |
| 54 |
|
| 55 |
|
| 56 |
last_change_spine_item_request = {} |
| 57 |
|
| 58 |
|
| 59 |
def _check_for_scroll_end(func, obj, args, report, apply_mouse_boundary_delay): |
| 60 |
before = scroll_viewport.block_pos() |
| 61 |
should_flip_progression_direction = func.apply(obj, args) |
| 62 |
|
| 63 |
now = window.performance.now() |
| 64 |
scroll_animator.sync(now) |
| 65 |
|
| 66 |
if scroll_viewport.block_pos() is before: |
| 67 |
csi = current_spine_item() |
| 68 |
## ignore duplicate requests |
| 69 |
if last_change_spine_item_request.name is csi.name and now - last_change_spine_item_request.at < 2000: |
| 70 |
return False |
| 71 |
## delay change on boundaries |
| 72 |
if apply_mouse_boundary_delay and opts.scroll_mouse_boundary_delay > 0: |
| 73 |
if last_change_spine_item_request.denied is None: |
| 74 |
last_change_spine_item_request.denied = now |
| 75 |
if now - last_change_spine_item_request.denied < opts.scroll_mouse_boundary_delay * 1000: |
| 76 |
return False |
| 77 |
last_change_spine_item_request.denied = None |
| 78 |
# perform change |
| 79 |
last_change_spine_item_request.name = csi.name |
| 80 |
last_change_spine_item_request.at = now |
| 81 |
go_to_previous_page = args[0] < 0 |
| 82 |
if should_flip_progression_direction: |
| 83 |
go_to_previous_page = not go_to_previous_page |
| 84 |
get_boss().send_message('next_spine_item', previous=go_to_previous_page) |
| 85 |
return False |
| 86 |
last_change_spine_item_request.denied = None |
| 87 |
if report: |
| 88 |
report_human_scroll(scroll_viewport.block_pos() - before) |
| 89 |
return True |
| 90 |
|
| 91 |
|
| 92 |
def check_for_scroll_end(func): |
| 93 |
return def (): |
| 94 |
return _check_for_scroll_end(func, this, arguments, False, True) |
| 95 |
|
| 96 |
|
| 97 |
def check_for_scroll_end_and_report(func): |
| 98 |
return def (): |
| 99 |
return _check_for_scroll_end(func, this, arguments, True, True) |
| 100 |
|
| 101 |
|
| 102 |
def check_for_scroll_end_and_report_no_delay(func): |
| 103 |
return def (): |
| 104 |
return _check_for_scroll_end(func, this, arguments, True, False) |
| 105 |
|
| 106 |
|
| 107 |
@check_for_scroll_end_and_report |
| 108 |
def scroll_by_and_check_next_page(y): |
| 109 |
scroll_viewport.scroll_by_in_block_direction(y) |
| 110 |
# This indicates to check_for_scroll_end_and_report that it should not |
| 111 |
# flip the page progression direction. |
| 112 |
return False |
| 113 |
|
| 114 |
|
| 115 |
def flow_onwheel(evt): |
| 116 |
di = db = 0 |
| 117 |
WheelEvent = window.WheelEvent |
| 118 |
# Y deltas always scroll in the previous and next page direction, |
| 119 |
# regardless of writing direction, since doing otherwise would |
| 120 |
# make mouse wheels mostly useless for scrolling in books written |
| 121 |
# vertically. |
| 122 |
if evt.deltaY: |
| 123 |
if evt.deltaMode is WheelEvent.DOM_DELTA_PIXEL: |
| 124 |
db = evt.deltaY |
| 125 |
elif evt.deltaMode is WheelEvent.DOM_DELTA_LINE: |
| 126 |
db = line_height() * evt.deltaY |
| 127 |
if evt.deltaMode is WheelEvent.DOM_DELTA_PAGE: |
| 128 |
db = (scroll_viewport.block_size() - 30) * evt.deltaY |
| 129 |
# X deltas scroll horizontally in both horizontal and vertical books. |
| 130 |
# It's more natural in both cases. |
| 131 |
if evt.deltaX: |
| 132 |
if evt.deltaMode is WheelEvent.DOM_DELTA_PIXEL: |
| 133 |
dx = evt.deltaX |
| 134 |
elif evt.deltaMode is WheelEvent.DOM_DELTA_LINE: |
| 135 |
dx = line_height() * evt.deltaX |
| 136 |
else: |
| 137 |
dx = (scroll_viewport.block_size() - 30) * evt.deltaX |
| 138 |
|
| 139 |
if scroll_viewport.horizontal_writing_mode: |
| 140 |
di = dx |
| 141 |
else: |
| 142 |
# Left goes forward, so make sure left is positive and right is negative, |
| 143 |
# which is the opposite of what the wheel sends. |
| 144 |
if scroll_viewport.rtl: |
| 145 |
db = -dx |
| 146 |
# Right goes forward, so the sign is correct. |
| 147 |
else: |
| 148 |
db = dx |
| 149 |
if Math.abs(di) >= 1: |
| 150 |
scroll_viewport.scroll_by_in_inline_direction(di) |
| 151 |
if Math.abs(db) >= 1: |
| 152 |
scroll_by_and_check_next_page(db) |
| 153 |
|
| 154 |
|
| 155 |
@check_for_scroll_end |
| 156 |
def goto_boundary(dir): |
| 157 |
position = 0 if dir is DIRECTION.Up else scroll_viewport.document_block_size() |
| 158 |
scroll_viewport.scroll_to_in_block_direction(position) |
| 159 |
get_boss().report_human_scroll() |
| 160 |
return False |
| 161 |
|
| 162 |
|
| 163 |
def _scroll_by_page(direction, flip_if_rtl_page_progression): |
| 164 |
b = scroll_viewport.block_size() - 10 |
| 165 |
scroll_viewport.scroll_by_in_block_direction(b * direction) |
| 166 |
|
| 167 |
# Let check_for_scroll_end_and_report know whether or not it should flip |
| 168 |
# the progression direction. |
| 169 |
return flip_if_rtl_page_progression and rtl_page_progression() |
| 170 |
|
| 171 |
|
| 172 |
@check_for_scroll_end_and_report |
| 173 |
def scroll_by_page(direction, flip_if_rtl_page_progression): |
| 174 |
_scroll_by_page(direction, flip_if_rtl_page_progression) |
| 175 |
|
| 176 |
|
| 177 |
@check_for_scroll_end_and_report_no_delay |
| 178 |
def scroll_by_page_no_delay(direction, flip_if_rtl_page_progression): |
| 179 |
_scroll_by_page(direction, flip_if_rtl_page_progression) |
| 180 |
|
| 181 |
|
| 182 |
def scroll_to_extend_annotation(backward, horizontal, by_page): |
| 183 |
direction = -1 if backward else 1 |
| 184 |
h = line_height() |
| 185 |
if by_page: |
| 186 |
h = (window.innerWidth if horizontal else window.innerHeight) - h |
| 187 |
if horizontal: |
| 188 |
before = window.pageXOffset |
| 189 |
window.scrollBy(h * direction, 0) |
| 190 |
return window.pageXOffset is not before |
| 191 |
before = window.pageYOffset |
| 192 |
window.scrollBy(0, h * direction) |
| 193 |
return window.pageYOffset is not before |
| 194 |
|
| 195 |
|
| 196 |
def is_auto_scroll_active(): |
| 197 |
return scroll_animator.is_active() |
| 198 |
|
| 199 |
|
| 200 |
def start_autoscroll(): |
| 201 |
scroll_animator.start(DIRECTION.Down, True) |
| 202 |
|
| 203 |
|
| 204 |
def toggle_autoscroll(): |
| 205 |
if is_auto_scroll_active(): |
| 206 |
cancel_scroll() |
| 207 |
running = False |
| 208 |
else: |
| 209 |
start_autoscroll() |
| 210 |
running = True |
| 211 |
get_boss().send_message('autoscroll_state_changed', running=running) |
| 212 |
|
| 213 |
|
| 214 |
def handle_shortcut(sc_name, evt): |
| 215 |
if sc_name is 'down': |
| 216 |
scroll_animator.start(DIRECTION.Down, False) |
| 217 |
return True |
| 218 |
if sc_name is 'up': |
| 219 |
scroll_animator.start(DIRECTION.Up, False) |
| 220 |
return True |
| 221 |
if sc_name is 'start_of_file': |
| 222 |
goto_boundary(DIRECTION.Up) |
| 223 |
return True |
| 224 |
if sc_name is 'end_of_file': |
| 225 |
goto_boundary(DIRECTION.Down) |
| 226 |
return True |
| 227 |
if sc_name is 'left': |
| 228 |
scroll_by_and_check_next_page(-15 if ltr_page_progression() else 15, 0) |
| 229 |
return True |
| 230 |
if sc_name is 'right': |
| 231 |
scroll_by_and_check_next_page(15 if ltr_page_progression() else -15, 0) |
| 232 |
return True |
| 233 |
if sc_name is 'start_of_book': |
| 234 |
get_boss().send_message('goto_doc_boundary', start=True) |
| 235 |
return True |
| 236 |
if sc_name is 'end_of_book': |
| 237 |
get_boss().send_message('goto_doc_boundary', start=False) |
| 238 |
return True |
| 239 |
if sc_name is 'pageup': |
| 240 |
scroll_by_page_no_delay(-1, False) |
| 241 |
return True |
| 242 |
if sc_name is 'pagedown': |
| 243 |
scroll_by_page_no_delay(1, False) |
| 244 |
return True |
| 245 |
if sc_name is 'toggle_autoscroll': |
| 246 |
toggle_autoscroll() |
| 247 |
return True |
| 248 |
|
| 249 |
if sc_name.startsWith('scrollspeed_'): |
| 250 |
scroll_animator.sync() |
| 251 |
|
| 252 |
return False |
| 253 |
|
| 254 |
|
| 255 |
def layout(is_single_page): |
| 256 |
add_visibility_listener() |
| 257 |
line_height(True) |
| 258 |
rem_size(True) |
| 259 |
set_css(document.body, margin='0', border_width='0', padding='0') |
| 260 |
body_style = window.getComputedStyle(document.body) |
| 261 |
# scroll viewport needs to know if we're in vertical mode, |
| 262 |
# since that will cause scrolling to happen left and right |
| 263 |
scroll_viewport.initialize_on_layout(body_style) |
| 264 |
document.documentElement.style.overflow = 'visible' if scroll_viewport.vertical_writing_mode else 'hidden' |
| 265 |
|
| 266 |
|
| 267 |
def auto_scroll_resume(): |
| 268 |
scroll_animator.wait = False |
| 269 |
scroll_animator.sync() |
| 270 |
|
| 271 |
|
| 272 |
def add_visibility_listener(): |
| 273 |
if add_visibility_listener.done: |
| 274 |
return |
| 275 |
add_visibility_listener.done = True |
| 276 |
# Pause auto-scroll while minimized |
| 277 |
document.addEventListener('visibilitychange', def (): |
| 278 |
if (document.visibilityState is 'visible'): |
| 279 |
scroll_animator.sync() |
| 280 |
else: |
| 281 |
scroll_animator.pause() |
| 282 |
) |
| 283 |
|
| 284 |
|
| 285 |
def cancel_scroll(): |
| 286 |
scroll_animator.stop() |
| 287 |
|
| 288 |
|
| 289 |
def is_scroll_end(pos): |
| 290 |
return not (0 <= pos <= scroll_viewport.document_block_size() - scroll_viewport.block_size()) |
| 291 |
|
| 292 |
|
| 293 |
DIRECTION = {'Up': -1, 'up': -1, 'Down': 1, 'down': 1, 'UP': -1, 'DOWN': 1} |
| 294 |
|
| 295 |
|
| 296 |
class ScrollAnimator: |
| 297 |
DURATION = 100 # milliseconds |
| 298 |
|
| 299 |
def __init__(self): |
| 300 |
self.animation_id = None |
| 301 |
self.auto = False |
| 302 |
self.auto_timer = None |
| 303 |
self.paused = False |
| 304 |
|
| 305 |
def is_running(self): |
| 306 |
return self.animation_id is not None or self.auto_timer is not None |
| 307 |
|
| 308 |
def is_active(self): |
| 309 |
return self.is_running() and (self.auto or self.auto_timer is not None) |
| 310 |
|
| 311 |
def start(self, direction, auto): |
| 312 |
cancel_drag_scroll() |
| 313 |
if self.wait: |
| 314 |
return |
| 315 |
|
| 316 |
now = window.performance.now() |
| 317 |
self.end_time = now + self.DURATION |
| 318 |
self.stop_auto_spine_transition() |
| 319 |
|
| 320 |
if not self.is_running() or direction is not self.direction or auto is not self.auto: |
| 321 |
if self.auto and not auto: |
| 322 |
self.pause() |
| 323 |
self.stop() |
| 324 |
self.auto = auto |
| 325 |
self.direction = direction |
| 326 |
self.start_time = now |
| 327 |
self.start_offset = scroll_viewport.block_pos() |
| 328 |
self.csi_idx = current_spine_item().index |
| 329 |
self.animation_id = window.requestAnimationFrame(self.auto_scroll if auto else self.smooth_scroll) |
| 330 |
|
| 331 |
def smooth_scroll(self, ts): |
| 332 |
duration = self.end_time - self.start_time |
| 333 |
if duration <= 0: |
| 334 |
self.animation_id = None |
| 335 |
return |
| 336 |
progress = max(0, min(1, (ts - self.start_time) / duration)) # max/min to account for jitter |
| 337 |
scroll_target = self.start_offset |
| 338 |
scroll_target += Math.trunc(self.direction * progress * duration * line_height() * opts.lines_per_sec_smooth) / 1000 |
| 339 |
|
| 340 |
scroll_viewport.scroll_to_in_block_direction(scroll_target) |
| 341 |
amt = scroll_viewport.block_pos() - self.start_offset |
| 342 |
|
| 343 |
if is_scroll_end(scroll_target) and (not opts.scroll_stop_boundaries or (abs(amt) < 3 and duration is self.DURATION)): |
| 344 |
# "Turn the page" if stop at boundaries option is false or |
| 345 |
# this is a new scroll action and we were already at the end |
| 346 |
self.animation_id = None |
| 347 |
self.wait = True |
| 348 |
report_human_scroll(amt) |
| 349 |
get_boss().send_message('next_spine_item', previous=self.direction is DIRECTION.Up) |
| 350 |
elif progress < 1: |
| 351 |
self.animation_id = window.requestAnimationFrame(self.smooth_scroll) |
| 352 |
elif self.paused: |
| 353 |
self.resume() |
| 354 |
else: |
| 355 |
self.animation_id = None |
| 356 |
report_human_scroll(amt) |
| 357 |
|
| 358 |
def auto_scroll(self, ts): |
| 359 |
elapsed = max(0, ts - self.start_time) # max to account for jitter |
| 360 |
scroll_target = self.start_offset |
| 361 |
scroll_target += Math.trunc(self.direction * elapsed * line_height() * opts.lines_per_sec_auto) / 1000 |
| 362 |
|
| 363 |
scroll_viewport.scroll_to_in_block_direction(scroll_target) |
| 364 |
scroll_finished = is_scroll_end(scroll_target) |
| 365 |
|
| 366 |
# report every second |
| 367 |
if elapsed >= 1000: |
| 368 |
self.sync(ts) |
| 369 |
|
| 370 |
if scroll_finished: |
| 371 |
self.pause() |
| 372 |
if opts.scroll_auto_boundary_delay >= 0: |
| 373 |
self.auto_timer = setTimeout(self.request_next_spine_item, opts.scroll_auto_boundary_delay * 1000) |
| 374 |
else: |
| 375 |
self.animation_id = window.requestAnimationFrame(self.auto_scroll) |
| 376 |
|
| 377 |
def request_next_spine_item(self): |
| 378 |
self.auto_timer = None |
| 379 |
get_boss().send_message('next_spine_item', previous=self.direction is DIRECTION.Up) |
| 380 |
|
| 381 |
def report(self): |
| 382 |
amt = scroll_viewport.block_pos() - self.start_offset |
| 383 |
if abs(amt) > 0 and self.csi_idx is current_spine_item().index: |
| 384 |
report_human_scroll(amt) |
| 385 |
|
| 386 |
def sync(self, ts): |
| 387 |
if self.auto: |
| 388 |
self.report() |
| 389 |
self.csi_idx = current_spine_item().index |
| 390 |
self.start_time = ts or window.performance.now() |
| 391 |
self.start_offset = scroll_viewport.block_pos() |
| 392 |
else: |
| 393 |
self.resume() |
| 394 |
|
| 395 |
def stop(self): |
| 396 |
self.auto = False |
| 397 |
if self.animation_id is not None: |
| 398 |
window.cancelAnimationFrame(self.animation_id) |
| 399 |
self.animation_id = None |
| 400 |
self.report() |
| 401 |
self.stop_auto_spine_transition() |
| 402 |
|
| 403 |
def stop_auto_spine_transition(self): |
| 404 |
if self.auto_timer is not None: |
| 405 |
clearTimeout(self.auto_timer) |
| 406 |
self.auto_timer = None |
| 407 |
self.paused = False |
| 408 |
|
| 409 |
def pause(self): |
| 410 |
if self.auto: |
| 411 |
self.paused = self.direction |
| 412 |
self.stop() |
| 413 |
else: |
| 414 |
self.paused = False |
| 415 |
|
| 416 |
# Resume auto-scroll |
| 417 |
def resume(self): |
| 418 |
if self.paused: |
| 419 |
self.start(self.paused, True) |
| 420 |
self.paused = False |
| 421 |
|
| 422 |
scroll_animator = ScrollAnimator() |
| 423 |
|
| 424 |
|
| 425 |
class FlickAnimator: |
| 426 |
SPEED_FACTOR = 0.04 |
| 427 |
DECEL_TIME_CONSTANT = 325 # milliseconds |
| 428 |
VELOCITY_HISTORY = 300 # milliseconds |
| 429 |
MIMUMUM_VELOCITY = 100 # pixels/sec |
| 430 |
|
| 431 |
def __init__(self): |
| 432 |
self.animation_id = None |
| 433 |
|
| 434 |
def cancel_current_drag_scroll(self): |
| 435 |
cancel_drag_scroll() |
| 436 |
|
| 437 |
def start(self, gesture): |
| 438 |
self.cancel_current_drag_scroll() |
| 439 |
self.vertical = gesture.axis is 'vertical' |
| 440 |
now = window.performance.now() |
| 441 |
points = times = None |
| 442 |
for i, t in enumerate(gesture.times): |
| 443 |
if now - t < self.VELOCITY_HISTORY: |
| 444 |
points, times = gesture.points[i:], gesture.times[i:] |
| 445 |
break |
| 446 |
if times and times.length > 1: |
| 447 |
elapsed = (times[-1] - times[0]) / 1000 |
| 448 |
if elapsed > 0 and points.length > 1: |
| 449 |
delta = points[0] - points[-1] |
| 450 |
velocity = delta / elapsed |
| 451 |
if abs(velocity) > self.MIMUMUM_VELOCITY: |
| 452 |
self.amplitude = self.SPEED_FACTOR * velocity |
| 453 |
self.start_time = now |
| 454 |
self.animation_id = window.requestAnimationFrame(self.auto_scroll) |
| 455 |
|
| 456 |
def auto_scroll(self, ts): |
| 457 |
if self.animation_id is None: |
| 458 |
return |
| 459 |
elapsed = window.performance.now() - self.start_time |
| 460 |
delta = self.amplitude * Math.exp(-elapsed / self.DECEL_TIME_CONSTANT) |
| 461 |
if abs(delta) >= 1: |
| 462 |
delta = Math.round(delta) |
| 463 |
if self.vertical: |
| 464 |
self.scroll_y(delta) |
| 465 |
else: |
| 466 |
self.scroll_x(delta) |
| 467 |
self.animation_id = window.requestAnimationFrame(self.auto_scroll) |
| 468 |
|
| 469 |
def scroll_y(self, delta): |
| 470 |
window.scrollBy(0, delta) |
| 471 |
|
| 472 |
def scroll_x(self, delta): |
| 473 |
window.scrollBy(delta, 0) |
| 474 |
|
| 475 |
def stop(self): |
| 476 |
if self.animation_id is not None: |
| 477 |
window.cancelAnimationFrame(self.animation_id) |
| 478 |
self.animation_id = None |
| 479 |
|
| 480 |
flick_animator = FlickAnimator() |
| 481 |
|
| 482 |
|
| 483 |
class DragScroller: |
| 484 |
DURATION = 100 # milliseconds |
| 485 |
|
| 486 |
def __init__(self): |
| 487 |
self.animation_id = None |
| 488 |
self.direction = 1 |
| 489 |
self.speed_factor = 1 |
| 490 |
self.start_time = self.end_time = 0 |
| 491 |
self.start_offset = 0 |
| 492 |
|
| 493 |
def is_running(self): |
| 494 |
return self.animation_id is not None |
| 495 |
|
| 496 |
def smooth_scroll(self, ts): |
| 497 |
duration = self.end_time - self.start_time |
| 498 |
if duration <= 0: |
| 499 |
self.animation_id = None |
| 500 |
self.start(self.direction, self.speed_factor) |
| 501 |
return |
| 502 |
progress = max(0, min(1, (ts - self.start_time) / duration)) # max/min to account for jitter |
| 503 |
scroll_target = self.start_offset |
| 504 |
scroll_target += Math.trunc(self.direction * progress * duration * line_height() * opts.lines_per_sec_smooth * self.speed_factor) / 1000 |
| 505 |
scroll_viewport.scroll_to_in_block_direction(scroll_target) |
| 506 |
|
| 507 |
if progress < 1: |
| 508 |
self.animation_id = window.requestAnimationFrame(self.smooth_scroll) |
| 509 |
else: |
| 510 |
self.animation_id = None |
| 511 |
self.start(self.direction, self.speed_factor) |
| 512 |
|
| 513 |
def start(self, direction, speed_factor): |
| 514 |
now = window.performance.now() |
| 515 |
self.end_time = now + self.DURATION |
| 516 |
if not self.is_running() or direction is not self.direction or speed_factor is not self.speed_factor: |
| 517 |
self.stop() |
| 518 |
self.direction = direction |
| 519 |
self.speed_factor = speed_factor |
| 520 |
self.start_time = now |
| 521 |
self.start_offset = scroll_viewport.block_pos() |
| 522 |
self.animation_id = window.requestAnimationFrame(self.smooth_scroll) |
| 523 |
|
| 524 |
def stop(self): |
| 525 |
if self.animation_id is not None: |
| 526 |
window.cancelAnimationFrame(self.animation_id) |
| 527 |
self.animation_id = None |
| 528 |
|
| 529 |
|
| 530 |
drag_scroller = DragScroller() |
| 531 |
|
| 532 |
|
| 533 |
def cancel_drag_scroll(): |
| 534 |
drag_scroller.stop() |
| 535 |
|
| 536 |
|
| 537 |
def start_drag_scroll(delta): |
| 538 |
limit = opts.margin_top if delta < 0 else opts.margin_bottom |
| 539 |
direction = 1 if delta >= 0 else -1 |
| 540 |
speed_factor = min(abs(delta), limit) / limit |
| 541 |
speed_factor *= (2 - speed_factor) # QuadOut Easing curve |
| 542 |
drag_scroller.start(direction, 2 * speed_factor) |
| 543 |
|
| 544 |
|
| 545 |
def handle_gesture(gesture): |
| 546 |
flick_animator.stop() |
| 547 |
if gesture.resolved_action is 'animated_scroll': |
| 548 |
flick_animator.start(gesture) |
| 549 |
elif gesture.resolved_action is 'pan': |
| 550 |
if gesture.points.length > 1 and not gesture.is_held: |
| 551 |
delta = gesture.points[-2] - gesture.points[-1] |
| 552 |
if Math.abs(delta) >= 1: |
| 553 |
if gesture.axis is 'vertical': |
| 554 |
# Vertical writing scrolls left and right, |
| 555 |
# so doing a vertical flick shouldn't change pages. |
| 556 |
if scroll_viewport.vertical_writing_mode: |
| 557 |
scroll_viewport.scroll_by(delta, 0) |
| 558 |
# However, it might change pages in horizontal writing |
| 559 |
else: |
| 560 |
scroll_by_and_check_next_page(delta) |
| 561 |
else: |
| 562 |
# A horizontal flick should check for new pages in |
| 563 |
# vertical modes, since they flow left and right. |
| 564 |
if scroll_viewport.vertical_writing_mode: |
| 565 |
scroll_by_and_check_next_page(delta) |
| 566 |
# In horizontal modes, just move by the delta. |
| 567 |
else: |
| 568 |
scroll_viewport.scroll_by(delta, 0) |
| 569 |
elif gesture.resolved_action is 'prev_page' or gesture.resolved_action is 'prev_screen': |
| 570 |
# should flip = False - previous is previous whether RTL or LTR. |
| 571 |
# flipping of this command is handled higher up |
| 572 |
scroll_by_page(-1, False) |
| 573 |
elif gesture.resolved_action is 'next_page' or gesture.resolved_action is 'next_screen': |
| 574 |
# should flip = False - next is next whether RTL or LTR. |
| 575 |
# flipping of this command is handled higher up |
| 576 |
scroll_by_page(1, False) |
| 577 |
|
| 578 |
|
| 579 |
anchor_funcs = { |
| 580 |
'pos_for_elem': def pos_for_elem(elem): |
| 581 |
if not elem: |
| 582 |
return {'block': 0, 'inline': 0} |
| 583 |
br = elem.getBoundingClientRect() |
| 584 |
# Start of object in the scrolling direction |
| 585 |
return { |
| 586 |
'block': scroll_viewport.viewport_to_document_block(scroll_viewport.rect_block_start(br), elem.ownerDocument), |
| 587 |
'inline': scroll_viewport.viewport_to_document_inline(scroll_viewport.rect_inline_start(br), elem.ownerDocument) |
| 588 |
} |
| 589 |
, |
| 590 |
'visibility': def visibility(pos): |
| 591 |
q = pos.block |
| 592 |
# Have to negate X if in RTL for the math to be correct, |
| 593 |
# as the value that the scroll viewport returns is negated |
| 594 |
if scroll_viewport.vertical_writing_mode and scroll_viewport.rtl: |
| 595 |
q = -q |
| 596 |
if q + 1 < scroll_viewport.block_pos(): |
| 597 |
# the + 1 is needed for visibility when top aligned, see: https://bugs.launchpad.net/calibre/+bug/1924890 |
| 598 |
return -1 |
| 599 |
if q <= scroll_viewport.block_pos() + scroll_viewport.block_size(): |
| 600 |
return 0 |
| 601 |
return 1 |
| 602 |
, |
| 603 |
'cmp': def cmp(a, b): |
| 604 |
return (a.block - b.block) or (a.inline - b.inline) |
| 605 |
, |
| 606 |
'get_bounding_client_rect': def (elem): |
| 607 |
return elem.getBoundingClientRect() |
| 608 |
, |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
def auto_scroll_action(action): |
| 613 |
if action is 'toggle': |
| 614 |
toggle_autoscroll() |
| 615 |
elif action is 'start': |
| 616 |
if not is_auto_scroll_active(): |
| 617 |
toggle_autoscroll() |
| 618 |
elif action is 'stop': |
| 619 |
if is_auto_scroll_active(): |
| 620 |
toggle_autoscroll() |
| 621 |
elif action is 'resume': |
| 622 |
auto_scroll_resume() |
| 623 |
return is_auto_scroll_active() |
| 624 |
|
| 625 |
|
| 626 |
def closest_preceding_element(p): |
| 627 |
while p and not p.scrollIntoView: |
| 628 |
p = p.previousSibling or p.parentNode |
| 629 |
return p |
| 630 |
|
| 631 |
|
| 632 |
def ensure_selection_visible(): |
| 633 |
s = window.getSelection() |
| 634 |
p = s.anchorNode |
| 635 |
if not p: |
| 636 |
return |
| 637 |
p = closest_preceding_element(p) |
| 638 |
if p?.scrollIntoView: |
| 639 |
p.scrollIntoView() |
| 640 |
r = s.getRangeAt(0) |
| 641 |
if not r: |
| 642 |
return |
| 643 |
rect = r.getBoundingClientRect() |
| 644 |
if not rect: |
| 645 |
return |
| 646 |
if rect.top < 0 or rect.top >= window.innerHeight or rect.left < 0 or rect.left >= window.innerWidth: |
| 647 |
wrapper = document.createElement('span') |
| 648 |
wrap_range(r, wrapper) |
| 649 |
wrapper.scrollIntoView() |
| 650 |
unwrap(wrapper) |
| 651 |
|
| 652 |
|
| 653 |
def ensure_selection_boundary_visible(use_end): |
| 654 |
sel = window.getSelection() |
| 655 |
try: |
| 656 |
rr = sel.getRangeAt(0) |
| 657 |
except: |
| 658 |
rr = None |
| 659 |
if rr: |
| 660 |
r = rr.getBoundingClientRect() |
| 661 |
if r: |
| 662 |
node = sel.focusNode if use_end else sel.anchorNode |
| 663 |
# Use logical viewport sizes as in vertical writing modes the |
| 664 |
# inline direction is vertical and the block direction horizontal |
| 665 |
inline_limit = scroll_viewport.inline_size() |
| 666 |
block_limit = scroll_viewport.block_size() |
| 667 |
x_beg = scroll_viewport.rect_inline_start(r) |
| 668 |
x_end = scroll_viewport.rect_inline_end(r) |
| 669 |
y_beg = scroll_viewport.rect_block_start(r) |
| 670 |
y_end = scroll_viewport.rect_block_end(r) |
| 671 |
if use_end: |
| 672 |
# Scroll the end of the selection to one line inside the |
| 673 |
# viewport start, the line_height() adjustment keeps the |
| 674 |
# boundary line itself visible |
| 675 |
if x_end < 0 or x_end > inline_limit: |
| 676 |
x = scroll_viewport.viewport_to_document_inline(x_end, doc=node.ownerDocument) - line_height() |
| 677 |
scroll_viewport.scroll_to_in_inline_direction(x, True) |
| 678 |
if y_end < 0 or y_end > block_limit: |
| 679 |
y = scroll_viewport.viewport_to_document_block(y_end, doc=node.ownerDocument) - line_height() |
| 680 |
scroll_viewport.scroll_to_in_block_direction(y, True) |
| 681 |
else: |
| 682 |
# Scroll if any part of the selection is not visible, so that |
| 683 |
# a selection running past the viewport end is brought into |
| 684 |
# view, keeping one line of context before the selection start |
| 685 |
if x_beg < 0 or x_beg > inline_limit or x_end < 0 or x_end > inline_limit: |
| 686 |
x = scroll_viewport.viewport_to_document_inline(x_beg, doc=node.ownerDocument) - line_height() |
| 687 |
scroll_viewport.scroll_to_in_inline_direction(x, True) |
| 688 |
if y_beg < 0 or y_beg > block_limit or y_end < 0 or y_end > block_limit: |
| 689 |
y = scroll_viewport.viewport_to_document_block(y_beg, doc=node.ownerDocument) - line_height() |
| 690 |
scroll_viewport.scroll_to_in_block_direction(y, True) |
| 691 |
|
| 692 |
|
| 693 |
def jump_to_cfi(cfi): |
| 694 |
# Jump to the position indicated by the specified conformal fragment |
| 695 |
# indicator. |
| 696 |
cfi_scroll_to(cfi, def (x, y): |
| 697 |
# block is vertical if text is horizontal |
| 698 |
if scroll_viewport.horizontal_writing_mode: |
| 699 |
scroll_viewport.scroll_to_in_block_direction(y) |
| 700 |
# block is horizontal if text is vertical |
| 701 |
else: |
| 702 |
scroll_viewport.scroll_to_in_block_direction(x) |
| 703 |
) |