More work on the in-browser viewer

This commit is contained in:
Kovid Goyal 2016-04-18 13:09:58 +05:30
parent 824a12c445
commit bf91769b37
5 changed files with 49 additions and 15 deletions

View File

@ -1,9 +1,11 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
from dom import set_css
from read_book.globals import get_boss
from keycodes import get_key
from utils import document_height, document_width
from read_book.settings import opts
def flow_to_scroll_fraction(frac):
window.scrollTo(0, document_height() * frac)
@ -93,3 +95,7 @@ def flow_onkeydown(evt):
scroll_by_page(-1 if key is 'pageup' else 1)
if handled:
evt.preventDefault()
def layout(is_single_page):
set_css(document.body, margin='0', border_width='0', padding='0 {}px'.format(opts.margin_side))

View File

@ -5,9 +5,10 @@ from __python__ import bound_methods
import traceback
from aes import GCM
from gettext import install
from read_book.globals import set_boss, set_current_spine_item, current_layout_mode, current_spine_item
from read_book.globals import set_boss, set_current_spine_item, current_layout_mode, current_spine_item, set_layout_mode
from read_book.resources import finalize_resources, unserialize_html
from read_book.flow_mode import flow_to_scroll_fraction, flow_onwheel, flow_onkeydown
from read_book.flow_mode import flow_to_scroll_fraction, flow_onwheel, flow_onkeydown, layout as flow_layout
from read_book.settings import apply_settings
from utils import debounce
class Boss:
@ -60,6 +61,8 @@ class Boss:
self.book = data.book
spine = self.book.manifest.spine
index = spine.indexOf(data.name)
set_layout_mode('flow' or data.settings.read_mode) # TODO: Change this
apply_settings(data.settings)
set_current_spine_item({'name':data.name, 'is_first':index is 0, 'is_last':index is spine.length - 1, 'initial_scroll_fraction':data.initial_scroll_fraction})
root_data = finalize_resources(self.book, data.name, data.resource_data)
unserialize_html(root_data, self.content_loaded)
@ -70,6 +73,8 @@ class Boss:
window.addEventListener('resize', debounce(self.onresize, 500))
window.addEventListener('wheel', self.onwheel)
window.addEventListener('keydown', self.onkeydown)
if current_layout_mode() is 'flow':
flow_layout()
csi = current_spine_item()
if csi.initial_scroll_fraction is not None:
if current_layout_mode() is 'flow':

View File

@ -0,0 +1,14 @@
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
opts = {}
def apply_settings(settings):
settings = settings or {}
opts.cols_per_screen = max(1, settings.cols_per_screen or 1)
opts.max_col_width = max(0, settings.max_col_width or 0)
opts.max_col_height = max(0, settings.max_col_height or 0)
opts.fit_images = False if settings.fit_images is False else True
opts.margin_side = max(0, settings.margin_side or 0)
apply_settings()

View File

@ -36,13 +36,11 @@ class View:
E.div(style='width: 100vw; height: 100vh; overflow: hidden; display: flex; align-items: stretch', # container for horizontally aligned panels
E.div(style='display: flex; flex-direction: column; align-items: stretch; flex-grow:2', # container for iframe and any other panels in the same column
E.div(style='flex-grow: 2; display:flex; align-items: stretch', # container for iframe and its overlay
E.div(style='width:{}px; height:100%'.format(sd.get('margin_side', 20)), id='book-left-margin'),
E.div(style='flex-grow:2; display:flex; align-items:stretch; flex-direction: column', # container for top and bottom margins
E.div(style='height:{}px; width:100%'.format(sd.get('margin_top', 20)), id='book-top-margin'),
E.div(style='height:{}px; width:100%; padding: 0 {}px'.format(sd.get('margin_top', 20), sd.get('margin_side', 20)), id='book-top-margin'),
E.iframe(id=iframe_id, seamless=True, sandbox='allow-popups allow-scripts', style='flex-grow: 2'),
E.div(style='height:{}px; width:100%'.format(sd.get('margin_bottom', 20)), id='book-bottom-margin'),
E.div(style='height:{}px; width:100%; padding: 0 {}px'.format(sd.get('margin_top', 20), sd.get('margin_side', 20)), id='book-bottom-margin'),
),
E.div(style='width:{}px; height:100%'.format(sd.get('margin_side', 20)), id='book-right-margin'),
E.div(style='position: absolute; top:0; left:0; width: 100%; height: 100%; display:none', id='book-overlay'), # overlay
)
)
@ -67,15 +65,17 @@ class View:
def iframe(self):
return document.getElementById(iframe_id)
def set_margins(self, margin_side=None, margin_top=None, margin_bottom=None):
def set_margins(self, margin_top=None, margin_bottom=None, margin_side=None):
sd = get_session_data()
margin_side = sd.get('margin_side') if margin_side is None else margin_side
margin_top = sd.get('margin_top') if margin_top is None else margin_top
margin_bottom = sd.get('margin_bottom') if margin_bottom is None else margin_bottom
document.getElementById('book-left-margin').style.width = margin_side + 'px'
document.getElementById('book-right-margin').style.width = margin_side + 'px'
document.getElementById('book-top-margin').style.height = margin_top + 'px'
document.getElementById('book-bottom-margin').style.height = margin_bottom + 'px'
t = document.getElementById('book-top-margin')
t.style.height = margin_top + 'px'
t.style.paddingLeft = t.style.paddingRight = margin_side + 'px'
b = document.getElementById('book-bottom-margin')
b.style.height = margin_bottom + 'px'
b.style.paddingLeft = b.style.paddingRight = margin_side + 'px'
def create_src_doc(self):
iframe_script = self.ui.interface_data.main_js.replace(/is_running_in_iframe\s*=\s*false/, 'is_running_in_iframe = true')
@ -113,7 +113,7 @@ class View:
def on_iframe_ready(self, data):
messenger.reset()
self.send_message('initialize', 'secret'=messenger.secret, 'translations'=self.ui.interface_data.translations)
self.send_message('initialize', secret=messenger.secret, translations=self.ui.interface_data.translations)
self.iframe_ready = True
if self.pending_load:
data = self.pending_load
@ -139,7 +139,12 @@ class View:
def show_name(self, name, initial_scroll_fraction=None, cfi=None):
if self.currently_showing.loading:
return
self.currently_showing = {'name':name, 'cfi':cfi, 'initial_scroll_fraction':initial_scroll_fraction, 'loading':True}
sd = get_session_data()
settings={
'margin_side': 0 if name is self.book.manifest.title_page_name else sd.get('margin_side'),
'read_mode': sd.get('read_mode'),
}
self.currently_showing = {'name':name, 'cfi':cfi, 'settings':settings, 'initial_scroll_fraction':initial_scroll_fraction, 'loading':True}
self.set_margins(0, 0, 0) if name is self.book.manifest.title_page_name else self.set_margins()
load_resources(self.ui.db, self.book, name, self.loaded_resources, self.show_spine_item)
@ -170,6 +175,9 @@ class View:
def show_spine_item_stage2(self, resource_data):
self.currently_showing.loading = False
self.send_message('display', resource_data=resource_data, book=self.book, name=self.currently_showing.name,
initial_scroll_fraction=self.currently_showing.initial_scroll_fraction)
self.send_message('display',
resource_data=resource_data, book=self.book, name=self.currently_showing.name,
initial_scroll_fraction=self.currently_showing.initial_scroll_fraction,
settings=self.currently_showing.settings,
)
self.encrypted_communications = True

View File

@ -20,6 +20,7 @@ defaults = {
'margin_side': 20,
'margin_top': 20,
'margin_bottom': 20,
'read_mode': 'paged',
}
def storage_available(which):