magnifier: Correct position of area
authorMatthias Clasen <mclasen@redhat.com>
Fri, 24 Mar 2023 19:30:27 +0000 (15:30 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 21 Apr 2023 06:38:46 +0000 (08:38 +0200)
The widget paintable uses the widgets bounds
as intrinsic size, so we need to offset from
that to the allocation, which is what the
coordinates are relative to.

gtk/gtkmagnifier.c

index 04db746c44317e567d979f0fe9aff394eb6165dc..e606ae23e8a9f247b718f1ee631722e1a7efffef 100644 (file)
@@ -20,6 +20,7 @@
 #include "gtkmagnifierprivate.h"
 #include "gtkwidgetprivate.h"
 #include "gtksnapshot.h"
+#include "gtkcssboxesprivate.h"
 
 enum {
   PROP_INSPECTED = 1,
@@ -105,10 +106,23 @@ gtk_magnifier_snapshot (GtkWidget   *widget,
 {
   GtkMagnifier *magnifier = GTK_MAGNIFIER (widget);
   double width, height, paintable_width, paintable_height;
-
-  if (gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (magnifier->paintable)) == NULL)
+  GtkWidget *inspected;
+  GtkCssBoxes boxes;
+  const graphene_rect_t *content_rect;
+  const graphene_rect_t *border_rect;
+  graphene_point_t offset;
+
+  inspected = gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (magnifier->paintable));
+  if (inspected == NULL)
     return;
 
+  gtk_css_boxes_init (&boxes, inspected);
+  content_rect = gtk_css_boxes_get_content_rect (&boxes);
+  border_rect = gtk_css_boxes_get_border_rect (&boxes);
+
+  offset.x = content_rect->origin.x - border_rect->origin.x;
+  offset.y = content_rect->origin.y - border_rect->origin.y;
+
   width = gtk_widget_get_width (widget);
   height = gtk_widget_get_height (widget);
   paintable_width = gdk_paintable_get_intrinsic_width (magnifier->paintable);
@@ -121,8 +135,8 @@ gtk_magnifier_snapshot (GtkWidget   *widget,
     gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (width / 2, height / 2));
   gtk_snapshot_scale (snapshot, magnifier->magnification, magnifier->magnification);
   gtk_snapshot_translate (snapshot, &GRAPHENE_POINT_INIT (
-                          - CLAMP (magnifier->x, 0, paintable_width),
-                          - CLAMP (magnifier->y, 0, paintable_height)));
+                          - CLAMP (magnifier->x + offset.x, 0, paintable_width),
+                          - CLAMP (magnifier->y + offset.y, 0, paintable_height)));
 
   gdk_paintable_snapshot (magnifier->paintable, snapshot, paintable_width, paintable_height);
   gtk_snapshot_restore (snapshot);