Only update tab/window title on scroll if needed

This way, if {scroll_pos} is not in the window/tab title template,
we don't redraw anything unnecessarily.

See #2233
This commit is contained in:
Florian Bruhin 2017-10-20 08:25:43 +02:00
parent 280dddda6b
commit dd927ded6b
2 changed files with 32 additions and 15 deletions

View File

@ -173,8 +173,18 @@ class TabbedBrowser(tabwidget.TabWidget):
widgets.append(widget)
return widgets
def _update_window_title(self):
"""Change the window title to match the current tab."""
def _update_window_title(self, field=None):
"""Change the window title to match the current tab.
Args:
idx: The tab index to update.
field: A field name which was updated. If given, the title
is only set if the given field is in the template.
"""
title_format = config.val.window.title_format
if field is not None and ('{' + field + '}') not in title_format:
return
idx = self.currentIndex()
if idx == -1:
# (e.g. last tab removed)
@ -183,7 +193,6 @@ class TabbedBrowser(tabwidget.TabWidget):
fields = self.get_tab_fields(idx)
fields['id'] = self._win_id
title_format = config.val.window.title_format
title = title_format.format(**fields)
self.window().setWindowTitle(title)
@ -696,8 +705,8 @@ class TabbedBrowser(tabwidget.TabWidget):
log.webview.debug("Not updating scroll position because index is "
"-1")
return
self._update_window_title()
self._update_tab_title(idx)
self._update_window_title('scroll_pos')
self._update_tab_title(idx, 'scroll_pos')
def _on_renderer_process_terminated(self, tab, status, code):
"""Show an error when a renderer process terminated."""

View File

@ -121,21 +121,29 @@ class TabWidget(QTabWidget):
"""Get the tab title user data."""
return self.tabBar().page_title(idx)
def _update_tab_title(self, idx):
"""Update the tab text for the given tab."""
def _update_tab_title(self, idx, field=None):
"""Update the tab text for the given tab.
Args:
idx: The tab index to update.
field: A field name which was updated. If given, the title
is only set if the given field is in the template.
"""
tab = self.widget(idx)
if tab.data.pinned:
fmt = config.val.tabs.title.format_pinned
else:
fmt = config.val.tabs.title.format
if (field is not None and
(fmt is None or ('{' + field + '}') not in fmt)):
return
fields = self.get_tab_fields(idx)
fields['title'] = fields['title'].replace('&', '&&')
fields['index'] = idx + 1
fmt = config.val.tabs.title.format
fmt_pinned = config.val.tabs.title.format_pinned
if tab.data.pinned:
title = '' if fmt_pinned is None else fmt_pinned.format(**fields)
else:
title = '' if fmt is None else fmt.format(**fields)
title = '' if fmt is None else fmt.format(**fields)
self.tabBar().setTabText(idx, title)
def get_tab_fields(self, idx):