Change foreground color on TextTag hover

I can do that in event listener loop by catch the x/y condition, but maybe some less expensive solution available to change (link) color on TextTag mouse hover?

For example css class, or anything else where I can define rgba or hex color logic once.

Unfortunately, you didn’t specify a script that you could get a better picture of your problem.

There are quite a lot of examples for using CSS, for example:

With some search, you should find a solution for your problem here.

But you can also concretize your question and we can look for a specific solution together.

Greetings

1 Like

Thanks for link, is any example how to assign CSS class to TextTag in GTK4?

About code example, I have only this horrible draft in Rust that takes CPU even work…

motion_controller.connect_motion({
    let text_view = widget.text_view.clone();
    let links = links.clone();
    let hover = hover.clone();
    move |_, window_x, window_y| {
        // Detect tag match current coords hovered
        let (buffer_x, buffer_y) = text_view.window_to_buffer_coords(
            TextWindowType::Widget,
            window_x as i32,
            window_y as i32,
        );

        // Reset link colors to default
        if let Some(tag) = hover.replace(None) {
            tag.set_foreground_rgba(Some(&link_color.0));
        }

        // Apply hover effect
        if let Some(iter) = text_view.iter_at_location(buffer_x, buffer_y) {
            for tag in iter.tags() {
                // Tag is link
                if let Some(uri) = links.get(&tag) {
                    // Toggle color
                    tag.set_foreground_rgba(Some(&link_color.1));

                    // Keep hovered tag in memory
                    hover.replace(Some(tag.clone()));

                    // Toggle cursor
                    text_view.set_cursor_from_name(Some("pointer"));

                    // Redraw required to apply changes immediately
                    text_view.queue_draw();

                    return;
                }
            }
        }

        // Restore defaults
        text_view.set_cursor_from_name(Some("text"));
        text_view.set_tooltip_text(None);
        text_view.queue_draw();
    }
});

There is no way to use CSS to style a TextTag: tags are not widgets, so they cannot use CSS.

1 Like

Thanks for short answer - I should keep it in mind!

Seems of these reason, also can’t assign any motion controller but use entire parent widget x/y…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.