Reset link hover pointer when output clears the highlight
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run

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
This commit is contained in:
Kovid Goyal 2026-10-23 17:02:12 +05:30
parent 34eda43194
commit 5cab872968
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
6 changed files with 84 additions and 12 deletions

View file

@ -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

View file

@ -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));

View file

@ -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;

View file

@ -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;
}

View file

@ -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);

View file

@ -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/')