E-book viewer: Fix indication of current section in Table of Contents sometimes wrong after changing font size. Fixes #2033205 [E-book viewer: incorrect ToC highligting after changing font size](https://bugs.launchpad.net/calibre/+bug/2033205)

This commit is contained in:
Kovid Goyal 2023-09-01 15:59:32 +05:30
parent 8271fcff30
commit e1d8b44561
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 28 additions and 22 deletions

View File

@ -402,7 +402,7 @@ class IframeBoss:
if cfi: if cfi:
self.jump_to_cfi('/' + cfi) self.jump_to_cfi('/' + cfi)
self.update_cfi() self.update_cfi()
self.update_toc_position() self.update_toc_position(True)
def number_of_columns_changed(self, data): def number_of_columns_changed(self, data):
opts.columns_per_screen = data.columns_per_screen opts.columns_per_screen = data.columns_per_screen
@ -548,8 +548,8 @@ class IframeBoss:
self.send_message( self.send_message(
'update_progress_frac', progress_frac=pf, file_progress_frac=fpf, page_counts=page_counts()) 'update_progress_frac', progress_frac=pf, file_progress_frac=fpf, page_counts=page_counts())
def update_toc_position(self): def update_toc_position(self, recalculate_toc_anchor_positions):
visible_anchors = update_visible_toc_anchors(self.book.manifest.toc_anchor_map, self.anchor_funcs) visible_anchors = update_visible_toc_anchors(self.book.manifest.toc_anchor_map, self.anchor_funcs, bool(recalculate_toc_anchor_positions))
self.send_message('update_toc_position', visible_anchors=visible_anchors) self.send_message('update_toc_position', visible_anchors=visible_anchors)
def onscroll(self): def onscroll(self):

View File

@ -198,32 +198,38 @@ def create_toc_panel(book, container, onclick):
toc_panel.style.flexShrink = '1' toc_panel.style.flexShrink = '1'
toc_panel.style.overflow = 'auto' toc_panel.style.overflow = 'auto'
def recalculate_toc_anchor_positions(tam, anchor_funcs):
name = current_spine_item().name
am = {}
anchors = v'[]'
pos_map = {}
for i, anchor in enumerate(tam[name] or v'[]'):
val = anchor_funcs.pos_for_elem()
if anchor.frag:
elem = document.getElementById(anchor.frag)
if elem:
val = anchor_funcs.pos_for_elem(elem)
am[anchor.id] = val
anchors.push(anchor.id)
pos_map[anchor.id] = i
# stable sort by position in document
anchors.sort(def (a, b): return anchor_funcs.cmp(am[a], am[b]) or (pos_map[a] - pos_map[b]);)
current_map = {'layout_mode': current_layout_mode(), 'width': scroll_viewport.width(), 'height': scroll_viewport.height(), 'pos_map': am, 'sorted_anchors':anchors}
set_toc_anchor_map(current_map)
return current_map
def current_toc_anchor_map(tam, anchor_funcs): def current_toc_anchor_map(tam, anchor_funcs):
current_map = toc_anchor_map() current_map = toc_anchor_map()
if not (current_map and current_map.layout_mode is current_layout_mode() and current_map.width is scroll_viewport.width() and current_map.height is scroll_viewport.height()): if not (current_map and current_map.layout_mode is current_layout_mode() and current_map.width is scroll_viewport.width() and current_map.height is scroll_viewport.height()):
name = current_spine_item().name current_map = recalculate_toc_anchor_positions(tam, anchor_funcs)
am = {}
anchors = v'[]'
pos_map = {}
for i, anchor in enumerate(tam[name] or v'[]'):
val = anchor_funcs.pos_for_elem()
if anchor.frag:
elem = document.getElementById(anchor.frag)
if elem:
val = anchor_funcs.pos_for_elem(elem)
am[anchor.id] = val
anchors.push(anchor.id)
pos_map[anchor.id] = i
# stable sort by position in document
anchors.sort(def (a, b): return anchor_funcs.cmp(am[a], am[b]) or (pos_map[a] - pos_map[b]);)
current_map = {'layout_mode': current_layout_mode(), 'width': scroll_viewport.width(), 'height': scroll_viewport.height(), 'pos_map': am, 'sorted_anchors':anchors}
set_toc_anchor_map(current_map)
return current_map return current_map
def update_visible_toc_anchors(toc_anchor_map, anchor_funcs): def update_visible_toc_anchors(toc_anchor_map, anchor_funcs, recalculate):
if recalculate:
recalculate_toc_anchor_positions(toc_anchor_map, anchor_funcs)
tam = current_toc_anchor_map(toc_anchor_map, anchor_funcs) tam = current_toc_anchor_map(toc_anchor_map, anchor_funcs)
before = after = None before = after = None
visible_anchors = {} visible_anchors = {}