Make `select_on_remove=prev` select previous sibling

Updates `tabs.select_on_remove=prev` for tree tab windows so that the
previous sibling is selected, instead of the previous tab in the tab
bar. I've found that the selected tab descending to the rightmost leaf
node of a tab group when I close a top level tab is never what I want.

It may have been more respectful of people's preferences and habits to
add a "previous-sibling" option for the `tabs.select_on_remove` option,
but that would be a fair bit of work. Turning that option into strings
and mapping them to QTabBar enum values at the only place they are used
wouldn't be so bad, but then there would be another place where non-tree
code would have to deal with a tree specific option. So I figured I
would be bold and change the default behaviour and see if anyone
complained.
This commit is contained in:
toofar 2025-04-06 15:56:33 +12:00
parent 7c1c54da42
commit 2f3885a77a
2 changed files with 66 additions and 0 deletions

View File

@ -15,6 +15,7 @@ from qutebrowser.mainwindow.tabbedbrowser import TabbedBrowser, _UndoEntry
from qutebrowser.mainwindow.treetabwidget import TreeTabWidget
from qutebrowser.browser import browsertab
from qutebrowser.misc import notree
from qutebrowser.qt.widgets import QTabBar
@dataclasses.dataclass
@ -152,6 +153,31 @@ class TreeTabbedBrowser(TabbedBrowser):
node = tab.node
parent = node.parent
current_tab = self.current_tab()
# Override tabs.select_on_remove behavior to be tree aware.
# The default behavior is in QTabBar.removeTab(), by way of
# QTabWidget.removeTab(). But here we are detaching the tab from the
# tree before those methods get called, so if we want to have a tree
# aware behavior we need to implement that here by selecting the new
# tab before the closing the current one.
if tab == current_tab:
selection_behavior = self.widget.tabBar().selectionBehaviorOnRemove()
# Given a tree structure like:
# - one
# - two
# - three (active)
# If the setting is "prev" (aka left) we want to end up with tab
# "one" selected after closing tab "three". Switch to either the
# current tab's previous sibling or its parent.
if selection_behavior == QTabBar.SelectionBehavior.SelectLeftTab:
siblings = parent.children
rel_index = siblings.index(node)
if rel_index == 0:
next_tab = parent.value
else:
next_tab = siblings[rel_index-1].value
self.widget.setCurrentWidget(next_tab)
if node.collapsed:
# Collapsed nodes have already been removed from the TabWidget so

View File

@ -356,3 +356,43 @@ Feature: Tree tab management
- about:blank?grandparent
- about:blank?parent (active)
"""
Scenario: Tabs.select_on_remove prev selects previous sibling
When I open about:blank?one
And I open about:blank?two in a new related tab
And I open about:blank?three in a new tab
And I run :set tabs.select_on_remove prev
And I run :tab-close
And I run :config-unset tabs.select_on_remove
Then the following tabs should be open:
"""
- about:blank?one (active)
- about:blank?two
"""
Scenario: Tabs.select_on_remove prev selects parent
When I open about:blank?one
And I open about:blank?two in a new related tab
And I open about:blank?three in a new sibling tab
And I run :set tabs.select_on_remove prev
And I run :tab-close
And I run :config-unset tabs.select_on_remove
Then the following tabs should be open:
"""
- about:blank?one (active)
- about:blank?two
"""
Scenario: Tabs.select_on_remove prev can be overridden
When I open about:blank?one
And I open about:blank?two in a new related tab
And I open about:blank?three in a new tab
And I run :tab-select ?two
And I run :set tabs.select_on_remove prev
And I run :tab-close --next
And I run :config-unset tabs.select_on_remove
Then the following tabs should be open:
"""
- about:blank?one
- about:blank?three (active)
"""