From c3b015e75e96911324e8bebd48236a06f70a65bf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 25 Sep 2016 13:32:51 +0530 Subject: [PATCH] Make the number of columns per screen automatic in paged mode --- src/pyj/read_book/paged_mode.pyj | 11 ++++++++--- src/pyj/read_book/settings.pyj | 2 +- src/pyj/read_book/view.pyj | 9 +++++---- src/pyj/session.pyj | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/pyj/read_book/paged_mode.pyj b/src/pyj/read_book/paged_mode.pyj index 4e9709ae61..fb069192fd 100644 --- a/src/pyj/read_book/paged_mode.pyj +++ b/src/pyj/read_book/paged_mode.pyj @@ -106,14 +106,19 @@ def layout(is_single_page): nonlocal _in_paged_mode, col_width, col_and_gap, screen_height, gap, screen_width, is_full_screen_layout, cols_per_screen body_style = window.getComputedStyle(document.body) first_layout = not _in_paged_mode + cps = opts.columns_per_screen + cps = cps.landscape if window.innerWidth > window.innerHeight else cps.portrait + if cps is 'auto': + cps = int(Math.floor(window.innerWidth / 500.0)) + cps = max(1, min(cps or 1, 20)) if first_layout: handle_rtl_body(body_style) # Check if the current document is a full screen layout like # cover, if so we treat it specially. single_screen = (document.body.scrollHeight < window.innerHeight + 75) first_layout = True - if not single_screen and opts.cols_per_screen > 1: - num = opts.cols_per_screen - 1 + if not single_screen and cps > 1: + num = cps - 1 elems = document.querySelectorAll('body > *') if elems.length == 1: # Workaround for the case when the content is wrapped in a @@ -125,7 +130,7 @@ def layout(is_single_page): num -= 1 create_page_div() - n = cols_per_screen = opts.cols_per_screen + n = cols_per_screen = cps # Calculate the column width so that cols_per_screen columns fit exactly in # the window width, with their separator margins ww = col_width = screen_width = window.innerWidth diff --git a/src/pyj/read_book/settings.pyj b/src/pyj/read_book/settings.pyj index e37a9e499b..5b30de8cc3 100644 --- a/src/pyj/read_book/settings.pyj +++ b/src/pyj/read_book/settings.pyj @@ -6,7 +6,7 @@ opts = {} def apply_settings(settings): settings = settings or {} - opts.cols_per_screen = max(1, settings.cols_per_screen or 1) + opts.columns_per_screen = settings.columns_per_screen or {'portrait':'auto', 'landscape':'auto'} opts.margin_left = max(0, settings.margin_left or 0) opts.margin_right = max(0, settings.margin_right or 0) opts.color_scheme = settings.color_scheme diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index d2514ccb41..f25c12c95d 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -102,7 +102,8 @@ class View: def show_chrome(self): self.overlay.show() - def set_margins(self, no_margins): + def set_margins(self): + no_margins = self.currently_showing.name is self.book.manifest.title_page_name sd = get_session_data() margin_left = 0 if no_margins else sd.get('margin_left') margin_right = 0 if no_margins else sd.get('margin_right') @@ -197,7 +198,7 @@ class View: return ans def on_resize(self): - pass + self.set_margins() # needed because of max text height/width def show_loading(self): title = self.book.metadata.title @@ -244,7 +245,7 @@ class View: 'margin_left': 0 if name is self.book.manifest.title_page_name else sd.get('margin_left'), 'margin_right': 0 if name is self.book.manifest.title_page_name else sd.get('margin_right'), 'read_mode': sd.get('read_mode'), - 'cols_per_screen': sd.get('cols_per_screen'), + 'columns_per_screen': sd.get('columns_per_screen'), 'color_scheme': self.get_color_scheme(True), } initial_position = initial_position or {'replace_history':False} @@ -254,7 +255,7 @@ class View: idx = spine.indexOf(name) if idx > -1: self.currently_showing.bookpos = 'epubcfi(/{})'.format(2 * (idx +1)) - self.set_margins(name is self.book.manifest.title_page_name) + self.set_margins() load_resources(self.ui.db, self.book, name, self.loaded_resources, self.show_spine_item) def goto_doc_boundary(self, data): diff --git a/src/pyj/session.pyj b/src/pyj/session.pyj index ab9ac181ab..6c12a80cac 100644 --- a/src/pyj/session.pyj +++ b/src/pyj/session.pyj @@ -25,7 +25,7 @@ defaults = { 'read_mode': 'paged', 'max_text_height': 0, 'max_text_width': 0, - 'cols_per_screen': 1, + 'columns_per_screen': {'portrait':'auto', 'landscape':'auto'}, 'current_color_scheme': 'white', 'user_color_schemes': {}, } @@ -38,7 +38,7 @@ is_local_setting = { 'read_mode': 'paged', 'max_text_height': True, 'max_text_width': True, - 'cols_per_screen': True, + 'columns_per_screen': True, 'current_color_scheme': True, }