Drag and drop: Switch tabs on hover and preview tab insertion

This commit is contained in:
白鸽 2026-09-22 22:22:40 +08:00 • committed by Kovid Goyal
parent a3448645be
commit 6155d085d2
6 changed files with 715 additions and 76 deletions

View file

@ -236,6 +236,11 @@ Detailed list of changes
0.49.1 [2026-09-24]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Dragging windows or single-window tabs: Hover over another tab to switch to it
and choose a split position. Drop a window at a tab edge or gap to create a new tab.
Reordering or moving whole tabs now shows an insertion marker without moving
the existing labels during the drag. See :doc:`overview` for details.
- Splits layout: Dragging a divider now resizes only its adjacent regions,
consistently after rearranging windows. Fix nested dividers not tracking
mouse movement correctly, including when returning from a minimum size.

View file

@ -187,6 +187,18 @@ Window to another, or even pop them out into a new OS Window. A tab containing a
single window can also be dropped into the content area of another tab to insert
its window at the highlighted edge.
When dragging a window, or a tab containing a single window, hover over the middle
of another tab for 0.6 seconds to switch to it. Keep dragging into its content area
to choose a split position before releasing the mouse. Dropping directly on the
middle of the tab moves the window into it.
The outer 10% at either end of a tab, the gaps between tabs and the ends of the
tab bar show an insertion marker. Dropping a window there creates a new tab at
that position. Dragging a whole tab inserts it at the marker while keeping the
other tabs in place during the drag. Tabs containing multiple windows remain
independent tabs, and do not switch to or merge into another tab on hover. For
vertical tab bars, insertion positions run from top to bottom.
You can drag window borders to resize windows. You can double click on empty regions
of the tab bar to create new tabs or double click on an existing tab to rename
it.

View file

@ -746,6 +746,12 @@ class TabExtent(NamedTuple):
return self.x.start <= x <= self.x.end and self.y.start <= y <= self.y.end
class WindowDropTarget(NamedTuple):
tab_id: int = 0
# None means an existing tab; zero means append, otherwise insert before this tab.
before_tab_id: int | None = None
class TabBar:
def __init__(self, os_window_id: int):
self.os_window_id = os_window_id
@ -754,6 +760,8 @@ class TabBar:
self.data_buffer_size = 0
self.blank_rects: tuple[Border, ...] = ()
self.tab_extents: Sequence[TabExtent] = ()
self.window_drop_insert_before: int | None = None
self.tab_drop_insert_before: int | None = None
self.laid_out_once = False
self.left_edge_is_default = True
self.right_edge_is_default = True
@ -1061,6 +1069,7 @@ class TabBar:
self.tab_extents = cr
s.erase_in_line(0, False) # Ensure no long titles bleed after the last tab
self.align()
self.draw_drop_insert_marker()
return self._update_edge_defaults(False)
def update_vertical(self, data: Sequence[TabBarData]) -> bool:
@ -1158,8 +1167,32 @@ class TabBar:
s.cursor.fg = as_rgb(0xFF0000)
s.draw('…')
self.tab_extents = tuple(cr)
self.draw_drop_insert_marker()
return self._update_edge_defaults(True)
def draw_drop_insert_marker(self) -> None:
before = self.tab_drop_insert_before if self.tab_drop_insert_before is not None else self.window_drop_insert_before
if before is None:
return
extents = tuple(te for te in self.tab_extents if te.tab_id > 0)
coordinate = 0
for te in extents:
cells = te.y if self.is_vertical else te.x
if te.tab_id == before:
coordinate = cells.start
break
coordinate = cells.end + 1
s = self.screen
s.cursor.fg, s.cursor.bg = self.active_fg, self.active_bg
s.cursor.bold, s.cursor.italic = False, False
if self.is_vertical:
s.cursor.x, s.cursor.y = 0, min(s.lines - 1, coordinate)
s.draw('━' * s.columns)
else:
s.cursor.x, s.cursor.y = min(s.columns - 1, coordinate), 0
s.draw('┃')
s.cursor.fg = s.cursor.bg = 0
def align_with_factor(self, factor: int = 1) -> None:
if not self.tab_extents:
return
@ -1188,3 +1221,50 @@ class TabBar:
def drag_axis_coordinate(self, x: int, y: int) -> int:
return y if self.is_vertical else x
def tab_insertion_target_at(self, x: int, y: int) -> int:
"The tab after the nearest insertion boundary, or zero to append."
if not self.laid_out_once:
return 0
g = self.window_geometry
coordinate = y - g.top if self.is_vertical else x - g.left
cell_size = self.cell_height if self.is_vertical else self.cell_width
limit = self.screen.lines if self.is_vertical else self.screen.columns
extents = tuple(te for te in self.tab_extents if te.tab_id > 0)
for i, te in enumerate(extents):
cells = te.y if self.is_vertical else te.x
start, end = cells.start * cell_size, min(limit, cells.end + 1) * cell_size
if i + 1 < len(extents):
next_cells = extents[i + 1].y if self.is_vertical else extents[i + 1].x
end = min(end, next_cells.start * cell_size)
if end > start and coordinate < (start + end) / 2:
return te.tab_id
return 0
def window_drop_target_at(self, x: int, y: int) -> WindowDropTarget:
"""The middle 80% of a tab accepts a window. Its outer 10% and gaps insert a new tab.
Called only for points inside the tab bar viewport, including its blank margins."""
if not self.laid_out_once:
return WindowDropTarget(before_tab_id=0)
g = self.window_geometry
coordinate = y - g.top if self.is_vertical else x - g.left
cell_size = self.cell_height if self.is_vertical else self.cell_width
limit = self.screen.lines if self.is_vertical else self.screen.columns
extents = tuple(te for te in self.tab_extents if te.tab_id > 0)
for i, te in enumerate(extents):
cells = te.y if self.is_vertical else te.x
start, end = cells.start * cell_size, min(limit, cells.end + 1) * cell_size
# Some custom renderers include the next tab's first cell in their extent.
if i + 1 < len(extents):
next_cells = extents[i + 1].y if self.is_vertical else extents[i + 1].x
end = min(end, next_cells.start * cell_size)
if end <= start:
continue
if coordinate < start + (end - start) * 0.1:
return WindowDropTarget(before_tab_id=te.tab_id)
if coordinate < end - (end - start) * 0.1:
return WindowDropTarget(tab_id=te.tab_id)
if coordinate < end:
before = extents[i + 1].tab_id if i + 1 < len(extents) else 0
return WindowDropTarget(before_tab_id=before)
return WindowDropTarget(before_tab_id=0)

View file

@ -24,6 +24,7 @@ from .fast_data_types import (
GLFW_PRESS,
GLFW_RELEASE,
add_tab,
add_timer,
attach_window,
buffer_keys_in_window,
current_focused_os_window_id,
@ -40,6 +41,7 @@ from .fast_data_types import (
monotonic,
next_window_id,
remove_tab,
remove_timer,
remove_window,
reorder_tabs,
replace_c0_codes_except_nl_space_tab,
@ -56,7 +58,7 @@ from .fast_data_types import (
from .layout.base import DragOverlayMode, Layout
from .layout.interface import all_layouts, create_layout_object_for, evict_cached_layouts
from .progress import ProgressState
from .tab_bar import TabBar, TabBarData, apply_title_template
from .tab_bar import TabBar, TabBarData, WindowDropTarget, apply_title_template
from .types import ac
from .typing_compat import EdgeLiteral, SessionTab, SessionType, TypedDict
from .utils import cmdline_for_hold, color_as_int, log_error, platform_window_id, resolved_shell, shlex_split, which
@ -1317,7 +1319,7 @@ class Tab: # {{{
class TabBeingDropped(NamedTuple):
data: TabBarData
tab_ids: Sequence[int] = ()
last_drop_move_coordinate: int = -1
target_tab_id: int = 0 # A single-pane tab dropped on the middle of another tab.
DropDirection = Literal['left', 'right', 'top', 'bottom']
@ -1341,6 +1343,15 @@ class TabManager: # {{{
window_being_dropped: WindowBeingDropped | None = None
window_drag_target_tab_id: int = 0
window_drag_over_me: bool = False
window_drag_hover_tab_id: int = 0
window_drag_hover_window_id: int = 0
window_drag_hover_timer: int = 0
window_drag_hover_deadline: float = 0
window_drag_hover_delay: float = 0.6
tab_drag_hover_tab_id: int = 0
tab_drag_hover_source_id: int = 0
tab_drag_hover_timer: int = 0
tab_drag_hover_deadline: float = 0
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
@ -1805,30 +1816,13 @@ class TabManager: # {{{
@property
def tab_bar_data(self) -> Sequence[TabBarData]:
at = self.active_tab
tab_being_dragged_from_here = False
dragged_tab_id, drag_started = get_tab_being_dragged()[:2]
if drag_started:
tab_being_dragged_from_here = self.tab_for_id(dragged_tab_id) is not None
window_drag_active = get_window_being_dragged()[1]
if self.tab_being_dropped is None:
wdtt = self.window_drag_target_tab_id
if tab_being_dragged_from_here:
tabs = tuple(t.data_for_tab_bar(t is at or t.id == wdtt) for t in self.tabs_to_be_shown_in_tab_bar if t.id != dragged_tab_id)
else:
tabs = tuple(t.data_for_tab_bar(t is at or t.id == wdtt) for t in self.tabs_to_be_shown_in_tab_bar)
if window_drag_active or get_options().tab_bar_show_new_tab_button:
tabs = tabs + (TabBarData(title='+', is_active=self.window_drag_target_tab_id == -1, os_window_id=self.os_window_id),)
return tabs
tmap = {t.id: t for t in self.tabs}
at = self.active_tab
ans = []
for tid in self.tab_being_dropped.tab_ids:
if tid == dragged_tab_id:
ans.append(self.tab_being_dropped.data)
else:
tab = tmap[tid]
ans.append(tab.data_for_tab_bar(tab is at))
return ans
wdtt = self.window_drag_target_tab_id
# Drag previews only draw an insertion marker. Keeping the real tabs in
# place also keeps custom renderers' tab counts and hit testing stable.
tabs = tuple(t.data_for_tab_bar(t is at or t.id == wdtt) for t in self.tabs_to_be_shown_in_tab_bar)
if get_window_being_dragged()[1] or get_options().tab_bar_show_new_tab_button:
tabs += (TabBarData(title='+', is_active=wdtt == -1, os_window_id=self.os_window_id),)
return tabs
def apply_tab_ordering(self, tab_ids: Sequence[int]) -> None:
id_map = {t.id: t for t in self.tabs}
@ -1841,54 +1835,42 @@ class TabManager: # {{{
@update_tab_bar_visibility
def on_tab_drop_move(self, tab_id: int = 0, is_dest: bool = False, x: int = 0, y: int = 0) -> None:
if not is_dest:
self._cancel_tab_drag_hover()
if self.tab_being_dropped:
self.tab_being_dropped = None
self.tab_bar.tab_drop_insert_before = None
self._set_drag_target_tab(0)
self.layout_tab_bar()
return
if self.tab_bar_should_be_visible:
all_tabs = [t.tab_id for t in self.tab_bar.last_laid_out_tabs if t.tab_id >= 0]
else:
all_tabs = [t.tab_id for t in self.tab_bar_data if t.tab_id >= 0]
force_update = False
if self.tab_being_dropped is None:
tab = get_boss().tab_for_id(tab_id)
if tab is None:
return
tab_data = tab.data_for_tab_bar(tab is get_boss().active_tab)
if tab_id not in all_tabs:
all_tabs.append(tab_id)
_, _, start_x, start_y = get_tab_being_dragged()
start_coordinate = self.tab_bar.drag_axis_coordinate(int(start_x), int(start_y))
self.tab_being_dropped = TabBeingDropped(data=tab_data, tab_ids=all_tabs, last_drop_move_coordinate=start_coordinate)
force_update = True
coordinate = self.tab_bar.drag_axis_coordinate(x, y)
if coordinate == self.tab_being_dropped.last_drop_move_coordinate and not force_update:
if (tab := get_boss().tab_for_id(tab_id)) is None:
self.on_tab_drop_move()
return
mouse_moved_towards_start = coordinate < self.tab_being_dropped.last_drop_move_coordinate
old_tab_ids = self.tab_being_dropped.tab_ids
idx_under_mouse = -1
if tab_id_under_mouse := self.tab_bar.tab_id_at(x, y):
with suppress(Exception):
idx_under_mouse = old_tab_ids.index(tab_id_under_mouse)
if idx_under_mouse < 0:
start = self.tab_bar.window_geometry.top if self.tab_bar.is_vertical else self.tab_bar.window_geometry.left
idx_under_mouse = 0 if coordinate < start else len(old_tab_ids) - 1
old_idx_under_mouse = old_tab_ids.index(tab_id)
idx_moved_towards_start = old_idx_under_mouse > idx_under_mouse
new_tab_ids = old_tab_ids
if mouse_moved_towards_start == idx_moved_towards_start:
new_tab_ids = list(old_tab_ids)
new_tab_ids[idx_under_mouse], new_tab_ids[old_idx_under_mouse] = new_tab_ids[old_idx_under_mouse], new_tab_ids[idx_under_mouse]
self.tab_being_dropped = self.tab_being_dropped._replace(last_drop_move_coordinate=coordinate, tab_ids=new_tab_ids)
if force_update or self.tab_being_dropped.tab_ids != old_tab_ids:
self.layout_tab_bar()
all_tabs = [t.id for t in self.tabs_to_be_shown_in_tab_bar]
before = self.tab_bar.tab_insertion_target_at(x, y)
target_tab_id = 0
if tab.windows.num_groups == 1:
target = self.tab_bar.window_drop_target_at(x, y)
if target.tab_id != tab_id and self.tab_for_id(target.tab_id) is not None:
target_tab_id = target.tab_id
self._set_tab_drag_hover_target(tab_id, target_tab_id)
idx = all_tabs.index(before) if before in all_tabs else len(all_tabs)
# Insert at the indicated boundary, preserving the order of every other tab.
order = [tid for tid in all_tabs[:idx] if tid != tab_id] + [tab_id] + [tid for tid in all_tabs[idx:] if tid != tab_id]
self.tab_being_dropped = TabBeingDropped(data=tab.data_for_tab_bar(tab is get_boss().active_tab), tab_ids=order, target_tab_id=target_tab_id)
marker = None if target_tab_id else before
if self.tab_bar.tab_drop_insert_before != marker:
self.tab_bar.tab_drop_insert_before = marker
self.mark_tab_bar_dirty()
@update_tab_bar_visibility
def on_tab_drop(self, x: int, y: int, bypass_move: bool = False) -> None:
if (td := self.tab_being_dropped) is None:
return
if (tab := get_boss().tab_for_id(td.data.tab_id)) is None:
self._cancel_tab_drag_hover()
self.tab_being_dropped = None
self.tab_bar.tab_drop_insert_before = None
self._set_drag_target_tab(0)
set_tab_being_dragged()
self.layout_tab_bar()
return
@ -1897,15 +1879,31 @@ class TabManager: # {{{
if (td := self.tab_being_dropped) is None:
return
self.tab_being_dropped = None
self.tab_bar.tab_drop_insert_before = None
self._cancel_tab_drag_hover()
self._set_drag_target_tab(0)
atid = self.active_tab.id if self.active_tab else 0
set_tab_being_dragged()
if (
td.target_tab_id
and td.target_tab_id != tab.id
and self.tab_for_id(td.target_tab_id) is not None
and tab.windows.num_groups == 1
and (window := tab.active_window) is not None
):
get_boss()._move_window_to(window, target_tab_id=td.target_tab_id)
self.layout_tab_bar()
return
if tab.os_window_id != self.os_window_id:
if (t := get_boss()._move_tab_to(tab, self.os_window_id)) is not None:
n = list(td.tab_ids)
idx = n.index(td.data.tab_id)
n[idx] = t.id
td = td._replace(tab_ids=n)
self.apply_tab_ordering(td.tab_ids)
else:
self.layout_tab_bar()
return
self.apply_tab_ordering([tid for tid in td.tab_ids if self.tab_for_id(tid) is not None])
if atid and tab.os_window_id == self.os_window_id and (tab := self.tab_for_id(atid)):
idx = self.tabs.index(tab)
self._set_active_tab(idx, store_in_history=False)
@ -1943,7 +1941,7 @@ class TabManager: # {{{
except OSError as e:
log_error(f'Failed to start tab drag: {e}')
set_tab_being_dragged()
self.mark_tab_bar_dirty() # re-render the tab bar in case it was drawn without the dragged tab
self.mark_tab_bar_dirty()
break
else:
set_tab_being_dragged()
@ -2067,11 +2065,84 @@ class TabManager: # {{{
self.window_drag_target_tab_id = tab_id
self.mark_tab_bar_dirty()
def _cancel_tab_drag_hover(self) -> None:
if self.tab_drag_hover_timer:
remove_timer(self.tab_drag_hover_timer)
self.tab_drag_hover_timer = 0
self.tab_drag_hover_tab_id = self.tab_drag_hover_source_id = 0
def _activate_tab_drag_hover(self, timer_id: int | None = None) -> None:
if timer_id is not None:
if timer_id != self.tab_drag_hover_timer:
return
self.tab_drag_hover_timer = 0
tab_id, source_id = self.tab_drag_hover_tab_id, self.tab_drag_hover_source_id
self._cancel_tab_drag_hover()
if not self.window_drag_over_me or get_tab_being_dragged()[:2] != (source_id, True):
return
source = get_boss().tab_for_id(source_id)
if source is None or source.windows.num_groups != 1:
return
if (tab := self.tab_for_id(tab_id)) is not None and tab is not source:
self.set_active_tab(tab)
def _set_tab_drag_hover_target(self, source_id: int, tab_id: int) -> None:
self._set_drag_target_tab(tab_id)
tab = self.tab_for_id(tab_id) if tab_id else None
if tab is None or tab is self.active_tab:
self._cancel_tab_drag_hover()
elif (tab_id, source_id) != (self.tab_drag_hover_tab_id, self.tab_drag_hover_source_id):
self._cancel_tab_drag_hover()
self.tab_drag_hover_tab_id, self.tab_drag_hover_source_id = tab_id, source_id
self.tab_drag_hover_deadline = monotonic() + self.window_drag_hover_delay
self.tab_drag_hover_timer = add_timer(self._activate_tab_drag_hover, self.window_drag_hover_delay, False)
elif monotonic() >= self.tab_drag_hover_deadline:
# Periodic Cocoa drag events also run in tracking modes that suspend timers.
self._activate_tab_drag_hover()
def _cancel_window_drag_hover(self) -> None:
if self.window_drag_hover_timer:
remove_timer(self.window_drag_hover_timer)
self.window_drag_hover_timer = 0
self.window_drag_hover_tab_id = self.window_drag_hover_window_id = 0
def _activate_window_drag_hover_tab(self, timer_id: int | None = None) -> None:
if timer_id is not None:
if timer_id != self.window_drag_hover_timer:
return
self.window_drag_hover_timer = 0
tab_id, window_id = self.window_drag_hover_tab_id, self.window_drag_hover_window_id
self._cancel_window_drag_hover()
if not self.window_drag_over_me or get_window_being_dragged()[:2] != (window_id, True):
return
if (tab := self.tab_for_id(tab_id)) is not None:
self.set_active_tab(tab)
def _set_window_drop_tab_target(self, target: WindowDropTarget | None = None, window_id: int = 0) -> None:
before = target.before_tab_id if target is not None else None
if self.tab_bar.window_drop_insert_before != before:
self.tab_bar.window_drop_insert_before = before
self.mark_tab_bar_dirty()
tab_id = target.tab_id if target is not None else 0
self._set_drag_target_tab(-1 if before is not None else tab_id)
tab = self.tab_for_id(tab_id) if tab_id else None
if tab is None or tab is self.active_tab:
self._cancel_window_drag_hover()
elif (tab_id, window_id) != (self.window_drag_hover_tab_id, self.window_drag_hover_window_id):
self._cancel_window_drag_hover()
self.window_drag_hover_tab_id, self.window_drag_hover_window_id = tab_id, window_id
self.window_drag_hover_deadline = monotonic() + self.window_drag_hover_delay
self.window_drag_hover_timer = add_timer(self._activate_window_drag_hover_tab, self.window_drag_hover_delay, False)
elif monotonic() >= self.window_drag_hover_deadline:
# Cocoa sends periodic drag updates even while stationary, including in
# tracking run loop modes where ordinary timers do not fire.
self._activate_window_drag_hover_tab()
def _clear_force_show_title_bars(self) -> None:
boss = get_boss()
for tm in boss.all_tab_managers:
tm._set_drag_target_window(0)
tm._set_drag_target_tab(0)
tm._set_window_drop_tab_target()
for tab in tm:
if tab.force_show_title_bars:
tab.force_show_title_bars = False
@ -2163,16 +2234,16 @@ class TabManager: # {{{
visibility is owned by set_drag_over_me(), which the caller must have already updated."""
if not is_dest:
self._set_drag_target_window(0)
self._set_drag_target_tab(0)
self._set_window_drop_tab_target()
return
from .fast_data_types import viewport_for_window
tab_bar = viewport_for_window(self.os_window_id)[1]
if tab_bar.left <= x < tab_bar.right and tab_bar.top <= y < tab_bar.bottom:
self._set_drag_target_window(0)
self._set_drag_target_tab(self.tab_bar.tab_id_at(x, y))
self._set_window_drop_tab_target(self.tab_bar.window_drop_target_at(x, y), window_id)
return
self._set_drag_target_tab(0)
self._set_window_drop_tab_target()
dest_window = self._find_window_at(x, y)
active_tab = self.active_tab
if dest_window is None or dest_window.id == window_id or active_tab is None:
@ -2198,21 +2269,29 @@ class TabManager: # {{{
# run before the on_drop data transfer completes, clearing window_drag_over_me
# and hiding the tab bar for the single-tab case before we get here.
self.set_drag_over_me(True)
central, tab_bar = viewport_for_window(self.os_window_id)[:2]
in_tab_bar = tab_bar.left <= x < tab_bar.right and tab_bar.top <= y < tab_bar.bottom
tab_target = self.tab_bar.window_drop_target_at(x, y) if in_tab_bar else None
self._clear_force_show_title_bars()
w = boss.window_id_map.get(window_id)
if w is None:
return
set_window_being_dragged()
self.mark_tab_bar_dirty()
central, tab_bar = viewport_for_window(self.os_window_id)[:2]
# Case 1: Drop on tab bar → move to that tab
in_tab_bar = tab_bar.left <= x < tab_bar.right and tab_bar.top <= y < tab_bar.bottom
if in_tab_bar:
if (tab_id := self.tab_bar.tab_id_at(x, y)) and (dest_tab := self.tab_for_id(tab_id)):
if tab_target is not None:
if tab_target.before_tab_id is not None:
order = [t.id for t in self.tabs]
before = tab_target.before_tab_id
idx = order.index(before) if before in order else len(order)
dest_tab = self.new_tab(empty_tab=True)
order.insert(idx, dest_tab.id)
boss._move_window_to(w, target_tab_id=dest_tab.id)
# Moving the sole window out of a source tab can remove that tab.
self.apply_tab_ordering([tid for tid in order if self.tab_for_id(tid) is not None])
self.set_active_tab(dest_tab)
elif (dest_tab := self.tab_for_id(tab_target.tab_id)) is not None:
boss._move_window_to(w, target_tab_id=dest_tab.id)
else:
boss._move_window_to(w, target_tab_id='new')
return
# Case 2: Drop in central area
@ -2257,6 +2336,8 @@ class TabManager: # {{{
return self.tab_bar.blank_rects if self.tab_bar_should_be_visible else ()
def destroy(self) -> None:
self._cancel_window_drag_hover()
self._cancel_tab_drag_hover()
for t in self:
t.destroy()
self.tab_bar.destroy()

View file

@ -5,7 +5,7 @@ from unittest.mock import patch
from kitty.fast_data_types import LEFT_EDGE, Color, Region
from kitty.options.utils import tab_title_wrap
from kitty.tab_bar import CellRange, TabBar, TabBarData, as_rgb, powerline_symbols, truncate_line, wrap_title
from kitty.tab_bar import CellRange, TabBar, TabBarData, TabExtent, WindowDropTarget, as_rgb, powerline_symbols, truncate_line, wrap_title
from kitty.utils import color_as_int
from .base import BaseTest
@ -24,6 +24,100 @@ class DummyBoss:
class TestTabBar(BaseTest):
def test_tab_insertion_uses_nearest_boundary(self):
tb = self.vertical_tab_bar()
for vertical in (False, True):
tb.is_vertical = vertical
tb.screen.resize(50, 100)
tb.cell_width, tb.cell_height = 10, 20
tb.tab_extents = tuple(
TabExtent(tid, CellRange(start, end), CellRange(start, end)) for tid, start, end in ((1, 2, 11), (2, 14, 23), (3, 26, 35), (-1, 38, 40))
)
cell_size = tb.cell_height if vertical else tb.cell_width
for coordinate, before in ((0, 1), (6.99, 1), (7, 2), (13, 2), (18.99, 2), (19, 3), (31, 0), (40, 0)):
pos = int(coordinate * cell_size)
x, y = (5, pos) if vertical else (pos, 5)
self.ae(tb.tab_insertion_target_at(x, y), before, (vertical, coordinate))
def test_tab_insertion_marker_does_not_replace_tabs(self):
tb = self.vertical_tab_bar()
data, extents, original = tb.last_laid_out_tabs, tb.tab_extents, self.screen_lines(tb)
with patch('kitty.tab_bar.update_tab_bar_edge_colors', return_value=None), patch('kitty.tab_bar.get_boss', return_value=DummyBoss()):
tb.tab_drop_insert_before = 2
tb.update(data)
self.ae(tb.last_laid_out_tabs, data)
self.ae(tb.tab_extents, extents)
self.assertIn('━', self.screen_lines(tb)[extents[1].y.start])
tb.tab_drop_insert_before = None
tb.update(data)
self.ae(self.screen_lines(tb), original)
def test_window_drop_tab_edges_and_gaps(self):
tb = self.vertical_tab_bar()
for vertical in (False, True):
with self.subTest(vertical=vertical):
tb.is_vertical = vertical
tb.screen.resize(50, 100)
tb.cell_width, tb.cell_height = 10, 20
tb.tab_extents = tuple(
TabExtent(tid, CellRange(start, end), CellRange(start, end)) for tid, start, end in ((1, 2, 11), (2, 14, 23), (-1, 26, 28))
)
cell_size = tb.cell_height if vertical else tb.cell_width
for coordinate, target in (
(0, WindowDropTarget(before_tab_id=1)),
(2.99, WindowDropTarget(before_tab_id=1)),
(3, WindowDropTarget(tab_id=1)),
(10.99, WindowDropTarget(tab_id=1)),
(11, WindowDropTarget(before_tab_id=2)),
(13, WindowDropTarget(before_tab_id=2)),
(14.99, WindowDropTarget(before_tab_id=2)),
(15, WindowDropTarget(tab_id=2)),
(23, WindowDropTarget(before_tab_id=0)),
(27, WindowDropTarget(before_tab_id=0)),
(40, WindowDropTarget(before_tab_id=0)),
):
pos = int(coordinate * cell_size)
x, y = (tb.window_geometry.left + 5, tb.window_geometry.top + pos) if vertical else (pos, 5)
self.ae(tb.window_drop_target_at(x, y), target, (vertical, coordinate))
def test_window_drop_custom_full_width_tabs(self):
tb = self.vertical_tab_bar()
tb.is_vertical = False
tb.screen.resize(1, 100)
tb.cell_width = 10
# A full-width custom renderer returns the cell after each tab, including the
# first cell of its neighbor. The synthetic '+' is entirely off screen.
tb.tab_extents = tuple(TabExtent(tid, CellRange(start, end)) for tid, start, end in ((1, 0, 50), (2, 50, 100), (-1, 100, 150)))
for x, target in (
(49, WindowDropTarget(before_tab_id=1)),
(50, WindowDropTarget(tab_id=1)),
(449, WindowDropTarget(tab_id=1)),
(450, WindowDropTarget(before_tab_id=2)),
(549, WindowDropTarget(before_tab_id=2)),
(550, WindowDropTarget(tab_id=2)),
(949, WindowDropTarget(tab_id=2)),
(950, WindowDropTarget(before_tab_id=0)),
(999, WindowDropTarget(before_tab_id=0)),
):
self.ae(tb.window_drop_target_at(x, 5), target, x)
tb.window_drop_insert_before = 0
tb.draw_drop_insert_marker()
self.ae(str(tb.screen.line(0))[-1], '┃')
def test_window_drop_marker_does_not_move_tabs(self):
tb = self.vertical_tab_bar()
data = tb.last_laid_out_tabs
extents, original = tb.tab_extents, self.screen_lines(tb)
with patch('kitty.tab_bar.update_tab_bar_edge_colors', return_value=None), patch('kitty.tab_bar.get_boss', return_value=DummyBoss()):
tb.window_drop_insert_before = 2
tb.update(data)
self.ae(tb.tab_extents, extents)
self.assertIn('━', self.screen_lines(tb)[extents[1].y.start])
tb.window_drop_insert_before = None
tb.update(data)
self.ae(tb.tab_extents, extents)
self.ae(self.screen_lines(tb), original)
def test_vertical_tab_bar_hit_testing(self) -> None:
self.set_options(
{

View file

@ -9,6 +9,7 @@ from unittest.mock import MagicMock, Mock, patch
from kitty.boss import Boss
from kitty.layout.interface import Splits, Stack, Tall
from kitty.tab_bar import TabBarData, WindowDropTarget
from kitty.tabs import DragOverlayMode, Tab, TabManager
from .base import BaseTest
@ -297,6 +298,371 @@ class TestFileDrop(BossDropTest):
hidden.on_drop.assert_not_called()
class TestTabInsertionPreview(BaseTest):
def make_tab(self, tab_id, os_window_id=7):
return SimpleNamespace(
id=tab_id,
os_window_id=os_window_id,
windows=SimpleNamespace(num_groups=2),
data_for_tab_bar=lambda active: TabBarData(title=f'tab {tab_id}', tab_id=tab_id, is_active=active),
)
def setUp(self):
super().setUp()
tabs = [self.make_tab(i) for i in (1, 2, 3, 4)]
self.source = self.make_tab(9, 8)
self.tm = tm = SimpleNamespace(
tabs=tabs,
tabs_to_be_shown_in_tab_bar=tabs,
active_tab=tabs[0],
os_window_id=7,
tab_bar=SimpleNamespace(
tab_drop_insert_before=None,
tab_insertion_target_at=Mock(return_value=2),
window_drop_target_at=Mock(return_value=WindowDropTarget(before_tab_id=2)),
),
tab_being_dropped=None,
window_drag_target_tab_id=0,
window_drag_over_me=True,
window_drag_hover_delay=0.6,
tab_drag_hover_tab_id=0,
tab_drag_hover_source_id=0,
tab_drag_hover_timer=0,
tab_drag_hover_deadline=0,
tab_bar_should_be_visible=True,
tab_bar_hidden=False,
mark_tab_bar_dirty=Mock(),
layout_tab_bar=Mock(),
resize=Mock(),
)
for name in (
'tab_for_id',
'on_tab_drop_move',
'on_tab_drop',
'apply_tab_ordering',
'_set_drag_target_tab',
'_cancel_tab_drag_hover',
'_set_tab_drag_hover_target',
'_activate_tab_drag_hover',
):
setattr(tm, name, partial(getattr(TabManager, name), tm))
tm._set_active_tab = Mock(side_effect=lambda idx, **kw: setattr(tm, 'active_tab', tm.tabs[idx]))
tm.set_active_tab = Mock(side_effect=lambda tab: setattr(tm, 'active_tab', tab))
def move(tab, os_window_id):
self.ae((tab.id, os_window_id), (9, 7))
moved = self.make_tab(99)
tm.tabs.append(moved)
tm.active_tab = moved
return moved
self.boss = SimpleNamespace(
tab_for_id=lambda tid: self.source if tid == 9 else tm.tab_for_id(tid),
active_tab=self.source,
_move_tab_to=Mock(side_effect=move),
_move_window_to=Mock(),
)
self.enterContext(patch('kitty.tabs.get_boss', return_value=self.boss))
self.enterContext(patch('kitty.tabs.is_tab_bar_visible', return_value=True))
self.enterContext(patch('kitty.tabs.set_tab_being_dragged'))
self.enterContext(patch('kitty.tabs.get_window_being_dragged', return_value=(0, False)))
self.drag = self.enterContext(patch('kitty.tabs.get_tab_being_dragged', return_value=(9, True, 0, 0)))
self.clock = self.enterContext(patch('kitty.tabs.monotonic', return_value=10.0))
self.add_timer = self.enterContext(patch('kitty.tabs.add_timer', side_effect=range(1, 100)))
self.remove_timer = self.enterContext(patch('kitty.tabs.remove_timer'))
self.enterContext(patch('kitty.tabs.get_options', return_value=SimpleNamespace(tab_bar_show_new_tab_button=False)))
self.reorder = self.enterContext(patch('kitty.tabs.reorder_tabs'))
def rendered_ids(self):
return [t.tab_id for t in TabManager.tab_bar_data.fget(self.tm)]
def test_cross_window_preview_keeps_existing_tabs_and_count(self):
for before, expected in ((1, [9, 1, 2, 3, 4]), (3, [1, 2, 9, 3, 4]), (0, [1, 2, 3, 4, 9])):
self.tm.tab_bar.tab_insertion_target_at.return_value = before
for _ in range(3):
self.tm.on_tab_drop_move(9, True, 200, 10)
self.ae(self.rendered_ids(), [1, 2, 3, 4])
self.ae([t.id for t in self.tm.tabs], [1, 2, 3, 4])
self.ae(self.tm.tab_being_dropped.tab_ids, expected)
self.ae(self.tm.tab_bar.tab_drop_insert_before, before)
self.boss._move_tab_to.assert_not_called()
self.reorder.assert_not_called()
def test_cross_window_drop_inserts_and_preserves_neighbors(self):
self.tm.on_tab_drop_move(9, True, 200, 10)
self.tm.on_tab_drop(200, 10)
self.ae([t.id for t in self.tm.tabs], [1, 99, 2, 3, 4])
self.boss._move_tab_to.assert_called_once_with(self.source, 7)
self.ae(self.tm.active_tab.id, 99)
self.assertIsNone(self.tm.tab_bar.tab_drop_insert_before)
self.assertIsNone(self.tm.tab_being_dropped)
def test_same_window_drop_inserts_instead_of_swapping(self):
self.tm.tab_bar.tab_insertion_target_at.return_value = 4
self.tm.on_tab_drop_move(1, True, 200, 10)
self.ae(self.rendered_ids(), [1, 2, 3, 4])
self.tm.on_tab_drop(200, 10)
self.ae([t.id for t in self.tm.tabs], [2, 3, 1, 4])
self.ae(self.tm.active_tab.id, 1)
self.boss._move_tab_to.assert_not_called()
def test_leaving_clears_marker_without_changing_order(self):
self.tm.on_tab_drop_move(9, True, 200, 10)
self.tm.on_tab_drop_move()
self.ae(self.rendered_ids(), [1, 2, 3, 4])
self.assertIsNone(self.tm.tab_bar.tab_drop_insert_before)
self.assertIsNone(self.tm.tab_being_dropped)
self.boss._move_tab_to.assert_not_called()
def test_pending_drop_finishes_at_previewed_boundary(self):
self.tm.on_tab_drop_move(9, True, 200, 10)
self.tm.tab_bar.tab_insertion_target_at.return_value = 0
self.tm.on_tab_drop(0, 0, bypass_move=True)
self.ae([t.id for t in self.tm.tabs], [1, 99, 2, 3, 4])
def test_closed_drag_source_clears_marker(self):
self.tm.on_tab_drop_move(9, True, 200, 10)
self.source = None
self.tm.on_tab_drop(200, 10)
self.assertIsNone(self.tm.tab_bar.tab_drop_insert_before)
self.assertIsNone(self.tm.tab_being_dropped)
self.ae(self.rendered_ids(), [1, 2, 3, 4])
def hover_single_pane(self, target_id=2):
self.source.windows.num_groups = 1
self.source.active_window = SimpleNamespace(id=90)
self.tm.tab_bar.window_drop_target_at.return_value = WindowDropTarget(tab_id=target_id)
self.tm.on_tab_drop_move(9, True, 200, 10)
def test_single_pane_tab_hover_switches_without_moving(self):
self.hover_single_pane()
self.ae(self.rendered_ids(), [1, 2, 3, 4])
self.assertIsNone(self.tm.tab_bar.tab_drop_insert_before)
self.ae(self.tm.tab_being_dropped.target_tab_id, 2)
self.tm.set_active_tab.assert_not_called()
callback, interval, repeats = self.add_timer.call_args.args
self.ae((interval, repeats), (0.6, False))
callback(self.tm.tab_drag_hover_timer)
self.ae(self.tm.active_tab.id, 2)
self.boss._move_window_to.assert_not_called()
self.boss._move_tab_to.assert_not_called()
def test_single_pane_tab_periodic_hover_switches_at_deadline(self):
self.hover_single_pane()
self.clock.return_value = 10.59
self.tm.on_tab_drop_move(9, True, 200, 10)
self.tm.set_active_tab.assert_not_called()
self.clock.return_value = 10.6
self.tm.on_tab_drop_move(9, True, 200, 10)
self.ae(self.tm.active_tab.id, 2)
self.remove_timer.assert_called_once_with(1)
self.tm.on_tab_drop_move(9, True, 200, 10)
self.ae(self.add_timer.call_count, 1)
def test_single_pane_tab_edge_cancels_hover_and_keeps_insertion(self):
self.hover_single_pane()
callback = self.add_timer.call_args.args[0]
self.tm.tab_bar.window_drop_target_at.return_value = WindowDropTarget(before_tab_id=2)
self.tm.on_tab_drop_move(9, True, 200, 10)
callback(1)
self.tm.set_active_tab.assert_not_called()
self.ae(self.tm.tab_bar.tab_drop_insert_before, 2)
self.ae(self.tm.tab_drag_hover_timer, 0)
self.tm.on_tab_drop(200, 10)
self.ae([t.id for t in self.tm.tabs], [1, 99, 2, 3, 4])
self.boss._move_window_to.assert_not_called()
def test_single_pane_tab_leaving_cancels_hover(self):
self.hover_single_pane()
callback = self.add_timer.call_args.args[0]
self.tm.on_tab_drop_move()
callback(1)
self.tm.set_active_tab.assert_not_called()
self.ae(self.tm.tab_drag_hover_timer, 0)
self.ae(self.tm.window_drag_target_tab_id, 0)
def test_single_pane_tab_center_drop_moves_the_pane(self):
self.hover_single_pane()
self.tm.on_tab_drop(200, 10)
self.boss._move_window_to.assert_called_once_with(self.source.active_window, target_tab_id=2)
self.boss._move_tab_to.assert_not_called()
self.reorder.assert_not_called()
self.ae(self.tm.tab_drag_hover_timer, 0)
def test_multiple_pane_tab_does_not_hover_switch(self):
self.tm.tab_bar.window_drop_target_at.return_value = WindowDropTarget(tab_id=2)
self.tm.on_tab_drop_move(9, True, 200, 10)
self.ae(self.tm.tab_being_dropped.target_tab_id, 0)
self.ae(self.tm.tab_bar.tab_drop_insert_before, 2)
self.add_timer.assert_not_called()
def test_adding_a_pane_during_hover_prevents_merging(self):
self.hover_single_pane()
self.source.windows.num_groups = 2
self.add_timer.call_args.args[0](self.tm.tab_drag_hover_timer)
self.tm.set_active_tab.assert_not_called()
self.tm.on_tab_drop(200, 10, bypass_move=True)
self.boss._move_window_to.assert_not_called()
self.ae([t.id for t in self.tm.tabs], [1, 99, 2, 3, 4])
def test_finished_tab_drag_cannot_switch_later(self):
self.hover_single_pane()
self.drag.return_value = (0, False, 0, 0)
self.add_timer.call_args.args[0](self.tm.tab_drag_hover_timer)
self.tm.set_active_tab.assert_not_called()
def test_tab_hover_ignores_stale_timer_after_changing_target(self):
self.hover_single_pane()
callback = self.add_timer.call_args.args[0]
self.hover_single_pane(3)
callback(1)
self.tm.set_active_tab.assert_not_called()
callback(2)
self.ae(self.tm.active_tab.id, 3)
class TestWindowDropTabs(BaseTest):
def setUp(self):
super().setUp()
self.tabs = [SimpleNamespace(id=i) for i in (1, 2, 3)]
self.tm = tm = SimpleNamespace(
tabs=self.tabs,
active_tab=self.tabs[0],
tab_bar=SimpleNamespace(window_drop_insert_before=None),
window_drag_target_tab_id=0,
window_drag_hover_tab_id=0,
window_drag_hover_window_id=0,
window_drag_hover_timer=0,
window_drag_hover_deadline=0,
window_drag_hover_delay=0.6,
window_drag_over_me=True,
os_window_id=7,
mark_tab_bar_dirty=Mock(),
set_drag_over_me=Mock(),
_clear_force_show_title_bars=Mock(),
)
for name in (
'tab_for_id',
'_set_drag_target_tab',
'_cancel_window_drag_hover',
'_activate_window_drag_hover_tab',
'_set_window_drop_tab_target',
'apply_tab_ordering',
):
setattr(tm, name, partial(getattr(TabManager, name), tm))
tm.set_active_tab = Mock(side_effect=lambda tab: setattr(tm, 'active_tab', tab))
self.clock = self.enterContext(patch('kitty.tabs.monotonic', return_value=10.0))
self.drag = self.enterContext(patch('kitty.tabs.get_window_being_dragged', return_value=(10, True, 0, 0)))
self.add_timer = self.enterContext(patch('kitty.tabs.add_timer', side_effect=range(1, 100)))
self.remove_timer = self.enterContext(patch('kitty.tabs.remove_timer'))
def hover(self, tab_id=2):
self.tm._set_window_drop_tab_target(WindowDropTarget(tab_id=tab_id), 10)
def test_hover_timer_switches_without_moving_window(self):
self.hover()
self.tm.set_active_tab.assert_not_called()
callback, interval, repeats = self.add_timer.call_args.args
self.ae((interval, repeats), (0.6, False))
callback(1)
self.tm.set_active_tab.assert_called_once_with(self.tabs[1])
self.ae(self.tm.window_drag_hover_timer, 0)
self.remove_timer.assert_not_called()
def test_periodic_drag_updates_switch_at_deadline(self):
self.hover()
self.clock.return_value = 10.59
self.hover()
self.tm.set_active_tab.assert_not_called()
self.clock.return_value = 10.6
self.hover()
self.tm.set_active_tab.assert_called_once_with(self.tabs[1])
self.remove_timer.assert_called_once_with(1)
self.hover()
self.ae(self.add_timer.call_count, 1)
def test_changing_targets_restarts_hover_and_ignores_stale_timer(self):
self.hover()
first_callback = self.add_timer.call_args.args[0]
self.hover(3)
first_callback(1)
self.tm.set_active_tab.assert_not_called()
self.add_timer.call_args.args[0](2)
self.tm.set_active_tab.assert_called_once_with(self.tabs[2])
def test_clearing_or_inserting_cancels_hover(self):
for target in (None, WindowDropTarget(before_tab_id=2), WindowDropTarget(before_tab_id=0)):
with self.subTest(target=target):
self.hover()
callback = self.add_timer.call_args.args[0]
timer = self.tm.window_drag_hover_timer
self.tm._set_window_drop_tab_target(target)
callback(timer)
self.tm.set_active_tab.assert_not_called()
self.ae(self.tm.window_drag_hover_timer, 0)
self.ae(self.tm.tab_bar.window_drop_insert_before, target.before_tab_id if target else None)
def test_ended_drag_or_closed_target_cannot_switch(self):
for closed in (False, True):
self.hover()
timer = self.tm.window_drag_hover_timer
if closed:
self.tabs.remove(self.tabs[1])
else:
self.drag.return_value = (0, False, 0, 0)
self.add_timer.call_args.args[0](timer)
self.tm.set_active_tab.assert_not_called()
self.drag.return_value = (10, True, 0, 0)
def drop(self, target, remove_source=False):
tm = self.tm
tm.tab_bar.window_drop_target_at = Mock(return_value=target)
window = SimpleNamespace(id=10)
def move(w, target_tab_id):
self.assertIs(w, window)
self.assertIsNotNone(tm.tab_for_id(target_tab_id))
if remove_source:
tm.tabs.remove(tm.tab_for_id(1))
def new_tab(**kw):
self.ae(kw, {'empty_tab': True})
tab = SimpleNamespace(id=4)
tm.tabs.append(tab)
return tab
tm.new_tab = Mock(side_effect=new_tab)
boss = SimpleNamespace(window_id_map={10: window}, _move_window_to=Mock(side_effect=move))
with (
patch('kitty.tabs.get_boss', return_value=boss),
patch('kitty.tabs.set_window_being_dragged'),
patch('kitty.tabs.reorder_tabs') as reorder,
patch('kitty.fast_data_types.viewport_for_window', return_value=(region(0, 30, 1000, 800), region(0, 0, 1000, 30))),
):
TabManager.on_window_drop(tm, 500, 10, 10)
return boss, reorder
def test_drop_inserts_at_requested_position_and_destination(self):
for before, expected in ((1, [4, 1, 2, 3]), (2, [1, 4, 2, 3]), (0, [1, 2, 3, 4])):
with self.subTest(before=before):
self.tm.tabs = self.tabs[:]
boss, reorder = self.drop(WindowDropTarget(before_tab_id=before))
self.ae([t.id for t in self.tm.tabs], expected)
reorder.assert_called_once_with(7, *expected)
boss._move_window_to.assert_called_once_with(boss.window_id_map[10], target_tab_id=4)
self.ae(self.tm.active_tab.id, 4)
def test_drop_preserves_order_when_source_tab_disappears(self):
self.drop(WindowDropTarget(before_tab_id=1), remove_source=True)
self.ae([t.id for t in self.tm.tabs], [4, 2, 3])
def test_center_drop_moves_to_existing_tab(self):
boss, reorder = self.drop(WindowDropTarget(tab_id=2))
boss._move_window_to.assert_called_once_with(boss.window_id_map[10], target_tab_id=2)
self.tm.new_tab.assert_not_called()
reorder.assert_not_called()
class TestWindowDropTargets(BaseTest):
"Drives the real TabManager drop handlers, whose hit testing is in OS Window coordinates"
@ -326,6 +692,7 @@ class TestWindowDropTargets(BaseTest):
window_drag_over_me=True,
tab_bar_hidden=False,
_set_drag_target_tab=Mock(),
_set_window_drop_tab_target=Mock(),
_clear_force_show_title_bars=Mock(),
mark_tab_bar_dirty=Mock(),
layout_tab_bar=Mock(),