macos: create new windows with slight origin offset
authorChristian Hergert <christian@hergert.me>
Fri, 25 Feb 2022 00:43:14 +0000 (16:43 -0800)
committerChristian Hergert <chergert@redhat.com>
Wed, 16 Mar 2022 19:22:52 +0000 (12:22 -0700)
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

index a0dd6dddae3275472df5005b76477459db8cdf18..7b508a4a9ec6551a48733e12cbdd58a04fa12ea7 100644 (file)
@@ -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>