From 34eda431940fc8d66ffd0ada7d4a6451fdbd6699 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 23 Oct 2026 11:05:32 +0530 Subject: [PATCH] Cleanup previous PR Follow up fixes for link hover feedback from PR #10723: - Recompute the cell under the mouse when modifiers change, so a stale cell is not highlighted when the mouse is in the window padding - Compare against the displayed highlight rather than the old modifiers, so a change of mouse tracking mode without mouse motion is handled - Update the hover in the OS window under the mouse, not only in the one receiving key events - Keep the hand pointer over a highlighted link when the program changes the pointer shape - Do not leave a link highlighted after opening it with a mapping that does not show hover feedback - Skip marking hyperlinks that are not going to be highlighted - Validate url_mappings in set_url_mappings() and ignore it in --debug-config output - Make the update_mock_pointer test helper restore only the state it overrides and keep heap state owned by the mock window --- kitty/debug_config.py | 2 +- kitty/mouse.c | 182 +++++++++++++++++++++++++++++++++--------- kitty/screen.c | 17 ++-- kitty/screen.h | 2 +- kitty/state.c | 8 +- kitty/state.h | 1 + kitty_tests/mouse.py | 35 ++++++++ 7 files changed, 195 insertions(+), 52 deletions(-) diff --git a/kitty/debug_config.py b/kitty/debug_config.py index 7a5ee8289..e7bf24a8e 100644 --- a/kitty/debug_config.py +++ b/kitty/debug_config.py @@ -84,7 +84,7 @@ def compare_opts(opts: KittyOpts, global_shortcuts: dict[str, SingleKey] | None, print() print('Config options different from defaults:') default_opts = load_config() - ignored = ('keymap', 'sequence_map', 'mousemap', 'map', 'mouse_map') + ignored = ('keymap', 'sequence_map', 'mousemap', 'url_mappings', 'map', 'mouse_map') changed_opts = [f for f in sorted(defaults._fields) if f not in ignored and getattr(opts, f) != getattr(defaults, f)] field_len = max(map(len, changed_opts)) if changed_opts else 20 fmt = f'{{:{field_len:d}s}}' diff --git a/kitty/mouse.c b/kitty/mouse.c index 80e812ce7..fe66141b4 100644 --- a/kitty/mouse.c +++ b/kitty/mouse.c @@ -433,17 +433,19 @@ handle_mouse_movement_in_kitty(Window *w, int button, bool mouse_cell_changed) { } } +static bool +url_is_clickable_with(Screen *screen, int modifiers) { + return OPT(url_mappings)[screen->modes.mouse_tracking_mode != NO_TRACKING] & (1ull << (modifiers & MOUSE_MAPPING_MODS_MASK)); +} + static void detect_url(Screen *screen, unsigned int x, unsigned int y, int modifiers) { - int hid = screen_detect_url(screen, x, y); + const bool clickable = url_is_clickable_with(screen, modifiers); + int hid = screen_detect_url(screen, x, y, clickable); zero_at_ptr(&screen->current_hyperlink_under_mouse); set_mouse_cursor_for_screen(screen); if (hid != 0) { - if (OPT(url_mappings)[screen->modes.mouse_tracking_mode != NO_TRACKING] & (1ull << (modifiers & MOUSE_MAPPING_MODS_MASK))) { - mouse_cursor_shape = POINTER_POINTER; - } else { - screen_mark_url(screen, 0, 0, 0, 0); - } + if (clickable) mouse_cursor_shape = POINTER_POINTER; screen->current_hyperlink_under_mouse.has_detected_url = hid < 0; if (hid > 0) { screen->current_hyperlink_under_mouse.id = (hyperlink_id_type)hid; @@ -708,7 +710,7 @@ handle_potential_drag(Window *w, int button) { debug("Sent drag start event to child\n"); } else if (w->drag_source.potential_drag.type == POTENTIAL_DRAG_URL) { w->drag_source.potential_drag.type = POTENTIAL_DRAG_NONE; - screen_detect_url(screen, w->drag_source.potential_drag.x, w->drag_source.potential_drag.y); + screen_detect_url(screen, w->drag_source.potential_drag.x, w->drag_source.potential_drag.y, true); if (screen->current_hyperlink_under_mouse.id || screen->current_hyperlink_under_mouse.has_detected_url) { screen_open_url(screen, "drag_url"); debug("Started URL drag\n"); @@ -805,9 +807,13 @@ add_press(Window *w, int button, int modifiers) { bool mouse_open_url(Window *w) { Screen *screen = w->render_data.screen; - // Opening a URL must work even when hover highlighting is disabled. - screen_detect_url(screen, w->mouse_pos.cell_x, w->mouse_pos.cell_y); - return screen_open_url(screen, "open_url"); + // Opening a URL must work even when hover highlighting is disabled, so + // mark it, then remove the mark if there was no hover highlight before. + const bool was_highlighted = screen->url_ranges.count > 0; + screen_detect_url(screen, w->mouse_pos.cell_x, w->mouse_pos.cell_y, true); + const bool opened = screen_open_url(screen, "open_url"); + if (!was_highlighted) screen_mark_url(screen, 0, 0, 0, 0); + return opened; } bool @@ -1187,6 +1193,8 @@ update_mouse_pointer_shape(void) { mouse_cursor_shape = scrollbar_drag_mouse_cursor; } else if (r.window->render_data.screen) { set_mouse_cursor_for_screen(r.window->render_data.screen); + // Keep the pointer consistent with a link hover highlight + if (r.window->render_data.screen->url_ranges.count && r.window->id == global_state.mouse_hover_in_window) mouse_cursor_shape = POINTER_POINTER; } } set_mouse_cursor(mouse_cursor_shape); @@ -1198,28 +1206,57 @@ leave_event(int modifiers) { set_currently_hovered_window(0, modifiers, false); } +static Window * +window_in_active_tab(OSWindow *osw, id_type window_id) { + if (!osw || !osw->num_tabs) return NULL; + Tab *t = osw->tabs + osw->active_tab; + for (unsigned i = 0; i < t->num_windows; i++) { + if (t->windows[i].id == window_id) return t->windows + i; + } + return NULL; +} + +// 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; - OSWindow *osw = global_state.callback_os_window; - if (old_mods == new_mods || !OPT(detect_urls) || !osw || !osw->num_tabs || !global_state.mouse_hover_in_window || 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) + 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; - Tab *tab = osw->tabs + osw->active_tab; - for (unsigned i = 0; i < tab->num_windows; i++) { - Window *w = tab->windows + i; - if (w->id != global_state.mouse_hover_in_window) continue; - Screen *screen = w->render_data.screen; - if (!w->visible || !screen || w->scrollbar.is_hovering || w->scrollbar.is_dragging) return false; - const uint64_t mappings = OPT(url_mappings)[screen->modes.mouse_tracking_mode != NO_TRACKING]; - if (((mappings >> old_mods) & 1) == ((mappings >> new_mods) & 1)) return false; - detect_url(screen, w->mouse_pos.cell_x, w->mouse_pos.cell_y, new_mods); - set_mouse_cursor(mouse_cursor_shape); - return true; + // 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); } - return false; + if (!w) return false; + Screen *screen = w->render_data.screen; + if (!w->visible || !screen || w->scrollbar.is_hovering || w->scrollbar.is_dragging) return false; + const bool was_highlighted = screen->url_ranges.count > 0; + OSWindow *callback_os_window = global_state.callback_os_window; + global_state.callback_os_window = osw; + // The stored cell is stale when the mouse is in the window padding + unsigned x, y; + bool in_left_half_of_cell, over_cell = cell_for_pos(w, &x, &y, &in_left_half_of_cell, osw); + bool changed = false; + // Compare against what is displayed rather than old_mods as the mouse + // tracking mode, and thus the applicable mappings, may have changed since + if (over_cell ? was_highlighted != url_is_clickable_with(screen, new_mods) : was_highlighted) { + if (over_cell) detect_url(screen, x, y, new_mods); + else { + screen_mark_url(screen, 0, 0, 0, 0); + zero_at_ptr(&screen->current_hyperlink_under_mouse); + set_mouse_cursor_for_screen(screen); + } + changed = was_highlighted != (screen->url_ranges.count > 0); + set_mouse_cursor(mouse_cursor_shape); + } + global_state.callback_os_window = callback_os_window; + return changed; } void @@ -1654,6 +1691,33 @@ set_wheel_mappings(PyObject *py_opts, Options *opts) { return true; } +// Bit i of url_mappings[grabbed] is set when a mouse button opens the hovered URL +// with the modifiers i, computed by finalize_mouse_mappings() in config.py +bool +set_url_mappings(PyObject *py_opts, Options *opts) { + memset(opts->url_mappings, 0, sizeof(opts->url_mappings)); + RAII_PyObject(url_mappings, PyObject_GetAttrString(py_opts, "url_mappings")); + if (!url_mappings) return false; + if (!PyDict_Check(url_mappings)) { + PyErr_SetString(PyExc_TypeError, "url_mappings must be a dict"); + return false; + } + for (size_t grabbed = 0; grabbed < arraysz(opts->url_mappings); grabbed++) { + PyObject *mask = PyDict_GetItemWithError(url_mappings, grabbed ? Py_True : Py_False); + if (!mask) { + if (PyErr_Occurred()) return false; + continue; // an empty dict, as in the unfinalized defaults, means no mappings + } + if (!PyLong_Check(mask)) { + PyErr_SetString(PyExc_TypeError, "url_mappings values must be ints"); + return false; + } + opts->url_mappings[grabbed] = PyLong_AsUnsignedLongLong(mask); + if (PyErr_Occurred()) return false; + } + return true; +} + static bool wheel_is_mapped(int button, bool grabbed, int mods) { return OPT(wheel_mappings)[button - MOUSE_WHEEL_UP][grabbed] & (1ull << mods); @@ -1988,19 +2052,42 @@ send_mock_mouse_event_to_window(PyObject *self UNUSED, PyObject *args) { static PyObject * update_mock_pointer(PyObject *self UNUSED, PyObject *args) { PyObject *capsule; - int old_mods, new_mods, inside = 1; + int old_mods, new_mods, inside = 1, in_padding = 0; unsigned drag = 0, border = 0; const char *pointer_shape = NULL; - if (!PyArg_ParseTuple(args, "O!ii|pIIs", &PyCapsule_Type, &capsule, &old_mods, &new_mods, &inside, &drag, &border, &pointer_shape)) return NULL; + PyObject *before_update = Py_None; + if (!PyArg_ParseTuple( + args, "O!ii|pIIzpO", &PyCapsule_Type, &capsule, &old_mods, &new_mods, &inside, &drag, &border, &pointer_shape, &in_padding, &before_update)) + return NULL; Window *w = PyCapsule_GetPointer(capsule, "Window"); if (!w) return NULL; Screen *screen = w->render_data.screen; - GlobalState saved = global_state; + // Only the global state used by mouse handling is overridden, so that is all that is restored + struct { + OSWindow *callback_os_window; + id_type mouse_hover_in_window, active_drag_in_window, tracked_drag_in_window, active_drag_resize, tab_being_dragged, window_being_dragged; + bool redirect_mouse_handling; + int mods_at_last_key_or_button_event; + } saved = { + global_state.callback_os_window, + global_state.mouse_hover_in_window, + global_state.active_drag_in_window, + global_state.tracked_drag_in_window, + global_state.active_drag_resize, + global_state.tab_being_dragged.id, + global_state.window_being_dragged.id, + global_state.redirect_mouse_handling, + global_state.mods_at_last_key_or_button_event}; MouseShape saved_shape = mouse_cursor_shape; - Window windows[] = {*w, *w}; - windows[1].id++; - windows[0].render_data.geometry = (WindowGeometry){.right = screen->columns * 10, .bottom = screen->lines * 20}; - windows[1].render_data.geometry = windows[0].render_data.geometry; + // The mock window is placed in a tab by value and copied back afterwards, so + // that any heap state the handlers change stays owned by it. The second + // window exists only to give the tab a border and owns nothing. + const WindowGeometry saved_geometry = w->render_data.geometry; + const WindowGeometry geometry = {.right = screen->columns * 10, .bottom = screen->lines * 20}; + Window windows[2] = {*w, {.id = w->id + 1, .visible = true, .render_data = {.screen = screen, .geometry = geometry}}}; + windows[0].render_data.geometry = geometry; + const unsigned saved_padding_right = w->padding.right; + windows[0].padding.right = 10; BorderRect rect = {.border_type = (int)w->id, .horizontal = border == 2}; rect.px.left = (unsigned)w->mouse_pos.global_x; rect.px.right = rect.px.left + 1; @@ -2017,7 +2104,7 @@ update_mock_pointer(PyObject *self UNUSED, PyObject *args) { .num_tabs = 1, .has_too_few_tabs = true, .fonts_data = (FONTS_DATA_HANDLE)&fonts, - .viewport_width = screen->columns * 10, + .viewport_width = screen->columns * 10 + 10, .viewport_height = screen->lines * 20, .mouse_x = w->mouse_pos.global_x, .mouse_y = w->mouse_pos.global_y}; @@ -2027,17 +2114,36 @@ update_mock_pointer(PyObject *self UNUSED, PyObject *args) { global_state.redirect_mouse_handling = false; global_state.mods_at_last_key_or_button_event = old_mods; mouse_event(-1, old_mods, -1); + if (in_padding) { + osw.mouse_x = geometry.right + 5; + mouse_event(-1, old_mods, -1); + } if (!inside) leave_event(old_mods); if (drag == 1) global_state.active_drag_in_window = w->id; else if (drag == 2) global_state.tracked_drag_in_window = w->id; global_state.mods_at_last_key_or_button_event = new_mods; - bool updated = false; - if (pointer_shape) { + bool updated = false, ok = true; + if (before_update != Py_None) { + RAII_PyObject(ret, PyObject_CallNoArgs(before_update)); + ok = ret != NULL; + } + if (ok && pointer_shape) { RAII_PyObject(ret, PyObject_CallMethod((PyObject *)screen, "change_pointer_shape", "ss", "=", pointer_shape)); if (ret) update_mouse_pointer_shape(); - } else updated = update_link_hover_for_modifiers(old_mods, new_mods); + } else if (ok) updated = update_link_hover_for_modifiers(old_mods, new_mods); MouseShape result = mouse_cursor_shape; - global_state = saved; + *w = windows[0]; + w->render_data.geometry = saved_geometry; + w->padding.right = saved_padding_right; + global_state.callback_os_window = saved.callback_os_window; + global_state.mouse_hover_in_window = saved.mouse_hover_in_window; + global_state.active_drag_in_window = saved.active_drag_in_window; + global_state.tracked_drag_in_window = saved.tracked_drag_in_window; + global_state.active_drag_resize = saved.active_drag_resize; + global_state.tab_being_dragged.id = saved.tab_being_dragged; + global_state.window_being_dragged.id = saved.window_being_dragged; + global_state.redirect_mouse_handling = saved.redirect_mouse_handling; + global_state.mods_at_last_key_or_button_event = saved.mods_at_last_key_or_button_event; mouse_cursor_shape = saved_shape; if (PyErr_Occurred()) return NULL; return Py_BuildValue("(iO)", result, updated ? Py_True : Py_False); diff --git a/kitty/screen.c b/kitty/screen.c index 508a07753..425eb9b17 100644 --- a/kitty/screen.c +++ b/kitty/screen.c @@ -4870,7 +4870,7 @@ detect_url_with_regex(Screen *self, index_type x, index_type y, TextCache *text_ // }}} int -screen_detect_url(Screen *screen, unsigned int x, unsigned int y) { +screen_detect_url(Screen *screen, unsigned int x, unsigned int y, bool mark) { bool has_url = false; index_type url_start, url_end = 0; Line *line = screen_visual_line(screen, y); @@ -4883,7 +4883,9 @@ screen_detect_url(Screen *screen, unsigned int x, unsigned int y) { if (line->cpu_cells[x].is_multicell && line->cpu_cells[x].x) x = x > line->cpu_cells[x].x ? x - line->cpu_cells[x].x : 0; hyperlink_id_type hid; if ((hid = line->cpu_cells[x].hyperlink_id)) { - screen_mark_hyperlink(screen, x, y); + // Marking a hyperlink scans the surrounding lines, so skip it when not needed + if (mark) screen_mark_hyperlink(screen, x, y); + else screen_mark_url(screen, 0, 0, 0, 0); return hid; } char_type sentinel = 0; @@ -4911,9 +4913,8 @@ screen_detect_url(Screen *screen, unsigned int x, unsigned int y) { screen_mark_url(screen, url_start, y, url_end, y_extended); } else if (OPT(detect_url_regex).count && detect_url_with_regex(screen, x, y, line->text_cache)) { has_url = true; - } else { - screen_mark_url(screen, 0, 0, 0, 0); } + if (!has_url || !mark) screen_mark_url(screen, 0, 0, 0, 0); return has_url ? -1 : 0; } @@ -5878,7 +5879,13 @@ WRAP0(tab) WRAP0(linefeed) WRAP0(carriage_return) WRAP2(set_margins, 1, 1) -WRAP2(detect_url, 0, 0) +static PyObject * +detect_url(Screen *self, PyObject *args) { + unsigned int x = 0, y = 0; + if (!PyArg_ParseTuple(args, "|II", &x, &y)) return NULL; + screen_detect_url(self, x, y, true); + Py_RETURN_NONE; +} WRAP0(rescale_images) static PyObject * diff --git a/kitty/screen.h b/kitty/screen.h index 03c0fea45..ae39cb111 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -358,7 +358,7 @@ void screen_pop_key_encoding_flags(Screen *self, uint32_t num); uint8_t screen_current_key_encoding_flags(Screen *self); void screen_modify_other_keys(Screen *self, unsigned, unsigned); void screen_report_key_encoding_flags(Screen *self); -int screen_detect_url(Screen *screen, unsigned int x, unsigned int y); +int screen_detect_url(Screen *screen, unsigned int x, unsigned int y, bool mark); int screen_cursor_at_a_shell_prompt(const Screen *); bool screen_prompt_supports_click_events(const Screen *, bool *is_relative); bool screen_fake_move_cursor_to_position(Screen *, index_type x, index_type y); diff --git a/kitty/state.c b/kitty/state.c index 3fd06de0e..2a070f87a 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -1152,13 +1152,7 @@ PYWRAP1(set_options) { global_state.debug_font_fallback = debug_font_fallback ? true : false; if (!convert_opts_from_python_opts(opts, &global_state.opts)) return NULL; if (!set_wheel_mappings(opts, &global_state.opts)) return NULL; - RAII_PyObject(url_mappings, PyObject_GetAttrString(opts, "url_mappings")); - if (!url_mappings) return NULL; - for (size_t i = 0; i < arraysz(global_state.opts.url_mappings); i++) { - PyObject *mask = PyDict_GetItem(url_mappings, i ? Py_True : Py_False); - global_state.opts.url_mappings[i] = mask ? PyLong_AsUnsignedLongLong(mask) : 0; - if (PyErr_Occurred()) return NULL; - } + if (!set_url_mappings(opts, &global_state.opts)) return NULL; if (!set_vertical_tab_bar_padding(opts, &global_state.opts)) return NULL; push_modifier_remap_to_glfw(); Py_XDECREF(global_state.options_object); diff --git a/kitty/state.h b/kitty/state.h index cd5060fa7..fb078f043 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -847,6 +847,7 @@ void update_ime_state_in_os_window(OSWindow *osw, Window *window, int update_foc void set_ignore_os_keyboard_processing(bool enabled); void push_modifier_remap_to_glfw(void); bool set_wheel_mappings(PyObject *py_opts, Options *opts); +bool set_url_mappings(PyObject *py_opts, Options *opts); void update_menu_bar_title(PyObject *title UNUSED); void change_live_resize_state(OSWindow *, bool); bool render_os_window(OSWindow *w, monotonic_t now, bool scan_for_animated_images); diff --git a/kitty_tests/mouse.py b/kitty_tests/mouse.py index 1335b258e..f515d591c 100644 --- a/kitty_tests/mouse.py +++ b/kitty_tests/mouse.py @@ -148,6 +148,41 @@ class TestMouse(BaseTest): self.ae(updated, False) self.ae(update_mock_pointer(w, 0, mods, True, 0, border), (shape, False)) + def test_link_hover_stays_consistent(self): + s = self.create_screen(cols=30) + s.draw('x' * 6 + 'https://example.invalid/') + w = create_mock_window(s) + normal_shape = send_mouse_event(w, x=2) + link_shape = send_mouse_event(w, x=29) + mods = GLFW_MOD_CONTROL | GLFW_MOD_SHIFT + + def configure(*lines): + set_options(load_config(overrides=('clear_all_mouse_actions yes', *lines))) + + def marked(): + return ''.join(s.text_for_marked_url()) + + # The cell under the mouse is stale once it moves into the padding + configure('mouse_map ctrl+shift+left release ungrabbed mouse_handle_click link') + send_mouse_event(w, x=29) + self.ae(update_mock_pointer(w, 0, mods, True, 0, 0, None, True), (normal_shape, False)) + self.ae(marked(), '') + send_mouse_event(w, x=29) + self.ae(update_mock_pointer(w, mods, 0, True, 0, 0, None, True), (normal_shape, True)) + self.ae(marked(), '') + + # The mouse tracking mode can change without the mouse moving + configure('mouse_map left click ungrabbed mouse_click_url') + send_mouse_event(w, x=10) + self.ae(update_mock_pointer(w, 0, GLFW_MOD_ALT, True, 0, 0, None, False, partial(parse_bytes, s, b'\x1b[?1000h'))[1], True) + self.ae(marked(), '') + parse_bytes(s, b'\x1b[?1000l') + + # Pointer shape changes from the program keep the hand over a highlighted link + send_mouse_event(w, x=10) + self.ae(update_mock_pointer(w, 0, 0, True, 0, 0, 'wait')[0], link_shape) + self.ae(marked(), 'https://example.invalid/') + def test_pointer_shape_during_mouse_drag(self): s = self.create_screen(cols=60) parse_bytes(s, b'\x1b[?1000h')