Drag and drop: Fix dropping a window on a temporarily shown title bar

The title bars shown only for a window drag were hidden before the drop
was classified, which moves the windows, so a drop on a title bar was
treated as a directional insert instead of a swap.

Classify the drop in on_window_drop() before hiding them. On Wayland the
drag source can finish before the drop is delivered, so when the drop is
on kitty, on_drag_source_finished() now leaves hiding them to the drop,
falling back to hiding them if the drop never arrives.

Closes #10709
This commit is contained in:
Kovid Goyal 2026-10-21 06:03:46 +05:30
parent e26634004d
commit d4d75646bd
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
5 changed files with 151 additions and 16 deletions

View file

@ -571,6 +571,10 @@ Detailed list of changes
- Custom shaders: Fix groups using :code:`attach` not having the same
:code:`animation_progress` as the group they are attached to (:iss:`10693`)
- Drag and drop: Fix dropping a window on the title bar of another window
inserting it at an edge of that window instead of swapping the two, when
the title bars are only shown temporarily, for example during the drag
0.49.2 [2026-10-01]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -438,6 +438,8 @@ class Boss:
self.os_window_map: dict[int, TabManager] = {}
self.os_window_death_actions: dict[int, Callable[[], None]] = {}
self.thumbnail_request_queue: deque[PendingThumbnailRequest] = deque()
# A window drag whose source finished before its drop on kitty was delivered
self.awaiting_window_drop = False
self.cursor_blinking = True
self.shutting_down = False
self.misc_config_errors: list[str] = []
@ -2194,6 +2196,10 @@ class Boss:
return window
def on_drop_move(self, os_window_id: int, x: int, y: int, from_self: bool, is_leave: bool) -> None:
if is_leave and from_self and self.awaiting_window_drop:
# The drop that was awaited is not going to be delivered
self.awaiting_window_drop = False
self._hide_force_shown_title_bars()
if (tm := self.os_window_map.get(os_window_id)) is None:
return
if from_self:
@ -2221,6 +2227,12 @@ class Boss:
q.on_window_drop_move(window_id, (not is_leave) and (q is tm), x, y)
def on_drop(self, os_window_id: int, drop: dict[str, bytes] | int, from_self: bool, x: int, y: int) -> None:
window_mime_key = f'application/net.kovidgoyal.kitty-window-{os.getpid()}'
tm = self.os_window_map.get(os_window_id)
if self.awaiting_window_drop:
self.awaiting_window_drop = False
if isinstance(drop, int) or tm is None or window_mime_key not in drop:
self._hide_force_shown_title_bars() # otherwise hidden by on_window_drop() after classifying the drop
if isinstance(drop, int):
import errno
@ -2230,9 +2242,8 @@ class Boss:
msg = os.strerror(drop)
self.show_error(_('Drop failed'), f'[{code}] {msg}')
return
if (tm := self.os_window_map.get(os_window_id)) is None:
if tm is None:
return
window_mime_key = f'application/net.kovidgoyal.kitty-window-{os.getpid()}'
if widb := drop.get(window_mime_key):
window_id = int(widb)
tm.on_window_drop(x, y, window_id)
@ -2283,19 +2294,36 @@ class Boss:
add_timer(remove_dropped_files, DROPPED_FILE_PROMISES_LIFETIME, False)
def _hide_force_shown_title_bars(self) -> None:
"Hide the window title bars that were shown only for the duration of a window drag"
for tm in self.all_tab_managers:
for t in tm:
if t.force_show_title_bars:
t.force_show_title_bars = False
t.relayout()
def on_drag_source_finished(
self, was_dropped: bool, was_canceled: bool, accepted_mime_type: str, action: int, data: dict[str, bytes] | None, needs_toplevel_on_wayland: bool
self,
was_dropped: bool,
was_canceled: bool,
accepted_mime_type: str,
action: int,
data: dict[str, bytes] | None,
needs_toplevel_on_wayland: bool,
dropped_on_kitty: bool,
) -> None:
window_mime_key = f'application/net.kovidgoyal.kitty-window-{os.getpid()}'
if wid_bytes := (data or {}).get(window_mime_key):
window_id = int(wid_bytes.decode())
if get_window_being_dragged()[0] == window_id:
# Drop was not handled by on_drop (e.g. dropped outside kitty or on Wayland)
for tm in self.all_tab_managers:
for t in tm:
if t.force_show_title_bars:
t.force_show_title_bars = False
t.relayout()
if dropped_on_kitty and not was_canceled:
# The drop will be delivered to on_drop() after this. Hiding the title bars
# shown for the drag now would change the window geometry it is classified
# against, so leave that to on_window_drop().
self.awaiting_window_drop = True
else:
self._hide_force_shown_title_bars()
set_window_being_dragged()
self._update_drag_over(None)
for tm in self.all_tab_managers:

View file

@ -1305,13 +1305,14 @@ drag_source_callback(GLFWwindow *window UNUSED, GLFWDragEvent *ev) {
{ \
call_boss( \
on_drag_source_finished, \
"OOsiOO", \
"OOsiOOO", \
ds.was_dropped && !global_state.drop_dest.os_window_id ? Py_True : Py_False, \
ds.was_canceled ? Py_True : Py_False, \
ds.accepted_mime_type ? ds.accepted_mime_type : "", \
ds.action, \
ds.drag_data ? ds.drag_data : Py_None, \
ds.needs_toplevel_on_wayland ? Py_True : Py_False); \
ds.needs_toplevel_on_wayland ? Py_True : Py_False, \
ds.was_dropped && global_state.drop_dest.os_window_id ? Py_True : Py_False); \
free_drag_source(); \
}
Window *w = NULL;

View file

@ -2150,6 +2150,7 @@ class TabManager: # {{{
if (w := boss.window_id_map.get(window_id)) is None:
set_window_being_dragged()
return
boss.awaiting_window_drop = False
opts = get_options()
min_w = opts.window_title_bar_min_windows
for tm in boss.all_tab_managers:
@ -2406,6 +2407,14 @@ class TabManager: # {{{
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
in_central = central.left <= x < central.right and central.top <= y < central.bottom
# Classify the drop before hiding the title bars shown only for the drag, as
# hiding them changes the window geometry the preview was drawn against.
dest_window = self._find_window_at(x, y) if tab_target is None and in_central else None
if dest_window is not None and dest_window.id == window_id:
dest_window = None
on_title_bar = dest_window is not None and self._pointer_in_title_bar_of(dest_window, y)
direction = self._drop_direction_for(dest_window, x, y) if dest_window is not None and not on_title_bar else None
self._clear_force_show_title_bars()
w = boss.window_id_map.get(window_id)
if w is None:
@ -2421,21 +2430,19 @@ class TabManager: # {{{
return
# Case 2: Drop in central area
in_central = central.left <= x < central.right and central.top <= y < central.bottom
if not in_central:
return
if (active_tab := self.active_tab) is None:
return
dest_window = self._find_window_at(x, y)
if dest_window is None or dest_window.id == window_id:
if dest_window is None:
# Dropped on empty space or self; if different tab, move there
if active_tab is not w.tabref():
boss._move_window_to(w, target_tab_id=active_tab.id)
return
if self._pointer_in_title_bar_of(dest_window, y):
if direction is None:
if (src_tab := w.tabref()) is dest_window.tabref() and src_tab is not None:
# Same tab: swap positions
src_tab.swap_windows(w, dest_window)
@ -2443,7 +2450,7 @@ class TabManager: # {{{
# Cross-tab title bar drop: move to the destination tab
boss._move_window_to(w, target_tab_id=active_tab.id)
else:
boss._insert_window_in_direction(w, dest_window, self._drop_direction_for(dest_window, x, y))
boss._insert_window_in_direction(w, dest_window, direction)
def update_progress(self) -> None:
self.num_of_windows_with_progress = 0

View file

@ -87,6 +87,8 @@ class BossDropTest(BaseTest):
_move_window_to=Mock(),
_insert_window_in_direction=Mock(),
window_id_map={},
awaiting_window_drop=False,
_hide_force_shown_title_bars=Mock(),
)
for name in ('_update_drag_over', '_reset_drop_previews', '_sole_window_of_tab', '_tab_merge_target'):
setattr(self.boss, name, partial(getattr(Boss, name), self.boss))
@ -224,7 +226,7 @@ class TestTabDropClassificationStability(BossDropTest):
class TestWaylandDragFinish(BossDropTest):
def finish(self, was_dropped: bool = True, was_canceled: bool = False) -> None:
Boss.on_drag_source_finished(self.boss, was_dropped, was_canceled, '', 0, self.data, True)
Boss.on_drag_source_finished(self.boss, was_dropped, was_canceled, '', 0, self.data, True, not was_dropped)
def set_pending_target(self, quadrant: int, direction: str | None) -> SimpleNamespace:
target = SimpleNamespace(id=20, tab_id=2)
@ -297,6 +299,58 @@ class TestWaylandDragFinish(BossDropTest):
self.boss._move_tab_to.assert_called_once_with(self.source)
class TestWindowDragFinish(BossDropTest):
"The title bars shown only for a window drag must stay until the drop has been classified"
def setUp(self):
super().setUp()
self.data = {f'application/net.kovidgoyal.kitty-window-{os.getpid()}': b'10'}
self.get_window_being_dragged.return_value = (10, True)
self.enterContext(patch('kitty.boss.set_window_being_dragged'))
def finish(self, was_dropped: bool = False, was_canceled: bool = False, dropped_on_kitty: bool = True) -> None:
Boss.on_drag_source_finished(self.boss, was_dropped, was_canceled, '', 0, self.data, True, dropped_on_kitty)
def test_drop_on_kitty_delivered_after_finish(self):
self.finish()
self.boss._hide_force_shown_title_bars.assert_not_called()
self.assertTrue(self.boss.awaiting_window_drop)
Boss.on_drop(self.boss, 1, self.data, True, 600, 400)
# on_window_drop() hides them once it has classified the drop
self.tm.on_window_drop.assert_called_once_with(600, 400, 10)
self.boss._hide_force_shown_title_bars.assert_not_called()
self.assertFalse(self.boss.awaiting_window_drop)
# the leave that follows a drop must not do anything further
Boss.on_drop_move(self.boss, 1, 600, 400, True, True)
self.boss._hide_force_shown_title_bars.assert_not_called()
def test_awaited_drop_that_never_arrives(self):
self.finish()
self.get_window_being_dragged.return_value = (0, False)
Boss.on_drop_move(self.boss, 1, 600, 400, True, True)
self.boss._hide_force_shown_title_bars.assert_called_once_with()
self.assertFalse(self.boss.awaiting_window_drop)
def test_awaited_drop_replaced_by_another_drop(self):
self.tm.active_tab = None
for drop in ({'text/plain': b'x'}, 5):
with self.subTest(drop=drop):
self.boss._hide_force_shown_title_bars.reset_mock()
self.boss.awaiting_window_drop = True
with patch.object(self.boss, 'show_error', Mock(), create=True):
Boss.on_drop(self.boss, 1, drop, False, 600, 400)
self.boss._hide_force_shown_title_bars.assert_called_once_with()
self.assertFalse(self.boss.awaiting_window_drop)
def test_title_bars_hidden_immediately_without_a_drop_on_kitty(self):
for was_dropped, was_canceled, dropped_on_kitty in ((True, False, False), (False, True, False), (False, True, True), (False, False, False)):
with self.subTest(was_dropped=was_dropped, was_canceled=was_canceled, dropped_on_kitty=dropped_on_kitty):
self.boss._hide_force_shown_title_bars.reset_mock()
self.finish(was_dropped, was_canceled, dropped_on_kitty)
self.boss._hide_force_shown_title_bars.assert_called_once_with()
self.assertFalse(self.boss.awaiting_window_drop)
class TestFileDrop(BossDropTest):
def test_hit_test_accounts_for_an_offset_tab_bar(self):
central = self.viewport.regions()[0]
@ -903,6 +957,47 @@ class TestWindowDropTargets(BaseTest):
continue
self.ae(TabManager._drop_direction_for(tm, dest, x, y), expected[edge])
def hide_title_bar_on_clear(self, tm, *windows):
"Make clearing the title bars shown for the drag hide them, moving the window content up as the real layout does"
def clear():
for w in windows:
if w.show_title_bar:
w.show_title_bar = False
w.geometry = region(w.geometry.left, w.geometry.top + 20, w.geometry.right, w.geometry.bottom)
tm._clear_force_show_title_bars = Mock(side_effect=clear)
def test_drop_on_temporarily_shown_title_bar_swaps(self):
central = region(0, 30, 1000, 800)
tab = MagicMock()
dest = self.make_window(20, region(5, 35, 995, 400), show_title_bar=True, tab=tab)
src = self.make_window(10, region(5, 400, 995, 795), show_title_bar=True, tab=tab)
tm, regions = self.make_tab_manager([dest, src], central, region(0, 0, 1000, 30))
self.hide_title_bar_on_clear(tm, dest, src)
boss = self.make_boss(src, dest)
preview = self.drop_window(tm, regions, boss, 500, 45, 10)
self.ae(preview.quadrant, 5)
tm._clear_force_show_title_bars.assert_called_once_with()
tab.swap_windows.assert_called_once_with(src, dest)
boss._insert_window_in_direction.assert_not_called()
boss._move_window_to.assert_not_called()
def test_drop_direction_matches_preview_when_title_bars_hide(self):
central = region(0, 30, 1000, 800)
g = region(5, 35, 995, 795)
# just below the centre of the window with its title bar shown, and so above
# the centre once hiding the title bar moves the window down
x, y = (g.left + g.right) // 2, (g.top + g.bottom) // 2 + 5
dest = self.make_window(20, g, show_title_bar=True)
src = self.make_window(10, g)
tm, regions = self.make_tab_manager([dest], central, region(0, 0, 1000, 30), mode=DragOverlayMode.axis_y)
self.hide_title_bar_on_clear(tm, dest)
boss = self.make_boss(src, dest)
preview = self.drop_window(tm, regions, boss, x, y, 10)
self.ae(preview.direction, 'bottom')
boss._insert_window_in_direction.assert_called_once_with(src, dest, 'bottom')
def test_title_bar_hover_targets_the_whole_window(self):
central = region(0, 30, 1000, 800)
g = region(5, 35, 995, 795)