[gtksourceview] view: ignore current line during selections



commit 35c85c1035b68278052315ba8af1f101d27e0b3c
Author: Christian Hergert <chergert redhat com>
Date:   Tue Dec 28 21:46:55 2021 -0800

    view: ignore current line during selections
    
    If we have highlight-current-line set, but there is a selection, avoid
    having both the line hightlight and the selection which is distracting.
    The selection boundary itself is descriptive of positioning during an
    interactive selection and it also avoids some double compositing with
    weird color combinations.
    
    If you really want the old behavior, we can discuss options to make that
    possible, but I definitely don't think it should be the default here.

 gtksourceview/gtksourcegutter.c             | 31 +++++++++++++++--------------
 gtksourceview/gtksourcegutterrenderertext.c |  8 ++++++--
 gtksourceview/gtksourceview.c               | 12 +++++++----
 3 files changed, 30 insertions(+), 21 deletions(-)
---
diff --git a/gtksourceview/gtksourcegutter.c b/gtksourceview/gtksourcegutter.c
index f124309a..e6083a95 100644
--- a/gtksourceview/gtksourcegutter.c
+++ b/gtksourceview/gtksourcegutter.c
@@ -698,10 +698,11 @@ gtk_source_gutter_snapshot (GtkWidget   *widget,
 {
        GtkSourceGutter *gutter = GTK_SOURCE_GUTTER (widget);
        GtkTextView *text_view = GTK_TEXT_VIEW (gutter->view);
+       GtkTextBuffer *buffer;
        const GList *list;
        GdkRectangle visible_rect;
-       GtkTextIter begin;
-       GtkTextIter end;
+       GtkTextIter begin, end;
+       GtkTextIter cur, sel;
        gboolean needs_wrap_first = FALSE;
        gboolean needs_wrap_last = FALSE;
 
@@ -712,6 +713,8 @@ gtk_source_gutter_snapshot (GtkWidget   *widget,
                return;
        }
 
+       buffer = gtk_text_view_get_buffer (text_view);
+
        gtk_text_view_get_visible_rect (text_view, &visible_rect);
        gtk_text_view_get_iter_at_location (text_view, &begin,
                                            visible_rect.x, visible_rect.y);
@@ -732,18 +735,21 @@ gtk_source_gutter_snapshot (GtkWidget   *widget,
                                                      needs_wrap_last);
 
        /* Draw the current-line highlight if necessary */
-       if (gtk_source_view_get_highlight_current_line (gutter->view))
+       if (gtk_source_view_get_highlight_current_line (gutter->view) &&
+           !gtk_text_buffer_get_selection_bounds (buffer, &cur, &sel))
        {
+               GdkRGBA highlight;
                guint cursor_line;
 
                cursor_line = _gtk_source_gutter_lines_get_cursor_line (gutter->lines);
 
                if (cursor_line >= gtk_source_gutter_lines_get_first (gutter->lines) &&
-                   cursor_line <= gtk_source_gutter_lines_get_last (gutter->lines))
+                   cursor_line <= gtk_source_gutter_lines_get_last (gutter->lines) &&
+                   _gtk_source_view_get_current_line_number_background (gutter->view, &highlight))
                {
-                       GdkRGBA highlight;
-                       gint y;
-                       gint height;
+                       int width = gtk_widget_get_width (widget);
+                       int height;
+                       int y;
 
                        gtk_source_gutter_lines_get_line_yrange (gutter->lines,
                                                                 cursor_line,
@@ -751,14 +757,9 @@ gtk_source_gutter_snapshot (GtkWidget   *widget,
                                                                 &y,
                                                                 &height);
 
-                       if (_gtk_source_view_get_current_line_number_background (gutter->view, &highlight))
-                       {
-                               int width = gtk_widget_get_width (widget);
-
-                               gtk_snapshot_append_color (snapshot,
-                                                          &highlight,
-                                                          &GRAPHENE_RECT_INIT (0, y, width, height));
-                       }
+                       gtk_snapshot_append_color (snapshot,
+                                                  &highlight,
+                                                  &GRAPHENE_RECT_INIT (0, y, width, height));
                }
        }
 
diff --git a/gtksourceview/gtksourcegutterrenderertext.c b/gtksourceview/gtksourcegutterrenderertext.c
index 9fa87857..a26a1052 100644
--- a/gtksourceview/gtksourcegutterrenderertext.c
+++ b/gtksourceview/gtksourcegutterrenderertext.c
@@ -25,7 +25,7 @@
 
 /**
  * GtkSourceGutterRendererText:
- * 
+ *
  * Renders text in the gutter.
  *
  * A `GtkSourceGutterRendererText` can be used to render text in a cell of
@@ -47,6 +47,7 @@ typedef struct
        gsize           text_len;
        Size            cached_sizes[5];
        guint           is_markup : 1;
+       guint           has_selection : 1;
 } GtkSourceGutterRendererTextPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceGutterRendererText, gtk_source_gutter_renderer_text, 
GTK_SOURCE_TYPE_GUTTER_RENDERER)
@@ -106,10 +107,13 @@ gtk_source_gutter_renderer_text_begin (GtkSourceGutterRenderer *renderer,
        GtkSourceGutterRendererText *text = GTK_SOURCE_GUTTER_RENDERER_TEXT (renderer);
        GtkSourceGutterRendererTextPrivate *priv = gtk_source_gutter_renderer_text_get_instance_private 
(text);
        GtkSourceView *view = gtk_source_gutter_renderer_get_view (GTK_SOURCE_GUTTER_RENDERER (renderer));
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
        GdkRGBA current;
 
        GTK_SOURCE_GUTTER_RENDERER_CLASS (gtk_source_gutter_renderer_text_parent_class)->begin (renderer, 
lines);
 
+       priv->has_selection = gtk_text_buffer_get_has_selection (buffer);
+
        g_clear_object (&priv->cached_layout);
        priv->cached_layout = gtk_widget_create_pango_layout (GTK_WIDGET (renderer), NULL);
 
@@ -164,7 +168,7 @@ gtk_source_gutter_renderer_text_snapshot_line (GtkSourceGutterRenderer *renderer
                                       priv->text_len);
        }
 
-       if (G_UNLIKELY (gtk_source_gutter_lines_is_cursor (lines, line)))
+       if (G_UNLIKELY (!priv->has_selection && gtk_source_gutter_lines_is_cursor (lines, line)))
        {
                PangoAttrList *attrs = pango_layout_get_attributes (layout);
 
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index 62cb9654..3141f20e 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -2654,14 +2654,18 @@ gtk_source_view_paint_current_line_highlight (GtkSourceView *view,
 {
        GtkSourceViewPrivate *priv = gtk_source_view_get_instance_private (view);
        GtkTextBuffer *buffer;
-       GtkTextIter cur;
+       GtkTextIter cur, sel;
        gint y;
        gint height;
 
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
-       gtk_text_buffer_get_iter_at_mark (buffer,
-                                         &cur,
-                                         gtk_text_buffer_get_insert (buffer));
+
+       /* Don't paint line if the selection cross multiple lines */
+       if (gtk_text_buffer_get_selection_bounds (buffer, &cur, &sel))
+       {
+               return;
+       }
+
        gtk_text_view_get_line_yrange (GTK_TEXT_VIEW (view), &cur, &y, &height);
 
        gtk_source_view_paint_line_background (view,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]