[gtk/wip/chergert/better-positioning: 1/2] macos: create new windows with slight origin offset




commit eea7cf30bb6bab2ff671d4be37d6a9371c39f71e
Author: Christian Hergert <christian hergert me>
Date:   Thu Feb 24 16:43:14 2022 -0800

    macos: create new windows with slight origin offset
    
    When creating new windows, it is better if we create them with a slight
    offset to where they were created before so that they are visible to the
    user separately from what they might be overshadowing.

 gdk/macos/gdkmacosdisplay-wm.c | 41 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 39 insertions(+), 2 deletions(-)
---
diff --git a/gdk/macos/gdkmacosdisplay-wm.c b/gdk/macos/gdkmacosdisplay-wm.c
index a0dd6dddae..7b508a4a9e 100644
--- a/gdk/macos/gdkmacosdisplay-wm.c
+++ b/gdk/macos/gdkmacosdisplay-wm.c
@@ -24,6 +24,9 @@
 #include "gdkmacossurface-private.h"
 #include "gdkmacostoplevelsurface-private.h"
 
+#define WARP_OFFSET_X 15
+#define WARP_OFFSET_Y 15
+
 static void
 _gdk_macos_display_position_toplevel_with_parent (GdkMacosDisplay *self,
                                                   GdkMacosSurface *surface,
@@ -79,6 +82,22 @@ _gdk_macos_display_position_toplevel_with_parent (GdkMacosDisplay *self,
   *y = surface_rect.y - surface->shadow_top;
 }
 
+static inline gboolean
+has_surface_at_origin (const GList *surfaces,
+                       int          x,
+                       int          y)
+{
+  for (const GList *iter = surfaces; iter; iter = iter->next)
+    {
+      GdkMacosSurface *surface = iter->data;
+
+      if (surface->root_x == x && surface->root_y == y)
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
 static void
 _gdk_macos_display_position_toplevel (GdkMacosDisplay *self,
                                       GdkMacosSurface *surface,
@@ -87,6 +106,7 @@ _gdk_macos_display_position_toplevel (GdkMacosDisplay *self,
 {
   cairo_rectangle_int_t surface_rect;
   GdkRectangle workarea;
+  const GList *surfaces;
   GdkMonitor *monitor;
   CGPoint mouse;
 
@@ -109,10 +129,27 @@ _gdk_macos_display_position_toplevel (GdkMacosDisplay *self,
   if (surface_rect.y < workarea.y)
     surface_rect.y = workarea.y;
 
-  /* TODO: If there is another window at this same position, perhaps we should move it */
-
   *x = surface_rect.x - surface->shadow_left;
   *y = surface_rect.y - surface->shadow_top;
+
+  /* Try to see if there are any other surfaces at this origin and if so,
+   * adjust until we get something better.
+   */
+  surfaces = _gdk_macos_display_get_surfaces (self);
+  while (has_surface_at_origin (surfaces, *x, *y))
+    {
+      *x += WARP_OFFSET_X;
+      *y += WARP_OFFSET_Y;
+
+      /* If we reached the bottom right, just bail and try the workspace origin */
+      if (*x + surface->shadow_left + WARP_OFFSET_X > workarea.x + workarea.width ||
+          *y + surface->shadow_top + WARP_OFFSET_Y > workarea.y + workarea.height)
+        {
+          *x = workarea.x - surface->shadow_left;
+          *y = workarea.y - surface->shadow_top;
+          return;
+        }
+    }
 }
 
 /*<private>


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