| 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 |
import traceback |
| 6 |
from elementmaker import E |
| 7 |
from gettext import gettext as _ |
| 8 |
|
| 9 |
import read_book.iframe # noqa |
| 10 |
from ajax import ajax_send |
| 11 |
from book_list.globals import get_session_data |
| 12 |
from book_list.home import update_book_in_recently_read_by_user_on_home_page |
| 13 |
from book_list.theme import cached_color_to_rgba, get_color, set_ui_colors |
| 14 |
from book_list.ui import show_panel |
| 15 |
from dom import add_extra_css, build_rule, clear, set_css, svgicon, unique_id |
| 16 |
from iframe_comm import create_wrapped_iframe |
| 17 |
from modals import error_dialog, warning_dialog |
| 18 |
from read_book.annotations import AnnotationsManager |
| 19 |
from read_book.bookmarks import create_new_bookmark |
| 20 |
from read_book.content_popup import ContentPopupOverlay |
| 21 |
from read_book.globals import ( |
| 22 |
current_book, current_spine_item, is_dark_theme, rtl_page_progression, runtime, |
| 23 |
set_current_spine_item, ui_operations |
| 24 |
) |
| 25 |
from read_book.goto import get_next_section |
| 26 |
from read_book.highlights import get_current_link_prefix, link_to_epubcfi |
| 27 |
from read_book.hints import Hints |
| 28 |
from read_book.open_book import add_book_to_recently_viewed |
| 29 |
from read_book.overlay import Overlay |
| 30 |
from read_book.prefs.colors import resolve_color_scheme |
| 31 |
from read_book.prefs.font_size import change_font_size_by, restore_default_font_size |
| 32 |
from read_book.prefs.fonts import current_zoom_step_size |
| 33 |
from read_book.prefs.head_foot import render_head_foot |
| 34 |
from read_book.prefs.scrolling import ( |
| 35 |
MIN_SCROLL_SPEED_AUTO as SCROLL_SPEED_STEP, change_scroll_speed |
| 36 |
) |
| 37 |
from read_book.prefs.user_stylesheet import modify_background_image_url_for_fetch |
| 38 |
from read_book.read_aloud import ReadAloud |
| 39 |
from read_book.read_audio_ebook import ReadAudioEbook |
| 40 |
from read_book.resources import load_resources |
| 41 |
from read_book.scrollbar import BookScrollbar |
| 42 |
from read_book.search import SearchOverlay |
| 43 |
from read_book.selection_bar import SelectionBar |
| 44 |
from read_book.shortcuts import create_shortcut_map |
| 45 |
from read_book.smil import get_smil_audio_map |
| 46 |
from read_book.timers import Timers |
| 47 |
from read_book.toc import get_current_toc_nodes, update_visible_toc_nodes, get_current_pagelist_items |
| 48 |
from read_book.touch import set_left_margin_handler, set_right_margin_handler |
| 49 |
from session import get_device_uuid, get_interface_data, all_settings |
| 50 |
from utils import ( |
| 51 |
default_context_menu_should_be_allowed, html_escape, parse_url_params, |
| 52 |
safe_set_inner_html, username_key |
| 53 |
) |
| 54 |
from viewer.constants import CHARS_PER_PAGE |
| 55 |
|
| 56 |
add_extra_css(def (): |
| 57 |
sel = '.book-side-margin' |
| 58 |
ans = build_rule( |
| 59 |
sel, cursor='pointer', text_align='center', height='100dvh', user_select='none', |
| 60 |
display='flex', align_items='center', justify_content='space-between', flex_direction='column', |
| 61 |
flex_shrink='0', |
| 62 |
) |
| 63 |
ans += build_rule(sel + ' > .arrow', display='none') |
| 64 |
ans += build_rule(sel + ' > *', max_width='100%', overflow='hidden') |
| 65 |
ans += build_rule(sel + ':hover > .not-arrow', display='none') |
| 66 |
ans += build_rule(sel + ':active > .not-arrow', display='none') |
| 67 |
ans += build_rule(sel + ':hover > .arrow', display='block') |
| 68 |
ans += build_rule(sel + ':active > .arrow', display='block', transform='scale(2)') |
| 69 |
ans += build_rule('.book-h-margin .visible-on-hover', visibility='hidden') |
| 70 |
ans += build_rule('.book-h-margin:hover .visible-on-hover', visibility='visible') |
| 71 |
ans += build_rule('.book-h-margin .visible-on-hover:hover', color=get_color('window-hover-foreground')) |
| 72 |
return ans |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
# Simple Overlays {{{ |
| 77 |
|
| 78 |
|
| 79 |
def show_controls_help(): |
| 80 |
container = document.getElementById('controls-help-overlay') |
| 81 |
container.style.display = 'block' |
| 82 |
container.style.backgroundColor = get_color('window-background') |
| 83 |
if not show_controls_help.listener_added: |
| 84 |
show_controls_help.listener_added = True |
| 85 |
container.addEventListener('click', def (): |
| 86 |
document.getElementById('controls-help-overlay').style.display = 'none' |
| 87 |
ui_operations.focus_iframe() |
| 88 |
) |
| 89 |
container.addEventListener('contextmenu', def (evt): |
| 90 |
evt.preventDefault(), evt.stopPropagation() |
| 91 |
document.getElementById('controls-help-overlay').style.display = 'none' |
| 92 |
ui_operations.focus_iframe() |
| 93 |
, {'passive': False}) |
| 94 |
container.addEventListener('keydown', def (event): |
| 95 |
event.preventDefault(), event.stopPropagation() |
| 96 |
document.getElementById('controls-help-overlay').style.display = 'none' |
| 97 |
ui_operations.focus_iframe() |
| 98 |
, {'passive': False}) |
| 99 |
|
| 100 |
def focus(): |
| 101 |
if container.style.display is 'block': |
| 102 |
container.querySelector('input').focus() |
| 103 |
window.setTimeout(focus, 10) |
| 104 |
|
| 105 |
if runtime.is_standalone_viewer: |
| 106 |
clear(container) |
| 107 |
container.appendChild(E.div( |
| 108 |
style='margin: 1rem', |
| 109 |
E.div(style='margin-top: 1rem'), |
| 110 |
E.div(style='margin-top: 1rem'), |
| 111 |
E.div(style='margin-top: 1rem'), |
| 112 |
E.div(style='margin-top: 1rem'), |
| 113 |
E.input(style='background: transparent; border-width: 0; outline: none', readonly='readonly'), |
| 114 |
)) |
| 115 |
div = container.lastChild.firstChild |
| 116 |
safe_set_inner_html(div, _('Welcome to the <b>calibre E-book viewer</b>!')) |
| 117 |
div = div.nextSibling |
| 118 |
safe_set_inner_html(div, _('Use the <b>PageUp/PageDn</b> or <b>Arrow keys</b> to turn pages')) |
| 119 |
div = div.nextSibling |
| 120 |
safe_set_inner_html(div, _('Press the <b>Esc</b> key or <b>{}</b> or <b>tap on the top third</b> of the text area to show the viewer controls').format( |
| 121 |
_('control+click') if 'macos' in window.navigator.userAgent else _('right click') |
| 122 |
)) |
| 123 |
div = div.nextSibling |
| 124 |
safe_set_inner_html(div, _('Press any key to continue…')) |
| 125 |
focus() |
| 126 |
return |
| 127 |
|
| 128 |
def msg(txt): |
| 129 |
return set_css(E.div(txt), padding='1ex 1em', text_align='center', margin='auto') |
| 130 |
|
| 131 |
left_msg = msg(_('Tap to turn back')) |
| 132 |
left_width = 'min(25vw, 1in)' |
| 133 |
right_msg = msg(_('Tap to turn page')) |
| 134 |
right_width = 'auto' |
| 135 |
left_grow = 0 |
| 136 |
right_grow = 1 |
| 137 |
if rtl_page_progression(): |
| 138 |
left_msg, right_msg = right_msg, left_msg |
| 139 |
left_width, right_width = right_width, left_width |
| 140 |
left_grow, right_grow = right_grow, left_grow |
| 141 |
|
| 142 |
# Clear it out if this is not the first time it's created. |
| 143 |
# Needed to correctly show it again in a different page progression direction. |
| 144 |
if container.firstChild: |
| 145 |
container.removeChild(container.firstChild) |
| 146 |
|
| 147 |
container.appendChild(E.div( |
| 148 |
style=f'overflow: hidden; width: 100vw; height: 100dvh; text-align: center;' |
| 149 |
+ f' font-size: 1.3rem; font-weight: bold; background: {get_color("window-background")};' |
| 150 |
+ 'display:flex; flex-direction: column; align-items: stretch', |
| 151 |
E.div( |
| 152 |
msg(_('Tap (or right click) for controls')), |
| 153 |
style='height: 25dvh; display:flex; align-items: center; border-bottom: solid 2px currentColor', |
| 154 |
), |
| 155 |
E.div( |
| 156 |
style='display: flex; align-items: stretch; flex-grow: 10', |
| 157 |
E.div(left_msg, style=f'width: {left_width}; flex-grow: {left_grow}; display:flex; align-items: center; border-right: solid 2px currentColor',), |
| 158 |
E.div(right_msg, style=f'width: {right_width}; display:flex; flex-grow: {right_grow}; align-items: center',), |
| 159 |
), |
| 160 |
)) |
| 161 |
|
| 162 |
|
| 163 |
# }}} |
| 164 |
|
| 165 |
|
| 166 |
def body_font_size(): |
| 167 |
ans = body_font_size.ans |
| 168 |
if not ans or window.devicePixelRatio is not body_font_size.dpr: |
| 169 |
body_font_size.dpr = window.devicePixelRatio |
| 170 |
q = window.getComputedStyle(document.body).fontSize |
| 171 |
if q and q.endsWith('px'): |
| 172 |
q = parseInt(q) |
| 173 |
if q and not isNaN(q): |
| 174 |
ans = body_font_size.ans = q |
| 175 |
return ans |
| 176 |
ans = body_font_size.ans = 16 |
| 177 |
return ans |
| 178 |
|
| 179 |
|
| 180 |
def header_footer_font_size(sz): |
| 181 |
return min(max(0, sz - 6), body_font_size()) |
| 182 |
|
| 183 |
|
| 184 |
def margin_elem(sd, which, id, onclick, oncontextmenu): |
| 185 |
sz = sd.get(which, 20) |
| 186 |
fsz = header_footer_font_size(sz) |
| 187 |
s = '; text-overflow: ellipsis; white-space: nowrap; overflow: hidden' |
| 188 |
ans = E.div( |
| 189 |
style=f'height:{sz}px; overflow: hidden; font-size:{fsz}px; width:100%; padding: 0;' |
| 190 |
+ ' display: flex; justify-content: space-between; align-items: center; user-select: none; cursor: pointer', |
| 191 |
class_='book-h-margin', |
| 192 |
id=id, |
| 193 |
E.div(style='margin-right: 1.5em' + s), |
| 194 |
E.div(style=s), |
| 195 |
E.div(style='margin-left: 1.5em' + s) |
| 196 |
) |
| 197 |
if onclick: |
| 198 |
ans.addEventListener('click', onclick) |
| 199 |
if oncontextmenu: |
| 200 |
ans.addEventListener('contextmenu', oncontextmenu) |
| 201 |
return ans |
| 202 |
|
| 203 |
|
| 204 |
def side_margin_elem(self, sd, which, icon): |
| 205 |
ans = E.div( |
| 206 |
E.div(class_='arrow', style='order: 3', svgicon(f'caret-{icon}', '100%', '100%')), |
| 207 |
E.div(style='order:1'), E.div(style='order:2', class_='not-arrow'), E.div(style='order:4'), |
| 208 |
style='width:{}px; user-select: none'.format(sd.get(f'margin_{which}', 20)), |
| 209 |
class_='book-side-margin', id=f'book-{which}-margin', |
| 210 |
onclick=self.side_margin_clicked.bind(None, which), |
| 211 |
oncontextmenu=self.margin_context_menu.bind(None, which), |
| 212 |
onwheel=self.on_margin_wheel.bind(None, which) |
| 213 |
) |
| 214 |
return ans |
| 215 |
|
| 216 |
|
| 217 |
class View: |
| 218 |
def __init__(self, container): |
| 219 |
self.container = container |
| 220 |
self.timers = Timers() |
| 221 |
self.reference_mode_enabled = False |
| 222 |
self.loaded_resources = {} |
| 223 |
self.current_progress_frac = self.current_file_progress_frac = 0 |
| 224 |
self.current_page_counts = {'current': 0, 'total': 0, 'pages_per_screen': 1} |
| 225 |
self.current_status_message = '' |
| 226 |
self.unsynced_message = '' |
| 227 |
self.last_position_data = None |
| 228 |
self.current_toc_node = self.current_toc_toplevel_node = None |
| 229 |
self.current_toc_families = [] |
| 230 |
self.report_cfi_callbacks = {} |
| 231 |
self.get_cfi_counter = 0 |
| 232 |
self.show_loading_callback_timer = None |
| 233 |
self.timer_ids = {'clock': 0} |
| 234 |
self.book_scrollbar = BookScrollbar(self) |
| 235 |
sd = get_session_data() |
| 236 |
self.keyboard_shortcut_map = create_shortcut_map(sd.get('keyboard_shortcuts')) |
| 237 |
if ui_operations.export_shortcut_map: |
| 238 |
ui_operations.export_shortcut_map(self.keyboard_shortcut_map) |
| 239 |
left_margin = side_margin_elem(self, sd, 'left', 'right' if sd.get('reverse_page_turn_zones') else 'left') |
| 240 |
set_left_margin_handler(left_margin) |
| 241 |
right_margin = side_margin_elem(self, sd, 'right', 'left' if sd.get('reverse_page_turn_zones') else 'right') |
| 242 |
set_right_margin_handler(right_margin) |
| 243 |
handlers = { |
| 244 |
'autoscroll_state_changed': def (data): |
| 245 |
self.autoscroll_active = v'!!data.running' |
| 246 |
if ui_operations.autoscroll_state_changed: |
| 247 |
ui_operations.autoscroll_state_changed(self.autoscroll_active) |
| 248 |
, |
| 249 |
'bump_font_size': self.bump_font_size, |
| 250 |
'content_loaded': self.on_content_loaded, |
| 251 |
'error': self.on_iframe_error, |
| 252 |
'invisible_text': self.on_invisible_text, |
| 253 |
'goto_doc_boundary': def (data): self.goto_doc_boundary(data.start);, |
| 254 |
'handle_keypress': self.on_handle_keypress, |
| 255 |
'handle_shortcut': self.on_handle_shortcut, |
| 256 |
'human_scroll': self.on_human_scroll, |
| 257 |
'next_section': self.on_next_section, |
| 258 |
'next_spine_item': self.on_next_spine_item, |
| 259 |
'print': self.on_print, |
| 260 |
'ready': self.on_iframe_ready, |
| 261 |
'report_cfi': self.on_report_cfi, |
| 262 |
'request_size': self.on_request_size, |
| 263 |
'scroll_to_anchor': self.on_scroll_to_anchor, |
| 264 |
'link_to_missing_activated': self.on_link_to_missing_activated, |
| 265 |
'selectionchange': self.on_selection_change, |
| 266 |
'update_selection_position': self.update_selection_position, |
| 267 |
'columns_per_screen_changed': self.on_columns_per_screen_changed, |
| 268 |
'show_chrome': self.show_chrome, |
| 269 |
'show_footnote': self.on_show_footnote, |
| 270 |
'update_cfi': self.on_update_cfi, |
| 271 |
'update_progress_frac': self.on_update_progress_frac, |
| 272 |
'update_toc_position': self.on_update_toc_position, |
| 273 |
'search_result_not_found': self.search_result_not_found, |
| 274 |
'search_result_discovered': self.search_result_discovered, |
| 275 |
'annotations': self.on_annotations_message, |
| 276 |
'tts': self.on_tts_message, |
| 277 |
'audio_ebook_message': self.on_audio_ebook_message, |
| 278 |
'hints': self.on_hints_message, |
| 279 |
'copy_text_to_clipboard': def (data): |
| 280 |
ui_operations.copy_selection(data.text, data.html) |
| 281 |
, |
| 282 |
'view_image': def (data): |
| 283 |
if ui_operations.view_image: |
| 284 |
ui_operations.view_image(data.calibre_src) |
| 285 |
, |
| 286 |
} |
| 287 |
|
| 288 |
iframe_id = unique_id('read-book-iframe') |
| 289 |
if runtime.is_standalone_viewer: |
| 290 |
entry_point = f'{runtime.FAKE_PROTOCOL}://{runtime.SANDBOX_HOST}/book/__index__' |
| 291 |
else: |
| 292 |
entry_point = 'read_book.iframe' |
| 293 |
iframe_kw = { |
| 294 |
'id': iframe_id, 'seamless': True, |
| 295 |
'sandbox': 'allow-popups allow-scripts allow-popups-to-escape-sandbox', |
| 296 |
'style': 'flex-grow: 2; width: 100%; max-width: 100%; min-width: 0; overflow: hidden;', 'allowfullscreen': 'true', |
| 297 |
} |
| 298 |
iframe, self.iframe_wrapper = create_wrapped_iframe(handlers, _('Bootstrapping book reader...'), entry_point, iframe_kw) |
| 299 |
container.appendChild( |
| 300 |
# container for horizontally aligned panels |
| 301 |
E.div(style='max-height: 100dvh; width: 100%; max-width: 100%; height: 100dvh;' |
| 302 |
+ ' overflow: hidden; overflow-x: hidden; display: flex; align-items: stretch', |
| 303 |
oncontextmenu=def (ev): |
| 304 |
if not default_context_menu_should_be_allowed(ev): |
| 305 |
ev.preventDefault() |
| 306 |
, |
| 307 |
# container for iframe and any other panels in the same column |
| 308 |
E.div(style='max-height: 100dvh; min-width: 0; display: flex; flex-direction: column; align-items: stretch; flex-grow:2', |
| 309 |
# container for iframe and its overlay |
| 310 |
E.div(style='position: relative; max-height: 100dvh; min-width: 0; flex-grow: 2; display:flex; align-items: stretch; overflow: hidden', |
| 311 |
left_margin, |
| 312 |
# container for top and bottom margins |
| 313 |
E.div(style='min-width: 0; flex-grow:2; display:flex; align-items:stretch; flex-direction: column; overflow-x: hidden', |
| 314 |
margin_elem(sd, 'margin_top', 'book-top-margin', self.top_margin_clicked, self.margin_context_menu.bind(None, 'top')), |
| 315 |
iframe, |
| 316 |
margin_elem(sd, 'margin_bottom', 'book-bottom-margin', self.bottom_margin_clicked, self.margin_context_menu.bind(None, 'bottom')), |
| 317 |
), |
| 318 |
right_margin, |
| 319 |
self.book_scrollbar.create(), |
| 320 |
# selection bar overlay |
| 321 |
E.div(style='position: absolute; top:0; left:0; width: 100%; height: 100%; display:none;', id='book-selection-bar-overlay'), |
| 322 |
# read aloud overlay |
| 323 |
E.div(style='position: absolute; top:0; left:0; width: 100%; height: 100%; display:none;', id='book-read-aloud-overlay'), |
| 324 |
E.div(style='position: absolute; top:0; left:0; width: 100%; height: 100%; display:none;', id='book-hints-overlay'), # hints overlay |
| 325 |
# search overlay |
| 326 |
E.div(style='position: absolute; top:0; left:0; width: 100%; height:100%; display:none', id=SearchOverlay.CONTAINER_ID), |
| 327 |
# content popup overlay |
| 328 |
E.div(style='position: absolute; top:0; left:0; width: 100%; height: 100%; display:none', id='book-content-popup-overlay'), |
| 329 |
# main overlay |
| 330 |
E.div(style='position: absolute; top:0; left:0; width: 100%; height: 100%; overflow: auto; display:none', id='book-overlay'), |
| 331 |
# controls help overlay |
| 332 |
E.div(style='position: absolute; top:0; left:0; width: 100%; height: 100%; display:none', id='controls-help-overlay'), |
| 333 |
# read audio ebook overlay |
| 334 |
E.div(style='position: absolute; bottom:0em; width: 100%; height: 100%; display:none', id='audio-ebooks-overlay'), |
| 335 |
) |
| 336 |
), |
| 337 |
), |
| 338 |
) |
| 339 |
self.current_color_scheme = resolve_color_scheme() |
| 340 |
if runtime.is_standalone_viewer: |
| 341 |
document.documentElement.addEventListener('keydown', self.handle_keypress, {'passive': False}) |
| 342 |
_hl_colors = all_settings.ui_highlight_colors.default |
| 343 |
set_ui_colors(self.current_color_scheme.is_dark_theme, _hl_colors.dark if self.current_color_scheme.is_dark_theme else _hl_colors.light) |
| 344 |
is_dark_theme(self.current_color_scheme.is_dark_theme) |
| 345 |
self.search_overlay = SearchOverlay(self) |
| 346 |
self.content_popup_overlay = ContentPopupOverlay(self) |
| 347 |
self.overlay = Overlay(self) |
| 348 |
self.selection_bar = SelectionBar(self) |
| 349 |
self.read_aloud = ReadAloud(self) |
| 350 |
self.read_audio_ebook = ReadAudioEbook(self) |
| 351 |
self.hints = Hints(self) |
| 352 |
self.modal_overlays = [self.selection_bar, self.read_aloud, self.hints, self.read_audio_ebook] |
| 353 |
self.processing_spine_item_display = False |
| 354 |
self.pending_load = None |
| 355 |
self.currently_showing = {'selection': {'empty': True}, 'on_load': []} |
| 356 |
self.book_scrollbar.apply_visibility() |
| 357 |
self.annotations_manager = AnnotationsManager(self) |
| 358 |
|
| 359 |
@property |
| 360 |
def iframe(self): |
| 361 |
return self.iframe_wrapper.iframe |
| 362 |
|
| 363 |
def copy_to_clipboard(self): |
| 364 |
self.iframe_wrapper.send_message('copy_selection') |
| 365 |
|
| 366 |
def show_not_a_library_book_error(self): |
| 367 |
error_dialog(_('Not a calibre library book'), _( |
| 368 |
'This book is not a part of a calibre library, so no calibre:// URL for it exists.')) |
| 369 |
|
| 370 |
def copy_current_location_to_clipboard(self, as_url): |
| 371 |
link_prefix = get_current_link_prefix() |
| 372 |
if not link_prefix and as_url: |
| 373 |
return self.show_not_a_library_book_error() |
| 374 |
self.get_current_cfi('copy-location-url', def (req_id, data): |
| 375 |
if as_url: |
| 376 |
text = link_to_epubcfi(data.cfi, link_prefix) |
| 377 |
else: |
| 378 |
text = data.cfi |
| 379 |
ui_operations.copy_selection(text) |
| 380 |
) |
| 381 |
|
| 382 |
def set_scrollbar_visibility(self, visible): |
| 383 |
sd = get_session_data() |
| 384 |
sd.set('book_scrollbar', bool(visible)) |
| 385 |
self.book_scrollbar.apply_visibility() |
| 386 |
|
| 387 |
def toggle_scrollbar(self): |
| 388 |
sd = get_session_data() |
| 389 |
self.set_scrollbar_visibility(not sd.get('book_scrollbar')) |
| 390 |
|
| 391 |
def on_annotations_message(self, data): |
| 392 |
self.selection_bar.handle_message(data) |
| 393 |
|
| 394 |
def on_tts_message(self, data): |
| 395 |
self.read_aloud.handle_message(data) |
| 396 |
|
| 397 |
def on_audio_ebook_message(self, data): |
| 398 |
self.read_audio_ebook.handle_message(data) |
| 399 |
|
| 400 |
def on_hints_message(self, data): |
| 401 |
self.hints.handle_message(data) |
| 402 |
|
| 403 |
def side_margin_clicked(self, which, event): |
| 404 |
backwards = which is 'left' |
| 405 |
if get_session_data().get('reverse_page_turn_zones'): |
| 406 |
backwards = not backwards |
| 407 |
if event.button is 0: |
| 408 |
event.preventDefault(), event.stopPropagation() |
| 409 |
sd = get_session_data() |
| 410 |
self.iframe_wrapper.send_message( |
| 411 |
'next_screen', backwards=backwards, |
| 412 |
flip_if_rtl_page_progression=True, |
| 413 |
all_pages_on_screen=sd.get('paged_margin_clicks_scroll_by_screen')) |
| 414 |
elif event.button is 2: |
| 415 |
event.preventDefault(), event.stopPropagation() |
| 416 |
window.setTimeout(self.show_chrome, 0) |
| 417 |
self.focus_iframe() |
| 418 |
|
| 419 |
def top_margin_clicked(self, event): |
| 420 |
if event.button is 0 or event.button is 2: |
| 421 |
event.preventDefault(), event.stopPropagation() |
| 422 |
self.show_chrome() |
| 423 |
else: |
| 424 |
self.focus_iframe() |
| 425 |
|
| 426 |
def bottom_margin_clicked(self, event): |
| 427 |
if event.button is 0 or event.button is 2: |
| 428 |
event.preventDefault(), event.stopPropagation() |
| 429 |
window.setTimeout(self.show_chrome, 0) |
| 430 |
self.focus_iframe() |
| 431 |
|
| 432 |
def margin_context_menu(self, which, event): |
| 433 |
event.preventDefault(), event.stopPropagation() |
| 434 |
self.show_chrome() |
| 435 |
|
| 436 |
def on_margin_wheel(self, which, event): |
| 437 |
event.preventDefault() |
| 438 |
self.send_wheel_event_to_iframe(event, f'margin-{which}') |
| 439 |
|
| 440 |
def send_wheel_event_to_iframe(self, event, location): |
| 441 |
evt = {'location': location} |
| 442 |
for attr in ('deltaX', 'deltaY', 'deltaMode', 'altKey', 'ctrlKey', 'shiftKey', 'metaKey'): |
| 443 |
evt[attr] = event[attr] |
| 444 |
self.iframe_wrapper.send_message('fake_wheel_event', evt=evt) |
| 445 |
|
| 446 |
def forward_gesture(self, gesture): |
| 447 |
self.iframe_wrapper.send_message('gesture_from_margin', gesture=gesture) |
| 448 |
|
| 449 |
def native_gesture(self, gesture): |
| 450 |
self.iframe_wrapper.send_message('native_gesture', gesture=gesture) |
| 451 |
|
| 452 |
def iframe_size(self): |
| 453 |
iframe = self.iframe |
| 454 |
l, r = document.getElementById('book-left-margin'), document.getElementById('book-right-margin') |
| 455 |
w = r.offsetLeft - l.offsetLeft - iframe.offsetLeft |
| 456 |
t, b = document.getElementById('book-top-margin'), document.getElementById('book-bottom-margin') |
| 457 |
h = b.offsetTop - t.offsetTop - iframe.offsetTop |
| 458 |
return w, h |
| 459 |
|
| 460 |
def on_request_size(self, data): |
| 461 |
# On iOS/Safari window.innerWidth/Height are incorrect inside an iframe |
| 462 |
window.scrollTo(0, 0) # ensure the window is at 0 because otherwise it sometimes moves down a bit on mobile thanks to the disappearing nav bar |
| 463 |
w, h = self.iframe_size() |
| 464 |
self.iframe_wrapper.send_message('window_size', width=w, height=h) |
| 465 |
|
| 466 |
def on_print(self, data): |
| 467 |
print(data.string) |
| 468 |
|
| 469 |
def on_human_scroll(self, data): |
| 470 |
if data.scrolled_by_frac is None: |
| 471 |
self.timers.reset_read_timer() |
| 472 |
else: |
| 473 |
name = self.currently_showing.name |
| 474 |
length = self.book.manifest.files[name]?.length |
| 475 |
if length: |
| 476 |
amt_scrolled = data.scrolled_by_frac * length |
| 477 |
self.timers.on_human_scroll(amt_scrolled) |
| 478 |
|
| 479 |
def on_handle_keypress(self, data): |
| 480 |
self.handle_keypress(data.evt) |
| 481 |
|
| 482 |
def handle_keypress(self, evt): |
| 483 |
if self.overlay.is_visible and evt.key is 'Escape': |
| 484 |
if self.overlay.handle_escape(): |
| 485 |
if evt.preventDefault: |
| 486 |
evt.preventDefault(), evt.stopPropagation() |
| 487 |
|
| 488 |
def overlay_visibility_changed(self, visible): |
| 489 |
if self.iframe_wrapper.send_message: |
| 490 |
self.iframe_wrapper.send_message('overlay_visibility_changed', visible=visible) |
| 491 |
if ui_operations.overlay_visibility_changed: |
| 492 |
ui_operations.overlay_visibility_changed(visible) |
| 493 |
if visible: |
| 494 |
for x in self.modal_overlays: |
| 495 |
x.hide() |
| 496 |
else: |
| 497 |
self.selection_bar.update_position() |
| 498 |
|
| 499 |
def on_handle_shortcut(self, data): |
| 500 |
if not data.name: |
| 501 |
return |
| 502 |
if data.name is 'back': |
| 503 |
window.history.back() |
| 504 |
elif data.name is 'forward': |
| 505 |
window.history.forward() |
| 506 |
elif data.name is 'show_chrome': |
| 507 |
self.show_chrome() |
| 508 |
elif data.name is 'show_chrome_force': |
| 509 |
self.show_chrome_force() |
| 510 |
elif data.name is 'toggle_toc': |
| 511 |
ui_operations.toggle_toc() |
| 512 |
elif data.name is 'toggle_bookmarks': |
| 513 |
if ui_operations.toggle_bookmarks: |
| 514 |
ui_operations.toggle_bookmarks() |
| 515 |
else: |
| 516 |
self.overlay.show_bookmarks() |
| 517 |
elif data.name is 'show_profiles': |
| 518 |
self.overlay.show_profiles() |
| 519 |
elif data.name is 'toggle_highlights': |
| 520 |
ui_operations.toggle_highlights() |
| 521 |
elif data.name is 'new_bookmark': |
| 522 |
self.new_bookmark() |
| 523 |
elif data.name is 'copy_to_clipboard': |
| 524 |
self.copy_to_clipboard() |
| 525 |
elif data.name is 'copy_location_to_clipboard' or data.name is 'copy_location_as_url_to_clipboard': |
| 526 |
self.copy_current_location_to_clipboard('url' in data.name) |
| 527 |
elif data.name is 'toggle_inspector': |
| 528 |
ui_operations.toggle_inspector() |
| 529 |
elif data.name is 'toggle_lookup': |
| 530 |
ui_operations.toggle_lookup() |
| 531 |
elif data.name is 'toggle_full_screen': |
| 532 |
ui_operations.toggle_full_screen() |
| 533 |
elif data.name is 'toggle_paged_mode': |
| 534 |
self.toggle_paged_mode() |
| 535 |
elif data.name is 'toggle_toolbar': |
| 536 |
self.toggle_toolbar() |
| 537 |
elif data.name is 'toggle_scrollbar': |
| 538 |
self.toggle_scrollbar() |
| 539 |
elif data.name is 'quit': |
| 540 |
ui_operations.quit() |
| 541 |
elif data.name is 'start_search': |
| 542 |
self.show_search() |
| 543 |
elif data.name is 'next_match': |
| 544 |
ui_operations.find_next() |
| 545 |
elif data.name is 'previous_match': |
| 546 |
ui_operations.find_next(True) |
| 547 |
elif data.name is 'increase_font_size': |
| 548 |
self.bump_font_size({'increase': True}) |
| 549 |
elif data.name is 'decrease_font_size': |
| 550 |
self.bump_font_size({'increase': False}) |
| 551 |
elif data.name is 'default_font_size': |
| 552 |
restore_default_font_size() |
| 553 |
elif data.name is 'toggle_full_screen': |
| 554 |
ui_operations.toggle_full_screen() |
| 555 |
elif data.name is 'toggle_reference_mode': |
| 556 |
self.toggle_reference_mode() |
| 557 |
elif data.name is 'read_aloud': |
| 558 |
self.start_read_aloud() |
| 559 |
elif data.name is 'toggle_hints': |
| 560 |
self.toggle_hints() |
| 561 |
elif data.name is 'toggle_read_aloud': |
| 562 |
self.toggle_read_aloud() |
| 563 |
elif data.name is 'reload_book': |
| 564 |
self.reload_book() |
| 565 |
elif data.name is 'sync_book': |
| 566 |
self.overlay.sync_book() |
| 567 |
elif data.name is 'next_section': |
| 568 |
self.on_next_section({'forward': True}) |
| 569 |
elif data.name is 'previous_section': |
| 570 |
self.on_next_section({'forward': False}) |
| 571 |
elif data.name is 'open_book': |
| 572 |
self.overlay.open_book() |
| 573 |
elif data.name is 'next': |
| 574 |
self.iframe_wrapper.send_message( |
| 575 |
'next_screen', backwards=False, |
| 576 |
flip_if_rtl_page_progression=False, |
| 577 |
all_pages_on_screen=get_session_data().get('paged_margin_clicks_scroll_by_screen')) |
| 578 |
elif data.name is 'previous': |
| 579 |
self.iframe_wrapper.send_message( |
| 580 |
'next_screen', backwards=True, |
| 581 |
flip_if_rtl_page_progression=False, |
| 582 |
all_pages_on_screen=get_session_data().get('paged_margin_clicks_scroll_by_screen')) |
| 583 |
elif data.name is 'clear_selection': |
| 584 |
self.iframe_wrapper.send_message('clear_selection') |
| 585 |
elif data.name is 'print': |
| 586 |
ui_operations.print_book() |
| 587 |
elif data.name is 'preferences': |
| 588 |
self.show_chrome({'initial_panel': 'show_prefs'}) |
| 589 |
elif data.name is 'metadata': |
| 590 |
self.overlay.show_metadata() |
| 591 |
elif data.name is 'edit_book': |
| 592 |
self.edit_book() |
| 593 |
elif data.name is 'goto_location': |
| 594 |
self.overlay.show_ask_for_location() |
| 595 |
elif data.name is 'select_all': |
| 596 |
self.iframe_wrapper.send_message('modify_selection', granularity='all') |
| 597 |
elif data.name.startsWith('shrink_selection_by_'): |
| 598 |
self.iframe_wrapper.send_message('modify_selection', direction='backward', granularity=data.name.rpartition('_')[-1]) |
| 599 |
elif data.name.startsWith('extend_selection_by_'): |
| 600 |
self.iframe_wrapper.send_message('modify_selection', direction='forward', granularity=data.name.rpartition('_')[-1]) |
| 601 |
elif data.name is 'extend_selection_to_start_of_line': |
| 602 |
self.iframe_wrapper.send_message('modify_selection', direction='backward', granularity='lineboundary') |
| 603 |
elif data.name is 'extend_selection_to_end_of_line': |
| 604 |
self.iframe_wrapper.send_message('modify_selection', direction='forward', granularity='lineboundary') |
| 605 |
elif data.name is 'scrollspeed_increase': |
| 606 |
self.update_scroll_speed(SCROLL_SPEED_STEP) |
| 607 |
elif data.name is 'scrollspeed_decrease': |
| 608 |
self.update_scroll_speed(-SCROLL_SPEED_STEP) |
| 609 |
elif data.name is 'toggle_autoscroll': |
| 610 |
self.toggle_autoscroll() |
| 611 |
elif data.name.startsWith('switch_color_scheme:'): |
| 612 |
self.switch_color_scheme(data.name.partition(':')[-1]) |
| 613 |
elif data.name is 'increase_number_of_columns': |
| 614 |
self.iframe_wrapper.send_message('change_number_of_columns', delta=1) |
| 615 |
elif data.name is 'decrease_number_of_columns': |
| 616 |
self.iframe_wrapper.send_message('change_number_of_columns', delta=-1) |
| 617 |
elif data.name is 'reset_number_of_columns': |
| 618 |
self.iframe_wrapper.send_message('change_number_of_columns', delta=0) |
| 619 |
else: |
| 620 |
self.iframe_wrapper.send_message('handle_navigation_shortcut', name=data.name) |
| 621 |
|
| 622 |
def edit_book(self): |
| 623 |
ui_operations.edit_book(current_spine_item(), self.current_file_progress_frac, self.currently_showing?.selection?.text) |
| 624 |
|
| 625 |
def on_selection_change(self, data): |
| 626 |
self.currently_showing.selection = { |
| 627 |
'text': data.text, 'empty': data.empty, 'start': data.selection_extents.start, |
| 628 |
'end': data.selection_extents.end, 'annot_id': data.annot_id, |
| 629 |
'drag_mouse_position': data.drag_mouse_position, |
| 630 |
'selection_change_caused_by_search': data.selection_change_caused_by_search, |
| 631 |
'rtl': data.rtl, 'vertical': data.vertical, |
| 632 |
'start_is_anchor': data.start_is_anchor |
| 633 |
} |
| 634 |
if ui_operations.selection_changed: |
| 635 |
ui_operations.selection_changed(self.currently_showing.selection.text, self.currently_showing.selection.annot_id) |
| 636 |
self.selection_bar.update_position() |
| 637 |
|
| 638 |
def new_bookmark(self): |
| 639 |
if ui_operations.new_bookmark: |
| 640 |
self.get_current_cfi('new-bookmark', ui_operations.new_bookmark) |
| 641 |
else: |
| 642 |
self.get_current_cfi('new-bookmark', def (req_id, data): |
| 643 |
create_new_bookmark(self.annotations_manager, data) |
| 644 |
) |
| 645 |
|
| 646 |
def update_selection_position(self, data): |
| 647 |
sel = self.currently_showing.selection |
| 648 |
sel.start = data.selection_extents.start |
| 649 |
sel.end = data.selection_extents.end |
| 650 |
self.selection_bar.update_position() |
| 651 |
|
| 652 |
def on_columns_per_screen_changed(self, data): |
| 653 |
sd = get_session_data() |
| 654 |
cps = sd.get('columns_per_screen') or {} |
| 655 |
cps[data.which] = int(data.cps) |
| 656 |
sd.set('columns_per_screen', cps) |
| 657 |
|
| 658 |
def switch_color_scheme(self, name): |
| 659 |
get_session_data().set('current_color_scheme', name) |
| 660 |
ui_operations.redisplay_book() |
| 661 |
|
| 662 |
def toggle_paged_mode(self): |
| 663 |
sd = get_session_data() |
| 664 |
mode = sd.get('read_mode') |
| 665 |
new_mode = 'flow' if mode is 'paged' else 'paged' |
| 666 |
sd.set('read_mode', new_mode) |
| 667 |
ui_operations.redisplay_book() |
| 668 |
|
| 669 |
def toggle_autoscroll(self): |
| 670 |
self.iframe_wrapper.send_message('toggle_autoscroll') |
| 671 |
|
| 672 |
def toggle_toolbar(self): |
| 673 |
sd = get_session_data() |
| 674 |
misc = sd.get('standalone_misc_settings') |
| 675 |
misc.show_actions_toolbar = v'!misc.show_actions_toolbar' |
| 676 |
sd.set('standalone_misc_settings', misc) |
| 677 |
|
| 678 |
def on_invisible_text(self, data): |
| 679 |
warning_dialog( |
| 680 |
_('Not found'), _('The text: <i>{}</i> is present on this page but not visible').format(html_escape(data.text)), |
| 681 |
on_close=def (): |
| 682 |
self.search_overlay.show() |
| 683 |
) |
| 684 |
|
| 685 |
def bump_font_size(self, data): |
| 686 |
mult = 1 if data.increase else -1 |
| 687 |
frac = 0.2 |
| 688 |
if runtime.is_standalone_viewer: |
| 689 |
frac = current_zoom_step_size() / 100 |
| 690 |
change_font_size_by(mult * frac) |
| 691 |
|
| 692 |
def on_show_footnote(self, data): |
| 693 |
self.show_content_popup() |
| 694 |
self.content_popup_overlay.show_footnote(data) |
| 695 |
|
| 696 |
def hide_overlays(self): |
| 697 |
self.overlay.hide() |
| 698 |
self.search_overlay.hide() |
| 699 |
self.content_popup_overlay.hide() |
| 700 |
self.focus_iframe() |
| 701 |
|
| 702 |
@property |
| 703 |
def overlay_prevents_navigation(self): |
| 704 |
for x in self.modal_overlays: |
| 705 |
if x.is_visible and x.prevent_navigation: |
| 706 |
return True |
| 707 |
return False |
| 708 |
|
| 709 |
def focus_iframe(self): |
| 710 |
for x in self.modal_overlays: |
| 711 |
if x.is_visible: |
| 712 |
x.focus() |
| 713 |
return |
| 714 |
self.iframe.contentWindow.focus() |
| 715 |
|
| 716 |
def start_read_aloud(self, dont_start_talking): |
| 717 |
if self.book.manifest.has_smil: |
| 718 |
for x in self.modal_overlays: |
| 719 |
if x is not self.read_audio_ebook: |
| 720 |
x.hide() |
| 721 |
self.read_audio_ebook.show() |
| 722 |
else: |
| 723 |
for x in self.modal_overlays: |
| 724 |
if x is not self.read_aloud: |
| 725 |
x.hide() |
| 726 |
self.read_aloud.show() |
| 727 |
if not dont_start_talking: |
| 728 |
self.read_aloud.play() |
| 729 |
|
| 730 |
def toggle_read_aloud(self): |
| 731 |
if self.read_audio_ebook.is_visible: |
| 732 |
self.read_audio_ebook.hide() |
| 733 |
elif self.read_aloud.is_visible: |
| 734 |
self.read_aloud.hide() |
| 735 |
else: |
| 736 |
self.start_read_aloud() |
| 737 |
|
| 738 |
def toggle_hints(self): |
| 739 |
if self.hints.is_visible: |
| 740 |
self.hints.hide() |
| 741 |
else: |
| 742 |
for x in self.modal_overlays: |
| 743 |
if x is not self.hints: |
| 744 |
x.hide() |
| 745 |
self.hints.show() |
| 746 |
|
| 747 |
def show_chrome(self, data): |
| 748 |
if self.content_popup_overlay.is_visible: |
| 749 |
self.content_popup_overlay.hide() |
| 750 |
return |
| 751 |
self.content_popup_overlay |
| 752 |
elements = {} |
| 753 |
if data and data.elements: |
| 754 |
elements = data.elements |
| 755 |
initial_panel = data?.initial_panel or None |
| 756 |
self.get_current_cfi('show-chrome', self.do_show_chrome.bind(None, elements, initial_panel)) |
| 757 |
|
| 758 |
def show_chrome_force(self): |
| 759 |
self.hide_overlays() |
| 760 |
self.show_chrome() |
| 761 |
|
| 762 |
def do_show_chrome(self, elements, initial_panel, request_id, cfi_data): |
| 763 |
self.hide_overlays() |
| 764 |
self.update_cfi_data(cfi_data) |
| 765 |
if initial_panel: |
| 766 |
getattr(self.overlay, initial_panel)() |
| 767 |
else: |
| 768 |
self.overlay.show(elements) |
| 769 |
|
| 770 |
def prepare_for_close(self): |
| 771 |
def close_prepared(request_id, cfi_data): |
| 772 |
ui_operations.close_prep_finished(cfi_data.cfi) |
| 773 |
|
| 774 |
self.get_current_cfi('prepare-close', close_prepared) |
| 775 |
|
| 776 |
def show_search(self, trigger): |
| 777 |
self.hide_overlays() |
| 778 |
text = self.currently_showing.selection.text |
| 779 |
if runtime.is_standalone_viewer: |
| 780 |
ui_operations.show_search(text or '', trigger) |
| 781 |
else: |
| 782 |
if text: |
| 783 |
self.search_overlay.set_text(text) |
| 784 |
self.search_overlay.show(text) |
| 785 |
if trigger and text: |
| 786 |
self.search_overlay.find_next() |
| 787 |
|
| 788 |
def show_content_popup(self): |
| 789 |
self.hide_overlays() |
| 790 |
self.content_popup_overlay.show() |
| 791 |
|
| 792 |
def set_margins(self): |
| 793 |
no_margins = self.currently_showing.name is self.book.manifest.title_page_name |
| 794 |
sd = get_session_data() |
| 795 |
margin_left = 0 if no_margins else sd.get('margin_left') |
| 796 |
margin_right = 0 if no_margins else sd.get('margin_right') |
| 797 |
margin_top = 0 if no_margins else sd.get('margin_top') |
| 798 |
margin_bottom = 0 if no_margins else sd.get('margin_bottom') |
| 799 |
# Use reader container size (not window size) so side nav/top bars |
| 800 |
# do not skew overlay/control positioning. |
| 801 |
container_width = self.container.clientWidth or window.innerWidth |
| 802 |
container_height = self.container.clientHeight or window.innerHeight |
| 803 |
max_text_height = sd.get('max_text_height') |
| 804 |
th = container_height - margin_top - margin_bottom |
| 805 |
if not no_margins and max_text_height > 100 and th > max_text_height: |
| 806 |
extra = (th - max_text_height) // 2 |
| 807 |
margin_top += extra |
| 808 |
margin_bottom += extra |
| 809 |
max_text_width = sd.get('max_text_width') |
| 810 |
tw = container_width - margin_left - margin_right |
| 811 |
if not no_margins and max_text_width > 100 and tw > max_text_width: |
| 812 |
extra = (tw - max_text_width) // 2 |
| 813 |
margin_left += extra |
| 814 |
margin_right += extra |
| 815 |
set_css(document.getElementById('book-top-margin'), height=margin_top + 'px', font_size=header_footer_font_size(margin_top) + 'px') |
| 816 |
set_css(document.getElementById('book-bottom-margin'), height=margin_bottom + 'px', font_size=header_footer_font_size(margin_bottom) + 'px') |
| 817 |
|
| 818 |
def side_margin(which, val): |
| 819 |
m = document.getElementById('book-{}-margin'.format(which)) |
| 820 |
set_css(m, width=val + 'px') |
| 821 |
val = min(val, 25) |
| 822 |
s = m.querySelector('.arrow').style |
| 823 |
s.width = val + 'px' |
| 824 |
s.height = val + 'px' |
| 825 |
side_margin('left', margin_left), side_margin('right', margin_right) |
| 826 |
|
| 827 |
def on_iframe_ready(self, data): |
| 828 |
data.width, data.height = self.iframe_size() |
| 829 |
if ui_operations.on_iframe_ready: |
| 830 |
ui_operations.on_iframe_ready() |
| 831 |
return self.do_pending_load |
| 832 |
|
| 833 |
def do_pending_load(self): |
| 834 |
if self.pending_load: |
| 835 |
data = self.pending_load |
| 836 |
self.pending_load = None |
| 837 |
self.show_spine_item_stage2(data) |
| 838 |
|
| 839 |
def on_iframe_error(self, data): |
| 840 |
title = data.title or _('There was an error processing the book') |
| 841 |
msg = _('Unknown error') |
| 842 |
if data.errkey: |
| 843 |
if data.errkey is 'no-auto-scroll-in-paged-mode': |
| 844 |
title = _('No auto scroll in paged mode') |
| 845 |
msg = _('Switch to flow mode (Viewer preferences->Page layout) to enable auto scrolling') |
| 846 |
elif data.errkey is 'changing-columns-in-flow-mode': |
| 847 |
title = _('In flow mode') |
| 848 |
msg = _('Cannot change number of pages per screen in flow mode, switch to paged mode first.') |
| 849 |
elif data.errkey = 'unhandled-error': |
| 850 |
window.onerror(data.msg, data.script_url, data.line_number, data.column_number, |
| 851 |
traceback.unserialize_error_for_formatting(data.error_object)) |
| 852 |
return |
| 853 |
if data.is_non_critical: |
| 854 |
warning_dialog(title, msg, data.details, on_close=ui_operations.focus_iframe) |
| 855 |
return |
| 856 |
ui_operations.show_error(title, msg, data.details) |
| 857 |
|
| 858 |
def apply_color_scheme(self): |
| 859 |
self.current_color_scheme = ans = resolve_color_scheme() |
| 860 |
iframe = self.iframe |
| 861 |
if runtime.is_standalone_viewer: |
| 862 |
_hl_colors = all_settings.ui_highlight_colors.default |
| 863 |
set_ui_colors(self.current_color_scheme.is_dark_theme, _hl_colors.dark if self.current_color_scheme.is_dark_theme else _hl_colors.light) |
| 864 |
else: |
| 865 |
iframe.style.colorScheme = 'dark' if self.current_color_scheme.is_dark_theme else 'light' |
| 866 |
|
| 867 |
is_dark_theme(self.current_color_scheme.is_dark_theme) |
| 868 |
for which in 'left top right bottom'.split(' '): |
| 869 |
m = document.getElementById('book-{}-margin'.format(which)) |
| 870 |
s = m.style |
| 871 |
mc = ans[f'margin_{which}'] |
| 872 |
if mc: |
| 873 |
s.backgroundColor, s.color = mc.split(':') |
| 874 |
else: |
| 875 |
s.color = ans.foreground |
| 876 |
s.backgroundColor = 'transparent' |
| 877 |
sd = get_session_data() |
| 878 |
for node in [iframe, iframe.parentNode]: |
| 879 |
node.style.backgroundColor = 'transparent' |
| 880 |
node.style.backgroundImage = 'none' |
| 881 |
s = iframe.parentNode.parentNode.style |
| 882 |
bg_image = sd.get('background_image') |
| 883 |
if bg_image: |
| 884 |
s.backgroundImage = f'url({modify_background_image_url_for_fetch(bg_image)})' |
| 885 |
fade = int(sd.get('background_image_fade')) |
| 886 |
if fade > 0: |
| 887 |
rgba = cached_color_to_rgba(ans.background) |
| 888 |
bg_image_fade = f'rgba({rgba[0]}, {rgba[1]}, {rgba[2]}, {fade/100})' |
| 889 |
iframe.parentNode.style.backgroundColor = bg_image_fade |
| 890 |
for which in 'left right'.split(' '): |
| 891 |
ms = document.getElementById(f'book-{which}-margin').style |
| 892 |
if ms.backgroundColor is 'transparent': |
| 893 |
ms.backgroundColor = bg_image_fade |
| 894 |
else: |
| 895 |
s.backgroundColor = ans.background or 'white' |
| 896 |
s.backgroundImage = 'none' |
| 897 |
if sd.get('background_image_style') is 'scaled': |
| 898 |
s.backgroundSize = '100% 100%' |
| 899 |
s.backgroundRepeat = 'no-repeat' |
| 900 |
s.backgroundAttachment = 'scroll' |
| 901 |
s.backgroundPosition = 'center' |
| 902 |
else: |
| 903 |
s.backgroundSize = 'auto' |
| 904 |
s.backgroundRepeat = 'repeat' |
| 905 |
s.backgroundAttachment = 'scroll' |
| 906 |
s.backgroundPosition = '0 0' |
| 907 |
|
| 908 |
self.content_popup_overlay.apply_color_scheme(ans.background, ans.foreground) |
| 909 |
self.book_scrollbar.apply_color_scheme(ans) |
| 910 |
return ans |
| 911 |
|
| 912 |
def on_resize(self): |
| 913 |
if self.book and self.currently_showing.name: |
| 914 |
sd = get_session_data() |
| 915 |
if sd.get('max_text_width') or sd.get('max_text_height'): |
| 916 |
self.set_margins() |
| 917 |
|
| 918 |
def show_loading_message(self, msg): |
| 919 |
self.overlay.show_loading_message(msg) |
| 920 |
|
| 921 |
def show_loading(self): |
| 922 |
msg = _('Loading next section from <i>{title}</i>, please wait…').format(title=self.book.metadata.title or _('Unknown')) |
| 923 |
if self.show_loading_callback_timer is not None: |
| 924 |
clearTimeout(self.show_loading_callback_timer) |
| 925 |
self.show_loading_callback_timer = setTimeout(self.show_loading_message.bind(None, msg), 200) |
| 926 |
|
| 927 |
def hide_loading(self): |
| 928 |
if window.read_book_initial_open_search_text: |
| 929 |
q = window.read_book_initial_open_search_text |
| 930 |
v'delete window.read_book_initial_open_search_text' |
| 931 |
self.search_overlay.do_initial_search(q.text, q.query) |
| 932 |
self.show_loading_message(_('Searching for: {}').format(q.query)) |
| 933 |
return |
| 934 |
if self.show_loading_callback_timer is not None: |
| 935 |
clearTimeout(self.show_loading_callback_timer) |
| 936 |
self.show_loading_callback_timer = None |
| 937 |
self.iframe.style.visibility = 'visible' |
| 938 |
self.overlay.hide_loading_message() |
| 939 |
self.focus_iframe() |
| 940 |
|
| 941 |
def parse_cfi(self, encoded_cfi, book): |
| 942 |
name = cfi = None |
| 943 |
if encoded_cfi and encoded_cfi.startswith('epubcfi(/'): |
| 944 |
cfi = encoded_cfi[len('epubcfi(/'):-1] |
| 945 |
snum, rest = cfi.partition('/')[::2] |
| 946 |
try: |
| 947 |
snum = int(snum) |
| 948 |
except Exception: |
| 949 |
print('Invalid spine number in CFI:', snum) |
| 950 |
if jstype(snum) is 'number': |
| 951 |
name = book.manifest.spine[(int(snum) // 2) - 1] or name |
| 952 |
cfi = '/' + rest |
| 953 |
return name, cfi |
| 954 |
|
| 955 |
def open_book_page(self): |
| 956 |
# Open the page for the current book in a new tab |
| 957 |
if self.book and self.book.key: |
| 958 |
show_panel('book_details', {'library_id': self.book.key[0], 'book_id': self.book.key[1] + ''}, replace=False) |
| 959 |
|
| 960 |
def clear_book_resource_caches(self): |
| 961 |
self.loaded_resources = {} |
| 962 |
self.content_popup_overlay.loaded_resources = {} |
| 963 |
|
| 964 |
def reload_book(self): |
| 965 |
self.clear_book_resource_caches() |
| 966 |
ui_operations.reload_book() |
| 967 |
|
| 968 |
def display_book(self, book, initial_position, is_redisplay): |
| 969 |
self.hide_overlays() |
| 970 |
self.iframe.focus() |
| 971 |
is_current_book = self.book and self.book.key == book.key |
| 972 |
self.book_load_started = True |
| 973 |
if is_current_book: |
| 974 |
if not is_redisplay: |
| 975 |
self.search_overlay.clear_caches(book) # could be a reload |
| 976 |
else: |
| 977 |
if self.book: |
| 978 |
self.iframe_wrapper.reset() |
| 979 |
self.content_popup_overlay.reset() |
| 980 |
self.clear_book_resource_caches() |
| 981 |
self.timers.start_book(book) |
| 982 |
self.search_overlay.clear_caches(book) |
| 983 |
unkey = username_key(get_interface_data().username) |
| 984 |
self.book = current_book.book = book |
| 985 |
hl = None |
| 986 |
if not is_redisplay: |
| 987 |
if self.read_audio_ebook: |
| 988 |
self.read_audio_ebook.hide() |
| 989 |
clear(self.read_audio_ebook.container) |
| 990 |
if runtime.is_standalone_viewer: |
| 991 |
hl = book.highlights |
| 992 |
v'delete book.highlights' |
| 993 |
else: |
| 994 |
if unkey and book.annotations_map[unkey]: |
| 995 |
hl = book.annotations_map[unkey].highlight |
| 996 |
self.annotations_manager.set_bookmarks(book.annotations_map[unkey].bookmark or []) |
| 997 |
self.annotations_manager.set_highlights(hl or []) |
| 998 |
if runtime.is_standalone_viewer: |
| 999 |
add_book_to_recently_viewed(book) |
| 1000 |
if ui_operations.update_last_read_time: |
| 1001 |
ui_operations.update_last_read_time(book) |
| 1002 |
pos = {'replace_history': True} |
| 1003 |
if not book.manifest.spine.length: |
| 1004 |
ui_operations.show_error(_('Invalid book'), _('This book is empty, with no items in the spine')) |
| 1005 |
return |
| 1006 |
name = book.manifest.spine[0] |
| 1007 |
cfi = None |
| 1008 |
if initial_position and initial_position.type is 'cfi' and initial_position.data.startswith('epubcfi(/'): |
| 1009 |
cfi = initial_position.data |
| 1010 |
else: |
| 1011 |
q = parse_url_params() |
| 1012 |
if q.bookpos and q.bookpos.startswith('epubcfi(/'): |
| 1013 |
cfi = q.bookpos |
| 1014 |
elif book.last_read_position and book.last_read_position[unkey]: |
| 1015 |
cfi = book.last_read_position[unkey] |
| 1016 |
cfiname, internal_cfi = self.parse_cfi(cfi, book) |
| 1017 |
if cfiname and internal_cfi: |
| 1018 |
name = cfiname |
| 1019 |
pos.type, pos.cfi = 'cfi', internal_cfi |
| 1020 |
navigated = False |
| 1021 |
if initial_position: |
| 1022 |
if initial_position.type is 'toc': |
| 1023 |
navigated = self.goto_toc_node(initial_position.data) |
| 1024 |
elif initial_position.type is 'bookpos': |
| 1025 |
navigated = True |
| 1026 |
self.goto_book_position(initial_position.data) |
| 1027 |
elif initial_position.type is 'ref': |
| 1028 |
navigated = self.goto_reference(initial_position.data) |
| 1029 |
if navigated: |
| 1030 |
self.hide_loading() |
| 1031 |
else: |
| 1032 |
self.show_name(name, initial_position=pos) |
| 1033 |
sd = get_session_data() |
| 1034 |
help_key = 'controls_help_shown_count' + ('_rtl_page_progression' if rtl_page_progression() else '') |
| 1035 |
if not self[help_key]: |
| 1036 |
self[help_key] = True |
| 1037 |
c = sd.get(help_key, 0) |
| 1038 |
if c < 2: |
| 1039 |
show_controls_help() |
| 1040 |
sd.set('controls_help_shown_count' + ('_rtl_page_progression' if rtl_page_progression() else ''), c + 1) |
| 1041 |
|
| 1042 |
def preferences_changed(self): |
| 1043 |
self.set_margins() |
| 1044 |
ui_operations.update_url_state(True) |
| 1045 |
ui_operations.redisplay_book() |
| 1046 |
|
| 1047 |
def redisplay_book(self): |
| 1048 |
# redisplay_book() is called when settings are changed |
| 1049 |
if not self.book: |
| 1050 |
if runtime.is_standalone_viewer: |
| 1051 |
self.overlay.open_book() |
| 1052 |
return |
| 1053 |
sd = get_session_data() |
| 1054 |
self.keyboard_shortcut_map = create_shortcut_map(sd.get('keyboard_shortcuts')) |
| 1055 |
if ui_operations.export_shortcut_map: |
| 1056 |
ui_operations.export_shortcut_map(self.keyboard_shortcut_map) |
| 1057 |
self.book_scrollbar.apply_visibility() |
| 1058 |
self.display_book(self.book, None, True) |
| 1059 |
|
| 1060 |
def iframe_settings(self, name): |
| 1061 |
sd = get_session_data() |
| 1062 |
cs = self.apply_color_scheme() |
| 1063 |
return { |
| 1064 |
'margin_left': 0 if name is self.book.manifest.title_page_name else sd.get('margin_left'), |
| 1065 |
'margin_right': 0 if name is self.book.manifest.title_page_name else sd.get('margin_right'), |
| 1066 |
'margin_top': 0 if name is self.book.manifest.title_page_name else sd.get('margin_top'), |
| 1067 |
'margin_bottom': 0 if name is self.book.manifest.title_page_name else sd.get('margin_bottom'), |
| 1068 |
'read_mode': sd.get('read_mode'), |
| 1069 |
'columns_per_screen': sd.get('columns_per_screen'), |
| 1070 |
'color_scheme': cs, |
| 1071 |
'override_book_colors': sd.get('override_book_colors'), |
| 1072 |
'is_dark_theme': cs.is_dark_theme, |
| 1073 |
'base_font_size': sd.get('base_font_size'), |
| 1074 |
'user_stylesheet': sd.get('user_stylesheet'), |
| 1075 |
'keyboard_shortcuts': sd.get('keyboard_shortcuts'), |
| 1076 |
'hide_tooltips': sd.get('hide_tooltips'), |
| 1077 |
'cover_preserve_aspect_ratio': sd.get('cover_preserve_aspect_ratio'), |
| 1078 |
'paged_wheel_scrolls_by_screen': sd.get('paged_wheel_scrolls_by_screen'), |
| 1079 |
'paged_wheel_section_jumps': sd.get('paged_wheel_section_jumps'), |
| 1080 |
'paged_pixel_scroll_threshold': sd.get('paged_pixel_scroll_threshold'), |
| 1081 |
'lines_per_sec_auto': sd.get('lines_per_sec_auto'), |
| 1082 |
'lines_per_sec_smooth': sd.get('lines_per_sec_smooth'), |
| 1083 |
'scroll_auto_boundary_delay': sd.get('scroll_auto_boundary_delay'), |
| 1084 |
'scroll_mouse_boundary_delay': sd.get('scroll_mouse_boundary_delay'), |
| 1085 |
'scroll_stop_boundaries': sd.get('scroll_stop_boundaries'), |
| 1086 |
'reverse_page_turn_zones': sd.get('reverse_page_turn_zones'), |
| 1087 |
'gesture_overrides': sd.get('gesture_overrides'), |
| 1088 |
} |
| 1089 |
|
| 1090 |
def show_name(self, name, initial_position=None): |
| 1091 |
if self.currently_showing.loading: |
| 1092 |
return |
| 1093 |
self.processing_spine_item_display = False |
| 1094 |
initial_position = initial_position or {'replace_history': False} |
| 1095 |
spine = self.book.manifest.spine |
| 1096 |
idx = spine.indexOf(name) |
| 1097 |
self.currently_showing = { |
| 1098 |
'name': name, 'settings': self.iframe_settings(name), 'initial_position': initial_position, |
| 1099 |
'loading': True, 'spine_index': idx, 'selection': {'empty': True}, 'on_load': [], |
| 1100 |
} |
| 1101 |
self.timers.set_chapter(name) |
| 1102 |
self.show_loading() |
| 1103 |
set_current_spine_item(name) |
| 1104 |
if idx > -1: |
| 1105 |
self.currently_showing.bookpos = 'epubcfi(/{})'.format(2 * (idx + 1)) |
| 1106 |
self.set_margins() |
| 1107 |
self.load_doc(name, self.show_spine_item) |
| 1108 |
|
| 1109 |
def load_doc(self, name, done_callback): |
| 1110 |
def cb(resource_data): |
| 1111 |
self.loaded_resources = resource_data |
| 1112 |
done_callback(resource_data) |
| 1113 |
load_resources(self.book, name, self.loaded_resources, cb) |
| 1114 |
|
| 1115 |
def goto_doc_boundary(self, start): |
| 1116 |
name = self.book.manifest.spine[0 if start else self.book.manifest.spine.length - 1] |
| 1117 |
self.show_name(name, initial_position={'type': 'frac', 'frac': 0 if start else 1, 'replace_history': False}) |
| 1118 |
|
| 1119 |
def goto_frac(self, frac): |
| 1120 |
if not self.book or not self.book.manifest: |
| 1121 |
return |
| 1122 |
chapter_start_page = 0 |
| 1123 |
total_length = self.book.manifest.spine_length |
| 1124 |
page = total_length * frac |
| 1125 |
chapter_frac = 0 |
| 1126 |
chapter_name = None |
| 1127 |
for name in self.book.manifest.spine: |
| 1128 |
chapter_length = self.book.manifest.files[name]?.length or 0 |
| 1129 |
chapter_end_page = chapter_start_page + chapter_length |
| 1130 |
if chapter_start_page <= page <= chapter_end_page: |
| 1131 |
num_pages = chapter_end_page - chapter_start_page - 1 |
| 1132 |
if num_pages > 0: |
| 1133 |
chapter_frac = (page - chapter_start_page) / num_pages |
| 1134 |
else: |
| 1135 |
chapter_frac = 0 |
| 1136 |
chapter_name = name |
| 1137 |
break |
| 1138 |
chapter_start_page = chapter_end_page |
| 1139 |
if not chapter_name: |
| 1140 |
chapter_name = self.book.manifest.spine[-1] |
| 1141 |
chapter_frac = max(0, min(chapter_frac, 1)) |
| 1142 |
if self.currently_showing.name is chapter_name: |
| 1143 |
self.iframe_wrapper.send_message('scroll_to_frac', frac=chapter_frac) |
| 1144 |
else: |
| 1145 |
self.show_name(chapter_name, initial_position={'type': 'frac', 'frac': chapter_frac, 'replace_history': True}) |
| 1146 |
|
| 1147 |
def goto_book_position(self, bpos): |
| 1148 |
pages = Math.ceil(self.current_position_data.book_length / CHARS_PER_PAGE) |
| 1149 |
val = max(0, min(float(bpos) / pages, 1)) |
| 1150 |
return self.goto_frac(val) |
| 1151 |
|
| 1152 |
def goto_pagelist_item(self, item): |
| 1153 |
name = item.dest |
| 1154 |
frag = item.frag or '' |
| 1155 |
self.show_name(name, initial_position={'type': 'pagelist_ref', 'anchor': frag, 'replace_history': False}) |
| 1156 |
|
| 1157 |
def on_link_to_missing_activated(self, data): |
| 1158 |
ui_operations.show_error(_('Invalid link'), _('This link points to the file {} which does not exist in the book').format(data.name)) |
| 1159 |
|
| 1160 |
def on_scroll_to_anchor(self, data): |
| 1161 |
self.show_name(data.name, initial_position={'type': 'anchor', 'anchor': data.frag, 'replace_history': False}) |
| 1162 |
|
| 1163 |
def link_in_content_popup_activated(self, name, frag, is_popup, title): |
| 1164 |
self.content_popup_overlay.hide() |
| 1165 |
if is_popup: |
| 1166 |
self.iframe_wrapper.send_message('fake_popup_activation', name=name, frag=frag, title=title) |
| 1167 |
else: |
| 1168 |
self.goto_named_destination(name, frag) |
| 1169 |
|
| 1170 |
def goto_cfi(self, bookpos, add_to_history): |
| 1171 |
cfiname, internal_cfi = self.parse_cfi(bookpos, self.book) |
| 1172 |
if cfiname and internal_cfi: |
| 1173 |
# Note that goto_cfi is used by back() so it must not add to |
| 1174 |
# history by default, otherwise forward() will not work |
| 1175 |
pos = {'replace_history': not add_to_history} |
| 1176 |
name = cfiname |
| 1177 |
pos.type, pos.cfi = 'cfi', internal_cfi |
| 1178 |
self.show_name(name, initial_position=pos) |
| 1179 |
return True |
| 1180 |
return False |
| 1181 |
|
| 1182 |
def goto_reference(self, reference): |
| 1183 |
if not self.book or not self.book.manifest: |
| 1184 |
return |
| 1185 |
index, refnum = reference.split('.') |
| 1186 |
index, refnum = int(index), (int(refnum) if refnum else 1) |
| 1187 |
chapter_name = self.book.manifest.spine[index] |
| 1188 |
if not chapter_name: |
| 1189 |
return False |
| 1190 |
if self.currently_showing.name is chapter_name: |
| 1191 |
self.iframe_wrapper.send_message('scroll_to_ref', refnum=refnum) |
| 1192 |
else: |
| 1193 |
self.show_name(chapter_name, initial_position={'type': 'ref', 'refnum': refnum, 'replace_history': True}) |
| 1194 |
return True |
| 1195 |
|
| 1196 |
def goto_named_destination(self, name, frag): |
| 1197 |
if self.currently_showing.name is name: |
| 1198 |
self.iframe_wrapper.send_message('scroll_to_anchor', frag=frag) |
| 1199 |
else: |
| 1200 |
spine = self.book.manifest.spine |
| 1201 |
idx = spine.indexOf(name) |
| 1202 |
if idx is -1: |
| 1203 |
error_dialog(_('Destination does not exist'), _( |
| 1204 |
'The file {} does not exist in this book').format(name), on_close=def (): |
| 1205 |
ui_operations.focus_iframe() |
| 1206 |
) |
| 1207 |
return False |
| 1208 |
self.show_name(name, initial_position={'type': 'anchor', 'anchor': frag, 'replace_history': False}) |
| 1209 |
return True |
| 1210 |
|
| 1211 |
def goto_toc_node(self, node_id): |
| 1212 |
if self.overlay_prevents_navigation: |
| 1213 |
return |
| 1214 |
toc = self.book.manifest.toc |
| 1215 |
found = False |
| 1216 |
|
| 1217 |
def process_node(x): |
| 1218 |
nonlocal found |
| 1219 |
if x.id is node_id: |
| 1220 |
self.goto_named_destination(x.dest or '', x.frag or '') |
| 1221 |
found = True |
| 1222 |
return |
| 1223 |
for c in x.children: |
| 1224 |
process_node(c) |
| 1225 |
if toc: |
| 1226 |
process_node(toc) |
| 1227 |
return found |
| 1228 |
|
| 1229 |
def sync_data_received(self, reading_pos_cfi, annotations_map): |
| 1230 |
if annotations_map: |
| 1231 |
ui_operations.annotations_synced(annotations_map) |
| 1232 |
if annotations_map.highlight: |
| 1233 |
if self.annotations_manager.merge_highlights(annotations_map.highlight): |
| 1234 |
hl = self.annotations_manager.highlights_for_currently_showing() |
| 1235 |
self.iframe_wrapper.send_message('replace_highlights', highlights=hl) |
| 1236 |
if reading_pos_cfi: |
| 1237 |
self.goto_cfi(reading_pos_cfi) |
| 1238 |
|
| 1239 |
def set_notes_for_highlight(self, uuid, notes): |
| 1240 |
if self.annotations_manager.set_notes_for_highlight(uuid, notes): |
| 1241 |
self.selection_bar.notes_edited(uuid) |
| 1242 |
self.selection_bar.update_position() |
| 1243 |
|
| 1244 |
def set_style_for_highlights(self, updates): |
| 1245 |
if self.annotations_manager.set_style_for_highlights(updates): |
| 1246 |
hl = self.annotations_manager.highlights_for_currently_showing() |
| 1247 |
self.iframe_wrapper.send_message('replace_highlights', highlights=hl) |
| 1248 |
|
| 1249 |
def add_notes_or_create_highlight(self, notes, style): |
| 1250 |
if self.selection_bar.is_visible: |
| 1251 |
return self.selection_bar.add_notes_or_create_highlight(notes, style) |
| 1252 |
self.show_error(_('No selection'), _('Cannot create highlight as there is no active selection')) |
| 1253 |
|
| 1254 |
def show_next_spine_item(self, previous): |
| 1255 |
spine = self.book.manifest.spine |
| 1256 |
idx = spine.indexOf(self.currently_showing.name) |
| 1257 |
if previous: |
| 1258 |
if idx is 0: |
| 1259 |
return False |
| 1260 |
idx = min(spine.length - 1, max(idx - 1, 0)) |
| 1261 |
self.show_name(spine[idx], initial_position={'type': 'frac', 'frac': 1, 'replace_history': True}) |
| 1262 |
else: |
| 1263 |
if idx is spine.length - 1: |
| 1264 |
return False |
| 1265 |
idx = max(0, min(spine.length - 1, idx + 1)) |
| 1266 |
self.show_name(spine[idx], initial_position={'type': 'frac', 'frac': 0, 'replace_history': True}) |
| 1267 |
return True |
| 1268 |
|
| 1269 |
def on_next_spine_item(self, data): |
| 1270 |
self.show_next_spine_item(data.previous) |
| 1271 |
|
| 1272 |
def on_next_section(self, data): |
| 1273 |
toc_node = get_next_section(data.forward) |
| 1274 |
if toc_node: |
| 1275 |
self.goto_named_destination(toc_node.dest, toc_node.frag) |
| 1276 |
|
| 1277 |
def get_current_cfi(self, request_id, callback): |
| 1278 |
self.get_cfi_counter += 1 |
| 1279 |
request_id += ':' + self.get_cfi_counter |
| 1280 |
self.report_cfi_callbacks[request_id] = callback |
| 1281 |
self.iframe_wrapper.send_message('get_current_cfi', request_id=request_id) |
| 1282 |
|
| 1283 |
def update_cfi_data(self, data): |
| 1284 |
if ui_operations.update_last_read_position: |
| 1285 |
ui_operations.update_last_read_position(data.cfi, data.progress_frac) |
| 1286 |
username = get_interface_data().username |
| 1287 |
if self.book: |
| 1288 |
self.currently_showing.bookpos = data.cfi |
| 1289 |
unkey = username_key(username) |
| 1290 |
if not self.book.last_read_position: |
| 1291 |
self.book.last_read_position = {} |
| 1292 |
self.book.last_read_position[unkey] = data.cfi |
| 1293 |
self.set_progress_frac(data.progress_frac, data.file_progress_frac, data.page_counts) |
| 1294 |
self.update_header_footer() |
| 1295 |
if ui_operations.update_last_read_time: |
| 1296 |
ui_operations.update_last_read_time(self.book) |
| 1297 |
return username |
| 1298 |
|
| 1299 |
def on_report_cfi(self, data): |
| 1300 |
cb = self.report_cfi_callbacks[data.request_id] |
| 1301 |
if cb: |
| 1302 |
cb(data.request_id.rpartition(':')[0], { |
| 1303 |
'cfi': data.cfi, |
| 1304 |
'progress_frac': data.progress_frac, |
| 1305 |
'file_progress_frac': data.file_progress_frac, |
| 1306 |
'selected_text': data.selected_text, |
| 1307 |
'selection_bounds': data.selection_bounds, |
| 1308 |
'page_counts': data.page_counts |
| 1309 |
}) |
| 1310 |
v'delete self.report_cfi_callbacks[data.request_id]' |
| 1311 |
|
| 1312 |
def on_update_progress_frac(self, data): |
| 1313 |
self.set_progress_frac(data.progress_frac, data.file_progress_frac, data.page_counts) |
| 1314 |
self.update_header_footer() |
| 1315 |
|
| 1316 |
def on_update_cfi(self, data): |
| 1317 |
overlay_shown = not self.processing_spine_item_display and self.overlay.is_visible |
| 1318 |
if overlay_shown or self.search_overlay.is_visible or self.content_popup_overlay.is_visible: |
| 1319 |
# Chrome on Android stupidly resizes the viewport when the on |
| 1320 |
# screen keyboard is displayed. This means that the push_state() |
| 1321 |
# below causes the overlay to be closed, making it impossible to |
| 1322 |
# type anything into text boxes. |
| 1323 |
# See https://bugs.chromium.org/p/chromium/issues/detail?id=404315 |
| 1324 |
return |
| 1325 |
username = self.update_cfi_data(data) |
| 1326 |
ui_operations.update_url_state(data.replace_history) |
| 1327 |
if username: |
| 1328 |
key = self.book.key |
| 1329 |
lrd = {'device': get_device_uuid(), 'cfi': data.cfi, 'pos_frac': data.progress_frac} |
| 1330 |
self.last_position_data = lrd |
| 1331 |
ajax_send('book-set-last-read-position/{library_id}/{book_id}/{fmt}'.format( |
| 1332 |
library_id=key[0], book_id=key[1], fmt=key[2]), lrd, def (end_type, xhr, ev): |
| 1333 |
if end_type is not 'load': |
| 1334 |
print('Failed to update last read position, AJAX call did not succeed') |
| 1335 |
) |
| 1336 |
update_book_in_recently_read_by_user_on_home_page(key[0], key[1], key[2], data.cfi) |
| 1337 |
|
| 1338 |
@property |
| 1339 |
def current_position_data(self): |
| 1340 |
if self.book?.manifest: |
| 1341 |
book_length = self.book.manifest.spine_length or 0 |
| 1342 |
name = self.currently_showing.name |
| 1343 |
chapter_length = self.book.manifest.files[name]?.length or 0 |
| 1344 |
else: |
| 1345 |
book_length = chapter_length = 0 |
| 1346 |
pos = { |
| 1347 |
'progress_frac': self.current_progress_frac, |
| 1348 |
'book_length': book_length, 'chapter_length': chapter_length, |
| 1349 |
'file_progress_frac': self.current_file_progress_frac, |
| 1350 |
'cfi': self.currently_showing?.bookpos, |
| 1351 |
'page_counts': self.current_page_counts, |
| 1352 |
} |
| 1353 |
return pos |
| 1354 |
|
| 1355 |
def show_status_message(self, msg, timeout): |
| 1356 |
self.current_status_message = msg or '' |
| 1357 |
self.update_header_footer() |
| 1358 |
if self.current_status_message: |
| 1359 |
if not timeout?: |
| 1360 |
timeout = 10000 |
| 1361 |
window.setTimeout(def (): self.show_status_message();, timeout) |
| 1362 |
|
| 1363 |
def upload_current_position(self, callback): |
| 1364 |
if not self.last_position_data or not self.book: |
| 1365 |
callback(True) |
| 1366 |
return |
| 1367 |
key = self.book.key |
| 1368 |
ajax_send('book-set-last-read-position/{library_id}/{book_id}/{fmt}'.format( |
| 1369 |
library_id=key[0], book_id=key[1], fmt=key[2]), |
| 1370 |
self.last_position_data, def (end_type, xhr, ev): |
| 1371 |
callback(end_type is 'load') |
| 1372 |
) |
| 1373 |
|
| 1374 |
def show_unsynced_indicator(self, visible): |
| 1375 |
self.unsynced_message = _('Unsynced changes') if visible else '' |
| 1376 |
self.update_header_footer() |
| 1377 |
|
| 1378 |
def create_template_renderer(self): |
| 1379 |
if not self.book: |
| 1380 |
return |
| 1381 |
pos = self.current_position_data |
| 1382 |
book_length = pos.book_length * max(0, 1 - pos.progress_frac) |
| 1383 |
chapter_length = pos.chapter_length * max(0, 1 - pos.file_progress_frac) |
| 1384 |
book_time = self.timers.time_for(book_length) |
| 1385 |
chapter_time = self.timers.time_for(chapter_length) |
| 1386 |
mi = self.book.metadata |
| 1387 |
sd = get_session_data() |
| 1388 |
view_mode = sd.get('read_mode') |
| 1389 |
|
| 1390 |
def render(div, name, which, override): |
| 1391 |
return render_head_foot( |
| 1392 |
div, |
| 1393 |
name, |
| 1394 |
which, |
| 1395 |
mi, |
| 1396 |
self.current_toc_node, |
| 1397 |
self.current_toc_toplevel_node, |
| 1398 |
self.current_pagelist_items, |
| 1399 |
book_time, |
| 1400 |
chapter_time, |
| 1401 |
pos, |
| 1402 |
override, |
| 1403 |
view_mode |
| 1404 |
) |
| 1405 |
|
| 1406 |
return render |
| 1407 |
|
| 1408 |
def update_header_footer(self): |
| 1409 |
renderer = self.create_template_renderer() |
| 1410 |
if not renderer: |
| 1411 |
return |
| 1412 |
sd = get_session_data() |
| 1413 |
has_clock = False |
| 1414 |
|
| 1415 |
def render_template(div, edge, name): |
| 1416 |
nonlocal has_clock |
| 1417 |
c = div.lastChild |
| 1418 |
b = c.previousSibling |
| 1419 |
a = b.previousSibling |
| 1420 |
if sd.get(f'margin_{edge}', 20) > 5: |
| 1421 |
override = (self.current_status_message or self.unsynced_message) if edge is 'bottom' else '' |
| 1422 |
hca = renderer(a, name, 'left', override) |
| 1423 |
hcb = renderer(b, name, 'middle', '') |
| 1424 |
hcc = renderer(c, name, 'right', '') |
| 1425 |
if hca or hcb or hcc: |
| 1426 |
has_clock = True |
| 1427 |
else: |
| 1428 |
clear(a), clear(b), clear(c) |
| 1429 |
|
| 1430 |
for edge in ('left', 'right', 'top', 'bottom'): |
| 1431 |
div = document.getElementById(f'book-{edge}-margin') |
| 1432 |
if div: |
| 1433 |
tname = {'left': 'left-margin', 'right': 'right-margin', 'top': 'header', 'bottom': 'footer'}[edge] |
| 1434 |
render_template(div, edge, tname) |
| 1435 |
if has_clock: |
| 1436 |
if not self.timer_ids.clock: |
| 1437 |
self.timer_ids.clock = window.setInterval(self.update_header_footer, 60000) |
| 1438 |
else: |
| 1439 |
if self.timer_ids.clock: |
| 1440 |
window.clearInterval(self.timer_ids.clock) |
| 1441 |
self.timer_ids.clock = 0 |
| 1442 |
|
| 1443 |
def on_update_toc_position(self, data): |
| 1444 |
update_visible_toc_nodes(data.visible_anchors) |
| 1445 |
self.current_toc_families = get_current_toc_nodes() |
| 1446 |
self.current_pagelist_items = get_current_pagelist_items() |
| 1447 |
if self.current_toc_families.length: |
| 1448 |
first = self.current_toc_families[0] |
| 1449 |
self.current_toc_node = first[-1] |
| 1450 |
self.current_toc_toplevel_node = first[0] |
| 1451 |
else: |
| 1452 |
self.current_toc_node = self.current_toc_toplevel_node = None |
| 1453 |
if runtime.is_standalone_viewer: |
| 1454 |
r = [] |
| 1455 |
for fam in self.current_toc_families: |
| 1456 |
if fam.length: |
| 1457 |
r.push(fam[-1].id) |
| 1458 |
ui_operations.update_current_toc_nodes(r) |
| 1459 |
self.update_header_footer() |
| 1460 |
|
| 1461 |
def show_spine_item(self, resource_data): |
| 1462 |
self.pending_load = resource_data |
| 1463 |
if self.iframe_wrapper.ready: |
| 1464 |
self.do_pending_load() |
| 1465 |
else: |
| 1466 |
self.iframe_wrapper.init() |
| 1467 |
|
| 1468 |
def show_spine_item_stage2(self, resource_data): |
| 1469 |
# We cannot encrypt this message because the resource data contains |
| 1470 |
# Blob objects which do not survive encryption |
| 1471 |
self.processing_spine_item_display = True |
| 1472 |
self.current_status_message = '' |
| 1473 |
self.iframe.style.visibility = 'hidden' |
| 1474 |
fdata = self.book.manifest.files[self.currently_showing.name] |
| 1475 |
smil_map = None |
| 1476 |
if self.book.manifest.has_smil and fdata: |
| 1477 |
smil_map = fdata.smil_map |
| 1478 |
self.currently_showing.smil_audio_map = get_smil_audio_map(smil_map) |
| 1479 |
self.iframe_wrapper.send_unencrypted_message('display', |
| 1480 |
resource_data=resource_data, book=self.book, name=self.currently_showing.name, |
| 1481 |
initial_position=self.currently_showing.initial_position, smil_map=smil_map or None, |
| 1482 |
settings=self.currently_showing.settings, reference_mode_enabled=self.reference_mode_enabled, |
| 1483 |
is_titlepage=self.currently_showing.name is self.book.manifest.title_page_name, |
| 1484 |
highlights=self.annotations_manager.highlights_for_currently_showing(), |
| 1485 |
) |
| 1486 |
|
| 1487 |
def on_content_loaded(self, data): |
| 1488 |
for x in self.modal_overlays: |
| 1489 |
if not x.dont_hide_on_content_loaded: |
| 1490 |
x.hide() |
| 1491 |
self.processing_spine_item_display = False |
| 1492 |
self.currently_showing.loading = False |
| 1493 |
self.hide_loading() |
| 1494 |
self.set_progress_frac(data.progress_frac, data.file_progress_frac, data.page_counts) |
| 1495 |
self.update_header_footer() |
| 1496 |
window.scrollTo(0, 0) # ensure window is at 0 on mobile where the navbar causes issues |
| 1497 |
if self.book_load_started: |
| 1498 |
self.book_load_started = False |
| 1499 |
if ui_operations.clear_history: |
| 1500 |
ui_operations.clear_history() |
| 1501 |
if ui_operations.content_file_changed: |
| 1502 |
ui_operations.content_file_changed(self.currently_showing.name) |
| 1503 |
if self.read_aloud.is_visible: |
| 1504 |
self.read_aloud.play() |
| 1505 |
for x in self.currently_showing.on_load: |
| 1506 |
x() |
| 1507 |
self.currently_showing.on_load = [] |
| 1508 |
|
| 1509 |
def set_progress_frac(self, progress_frac, file_progress_frac, page_counts): |
| 1510 |
self.current_progress_frac = progress_frac or 0 |
| 1511 |
self.current_file_progress_frac = file_progress_frac or 0 |
| 1512 |
self.current_page_counts = page_counts |
| 1513 |
self.book_scrollbar.sync_to_contents(self.current_progress_frac) |
| 1514 |
|
| 1515 |
def update_font_size(self): |
| 1516 |
self.iframe_wrapper.send_message('change_font_size', base_font_size=get_session_data().get('base_font_size')) |
| 1517 |
|
| 1518 |
def viewer_font_size_changed(self): |
| 1519 |
self.iframe_wrapper.send_message('viewer_font_size_changed', base_font_size=get_session_data().get('base_font_size')) |
| 1520 |
|
| 1521 |
def update_scroll_speed(self, amt): |
| 1522 |
self.iframe_wrapper.send_message('change_scroll_speed', lines_per_sec_auto=change_scroll_speed(amt)) |
| 1523 |
|
| 1524 |
def update_color_scheme(self): |
| 1525 |
cs = self.apply_color_scheme() |
| 1526 |
self.iframe_wrapper.send_message('change_color_scheme', color_scheme=cs) |
| 1527 |
|
| 1528 |
def toggle_reference_mode(self): |
| 1529 |
self.reference_mode_enabled = not self.reference_mode_enabled |
| 1530 |
self.iframe_wrapper.send_message('set_reference_mode', enabled=self.reference_mode_enabled) |
| 1531 |
if ui_operations.reference_mode_changed: |
| 1532 |
ui_operations.reference_mode_changed(self.reference_mode_enabled) |
| 1533 |
|
| 1534 |
def discover_search_result(self, sr): |
| 1535 |
if sr.search_finished: |
| 1536 |
if self.search_result_discovery: |
| 1537 |
self.search_result_discovery.finished = True |
| 1538 |
if (not self.search_result_discovery.discovered |
| 1539 |
and self.search_result_discovery.first_search_result |
| 1540 |
and self.search_result_discovery.queue.length is 0): |
| 1541 |
sr = self.search_result_discovery.first_search_result |
| 1542 |
sr.force_jump_to = True |
| 1543 |
self.search_result_discovery.jump_forced = True |
| 1544 |
self.show_search_result(sr) |
| 1545 |
return |
| 1546 |
if sr.result_num is 1: |
| 1547 |
self.search_result_discovery = { |
| 1548 |
'queue': [], 'on_discovery': sr.on_discovery, 'in_flight': None, 'discovered': False, |
| 1549 |
'first_search_result': sr, 'finished': False, 'jump_forced': False, |
| 1550 |
} |
| 1551 |
if not self.search_result_discovery or self.search_result_discovery.discovered or self.search_result_discovery.on_discovery is not sr.on_discovery: |
| 1552 |
return |
| 1553 |
self.search_result_discovery.queue.push(sr) |
| 1554 |
if not self.search_result_discovery.in_flight: |
| 1555 |
self.show_search_result(self.search_result_discovery.queue.shift()) |
| 1556 |
|
| 1557 |
def handle_search_result_discovery(self, sr, discovered): |
| 1558 |
if self.search_result_discovery?.on_discovery is sr.on_discovery: |
| 1559 |
self.search_result_discovery.in_flight = None |
| 1560 |
if discovered: |
| 1561 |
if not self.search_result_discovery.discovered: |
| 1562 |
self.search_result_discovery.discovered = True |
| 1563 |
ui_operations.search_result_discovered(sr) |
| 1564 |
elif not self.search_result_discovery.discovered and self.search_result_discovery.queue.length: |
| 1565 |
self.show_search_result(self.search_result_discovery.queue.shift()) |
| 1566 |
elif not self.search_result_discovery.discovered and self.search_result_discovery.finished and not self.search_result_discovery.jump_forced: |
| 1567 |
sr = self.search_result_discovery.first_search_result |
| 1568 |
sr.force_jump_to = True |
| 1569 |
self.search_result_discovery.jump_forced = True |
| 1570 |
self.show_search_result(sr) |
| 1571 |
|
| 1572 |
def search_result_discovered(self, data): |
| 1573 |
self.handle_search_result_discovery(data.search_result, data.discovered) |
| 1574 |
|
| 1575 |
def search_result_not_found(self, data): |
| 1576 |
if ui_operations.search_result_not_found: |
| 1577 |
ui_operations.search_result_not_found(data.search_result) |
| 1578 |
self.handle_search_result_discovery(data.search_result, False) |
| 1579 |
|
| 1580 |
def show_search_result(self, sr): |
| 1581 |
if self.currently_showing.name is sr.file_name: |
| 1582 |
if self.currently_showing.loading: |
| 1583 |
self.currently_showing.on_load.push(self.show_search_result.bind(None, sr)) |
| 1584 |
else: |
| 1585 |
self.iframe_wrapper.send_message('show_search_result', search_result=sr) |
| 1586 |
else: |
| 1587 |
self.show_name(sr.file_name, initial_position={'type': 'search_result', 'search_result': sr, 'replace_history': True}) |
| 1588 |
if self.search_result_discovery?.on_discovery is sr.on_discovery: |
| 1589 |
self.search_result_discovery.in_flight = sr.result_num |
| 1590 |
|
| 1591 |
def highlight_action(self, uuid, which): |
| 1592 |
spine = self.book.manifest.spine |
| 1593 |
spine_index = self.annotations_manager.spine_index_for_highlight(uuid, spine) |
| 1594 |
if spine_index < 0 or spine_index >= spine.length: |
| 1595 |
if which is 'edit': |
| 1596 |
self.selection_bar.report_failed_edit_highlight(uuid) |
| 1597 |
return |
| 1598 |
if which is 'edit': |
| 1599 |
if self.currently_showing.spine_index is spine_index: |
| 1600 |
self.selection_bar.edit_highlight(uuid) |
| 1601 |
else: |
| 1602 |
self.show_name(spine[spine_index], initial_position={'type': 'edit_annotation', 'uuid': uuid, 'replace_history': True}) |
| 1603 |
elif which is 'delete': |
| 1604 |
self.selection_bar.remove_highlight_with_id(uuid) |
| 1605 |
elif which is 'goto': |
| 1606 |
cfi = self.annotations_manager.cfi_for_highlight(uuid, spine_index) |
| 1607 |
if cfi: |
| 1608 |
self.goto_cfi(cfi, True) |