Restore active tab after same-window tab reordering

This commit is contained in:
白鸽 2026-09-28 12:55:26 +08:00
parent 132d00dd09
commit ab8ef49c02
4 changed files with 75 additions and 4 deletions

View file

@ -202,6 +202,9 @@ Detailed list of changes
0.49.2 [future]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Drag and drop: Restore the previously active tab after dropping a dragged tab
on the same OS window's tab bar, even if it was reordered after a hover switch
- Sessions: Expand launch option variables when an option value matches the command name
- Preserve transparent background colors when applying unrelated launch color overrides

View file

@ -2142,12 +2142,13 @@ class Boss:
if tm is not None:
tm.update_tab_bar_data()
def _reset_drop_previews(self) -> None:
"Clear all drag and drop UI state, once a drag has ended"
def _reset_drop_previews(self, restore_tab_drag_focus_for: TabManager | None = None) -> None:
"Clear drag UI state and restore pre-hover focus after a same-window tab reorder"
self._update_drag_over(None)
for q in self.all_tab_managers:
q.on_window_drop_move()
q.on_tab_drop_move()
q.finish_tab_drag_hover(restore_focus=q is restore_tab_drag_focus_for)
q.layout_tab_bar() # ensure tab bar is fully updated
def _update_drag_over(self, tm: TabManager | None) -> None:
@ -2222,14 +2223,17 @@ class Boss:
if (tidb := drop.get(f'application/net.kovidgoyal.kitty-tab-{os.getpid()}')) and (tab := self.tab_for_id(int(tidb))):
tab_bar = viewport_for_window(os_window_id)[1]
in_tab_bar = tab_bar.left <= x < tab_bar.right and tab_bar.top <= y < tab_bar.bottom
restore_tab_drag_focus_for = None
if (merge_window := self._tab_merge_target(tab, tm, x, y)) is not None:
tm.on_window_drop(x, y, merge_window.id)
elif in_tab_bar or tab.os_window_id != tm.os_window_id:
if in_tab_bar and tab.os_window_id == tm.os_window_id:
restore_tab_drag_focus_for = tm
tm.on_tab_drop(x, y)
else:
self._move_tab_to(tab)
set_tab_being_dragged()
self._reset_drop_previews()
self._reset_drop_previews(restore_tab_drag_focus_for)
return
central, tab_bar = viewport_for_window(os_window_id)[:2]
if central.left <= x < central.right and central.top <= y < central.bottom:
@ -2308,7 +2312,9 @@ class Boss:
self._reset_drop_previews()
return
if tm.tab_being_dropped:
restore_tab_drag_focus_for = tm if tab.os_window_id == tm.os_window_id else None
tm.on_tab_drop(0, 0, bypass_move=True)
self._reset_drop_previews(restore_tab_drag_focus_for)
return
set_tab_being_dragged()
self._update_drag_over(None)
@ -2317,6 +2323,11 @@ class Boss:
tm.on_tab_drop_move()
if was_dropped and not was_canceled and tab is not None: # detach tab into new OS Window
self._move_tab_to(tab)
# Cocoa can finish an internal drag before on_drop receives its data.
# Keep the saved focus until that callback classifies the actual drop.
if was_canceled or was_dropped or needs_toplevel_on_wayland:
for tm in self.all_tab_managers:
tm.finish_tab_drag_hover(restore_focus=False)
@ac(
'win',

View file

@ -1354,6 +1354,8 @@ class TabManager: # {{{
window_drag_over_me: bool = False
drag_hover: DragHover | None = None
drag_hover_delay: float = 0.6
tab_drag_focus_before_hover: int = 0
tab_drag_history_before_hover: tuple[int, ...] = ()
def __init__(self, os_window_id: int, args: CLIOptions, wm_class: str, wm_name: str, startup_session: SessionType | None = None):
self.os_window_id = os_window_id
@ -2054,6 +2056,18 @@ class TabManager: # {{{
if h.timer:
remove_timer(h.timer)
def finish_tab_drag_hover(self, restore_focus: bool = True) -> None:
"""Discard a tab drag's hover selection, restoring the previous tab if needed."""
tab_id = self.tab_drag_focus_before_hover
history = self.tab_drag_history_before_hover
self.tab_drag_focus_before_hover = 0
self.tab_drag_history_before_hover = ()
if restore_focus and tab_id and (tab := self.tab_for_id(tab_id)) is not None:
if tab is not self.active_tab:
self.set_active_tab(tab)
self.active_tab_history.clear()
self.active_tab_history.extend(tid for tid in history if self.tab_for_id(tid) is not None)
def _activate_drag_hover(self, timer_id: int | None = None) -> None:
if (h := self.drag_hover) is None or (timer_id is not None and timer_id != h.timer):
return
@ -2073,6 +2087,9 @@ class TabManager: # {{{
if source is None or source.windows.num_groups != 1 or source.id == h.tab_id:
return
if (tab := self.tab_for_id(h.tab_id)) is not None:
if not h.source_is_window and not self.tab_drag_focus_before_hover and self.active_tab is not None:
self.tab_drag_focus_before_hover = self.active_tab.id
self.tab_drag_history_before_hover = tuple(self.active_tab_history)
self.set_active_tab(tab)
def _update_drag_hover(self, tab_id: int, source_id: int, source_is_window: bool) -> None:

View file

@ -2,6 +2,7 @@
# License: GPL v3 Copyright: 2026, kitty contributors
import os
from collections import deque
from contextlib import nullcontext
from functools import partial
from types import SimpleNamespace
@ -58,7 +59,7 @@ class FakeTabManager:
self.viewport, self.os_window_id, self.active_tab = viewport, os_window_id, active_tab
self.tab_being_dropped: object = None
self.window_being_dropped: object = None
for name in ('on_tab_drop_move', 'on_window_drop_move', 'on_window_drop', 'on_tab_drop', 'layout_tab_bar'):
for name in ('on_tab_drop_move', 'on_window_drop_move', 'on_window_drop', 'on_tab_drop', 'finish_tab_drag_hover', 'layout_tab_bar'):
setattr(self, name, Mock(name=name))
def set_drag_over_me(self, over: bool) -> None:
@ -126,6 +127,7 @@ class TestTabDropClassification(BossDropTest):
self.ae(self.drag_move(600, 400), ((10, True, 600, 400), (1, False, 600, 400)))
self.ae(self.drop(600, 400), 'merge')
self.tm.on_window_drop.assert_called_once_with(600, 400, 10)
self.tm.finish_tab_drag_hover.assert_called_with(restore_focus=False)
self.boss._move_tab_to.assert_not_called()
self.set_tab_being_dragged.assert_called_once_with()
@ -140,6 +142,12 @@ class TestTabDropClassification(BossDropTest):
self.ae(self.drag_move(600, 10), ((0, False, 600, 10), (1, True, 600, 10)))
self.ae(self.drop(600, 10), 'tab_drop')
self.tm.on_tab_drop.assert_called_once_with(600, 10)
self.tm.finish_tab_drag_hover.assert_called_with(restore_focus=True)
def test_cross_window_tab_bar_drop_keeps_destination_focus(self):
self.source.os_window_id = 9
self.ae(self.drop(600, 10), 'tab_drop')
self.tm.finish_tab_drag_hover.assert_called_with(restore_focus=False)
def test_multi_window_tab_keeps_tab_behavior(self):
self.source.windows.num_groups = 2
@ -344,6 +352,9 @@ class TestTabInsertionPreview(BaseTest):
window_drag_over_me=True,
drag_hover=None,
drag_hover_delay=0.6,
tab_drag_focus_before_hover=0,
tab_drag_history_before_hover=(),
active_tab_history=deque(),
tab_bar_should_be_visible=True,
tab_bar_hidden=False,
mark_tab_bar_dirty=Mock(),
@ -359,6 +370,7 @@ class TestTabInsertionPreview(BaseTest):
'_cancel_drag_hover',
'_update_drag_hover',
'_activate_drag_hover',
'finish_tab_drag_hover',
)
def move(tab, os_window_id):
@ -472,6 +484,34 @@ class TestTabInsertionPreview(BaseTest):
self.boss._move_tab_to.assert_not_called()
self.remove_timer.assert_not_called()
def test_tab_bar_drop_restores_focus_after_hover_with_or_without_reorder(self):
source = self.tm.tabs[0]
source.windows.num_groups = 1
self.source = source
self.drag.return_value = (source.id, True, 0, 0)
self.tm.tab_bar.window_drop_target_at.return_value = WindowDropTarget(tab_id=2)
for before, expected in ((2, [1, 2, 3, 4]), (0, [2, 3, 4, 1])):
with self.subTest(before=before):
self.tm.tabs[:] = sorted(self.tm.tabs, key=lambda t: t.id)
self.tm.active_tab_idx = 2 # The dragged tab need not be the active one
self.tm.active_tab_history = deque([4])
self.tm.tab_bar.tab_insertion_target_at.return_value = before
self.tm.on_tab_drop_move(source.id, True, 200, 10)
self.add_timer.call_args.args[0](self.tm.drag_hover.timer)
self.ae(self.tm.active_tab.id, 2)
self.tm.on_tab_drop(200, 10)
self.tm.finish_tab_drag_hover()
self.ae(self.tm.ids(), expected)
self.ae(self.tm.active_tab.id, 3)
self.ae(list(self.tm.active_tab_history), [4])
def test_split_drop_keeps_hovered_tab_active(self):
self.hover_single_pane()
self.add_timer.call_args.args[0](self.tm.drag_hover.timer)
self.tm.finish_tab_drag_hover(restore_focus=False)
self.ae(self.tm.active_tab.id, 2)
self.ae(self.tm.tab_drag_focus_before_hover, 0)
def test_single_pane_tab_periodic_hover_switches_at_deadline(self):
self.hover_single_pane()
self.clock.return_value = 10.59