Add gdk_surface_get_scale
authorMatthias Clasen <mclasen@redhat.com>
Sat, 1 Apr 2023 18:51:11 +0000 (14:51 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 1 Apr 2023 23:09:16 +0000 (19:09 -0400)
Add a scale property to GdkSurface and use the
fractional scale for it on Wayland.

gdk/broadway/gdksurface-broadway.c
gdk/gdksurface.c
gdk/gdksurface.h
gdk/gdksurfaceprivate.h
gdk/macos/gdkmacossurface.c
gdk/wayland/gdksurface-wayland.c
gdk/win32/gdksurface-win32.c
gdk/x11/gdksurface-x11.c

index 9fccfa05e9e2599d5e21dbca6ef0979c074157b4..f64392b658a8b28125a6439cabef666c099c420d 100644 (file)
@@ -407,17 +407,10 @@ gdk_broadway_surface_hide (GdkSurface *surface)
   _gdk_surface_clear_update_area (surface);
 }
 
-static int
-gdk_broadway_surface_get_scale_factor (GdkSurface *surface)
+static double
+gdk_broadway_surface_get_scale (GdkSurface *surface)
 {
-  GdkBroadwayDisplay *broadway_display;
-
-  if (GDK_SURFACE_DESTROYED (surface))
-    return 1;
-
-  broadway_display = GDK_BROADWAY_DISPLAY (gdk_surface_get_display (surface));
-
-  return broadway_display->scale_factor;
+  return GDK_BROADWAY_DISPLAY (gdk_surface_get_display (surface))->scale_factor;
 }
 
 static void
@@ -1271,7 +1264,7 @@ gdk_broadway_surface_class_init (GdkBroadwaySurfaceClass *klass)
   impl_class->beep = gdk_broadway_surface_beep;
   impl_class->destroy_notify = gdk_broadway_surface_destroy_notify;
   impl_class->drag_begin = _gdk_broadway_surface_drag_begin;
-  impl_class->get_scale_factor = gdk_broadway_surface_get_scale_factor;
+  impl_class->get_scale = gdk_broadway_surface_get_scale;
 }
 
 #define LAST_PROP 1
index bcdff00db3db835f1f0690c8640362c5a1bf6a38..6aeba0bee8dc532343154d864b05f75a77e15495 100644 (file)
@@ -95,6 +95,7 @@ enum {
   PROP_WIDTH,
   PROP_HEIGHT,
   PROP_SCALE_FACTOR,
+  PROP_SCALE,
   LAST_PROP
 };
 
@@ -489,6 +490,12 @@ gdk_surface_init (GdkSurface *surface)
                                                  NULL, g_object_unref);
 }
 
+static double
+gdk_surface_real_get_scale (GdkSurface *surface)
+{
+  return 1.0;
+}
+
 static void
 gdk_surface_class_init (GdkSurfaceClass *klass)
 {
@@ -499,6 +506,7 @@ gdk_surface_class_init (GdkSurfaceClass *klass)
   object_class->get_property = gdk_surface_get_property;
 
   klass->beep = gdk_surface_real_beep;
+  klass->get_scale = gdk_surface_real_get_scale;
 
   /**
    * GdkSurface:cursor: (attributes org.gtk.Property.get=gdk_surface_get_cursor org.gtk.Property.set=gdk_surface_set_cursor)
@@ -570,6 +578,18 @@ gdk_surface_class_init (GdkSurfaceClass *klass)
                         1, G_MAXINT, 1,
                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
+  /**
+   * GdkSurface:scale: (attributes org.gtk.Property.get=gdk_surface_get_scale)
+   *
+   * The scale of the surface.
+   *
+   * Since: 4.12
+   */
+  properties[PROP_SCALE] =
+      g_param_spec_double ("scale", NULL, NULL,
+                        1., G_MAXDOUBLE, 1.,
+                        G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 
   /**
@@ -800,6 +820,10 @@ gdk_surface_get_property (GObject    *object,
       g_value_set_int (value, gdk_surface_get_scale_factor (surface));
       break;
 
+    case PROP_SCALE:
+      g_value_set_double (value, gdk_surface_get_scale (surface));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -2360,10 +2384,10 @@ _gdk_windowing_got_event (GdkDisplay *display,
  *   with it.
  */
 cairo_surface_t *
-gdk_surface_create_similar_surface (GdkSurface *     surface,
-                                    cairo_content_t content,
-                                    int             width,
-                                    int             height)
+gdk_surface_create_similar_surface (GdkSurface      *surface,
+                                    cairo_content_t  content,
+                                    int              width,
+                                    int              height)
 {
   cairo_surface_t *similar_surface;
   int scale;
@@ -2596,25 +2620,40 @@ gdk_surface_get_frame_clock (GdkSurface *surface)
  * pixel-based data the scale value can be used to determine whether to
  * use a pixel resource with higher resolution data.
  *
- * The scale of a surface may change during runtime.
+ * The scale may change during the lifetime of the surface.
  *
  * Returns: the scale factor
  */
 int
 gdk_surface_get_scale_factor (GdkSurface *surface)
 {
-  GdkSurfaceClass *class;
-
   g_return_val_if_fail (GDK_IS_SURFACE (surface), 1);
 
-  if (GDK_SURFACE_DESTROYED (surface))
-    return 1;
+  return (int) ceil (gdk_surface_get_scale (surface));
+}
 
-  class = GDK_SURFACE_GET_CLASS (surface);
-  if (class->get_scale_factor)
-    return class->get_scale_factor (surface);
+/**
+ * gdk_surface_get_scale: (attributes org.gtk.Method.get_property=scale)
+ * @surface: surface to get scale for
+ *
+ * Returns the internal scale that maps from surface coordinates
+ * to the actual device pixels.
+ *
+ * The scale may change during the lifetime of the surface.
+ *
+ * Returns: the scale
+ *
+ * Since: 4.12
+ */
+double
+gdk_surface_get_scale (GdkSurface *surface)
+{
+  g_return_val_if_fail (GDK_IS_SURFACE (surface), 1.);
+
+  if (GDK_SURFACE_DESTROYED (surface))
+    return 1.;
 
-  return 1;
+  return GDK_SURFACE_GET_CLASS (surface)->get_scale (surface);
 }
 
 /**
index f7f3e0273dca0745da81930d882dcbcf7d55cde8..6936ff7d33863ba708eadda23ca7c9a3b56964ae 100644 (file)
@@ -97,6 +97,9 @@ gboolean gdk_surface_translate_coordinates (GdkSurface *from,
 GDK_AVAILABLE_IN_ALL
 int           gdk_surface_get_scale_factor  (GdkSurface     *surface);
 
+GDK_AVAILABLE_IN_4_12
+double        gdk_surface_get_scale         (GdkSurface     *surface);
+
 GDK_AVAILABLE_IN_ALL
 gboolean      gdk_surface_get_device_position (GdkSurface      *surface,
                                                GdkDevice       *device,
index f4a37af4e341265e6ff27ca9b2af20333f45dc57..04a7597ad36f770f7fe95fca3dd1ce48c1e3701d 100644 (file)
@@ -154,7 +154,7 @@ struct _GdkSurfaceClass
                                          double              dx,
                                          double              dy);
 
-  int          (* get_scale_factor)       (GdkSurface      *surface);
+  double       (* get_scale)              (GdkSurface      *surface);
 
   void         (* set_opaque_region)      (GdkSurface      *surface,
                                            cairo_region_t *region);
index a002dc7db325a774ed0e1aa443c8b0c5e4c36247..3f90a13bbf9ac68e506664f1dabc9edf8369ad3f 100644 (file)
@@ -234,8 +234,8 @@ gdk_macos_surface_hide (GdkSurface *surface)
     }
 }
 
-static int
-gdk_macos_surface_get_scale_factor (GdkSurface *surface)
+static double
+gdk_macos_surface_get_scale (GdkSurface *surface)
 {
   GdkMacosSurface *self = (GdkMacosSurface *)surface;
 
@@ -585,7 +585,7 @@ gdk_macos_surface_class_init (GdkMacosSurfaceClass *klass)
   surface_class->get_device_state = gdk_macos_surface_get_device_state;
   surface_class->get_geometry = gdk_macos_surface_get_geometry;
   surface_class->get_root_coords = gdk_macos_surface_get_root_coords;
-  surface_class->get_scale_factor = gdk_macos_surface_get_scale_factor;
+  surface_class->get_scale = gdk_macos_surface_get_scale;
   surface_class->hide = gdk_macos_surface_hide;
   surface_class->set_input_region = gdk_macos_surface_set_input_region;
   surface_class->set_opaque_region = gdk_macos_surface_set_opaque_region;
index f19887460c9ec13a7f19de10842f1abf7efcd124..76112b8256975c65ed22dacee8997ae40e76b6fc 100644 (file)
@@ -269,7 +269,10 @@ gdk_wayland_surface_update_size (GdkSurface               *surface,
   if (height_changed)
     g_object_notify (G_OBJECT (surface), "height");
   if (scale_changed)
-    g_object_notify (G_OBJECT (surface), "scale-factor");
+    {
+      g_object_notify (G_OBJECT (surface), "scale-factor");
+      g_object_notify (G_OBJECT (surface), "scale");
+    }
 
   _gdk_surface_update_size (surface);
 }
@@ -1253,15 +1256,12 @@ gdk_wayland_surface_destroy_notify (GdkSurface *surface)
   g_object_unref (surface);
 }
 
-static int
-gdk_wayland_surface_get_scale_factor (GdkSurface *surface)
+static double
+gdk_wayland_surface_get_scale (GdkSurface *surface)
 {
   GdkWaylandSurface *impl = GDK_WAYLAND_SURFACE (surface);
 
-  if (GDK_SURFACE_DESTROYED (surface))
-    return 1;
-
-  return gdk_fractional_scale_to_int (&impl->scale);
+  return gdk_fractional_scale_to_double (&impl->scale);
 }
 
 static void
@@ -1313,7 +1313,7 @@ gdk_wayland_surface_class_init (GdkWaylandSurfaceClass *klass)
 
   surface_class->destroy_notify = gdk_wayland_surface_destroy_notify;
   surface_class->drag_begin = _gdk_wayland_surface_drag_begin;
-  surface_class->get_scale_factor = gdk_wayland_surface_get_scale_factor;
+  surface_class->get_scale = gdk_wayland_surface_get_scale;
   surface_class->set_opaque_region = gdk_wayland_surface_set_opaque_region;
   surface_class->request_layout = gdk_wayland_surface_request_layout;
 
index 5a32e78f3047fc6f50677172b15d4b45eddd66c7..8603ee2e9eb34570700451e69dec5269858a39a1 100644 (file)
@@ -4445,16 +4445,13 @@ gdk_win32_surface_set_shadow_width (GdkSurface *window,
 }
 
 
-int
-_gdk_win32_surface_get_scale_factor (GdkSurface *surface)
+double
+_gdk_win32_surface_get_scale (GdkSurface *surface)
 {
   GdkDisplay *display;
   GdkWin32Surface *impl;
   GdkWin32Display *win32_display;
 
-  if (GDK_SURFACE_DESTROYED (surface))
-    return 1;
-
   g_return_val_if_fail (surface != NULL, 1);
 
   display = gdk_surface_get_display (surface);
@@ -4654,7 +4651,7 @@ gdk_win32_surface_class_init (GdkWin32SurfaceClass *klass)
 
   impl_class->destroy_notify = gdk_win32_surface_destroy_notify;
   impl_class->drag_begin = _gdk_win32_surface_drag_begin;
-  impl_class->get_scale_factor = _gdk_win32_surface_get_scale_factor;
+  impl_class->get_scale = _gdk_win32_surface_get_scale;
   impl_class->request_layout = _gdk_win32_surface_request_layout;
   impl_class->compute_size = _gdk_win32_surface_compute_size;
 }
index 8f56fe5e74da09c0ad9c936c75607648b1c42a03..284895ce0e654ba1cd3072004fb0e68169bdf606 100644 (file)
@@ -2092,6 +2092,7 @@ _gdk_x11_surface_set_surface_scale (GdkSurface *surface,
   gdk_surface_invalidate_rect (surface, NULL);
 
   g_object_notify (G_OBJECT (surface), "scale-factor");
+  g_object_notify (G_OBJECT (surface), "scale");
 }
 
 void
@@ -4741,14 +4742,11 @@ gdk_x11_surface_get_xid (GdkSurface *surface)
   return GDK_X11_SURFACE (surface)->xid;
 }
 
-static int
-gdk_x11_surface_get_scale_factor (GdkSurface *surface)
+static double
+gdk_x11_surface_get_scale (GdkSurface *surface)
 {
   GdkX11Surface *impl = GDK_X11_SURFACE (surface);
 
-  if (GDK_SURFACE_DESTROYED (surface))
-    return 1;
-
   return impl->surface_scale;
 }
 
@@ -4888,7 +4886,7 @@ gdk_x11_surface_class_init (GdkX11SurfaceClass *klass)
 
   impl_class->destroy_notify = gdk_x11_surface_destroy_notify;
   impl_class->drag_begin = _gdk_x11_surface_drag_begin;
-  impl_class->get_scale_factor = gdk_x11_surface_get_scale_factor;
+  impl_class->get_scale = gdk_x11_surface_get_scale;
   impl_class->set_opaque_region = gdk_x11_surface_set_opaque_region;
   impl_class->request_layout = gdk_x11_surface_request_layout;
   impl_class->compute_size = gdk_x11_surface_compute_size;