From b96efddbdc90bc925e8f27c67d168782fd05490b Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 15 May 2014 00:02:40 +0200 Subject: [PATCH] Make tab close buttons work --- TODO | 2 -- qutebrowser/widgets/_tabbedbrowser.py | 52 +++++++++++++++++---------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index f47620976..ac55e8e5e 100644 --- a/TODO +++ b/TODO @@ -92,8 +92,6 @@ Bugs - Command history seems to be broken -- Close buttons broken in tabs - - Config errors on start shouldn't be hard errors (e.g. when an user-stylesheet file is deleted) diff --git a/qutebrowser/widgets/_tabbedbrowser.py b/qutebrowser/widgets/_tabbedbrowser.py index f52c8210c..b7742b3e1 100644 --- a/qutebrowser/widgets/_tabbedbrowser.py +++ b/qutebrowser/widgets/_tabbedbrowser.py @@ -94,6 +94,7 @@ class TabbedBrowser(TabWidget): def __init__(self, parent=None): super().__init__(parent) + self.tabCloseRequested.connect(self.on_tab_close_requested) self.currentChanged.connect(self.on_current_changed) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self._tabs = [] @@ -157,21 +158,40 @@ class TabbedBrowser(TabWidget): tab.titleChanged.connect(self.on_title_changed) tab.iconChanged.connect(self.on_icon_changed) - def _close_tab(self, tab): - """Close the given tab. + def _close_tab(self, tab_or_idx): + """Close a tab with either index or tab given. Args: - tab: The QTabWidget to close. + tab_or_index: Either the QWebView to be closed or an index. """ - idx = self.indexOf(tab) - if idx == -1: - raise ValueError("tab {} is not contained in " - "TabbedWidget!".format(tab)) - url = tab.url() - if not url.isEmpty(): - self._url_stack.append(url) - self.removeTab(idx) - tab.shutdown(callback=partial(self._cb_tab_shutdown, tab)) + try: + idx = int(tab_or_idx) + except TypeError: + tab = tab_or_idx + idx = self.indexOf(tab_or_idx) + if idx == -1: + raise ValueError("tab {} is not contained in " + "TabbedWidget!".format(tab)) + else: + tab = self.widget(idx) + if tab is None: + raise ValueError("invalid index {}!".format(idx)) + last_close = config.get('tabbar', 'last-close') + if self.count() > 1: + url = tab.url() + if not url.isEmpty(): + self._url_stack.append(url) + self.removeTab(idx) + tab.shutdown(callback=partial(self._cb_tab_shutdown, tab)) + elif last_close == 'quit': + self.quit.emit() + elif last_close == 'blank': + tab.openurl('about:blank') + + @pyqtSlot(int) + def on_tab_close_requested(self, idx): + """Close a tab via an index.""" + self._close_tab(idx) def _tab_move_absolute(self, idx): """Get an index for moving a tab absolutely. @@ -285,13 +305,7 @@ class TabbedBrowser(TabWidget): tab = self.cntwidget(count) if tab is None: return - last_close = config.get('tabbar', 'last-close') - if self.count() > 1: - self._close_tab(tab) - elif last_close == 'quit': - self.quit.emit() - elif last_close == 'blank': - tab.openurl('about:blank') + self._close_tab(tab) @cmdutils.register(instance='mainwindow.tabs') def only(self):