From 8ef6938e0304355fb7262e40ee10794acd449a23 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 4 Oct 2019 19:55:17 +0530 Subject: [PATCH] Viewer: Allow showing the "position in book" as was displayed in the top left of the viewer in calibre 3 in the header and footer. Fixes #1846700 [Absolute position for book viewer](https://bugs.launchpad.net/calibre/+bug/1846700) --- src/pyj/read_book/prefs/head_foot.pyj | 15 ++++++++++++++- src/pyj/read_book/view.pyj | 7 ++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/pyj/read_book/prefs/head_foot.pyj b/src/pyj/read_book/prefs/head_foot.pyj index cd816c32d9..6eae29f6fd 100644 --- a/src/pyj/read_book/prefs/head_foot.pyj +++ b/src/pyj/read_book/prefs/head_foot.pyj @@ -37,6 +37,8 @@ def create_item(region, label): opt(_('Time to read book'), 'time-book'), opt(_('Time to read chapter'), 'time-chapter'), opt(_('Time to read chapter and book'), 'time-chapter-book'), + opt(_('Position in book'), 'pos-book'), + opt(_('Position in chapter'), 'pos-chapter'), opt(_('Clock'), 'clock'), )) ) @@ -125,7 +127,13 @@ def format_time_left(seconds): return _('{} h {} mins').format(hours, minutes) -def render_head_foot(div, which, region, progress_frac, metadata, current_toc_node, current_toc_toplevel_node, book_time, chapter_time): +def format_pos(progress_frac, length): + pages = Math.ceil(length / 1000) + pos = progress_frac * pages + return f'{pos:.1f} / {pages}' + + +def render_head_foot(div, which, region, progress_frac, metadata, current_toc_node, current_toc_toplevel_node, book_time, chapter_time, pos): template = get_session_data().get(which) or {} field = template[region] or 'empty' interface_data = get_interface_data() @@ -161,6 +169,11 @@ def render_head_foot(div, which, region, progress_frac, metadata, current_toc_no text = format_time_left(chapter_time) else: text = '{} ({})'.format(format_time_left(chapter_time), format_time_left(book_time)) + elif field.startswith('pos-'): + if field is 'pos-book': + text = format_pos(pos.current_progress_frac, pos.book_length) + else: + text = format_pos(pos.current_file_progress_frac, pos.chapter_length) if not text: text = '\xa0' if text is not div.textContent: diff --git a/src/pyj/read_book/view.pyj b/src/pyj/read_book/view.pyj index f7dd9d8e65..7dae1d7635 100644 --- a/src/pyj/read_book/view.pyj +++ b/src/pyj/read_book/view.pyj @@ -726,6 +726,7 @@ class View: chapter_length = self.book.manifest.files[name]?.length or 0 else: book_length = chapter_length = 0 + pos = {'current_progress_frac': self.current_progress_frac, 'book_length': book_length, 'chapter_length': chapter_length, 'current_file_progress_frac': self.current_file_progress_frac} book_length *= max(0, 1 - self.current_progress_frac) chapter_length *= max(0, 1 - self.current_file_progress_frac) book_time = self.timers.time_for(book_length) @@ -735,9 +736,9 @@ class View: nonlocal has_clock if sd.get(sz_attr, 20) > 5: mi = self.book.metadata - texta, hca = render_head_foot(div.firstChild, name, 'left', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node, book_time, chapter_time) - textb, hcb = render_head_foot(div.firstChild.nextSibling, name, 'middle', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node, book_time, chapter_time) - textc, hcc = render_head_foot(div.lastChild, name, 'right', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node, book_time, chapter_time) + texta, hca = render_head_foot(div.firstChild, name, 'left', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node, book_time, chapter_time, pos) + textb, hcb = render_head_foot(div.firstChild.nextSibling, name, 'middle', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node, book_time, chapter_time, pos) + textc, hcc = render_head_foot(div.lastChild, name, 'right', self.current_progress_frac, mi, self.current_toc_node, self.current_toc_toplevel_node, book_time, chapter_time, pos) has_clock = hca or hcb or hcc if textc and not textb and not texta: # Want right-aligned