hyperlink_target_position: Add a tooltip value

Displays the URL in a box sized to fit it, just below the hovered cell,
like a web browser tooltip. Split render_a_bar() into rendering the text
and drawing it so the width of the rendered text can be used to position
the box.

Also redraw when the hovered cell within a hyperlink changes, as the
position of the displayed target now depends on it.
This commit is contained in:
Kovid Goyal 2026-10-29 19:24:32 +05:30
parent 94938d53ee
commit b62e21311d
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
9 changed files with 112 additions and 40 deletions

View file

@ -232,7 +232,8 @@ Detailed list of changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- A new option :opt:`hyperlink_target_position` to control where the URL shown
by :opt:`show_hyperlink_targets` is displayed (:iss:`10770`)
by :opt:`show_hyperlink_targets` is displayed, including as a browser style
tooltip next to the mouse cursor (:iss:`10770`)
- macOS: Commit in-progress IME or dictation text instead of discarding it when
the OS window loses focus, matching native macOS applications

View file

@ -442,6 +442,7 @@ static void
detect_url(Screen *screen, unsigned int x, unsigned int y, int modifiers) {
const bool clickable = url_is_clickable_with(screen, modifiers);
int hid = screen_detect_url(screen, x, y, clickable);
const __typeof__(screen->current_hyperlink_under_mouse) previous = screen->current_hyperlink_under_mouse;
zero_at_ptr(&screen->current_hyperlink_under_mouse);
set_mouse_cursor_for_screen(screen);
if (hid != 0) {
@ -453,6 +454,12 @@ detect_url(Screen *screen, unsigned int x, unsigned int y, int modifiers) {
screen->current_hyperlink_under_mouse.y = y;
}
}
// The position of the displayed hyperlink target depends on the hovered cell
if (OPT(show_hyperlink_targets) != SHOW_HYPERLINK_TARGETS_NEVER && global_state.callback_os_window &&
(previous.id || screen->current_hyperlink_under_mouse.id) &&
(previous.id != screen->current_hyperlink_under_mouse.id || previous.x != screen->current_hyperlink_under_mouse.x ||
previous.y != screen->current_hyperlink_under_mouse.y))
global_state.callback_os_window->needs_render = true;
}
static bool

View file

@ -1027,13 +1027,15 @@ are pressed, the URL is shown as long as the configured modifier is among them.
opt(
'hyperlink_target_position',
'top',
choices=('top', 'bottom', 'below'),
choices=('top', 'bottom', 'below', 'tooltip'),
ctype='hyperlink_target_position',
long_text="""
Where to display the URL shown by :opt:`show_hyperlink_targets`. :code:`top`
and :code:`bottom` display it along that edge of the window, :code:`below`
displays it immediately below the hovered line. In all cases, if the URL would
cover the hovered line, it is displayed on the opposite side instead.
displays it immediately below the hovered line. :code:`tooltip` displays it
in a small box, sized to fit the URL, just below the mouse cursor, like a web
browser tooltip. In all cases, if the URL would cover the hovered line, it is
displayed on the opposite side instead.
""",
)

View file

@ -1058,7 +1058,7 @@ class Parser:
raise ValueError(f"The value {val} is not a valid choice for hyperlink_target_position")
ans["hyperlink_target_position"] = val
choices_for_hyperlink_target_position = frozenset(('top', 'bottom', 'below'))
choices_for_hyperlink_target_position = frozenset(('top', 'bottom', 'below', 'tooltip'))
def inactive_border_color(self, val: str, ans: dict[str, typing.Any]) -> None:
ans['inactive_border_color'] = to_color(val)

View file

@ -147,7 +147,9 @@ static inline HyperlinkTargetPosition
hyperlink_target_position(PyObject *x) {
const char *in = PyUnicode_AsUTF8(x);
if (!in) return HYPERLINK_TARGET_TOP;
if (in[0] == 'b') return in[1] == 'e' ? HYPERLINK_TARGET_BELOW : HYPERLINK_TARGET_BOTTOM;
if (strcmp(in, "bottom") == 0) return HYPERLINK_TARGET_BOTTOM;
if (strcmp(in, "below") == 0) return HYPERLINK_TARGET_BELOW;
if (strcmp(in, "tooltip") == 0) return HYPERLINK_TARGET_TOOLTIP;
return HYPERLINK_TARGET_TOP;
}

View file

@ -23,7 +23,7 @@ choices_for_allow_remote_control = typing.Literal['password', 'socket-only', 'so
choices_for_background_image_layout = typing.Literal['mirror-tiled', 'scaled', 'tiled', 'clamped', 'centered', 'cscaled']
choices_for_default_pointer_shape = typing.Literal['arrow', 'beam', 'text', 'pointer', 'hand', 'help', 'wait', 'progress', 'crosshair', 'cell', 'vertical-text', 'move', 'e-resize', 'ne-resize', 'nw-resize', 'n-resize', 'se-resize', 'sw-resize', 's-resize', 'w-resize', 'ew-resize', 'ns-resize', 'nesw-resize', 'nwse-resize', 'zoom-in', 'zoom-out', 'alias', 'copy', 'not-allowed', 'no-drop', 'grab', 'grabbing']
choices_for_focus_follows_mouse = typing.Literal['no', 'n', 'false', 'y', 'yes', 'true', 'drop']
choices_for_hyperlink_target_position = typing.Literal['top', 'bottom', 'below']
choices_for_hyperlink_target_position = typing.Literal['top', 'bottom', 'below', 'tooltip']
choices_for_linux_display_server = typing.Literal['auto', 'wayland', 'x11']
choices_for_macos_colorspace = typing.Literal['srgb', 'default', 'displayp3']
choices_for_macos_show_window_title_in = typing.Literal['all', 'menubar', 'none', 'window']

View file

@ -1641,26 +1641,8 @@ window_bar_rect(const UIRenderData *ui) {
return (Viewport){.left = ui->screen_left, .top = ui->screen_top, .width = ui->screen_width, .height = ui->cell_height + 2 + 2 * bar_border_width(ui)};
}
static unsigned
render_a_bar(const UIRenderData *ui, WindowBarData *bar, PyObject *title, Viewport border_rect) {
const double thickness = thickness_as_float(ui->os_window, 1);
const unsigned border_width = bar_border_width(ui);
// The rounded rect shader anti-aliases each edge over a band extending
// one pixel on either side of it, so keep the drawn line at least half a
// pixel inside the border_width pixels reserved for it, otherwise it would
// bleed onto the rendered title.
const float drawn_thickness = (float)MIN(thickness, border_width - 0.5);
if (border_rect.width <= 2 * border_width || border_rect.height <= 2 * border_width) return 0;
unsigned bar_height = border_rect.height - 2 * border_width;
unsigned bar_width = border_rect.width - 2 * border_width;
if (!bar->buf || bar->width != bar_width || bar->height != bar_height) {
free(bar->buf);
bar->buf = malloc((size_t)4 * bar_width * bar_height);
if (!bar->buf) return 0;
bar->height = bar_height;
bar->width = bar_width;
bar->needs_render = true;
}
static void
bar_colors(const UIRenderData *ui, color_type *fg, color_type *bg) {
#define RGBCOL(which, fallback) \
(0xff000000 | colorprofile_to_color_with_fallback( \
ui->screen->color_profile, \
@ -1668,18 +1650,61 @@ render_a_bar(const UIRenderData *ui, WindowBarData *bar, PyObject *title, Viewpo
ui->screen->color_profile->configured.which, \
ui->screen->color_profile->overridden.fallback, \
ui->screen->color_profile->configured.fallback))
color_type fg = RGBCOL(default_fg, default_fg), bg = RGBCOL(default_bg, default_bg);
*fg = RGBCOL(default_fg, default_fg);
*bg = RGBCOL(default_bg, default_bg);
#undef RGBCOL
}
// Render title into bar->buf, if needed. The rendered text is max_width x
// height pixels, unless fit_to_text is true, in which case its width is reduced
// to the width of the text. The actual width is stored in bar->width.
static bool
render_bar_text(
const UIRenderData *ui, WindowBarData *bar, PyObject *title, unsigned max_width, unsigned height, bool fit_to_text, color_type fg, color_type bg) {
if (!title) return false;
if (!bar->buf || bar->requested_width != max_width || bar->height != height || bar->fit_to_text != fit_to_text) {
free(bar->buf);
bar->buf = malloc((size_t)4 * max_width * height);
if (!bar->buf) return false;
bar->requested_width = max_width;
bar->width = max_width;
bar->height = height;
bar->fit_to_text = fit_to_text;
bar->needs_render = true;
}
if (bar->last_drawn_title_object_id != title || bar->needs_render) {
static char titlebuf[2048] = {0};
if (!title) return 0;
snprintf(titlebuf, arraysz(titlebuf), " %s", PyUnicode_AsUTF8(title));
snprintf(titlebuf, arraysz(titlebuf), fit_to_text ? " %s " : " %s", PyUnicode_AsUTF8(title));
size_t actual_width = max_width;
if (!draw_window_title(
ui->os_window->fonts_data->font_sz_in_pts, ui->os_window->fonts_data->logical_dpi_y, titlebuf, fg, bg, bar->buf, bar_width, bar_height, NULL))
return 0;
ui->os_window->fonts_data->font_sz_in_pts,
ui->os_window->fonts_data->logical_dpi_y,
titlebuf,
fg,
bg,
bar->buf,
max_width,
height,
fit_to_text ? &actual_width : NULL))
return false;
bar->width = MAX(1u, (unsigned)actual_width);
Py_CLEAR(bar->last_drawn_title_object_id);
bar->last_drawn_title_object_id = Py_NewRef(title);
}
return true;
}
// Draw the text previously rendered by render_bar_text() surrounded by a
// border. border_rect must be the size of the rendered text plus the borders.
static void
draw_bar(const UIRenderData *ui, const WindowBarData *bar, Viewport border_rect, color_type fg, color_type bg) {
const double thickness = thickness_as_float(ui->os_window, 1);
const unsigned border_width = bar_border_width(ui);
// The rounded rect shader anti-aliases each edge over a band extending
// one pixel on either side of it, so keep the drawn line at least half a
// pixel inside the border_width pixels reserved for it, otherwise it would
// bleed onto the rendered title.
const float drawn_thickness = (float)MIN(thickness, border_width - 0.5);
static ImageRenderData data = {.group_count = 1};
gpu_data_for_image(&data, -1, 1, 1, -1);
glGenTextures(1, &data.texture_id);
@ -1689,7 +1714,7 @@ render_a_bar(const UIRenderData *ui, WindowBarData *bar, PyObject *title, Viewpo
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, bar_width, bar_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bar->buf);
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB_ALPHA, bar->width, bar->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bar->buf);
bind_program(GRAPHICS_PROGRAM);
const unsigned sh = ui->full_framebuffer_height;
// first blank the area to be drawn to background
@ -1697,12 +1722,22 @@ render_a_bar(const UIRenderData *ui, WindowBarData *bar, PyObject *title, Viewpo
blank_canvas(ui->bg_alpha, bg, false);
disable_scissor();
// then draw the rendered text
save_viewport_using_top_left_origin(border_rect.left + border_width, border_rect.top + border_width, bar_width, bar_height, sh);
save_viewport_using_top_left_origin(border_rect.left + border_width, border_rect.top + border_width, bar->width, bar->height, sh);
draw_graphics(GRAPHICS_PROGRAM, &data, 0, 1, 1.f);
restore_viewport();
free_texture(&data.texture_id);
// finally draw border with transparent bg
draw_rounded_rect(border_rect, sh, drawn_thickness, ui->cell_width, fg, bg, 0.f, 0, 0.f);
}
static unsigned
render_a_bar(const UIRenderData *ui, WindowBarData *bar, PyObject *title, Viewport border_rect) {
const unsigned border_width = bar_border_width(ui);
if (border_rect.width <= 2 * border_width || border_rect.height <= 2 * border_width) return 0;
color_type fg, bg;
bar_colors(ui, &fg, &bg);
if (!render_bar_text(ui, bar, title, border_rect.width - 2 * border_width, border_rect.height - 2 * border_width, false, fg, bg)) return 0;
draw_bar(ui, bar, border_rect, fg, bg);
return border_rect.height;
}
@ -1735,11 +1770,29 @@ hyperlink_target_bar_top(const UIRenderData *ui, unsigned bar_height) {
switch (OPT(hyperlink_target_position)) {
case HYPERLINK_TARGET_TOP: top = row_top < bar_height ? max_top : 0; break;
case HYPERLINK_TARGET_BOTTOM: top = row_bottom > max_top ? 0 : max_top; break;
case HYPERLINK_TARGET_BELOW: top = row_bottom > max_top ? row_top - bar_height : row_bottom; break;
case HYPERLINK_TARGET_BELOW:
case HYPERLINK_TARGET_TOOLTIP: top = row_bottom > max_top ? row_top - bar_height : row_bottom; break;
}
return (unsigned)MAX(0., MIN(top, max_top));
}
// Draw the URL in a box sized to fit it, starting at the hovered cell, moved
// left as needed to fit in the window. rect is the full width bar it replaces.
static void
draw_hyperlink_target_tooltip(const UIRenderData *ui, Viewport rect, PyObject *url) {
const unsigned border_width = bar_border_width(ui);
if (rect.width <= 2 * border_width || rect.height <= 2 * border_width) return;
WindowBarData *bar = &ui->window->title_bar_data;
color_type fg, bg;
bar_colors(ui, &fg, &bg);
if (!render_bar_text(ui, bar, url, rect.width - 2 * border_width, rect.height - 2 * border_width, true, fg, bg)) return;
const unsigned width = bar->width + 2 * border_width;
const unsigned left = ui->screen->current_hyperlink_under_mouse.x * ui->cell_width;
rect.left += MIN(left, rect.width - width);
rect.width = width;
draw_bar(ui, bar, rect, fg, bg);
}
static void
draw_hyperlink_target(const UIRenderData *ui) {
if (!has_hyperlink_target(ui->os_window, ui->window, ui->screen)) return;
@ -1762,9 +1815,10 @@ draw_hyperlink_target(const UIRenderData *ui) {
bd->needs_render = true;
}
if (bd->last_drawn_title_object_id == NULL) return;
PyObject *ref = Py_NewRef(bd->last_drawn_title_object_id); // render_a_bar clears bd->last_drawn_title_object_id
PyObject *ref = Py_NewRef(bd->last_drawn_title_object_id); // render_bar_text() clears bd->last_drawn_title_object_id
rect.top += hyperlink_target_bar_top(ui, rect.height);
render_a_bar(ui, &window->title_bar_data, bd->last_drawn_title_object_id, rect);
if (OPT(hyperlink_target_position) == HYPERLINK_TARGET_TOOLTIP) draw_hyperlink_target_tooltip(ui, rect, bd->last_drawn_title_object_id);
else render_a_bar(ui, &window->title_bar_data, bd->last_drawn_title_object_id, rect);
Py_DECREF(ref);
}

View file

@ -44,7 +44,12 @@ typedef enum ShowHyperlinkTargets {
SHOW_HYPERLINK_TARGETS_SUPER = 8,
SHOW_HYPERLINK_TARGETS_ALT = 16
} ShowHyperlinkTargets;
typedef enum HyperlinkTargetPosition { HYPERLINK_TARGET_TOP = 0, HYPERLINK_TARGET_BOTTOM = 1, HYPERLINK_TARGET_BELOW = 2 } HyperlinkTargetPosition;
typedef enum HyperlinkTargetPosition {
HYPERLINK_TARGET_TOP = 0,
HYPERLINK_TARGET_BOTTOM = 1,
HYPERLINK_TARGET_BELOW = 2,
HYPERLINK_TARGET_TOOLTIP = 3
} HyperlinkTargetPosition;
struct MenuItem {
const char **location;
@ -258,7 +263,8 @@ typedef struct PendingClick {
typedef struct WindowBarData {
unsigned width, height;
unsigned width, height, requested_width;
bool fit_to_text;
uint8_t *buf;
PyObject *last_drawn_title_object_id;
hyperlink_id_type hyperlink_id_for_title_object;

View file

@ -385,7 +385,7 @@ def conf_parsing(self):
radius_opts = p('window_border_radius 7px')
self.ae(radius_opts.window_border_radius, (7.0, 'px'))
self.ae(defaults.hyperlink_target_position, 'top')
for pos in ('top', 'bottom', 'below'):
for pos in ('top', 'bottom', 'below', 'tooltip'):
self.ae(p(f'hyperlink_target_position {pos}').hyperlink_target_position, pos)
self.ae(p('hyperlink_target_position xxx', bad_line_num=1).hyperlink_target_position, 'top')
radius_opts = p('window_border_radius -2')