From ed119ae246d26055a0be8f2cbd0e7c8c5fd1c1af Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 10 Aug 2024 08:12:59 -0400 Subject: [PATCH] gtk: Port selection drawing to new apis Preserve color state information when drawing selections or block cursors in GtkTextView. --- gtk/gtktextlayout.c | 56 ++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/gtk/gtktextlayout.c b/gtk/gtktextlayout.c index 177584f42b..154579f418 100644 --- a/gtk/gtktextlayout.c +++ b/gtk/gtktextlayout.c @@ -3867,7 +3867,7 @@ render_para (GskPangoRenderer *crenderer, GtkTextLineDisplay *line_display, int selection_start_index, int selection_end_index, - const GdkRGBA *selection, + const GdkColor *selection_color, gboolean draw_selection_text, float cursor_alpha) { @@ -3921,11 +3921,11 @@ render_para (GskPangoRenderer *crenderer, */ if (selection_start_index < byte_offset && selection_end_index > pango_layout_line_get_length (line) + byte_offset && - selection->alpha >= 1) + gdk_color_is_opaque (selection_color)) { - gtk_snapshot_append_color (crenderer->snapshot, - selection, - &GRAPHENE_RECT_INIT (line_display->left_margin, + gtk_snapshot_append_color2 (crenderer->snapshot, + selection_color, + &GRAPHENE_RECT_INIT (line_display->left_margin, selection_y, screen_width, selection_height)); @@ -3990,7 +3990,7 @@ render_para (GskPangoRenderer *crenderer, PANGO_PIXELS (line_rect.width) - bounds.origin.x); - gtk_snapshot_append_color (crenderer->snapshot, selection, &bounds); + gtk_snapshot_append_color2 (crenderer->snapshot, selection_color, &bounds); if (draw_selection_text) { @@ -4009,12 +4009,12 @@ render_para (GskPangoRenderer *crenderer, if (line_rect.x > line_display->left_margin * PANGO_SCALE && ((line_display->direction == GTK_TEXT_DIR_LTR && selection_start_index < byte_offset) || (line_display->direction == GTK_TEXT_DIR_RTL && selection_end_index > byte_offset + pango_layout_line_get_length (line)))) - gtk_snapshot_append_color (crenderer->snapshot, - selection, - &GRAPHENE_RECT_INIT (line_display->left_margin, - selection_y, - PANGO_PIXELS (line_rect.x) - line_display->left_margin, - selection_height)); + gtk_snapshot_append_color2 (crenderer->snapshot, + selection_color, + &GRAPHENE_RECT_INIT (line_display->left_margin, + selection_y, + PANGO_PIXELS (line_rect.x) - line_display->left_margin, + selection_height)); if (line_rect.x + line_rect.width < (screen_width + line_display->left_margin) * PANGO_SCALE && @@ -4025,9 +4025,9 @@ render_para (GskPangoRenderer *crenderer, + screen_width - PANGO_PIXELS (line_rect.x) - PANGO_PIXELS (line_rect.width); - gtk_snapshot_append_color (crenderer->snapshot, - selection, - &GRAPHENE_RECT_INIT (PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width), + gtk_snapshot_append_color2 (crenderer->snapshot, + selection_color, + &GRAPHENE_RECT_INIT (PANGO_PIXELS (line_rect.x) + PANGO_PIXELS (line_rect.width), selection_y, nonlayout_width, selection_height)); @@ -4042,7 +4042,7 @@ render_para (GskPangoRenderer *crenderer, { GtkCssNode *node; GtkCssStyle *style; - const GdkRGBA *cursor_color; + GdkColor cursor_color; graphene_rect_t bounds = { .origin.x = line_display->x_offset + line_display->block_cursor.x, .origin.y = line_display->block_cursor.y + line_display->top_margin, @@ -4056,10 +4056,11 @@ render_para (GskPangoRenderer *crenderer, node = gtk_widget_get_css_node (crenderer->widget); style = gtk_css_node_get_style (node); - cursor_color = gtk_css_color_value_get_rgba (style->used->caret_color); + gtk_css_color_to_color (gtk_css_color_value_get_color (style->used->caret_color), + &cursor_color); gtk_snapshot_push_opacity (crenderer->snapshot, cursor_alpha); - gtk_snapshot_append_color (crenderer->snapshot, cursor_color, &bounds); + gtk_snapshot_append_color2 (crenderer->snapshot, &cursor_color, &bounds); /* draw text under the cursor if any */ if (!line_display->cursor_at_line_end) @@ -4114,8 +4115,7 @@ gtk_text_layout_snapshot (GtkTextLayout *layout, int selection_end_line; gboolean have_selection; gboolean draw_selection_text; - const GdkRGBA *selection; - const GdkRGBA *color; + GdkColor selection_color; GtkSnapshot *cursor_snapshot; GtkTextBTree *btree; GtkTextLine *first_line; @@ -4146,8 +4146,6 @@ gtk_text_layout_snapshot (GtkTextLayout *layout, node = gtk_widget_get_css_node (widget); style = gtk_css_node_get_style (node); - color = gtk_css_color_value_get_rgba (style->used->color); - gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (0, offset_y)); offset_y = 0; @@ -4159,7 +4157,8 @@ gtk_text_layout_snapshot (GtkTextLayout *layout, crenderer->widget = widget; crenderer->snapshot = snapshot; - gdk_color_init_from_rgba (&crenderer->fg_color, color); + gtk_css_color_to_color (gtk_css_color_value_get_color (style->used->color), + &crenderer->fg_color); have_selection = gtk_text_buffer_get_selection_bounds (layout->buffer, &selection_start, @@ -4167,7 +4166,6 @@ gtk_text_layout_snapshot (GtkTextLayout *layout, if (have_selection) { GtkCssNode *selection_node; - const GdkRGBA *text_color; selection_start_line = gtk_text_iter_get_line (&selection_start); selection_end_line = gtk_text_iter_get_line (&selection_end); @@ -4175,16 +4173,16 @@ gtk_text_layout_snapshot (GtkTextLayout *layout, selection_node = gtk_text_view_get_selection_node ((GtkTextView*)widget); style = gtk_css_node_get_style (selection_node); - selection = gtk_css_color_value_get_rgba (style->used->background_color); - text_color = gtk_css_color_value_get_rgba (style->used->color); + gtk_css_color_to_color (gtk_css_color_value_get_color (style->used->background_color), + &selection_color); - draw_selection_text = text_color->alpha > 0; + draw_selection_text = !gdk_color_is_clear (&crenderer->fg_color); } else { selection_start_line = -1; selection_end_line = -1; - selection = NULL; + gdk_color_init (&selection_color, GDK_COLOR_STATE_SRGB, (float[]) { 0, 0, 0, 0 }); draw_selection_text = FALSE; } @@ -4260,7 +4258,7 @@ gtk_text_layout_snapshot (GtkTextLayout *layout, gtk_snapshot_push_collect (snapshot); render_para (crenderer, line_display, selection_start_index, selection_end_index, - selection, + &selection_color, draw_selection_text, cursor_alpha); line_display->node = gtk_snapshot_pop_collect (snapshot);