From 5cab872968d99856335867ca2cfa8a764d31e4e2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 23 Oct 2026 17:02:12 +0530 Subject: [PATCH] Reset link hover pointer when output clears the highlight Program output can remove the highlighted link under the mouse, which left the hand pointer and the hovered link state stale until the mouse moved or the modifiers changed. After parsing output, if the highlight in the hovered window was cleared, reset the hovered link and re-derive the pointer shape, using the current mouse tracking mode. Closes #10725 --- docs/changelog.rst | 2 +- kitty/child-monitor.c | 2 ++ kitty/mouse.c | 49 +++++++++++++++++++++++++++++++++---------- kitty/screen.c | 2 ++ kitty/state.h | 1 + kitty_tests/mouse.py | 40 +++++++++++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 12 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 78401dc02..5e402fcc9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -238,7 +238,7 @@ Detailed list of changes - Show the hand pointer and hover highlighting over links only when the pressed modifiers match a configured mouse action for opening the link with a single click. Update both immediately when modifiers are pressed or released, even - without moving the mouse. + without moving the mouse, or when program output removes the link. - A new option :opt:`restore_session_after_logout` to save the state of every kitty instance, including the scrollback of every window, when logging diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index ded97e824..42e03bcc5 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -551,7 +551,9 @@ parse_input_once(ChildMonitor *self, PyObject *a UNUSED) { static bool do_parse(ChildMonitor *self, Screen *screen, monotonic_t now, bool flush) { ParseData pd = {.dump_callback = self->dump_callback, .now = now}; + const bool had_url_highlight = screen->url_ranges.count > 0; self->parse_func(screen, &pd, flush); + if (had_url_highlight && !screen->url_ranges.count) link_hover_cleared_by_output(screen); if (pd.input_read) { if (pd.write_space_created) wakeup_io_loop(self, false); if (screen->paused_rendering.expires_at) set_maximum_wait(MAX(0, screen->paused_rendering.expires_at - now)); diff --git a/kitty/mouse.c b/kitty/mouse.c index fe66141b4..759c7f037 100644 --- a/kitty/mouse.c +++ b/kitty/mouse.c @@ -1216,23 +1216,50 @@ window_in_active_tab(OSWindow *osw, id_type window_id) { return NULL; } +static bool +mouse_is_busy(void) { + return global_state.redirect_mouse_handling || global_state.active_drag_in_window || global_state.tracked_drag_in_window || + global_state.tab_being_dragged.id || global_state.window_being_dragged.id || global_state.active_drag_resize; +} + +static Window * +hovered_window(OSWindow **osw) { + const id_type wid = global_state.mouse_hover_in_window; + if (!wid) return NULL; + // The mouse can be over an OS window other than the one receiving events + *osw = global_state.callback_os_window; + Window *w = window_in_active_tab(*osw, wid); + for (size_t i = 0; !w && i < global_state.num_os_windows; i++) { + *osw = global_state.os_windows + i; + w = window_in_active_tab(*osw, wid); + } + return w; +} + +// Output can clear the link hover highlight, so keep the pointer and the +// hovered link consistent with it when the screen is under the mouse. +void +link_hover_cleared_by_output(Screen *screen) { + OSWindow *osw; + Window *w = hovered_window(&osw); + if (!w || w->render_data.screen != screen) return; + zero_at_ptr(&screen->current_hyperlink_under_mouse); + if (mouse_cursor_shape != POINTER_POINTER || mouse_is_busy()) return; + OSWindow *callback_os_window = global_state.callback_os_window; + global_state.callback_os_window = osw; + update_mouse_pointer_shape(); + global_state.callback_os_window = callback_os_window; +} + // Show or remove the link hover highlight in the window under the mouse when // modifiers change without the mouse moving. Returns true if the highlight changed. bool update_link_hover_for_modifiers(int old_mods, int new_mods) { old_mods &= MOUSE_MAPPING_MODS_MASK; new_mods &= MOUSE_MAPPING_MODS_MASK; - const id_type wid = global_state.mouse_hover_in_window; - if (old_mods == new_mods || !OPT(detect_urls) || !wid || global_state.redirect_mouse_handling || global_state.active_drag_in_window || - global_state.tracked_drag_in_window || global_state.tab_being_dragged.id || global_state.window_being_dragged.id || global_state.active_drag_resize) - return false; - // The mouse can be over an OS window other than the one receiving key events - OSWindow *osw = global_state.callback_os_window; - Window *w = window_in_active_tab(osw, wid); - for (size_t i = 0; !w && i < global_state.num_os_windows; i++) { - osw = global_state.os_windows + i; - w = window_in_active_tab(osw, wid); - } + if (old_mods == new_mods || !OPT(detect_urls) || mouse_is_busy()) return false; + OSWindow *osw; + Window *w = hovered_window(&osw); if (!w) return false; Screen *screen = w->render_data.screen; if (!w->visible || !screen || w->scrollbar.is_hovering || w->scrollbar.is_dragging) return false; diff --git a/kitty/screen.c b/kitty/screen.c index 425eb9b17..6f0e329e5 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -7138,8 +7138,10 @@ static PyObject * test_parse_written_data(Screen *screen, PyObject *args) { ParseData pd = {.now = monotonic()}; if (!PyArg_ParseTuple(args, "|O", &pd.dump_callback)) return NULL; + const bool had_url_highlight = screen->url_ranges.count > 0; if (pd.dump_callback && pd.dump_callback != Py_None) parse_worker_dump(screen, &pd, true); else parse_worker(screen, &pd, true); + if (had_url_highlight && !screen->url_ranges.count) link_hover_cleared_by_output(screen); Py_RETURN_NONE; } diff --git a/kitty/state.h b/kitty/state.h index fb078f043..020ad3ff1 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -853,6 +853,7 @@ void change_live_resize_state(OSWindow *, bool); bool render_os_window(OSWindow *w, monotonic_t now, bool scan_for_animated_images); void update_mouse_pointer_shape(void); bool update_link_hover_for_modifiers(int old_mods, int new_mods); +void link_hover_cleared_by_output(Screen *screen); void adjust_window_size_for_csd(OSWindow *w, int width, int height, int *adjusted_width, int *adjusted_height); void dispatch_buffered_keys(Window *w); void free_buffered_keys(Window *w); diff --git a/kitty_tests/mouse.py b/kitty_tests/mouse.py index f515d591c..6f2a406cf 100644 --- a/kitty_tests/mouse.py +++ b/kitty_tests/mouse.py @@ -148,6 +148,46 @@ class TestMouse(BaseTest): self.ae(updated, False) self.ae(update_mock_pointer(w, 0, mods, True, 0, border), (shape, False)) + def test_link_hover_cleared_by_output(self): + s = self.create_screen(cols=60) + w = create_mock_window(s) + mods = GLFW_MOD_CONTROL | GLFW_MOD_SHIFT + set_options(load_config(overrides=('clear_all_mouse_actions yes', 'mouse_map ctrl+shift+left release ungrabbed mouse_handle_click link'))) + + def marked(): + return ''.join(s.text_for_marked_url()) + + def setup(): + s.reset() + s.draw('https://example.invalid/') + return send_mouse_event(w, x=2, modifiers=mods) + + parse_bytes(s, b'\x1b[?1000h') + grabbed_shape = send_mouse_event(w, x=50) + s.reset() + normal_shape = send_mouse_event(w, x=50) + for output, expected in ( + (b' suffix', normal_shape), + (b'\r\x1b[Kplain text', normal_shape), + (b'\x1b[2J', normal_shape), + (b'\x1b[?1000h\r\x1b[K', grabbed_shape), + (b'\x1b[?1000h', None), + (b'\x1b[3;1Hother line', None), + ): + with self.subTest(output=output): + link_shape = setup() + self.assertNotIn(link_shape, (normal_shape, grabbed_shape)) + self.ae(marked(), 'https://example.invalid/') + # Output alone, with the modifiers still held, must update the pointer + shape = update_mock_pointer(w, mods, mods, True, 0, 0, None, False, partial(parse_bytes, s, output))[0] + self.ae(shape, expected or link_shape) + self.ae(bool(marked()), expected is None) + if expected is not None: + # Releasing the modifiers after the output changes nothing + setup() + self.ae(update_mock_pointer(w, mods, 0, True, 0, 0, None, False, partial(parse_bytes, s, output)), (expected, False)) + self.ae(marked(), '') + def test_link_hover_stays_consistent(self): s = self.create_screen(cols=30) s.draw('x' * 6 + 'https://example.invalid/')