Only apply visibility toggle if we have >10 tabs

This commit is contained in:
Jay Kamat 2018-09-29 12:25:49 -07:00
parent 3081d017ce
commit a5f9115b2f
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
2 changed files with 31 additions and 14 deletions

View File

@ -21,6 +21,7 @@
import functools
import enum
import contextlib
import attr
from PyQt5.QtCore import (pyqtSignal, pyqtSlot, Qt, QSize, QRect, QPoint,
@ -214,19 +215,35 @@ class TabWidget(QTabWidget):
fields['scroll_pos'] = scroll_pos
return fields
@contextlib.contextmanager
def _toggle_visibility(self, force_toggle=False):
"""Toggle visibility while running.
Every single call to setTabText calls the size hinting functions for
every single tab, which are slow. Since we know we are updating all
the tab's titles, we can delay this processing by making the tab
non-visible. To avoid flickering, disable repaint updates whlie we
work.
Args:
force_toggle: Whether to always force the toggle, or only do it
if we have enough tabs for it to matter
"""
if self.count() > 10:
force_toggle = True
if force_toggle:
self.setUpdatesEnabled(False)
self.setVisible(False)
yield
if force_toggle:
self.setVisible(True)
self.setUpdatesEnabled(True)
def update_tab_titles(self):
"""Update all texts."""
# Every single call to setTabText calls the size hinting functions for
# every single tab, which are slow. Since we know we are updating all
# the tab's titles, we can delay this processing by making the tab
# non-visible. To avoid flickering, disable repaint updates whlie we
# work.
self.setUpdatesEnabled(False)
self.setVisible(False)
for idx in range(self.count()):
self.update_tab_title(idx)
self.setVisible(True)
self.setUpdatesEnabled(True)
with self._toggle_visibility():
for idx in range(self.count()):
self.update_tab_title(idx)
def tabInserted(self, idx):
"""Update titles when a tab was inserted."""

View File

@ -24,7 +24,7 @@ import functools
import pytest
from PyQt5.QtGui import QIcon, QPixmap
from qutebrowser.mainwindow import tabwidget, tabbedbrowser
from qutebrowser.mainwindow import tabwidget
from qutebrowser.utils import usertypes
@ -108,7 +108,7 @@ class TestTabWidget:
assert first_size == widget.tabBar().tabSizeHint(i)
assert first_size_min == widget.tabBar().minimumTabSizeHint(i)
@pytest.mark.parametrize("num_tabs", [4, 10])
@pytest.mark.parametrize("num_tabs", [4, 10, 50, 100])
def test_update_tab_titles_benchmark(self, benchmark, widget,
qtbot, fake_web_tab, num_tabs):
"""Benchmark for update_tab_titles."""
@ -120,7 +120,7 @@ class TestTabWidget:
benchmark(widget.update_tab_titles)
@pytest.mark.parametrize("num_tabs", [4, 10])
@pytest.mark.parametrize("num_tabs", [4, 100])
@pytest.mark.parametrize("rev", [True, False])
def test_add_remove_tab_benchmark(self, benchmark, widget,
qtbot, fake_web_tab, num_tabs, rev):