[gtk/wip/chergert/for-4-6: 9/56] macos: calculate best monitor when changing screens




commit 433de2849d7d770a101a31eef67fab5388d3ad11
Author: Christian Hergert <christian hergert me>
Date:   Fri Feb 25 13:52:08 2022 -0800

    macos: calculate best monitor when changing screens
    
    When we change screens, we can keep track of the best monitor so that we
    can use it to register CVDisplayLink callbacks.

 gdk/macos/gdkmacossurface-private.h |  1 +
 gdk/macos/gdkmacossurface.c         | 46 ++++++++++++++++++-------------------
 2 files changed, 24 insertions(+), 23 deletions(-)
---
diff --git a/gdk/macos/gdkmacossurface-private.h b/gdk/macos/gdkmacossurface-private.h
index b853211afc..5ac4201a42 100644
--- a/gdk/macos/gdkmacossurface-private.h
+++ b/gdk/macos/gdkmacossurface-private.h
@@ -49,6 +49,7 @@ struct _GdkMacosSurface
   GdkMacosBuffer *buffer;
   GdkMacosBuffer *front;
   GPtrArray *monitors;
+  GdkMonitor *best_monitor;
   char *title;
 
   int root_x;
diff --git a/gdk/macos/gdkmacossurface.c b/gdk/macos/gdkmacossurface.c
index e437357a96..bdf5335ef5 100644
--- a/gdk/macos/gdkmacossurface.c
+++ b/gdk/macos/gdkmacossurface.c
@@ -400,6 +400,8 @@ gdk_macos_surface_destroy (GdkSurface *surface,
   GdkMacosWindow *window = g_steal_pointer (&self->window);
   GdkFrameClock *frame_clock;
 
+  g_clear_object (&self->best_monitor);
+
   if ((frame_clock = gdk_surface_get_frame_clock (GDK_SURFACE (self))))
     {
       g_signal_handlers_disconnect_by_func (frame_clock,
@@ -985,11 +987,13 @@ void
 _gdk_macos_surface_monitor_changed (GdkMacosSurface *self)
 {
   GListModel *monitors;
+  GdkMonitor *best = NULL;
   GdkRectangle rect;
   GdkRectangle intersect;
   GdkDisplay *display;
   GdkMonitor *monitor;
   guint n_monitors;
+  int best_area = 0;
 
   g_return_if_fail (GDK_IS_MACOS_SURFACE (self));
 
@@ -1032,29 +1036,10 @@ _gdk_macos_surface_monitor_changed (GdkMacosSurface *self)
   g_clear_object (&self->buffer);
   g_clear_object (&self->front);
 
-  _gdk_macos_surface_configure (self);
-
-  gdk_surface_invalidate_rect (GDK_SURFACE (self), NULL);
-}
-
-GdkMonitor *
-_gdk_macos_surface_get_best_monitor (GdkMacosSurface *self)
-{
-  GdkMonitor *best = NULL;
-  GdkRectangle rect;
-  int best_area = 0;
-
-  g_return_val_if_fail (GDK_IS_MACOS_SURFACE (self), NULL);
-
-  rect.x = self->root_x;
-  rect.y = self->root_y;
-  rect.width = GDK_SURFACE (self)->width;
-  rect.height = GDK_SURFACE (self)->height;
-
+  /* Determine the best-fit monitor */
   for (guint i = 0; i < self->monitors->len; i++)
     {
-      GdkMonitor *monitor = g_ptr_array_index (self->monitors, i);
-      GdkRectangle intersect;
+      monitor = g_ptr_array_index (self->monitors, i);
 
       if (gdk_rectangle_intersect (&monitor->geometry, &rect, &intersect))
         {
@@ -1062,13 +1047,28 @@ _gdk_macos_surface_get_best_monitor (GdkMacosSurface *self)
 
           if (area > best_area)
             {
-              best = monitor;
               best_area = area;
+              best = monitor;
             }
         }
     }
 
-  return best;
+  if (g_set_object (&self->best_monitor, best))
+    {
+      /* TODO: change frame clock to new monitor */
+    }
+
+  _gdk_macos_surface_configure (self);
+
+  gdk_surface_invalidate_rect (GDK_SURFACE (self), NULL);
+}
+
+GdkMonitor *
+_gdk_macos_surface_get_best_monitor (GdkMacosSurface *self)
+{
+  g_return_val_if_fail (GDK_IS_MACOS_SURFACE (self), NULL);
+
+  return self->best_monitor;
 }
 
 NSView *


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