Clean up pinned_tab width implementation

Misc fixes from PR
This commit is contained in:
Jay Kamat 2017-09-20 20:48:48 -04:00
parent e49aa35c75
commit d5c2f2855a
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
2 changed files with 16 additions and 23 deletions

View File

@ -92,14 +92,12 @@ class TabWidget(QTabWidget):
bar.update(bar.tabRect(idx)) bar.update(bar.tabRect(idx))
def set_tab_pinned(self, tab: QWidget, def set_tab_pinned(self, tab: QWidget,
pinned: bool, *, loading: bool = False) -> None: pinned: bool) -> None:
"""Set the tab status as pinned. """Set the tab status as pinned.
Args: Args:
tab: The tab to pin tab: The tab to pin
pinned: Pinned tab state to set. pinned: Pinned tab state to set.
loading: Whether to ignore current data state when
counting pinned_count.
""" """
bar = self.tabBar() bar = self.tabBar()
idx = self.indexOf(tab) idx = self.indexOf(tab)
@ -426,17 +424,17 @@ class TabBar(QTabBar):
return return
super().mousePressEvent(e) super().mousePressEvent(e)
def minimumTabSizeHint(self, index, elipsis=True): def minimumTabSizeHint(self, index, ellipsis: bool = True):
"""Set the minimum tab size to indicator/icon/... text. """Set the minimum tab size to indicator/icon/... text.
Args: Args:
index: The index of the tab to get a size hint for. index: The index of the tab to get a size hint for.
elipsis: Whether to use elipsis to calculate width ellipsis: Whether to use ellipsis to calculate width
instead of the tab's text. instead of the tab's text.
Return: Return:
A QSize of the smallest tab size we can make. A QSize of the smallest tab size we can make.
""" """
text = '\u2026' if elipsis else self.tabText(index) text = '\u2026' if ellipsis else self.tabText(index)
icon = self.tabIcon(index) icon = self.tabIcon(index)
padding = config.val.tabs.padding padding = config.val.tabs.padding
padding_h = padding.left + padding.right padding_h = padding.left + padding.right
@ -454,23 +452,19 @@ class TabBar(QTabBar):
padding_h + config.val.tabs.width.indicator) padding_h + config.val.tabs.width.indicator)
return QSize(width, height) return QSize(width, height)
def _tabTotalWidthPinned(self): def _tab_total_width_pinned(self):
"""Get the current total width of pinned tabs. """Get the current total width of pinned tabs.
This width is calculated assuming no shortening due to elipsis.""" This width is calculated assuming no shortening due to ellipsis."""
return sum( return sum(self.minimumTabSizeHint(idx, ellipsis=False).width()
# Get the width (int) from minimum size for idx in range(self.count())
map(lambda tab: tab.width(), if self._tab_pinned(idx))
# Get the minimum size of tabs, no elipsis
map(functools.partial(self.minimumTabSizeHint, elipsis=False),
# Get only pinned tabs (from indexes)
filter(self._tabPinned, range(self.count())))))
def _pinnedCount(self) -> int: def _pinnedCount(self) -> int:
"""Get the number of pinned tabs.""" """Get the number of pinned tabs."""
return len(list(filter(self._tabPinned, range(self.count())))) return sum(self._tab_pinned(idx) for idx in range(self.count()))
def _tabPinned(self, index: int) -> bool: def _tab_pinned(self, index: int) -> bool:
"""Return True if tab is pinned.""" """Return True if tab is pinned."""
try: try:
return self.tab_data(index, 'pinned') return self.tab_data(index, 'pinned')
@ -506,15 +500,15 @@ class TabBar(QTabBar):
# want to ensure it's valid in this special case. # want to ensure it's valid in this special case.
return QSize() return QSize()
else: else:
pinned = self._tabPinned(index) pinned = self._tab_pinned(index)
no_pinned_count = self.count() - self._pinnedCount() no_pinned_count = self.count() - self._pinnedCount()
pinned_width = self._tabTotalWidthPinned() pinned_width = self._tab_total_width_pinned()
no_pinned_width = self.width() - pinned_width no_pinned_width = self.width() - pinned_width
if pinned: if pinned:
# Give pinned tabs the minimum size they need to display their # Give pinned tabs the minimum size they need to display their
# titles, let QT handle scaling it down if we get too small. # titles, let Qt handle scaling it down if we get too small.
width = self.minimumTabSizeHint(index, elipsis=False).width() width = self.minimumTabSizeHint(index, ellipsis=False).width()
else: else:
width = no_pinned_width / no_pinned_count width = no_pinned_width / no_pinned_count

View File

@ -393,8 +393,7 @@ class SessionManager(QObject):
if tab.get('active', False): if tab.get('active', False):
tab_to_focus = i tab_to_focus = i
if new_tab.data.pinned: if new_tab.data.pinned:
tabbed_browser.set_tab_pinned( tabbed_browser.set_tab_pinned(new_tab, new_tab.data.pinned)
new_tab, new_tab.data.pinned, loading=True)
if tab_to_focus is not None: if tab_to_focus is not None:
tabbed_browser.setCurrentIndex(tab_to_focus) tabbed_browser.setCurrentIndex(tab_to_focus)
if win.get('active', False): if win.get('active', False):