[gtk/wip/baedert/for-master] label: Refactor snapshot()



commit 9eaeaa8452b578ddceb4f464b507f1ab1a265246
Author: Timm Bäder <mail baedert org>
Date:   Mon Dec 21 12:26:22 2020 +0100

    label: Refactor snapshot()
    
    Use two early-out style if statements to reduce indentation in this
    entire function.

 gtk/gtklabel.c | 153 +++++++++++++++++++++++++++------------------------------
 1 file changed, 73 insertions(+), 80 deletions(-)
---
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index 9994d1a8fe..b27995df58 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -3440,42 +3440,84 @@ gtk_label_snapshot (GtkWidget   *widget,
   GtkLabelSelectionInfo *info;
   GtkStyleContext *context;
   int lx, ly;
-  int width, height, x;
+  int width, height;
 
-  info = self->select_info;
+  if (!self->text || (*self->text == '\0'))
+    return;
 
   gtk_label_ensure_layout (self);
 
   context = _gtk_widget_get_style_context (widget);
+  get_layout_location (self, &lx, &ly);
+
+  gtk_snapshot_render_layout (snapshot, context, lx, ly, self->layout);
+
+  info = self->select_info;
+  if (!info)
+    return;
 
   width = gtk_widget_get_width (widget);
   height = gtk_widget_get_height (widget);
-  x = 0;
 
-  if (self->text && (*self->text != '\0'))
+  if (info->selection_anchor != info->selection_end)
     {
-      get_layout_location (self, &lx, &ly);
+      int range[2];
+      cairo_region_t *range_clip;
+      cairo_rectangle_int_t clip_rect;
+      int i;
 
-      gtk_snapshot_render_layout (snapshot, context, lx, ly, self->layout);
+      range[0] = MIN (info->selection_anchor, info->selection_end);
+      range[1] = MAX (info->selection_anchor, info->selection_end);
 
-      if (info && (info->selection_anchor != info->selection_end))
+      gtk_style_context_save_to_node (context, info->selection_node);
+
+      range_clip = gdk_pango_layout_get_clip_region (self->layout, lx, ly, range, 1);
+      for (i = 0; i < cairo_region_num_rectangles (range_clip); i++)
         {
-          int range[2];
-          cairo_region_t *range_clip;
-          cairo_rectangle_int_t clip_rect;
-          int i;
+          cairo_region_get_rectangle (range_clip, i, &clip_rect);
+
+          gtk_snapshot_push_clip (snapshot, &GRAPHENE_RECT_FROM_RECT (&clip_rect));
+          gtk_snapshot_render_background (snapshot, context, 0, 0, width, height);
+          gtk_snapshot_render_layout (snapshot, context, lx, ly, self->layout);
+          gtk_snapshot_pop (snapshot);
+        }
 
-          range[0] = info->selection_anchor;
-          range[1] = info->selection_end;
+      cairo_region_destroy (range_clip);
 
-          if (range[0] > range[1])
-            {
-              int tmp = range[0];
-              range[0] = range[1];
-              range[1] = tmp;
-            }
+      gtk_style_context_restore (context);
+    }
+  else
+    {
+      GtkLabelLink *focus_link;
+      GtkLabelLink *active_link;
+      int range[2];
+      cairo_region_t *range_clip;
+      cairo_rectangle_int_t clip_rect;
+      int i;
+      GdkRectangle rect;
 
-          gtk_style_context_save_to_node (context, info->selection_node);
+      if (info->selectable &&
+          gtk_widget_has_focus (widget) &&
+          gtk_widget_is_drawable (widget))
+        {
+          PangoDirection cursor_direction;
+
+          cursor_direction = get_cursor_direction (self);
+          gtk_snapshot_render_insertion_cursor (snapshot, context,
+                                                lx, ly,
+                                                self->layout, self->select_info->selection_end,
+                                                cursor_direction);
+        }
+
+      focus_link = gtk_label_get_focus_link (self, NULL);
+      active_link = info->active_link;
+
+      if (active_link)
+        {
+          range[0] = active_link->start;
+          range[1] = active_link->end;
+
+          gtk_style_context_save_to_node (context, active_link->cssnode);
 
           range_clip = gdk_pango_layout_get_clip_region (self->layout, lx, ly, range, 1);
           for (i = 0; i < cairo_region_num_rectangles (range_clip); i++)
@@ -3483,7 +3525,7 @@ gtk_label_snapshot (GtkWidget   *widget,
               cairo_region_get_rectangle (range_clip, i, &clip_rect);
 
               gtk_snapshot_push_clip (snapshot, &GRAPHENE_RECT_FROM_RECT (&clip_rect));
-              gtk_snapshot_render_background (snapshot, context, x, 0, width, height);
+              gtk_snapshot_render_background (snapshot, context, 0, 0, width, height);
               gtk_snapshot_render_layout (snapshot, context, lx, ly, self->layout);
               gtk_snapshot_pop (snapshot);
             }
@@ -3492,71 +3534,22 @@ gtk_label_snapshot (GtkWidget   *widget,
 
           gtk_style_context_restore (context);
         }
-      else if (info)
-        {
-          GtkLabelLink *focus_link;
-          GtkLabelLink *active_link;
-          int range[2];
-          cairo_region_t *range_clip;
-          cairo_rectangle_int_t clip_rect;
-          int i;
-          GdkRectangle rect;
-
-          if (info->selectable &&
-              gtk_widget_has_focus (widget) &&
-              gtk_widget_is_drawable (widget))
-            {
-              PangoDirection cursor_direction;
-
-              cursor_direction = get_cursor_direction (self);
-              gtk_snapshot_render_insertion_cursor (snapshot, context,
-                                                    lx, ly,
-                                                    self->layout, self->select_info->selection_end,
-                                                    cursor_direction);
-            }
 
-          focus_link = gtk_label_get_focus_link (self, NULL);
-          active_link = info->active_link;
-
-          if (active_link)
-            {
-              range[0] = active_link->start;
-              range[1] = active_link->end;
-
-              gtk_style_context_save_to_node (context, active_link->cssnode);
-
-              range_clip = gdk_pango_layout_get_clip_region (self->layout, lx, ly, range, 1);
-              for (i = 0; i < cairo_region_num_rectangles (range_clip); i++)
-                {
-                  cairo_region_get_rectangle (range_clip, i, &clip_rect);
-
-                  gtk_snapshot_push_clip (snapshot, &GRAPHENE_RECT_FROM_RECT (&clip_rect));
-                  gtk_snapshot_render_background (snapshot, context, x, 0, width, height);
-                  gtk_snapshot_render_layout (snapshot, context, lx, ly, self->layout);
-                  gtk_snapshot_pop (snapshot);
-                }
-
-              cairo_region_destroy (range_clip);
-
-              gtk_style_context_restore (context);
-            }
-
-          if (focus_link && gtk_widget_has_visible_focus (widget))
-            {
-              range[0] = focus_link->start;
-              range[1] = focus_link->end;
+      if (focus_link && gtk_widget_has_visible_focus (widget))
+        {
+          range[0] = focus_link->start;
+          range[1] = focus_link->end;
 
-              gtk_style_context_save_to_node (context, focus_link->cssnode);
+          gtk_style_context_save_to_node (context, focus_link->cssnode);
 
-              range_clip = gdk_pango_layout_get_clip_region (self->layout, lx, ly, range, 1);
-              cairo_region_get_extents (range_clip, &rect);
+          range_clip = gdk_pango_layout_get_clip_region (self->layout, lx, ly, range, 1);
+          cairo_region_get_extents (range_clip, &rect);
 
-              gtk_snapshot_render_focus (snapshot, context, rect.x, rect.y, rect.width, rect.height);
+          gtk_snapshot_render_focus (snapshot, context, rect.x, rect.y, rect.width, rect.height);
 
-              cairo_region_destroy (range_clip);
+          cairo_region_destroy (range_clip);
 
-              gtk_style_context_restore (context);
-            }
+          gtk_style_context_restore (context);
         }
     }
 }


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