From 302961a86aa0ba2a2998e8c21e0319f4f4a91f96 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Wed, 28 Jun 2017 21:15:49 -0700 Subject: Refactor set_tab_pinned to take a tab widget. See #2759 --- qutebrowser/browser/commands.py | 9 +++++++-- qutebrowser/mainwindow/tabbedbrowser.py | 2 +- qutebrowser/mainwindow/tabwidget.py | 9 +++++---- qutebrowser/misc/sessions.py | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 3d8516fe2..54ee0d53e 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -282,7 +282,12 @@ class CommandDispatcher: to_pin = not tab.data.pinned tab_index = self._current_index() if count is None else count - 1 cmdutils.check_overflow(tab_index + 1, 'int') - self._tabbed_browser.set_tab_pinned(tab_index, to_pin) + tab = self._cntwidget(count) + + if tab is None: + raise cmdexc.CommandError("Unable to find tab '{}'.".format(count)) + + self._tabbed_browser.set_tab_pinned(tab, to_pin) @cmdutils.register(instance='command-dispatcher', name='open', maxsplit=0, scope='window') @@ -515,7 +520,7 @@ class CommandDispatcher: newtab.data.keep_icon = True newtab.history.deserialize(history) newtab.zoom.set_factor(curtab.zoom.factor()) - new_tabbed_browser.set_tab_pinned(idx, curtab.data.pinned) + new_tabbed_browser.set_tab_pinned(newtab, curtab.data.pinned) return newtab @cmdutils.register(instance='command-dispatcher', scope='window') diff --git a/qutebrowser/mainwindow/tabbedbrowser.py b/qutebrowser/mainwindow/tabbedbrowser.py index e0468330e..94508ab63 100644 --- a/qutebrowser/mainwindow/tabbedbrowser.py +++ b/qutebrowser/mainwindow/tabbedbrowser.py @@ -342,7 +342,7 @@ class TabbedBrowser(tabwidget.TabWidget): newtab = self.tabopen(url, background=False, idx=idx) newtab.history.deserialize(history_data) - self.set_tab_pinned(idx, pinned) + self.set_tab_pinned(newtab, pinned) @pyqtSlot('QUrl', bool) def openurl(self, url, newtab): diff --git a/qutebrowser/mainwindow/tabwidget.py b/qutebrowser/mainwindow/tabwidget.py index 2c4eb6eab..c7823f84e 100644 --- a/qutebrowser/mainwindow/tabwidget.py +++ b/qutebrowser/mainwindow/tabwidget.py @@ -26,7 +26,7 @@ from PyQt5.QtCore import (pyqtSignal, pyqtSlot, Qt, QSize, QRect, QPoint, QTimer, QUrl) from PyQt5.QtWidgets import (QTabWidget, QTabBar, QSizePolicy, QCommonStyle, QStyle, QStylePainter, QStyleOptionTab, - QStyleFactory) + QStyleFactory, QWidget) from PyQt5.QtGui import QIcon, QPalette, QColor from qutebrowser.utils import qtutils, objreg, utils, usertypes, log @@ -94,17 +94,18 @@ class TabWidget(QTabWidget): bar.set_tab_data(idx, 'indicator-color', color) bar.update(bar.tabRect(idx)) - def set_tab_pinned(self, idx, pinned, *, loading=False): + def set_tab_pinned(self, tab: QWidget, + pinned: bool, *, loading: bool = False) -> None: """Set the tab status as pinned. Args: - idx: The tab index. + tab: The tab to pin pinned: Pinned tab state to set. loading: Whether to ignore current data state when counting pinned_count. """ bar = self.tabBar() - tab = self.widget(idx) + idx = self.indexOf(tab) # Only modify pinned_count if we had a change # always modify pinned_count if we are loading diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index 5ce4ee66d..26656e1ee 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -406,7 +406,7 @@ class SessionManager(QObject): tab_to_focus = i if new_tab.data.pinned: tabbed_browser.set_tab_pinned( - i, new_tab.data.pinned, loading=True) + new_tab, new_tab.data.pinned, loading=True) if tab_to_focus is not None: tabbed_browser.setCurrentIndex(tab_to_focus) if win.get('active', False): -- cgit v1.2.3-54-g00ecf From 596dee69d61baeb5cdbed8d9d044597f2038d275 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Thu, 29 Jun 2017 20:04:02 -0700 Subject: Clean up pin_tab Also add a test case for :pin-tab with an invalid count --- qutebrowser/browser/commands.py | 9 +-------- tests/end2end/features/tabs.feature | 11 +++++++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 54ee0d53e..f21115db5 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -277,16 +277,9 @@ class CommandDispatcher: """ tab = self._cntwidget(count) if tab is None: - return + raise cmdexc.CommandError("Tab {} does not exist!".format(count)) to_pin = not tab.data.pinned - tab_index = self._current_index() if count is None else count - 1 - cmdutils.check_overflow(tab_index + 1, 'int') - tab = self._cntwidget(count) - - if tab is None: - raise cmdexc.CommandError("Unable to find tab '{}'.".format(count)) - self._tabbed_browser.set_tab_pinned(tab, to_pin) @cmdutils.register(instance='command-dispatcher', name='open', diff --git a/tests/end2end/features/tabs.feature b/tests/end2end/features/tabs.feature index 9980b448f..1d3f22b10 100644 --- a/tests/end2end/features/tabs.feature +++ b/tests/end2end/features/tabs.feature @@ -1073,6 +1073,17 @@ Feature: Tab management - data/numbers/2.txt (pinned) - data/numbers/3.txt (active) + Scenario: :tab-pin with an invalid count + When I open data/numbers/1.txt + And I open data/numbers/2.txt in a new tab + And I open data/numbers/3.txt in a new tab + And I run :tab-pin with count 23 + Then the error "Tab 23 does not exist!" should be shown + And the following tabs should be open: + - data/numbers/1.txt + - data/numbers/2.txt + - data/numbers/3.txt (active) + Scenario: Pinned :tab-close prompt yes When I open data/numbers/1.txt And I run :tab-pin -- cgit v1.2.3-54-g00ecf From 2fbadc46d21e5a76d94904e951105464c1acebfb Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Fri, 30 Jun 2017 09:57:39 -0700 Subject: Remove error when count is invalid to :tab-pin --- qutebrowser/browser/commands.py | 2 +- tests/end2end/features/tabs.feature | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index f21115db5..0b448a845 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -277,7 +277,7 @@ class CommandDispatcher: """ tab = self._cntwidget(count) if tab is None: - raise cmdexc.CommandError("Tab {} does not exist!".format(count)) + return to_pin = not tab.data.pinned self._tabbed_browser.set_tab_pinned(tab, to_pin) diff --git a/tests/end2end/features/tabs.feature b/tests/end2end/features/tabs.feature index 1d3f22b10..8097f390e 100644 --- a/tests/end2end/features/tabs.feature +++ b/tests/end2end/features/tabs.feature @@ -1078,8 +1078,7 @@ Feature: Tab management And I open data/numbers/2.txt in a new tab And I open data/numbers/3.txt in a new tab And I run :tab-pin with count 23 - Then the error "Tab 23 does not exist!" should be shown - And the following tabs should be open: + Then the following tabs should be open: - data/numbers/1.txt - data/numbers/2.txt - data/numbers/3.txt (active) -- cgit v1.2.3-54-g00ecf