summaryrefslogtreecommitdiff
path: root/gdk/macos/gdkmacosdisplay-wm.c
blob: 7b508a4a9ec6551a48733e12cbdd58a04fa12ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * Copyright © 2022 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "gdkmacosdisplay-private.h"
#include "gdkmacosmonitor.h"
#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,
                                                  GdkMacosSurface *parent,
                                                  int             *x,
                                                  int             *y)
{
  GdkRectangle surface_rect;
  GdkRectangle parent_rect;
  GdkRectangle workarea;
  GdkMonitor *monitor;

  g_assert (GDK_IS_MACOS_DISPLAY (self));
  g_assert (GDK_IS_MACOS_TOPLEVEL_SURFACE (surface));
  g_assert (GDK_IS_MACOS_TOPLEVEL_SURFACE (parent));

  /* If x/y is set, we should place relative to parent */
  if (GDK_SURFACE (surface)->x != 0 || GDK_SURFACE (surface)->y != 0)
    {
      *x = parent->root_x + GDK_SURFACE (surface)->x;
      *y = parent->root_y + GDK_SURFACE (surface)->y;
      return;
    }

  /* Try to center on top of the parent but also try to make the whole thing
   * visible in case that lands us under the topbar/panel/etc.
   */
  surface_rect.x = surface->root_x + surface->shadow_left;
  surface_rect.y = surface->root_y + surface->shadow_top;
  surface_rect.width = GDK_SURFACE (surface)->width - surface->shadow_left - surface->shadow_right;
  surface_rect.height = GDK_SURFACE (surface)->height - surface->shadow_top - surface->shadow_bottom;

  parent_rect.x = parent->root_x + surface->shadow_left;
  parent_rect.y = parent->root_y + surface->shadow_top;
  parent_rect.width = GDK_SURFACE (parent)->width - surface->shadow_left - surface->shadow_right;
  parent_rect.height = GDK_SURFACE (parent)->height - surface->shadow_top - surface->shadow_bottom;

  /* Try to place centered atop parent */
  surface_rect.x = parent_rect.x + ((parent_rect.width - surface_rect.width) / 2);
  surface_rect.y = parent_rect.y + ((parent_rect.height - surface_rect.height) / 2);

  /* Now make sure that we don't overlap the top-bar */
  monitor = _gdk_macos_surface_get_best_monitor (parent);
  gdk_macos_monitor_get_workarea (monitor, &workarea);

  if (surface_rect.x < workarea.x)
    surface_rect.x = workarea.x;

  if (surface_rect.y < workarea.y)
    surface_rect.y = workarea.y;

  *x = surface_rect.x - surface->shadow_left;
  *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,
                                      int             *x,
                                      int             *y)
{
  cairo_rectangle_int_t surface_rect;
  GdkRectangle workarea;
  const GList *surfaces;
  GdkMonitor *monitor;
  CGPoint mouse;

  g_assert (GDK_IS_MACOS_DISPLAY (self));
  g_assert (GDK_IS_MACOS_TOPLEVEL_SURFACE (surface));

  mouse = [NSEvent mouseLocation];
  monitor = _gdk_macos_display_get_monitor_at_display_coords (self, mouse.x, mouse.y);
  gdk_macos_monitor_get_workarea (monitor, &workarea);

  /* First place at top-left of current monitor */
  surface_rect.width = GDK_SURFACE (surface)->width - surface->shadow_left - surface->shadow_right;
  surface_rect.height = GDK_SURFACE (surface)->height - surface->shadow_top - surface->shadow_bottom;
  surface_rect.x = workarea.x + ((workarea.width - surface_rect.width) / 2);
  surface_rect.y = workarea.y + ((workarea.height - surface_rect.height) / 2);

  if (surface_rect.x < workarea.x)
    surface_rect.x = workarea.x;

  if (surface_rect.y < workarea.y)
    surface_rect.y = workarea.y;

  *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>
 * _gdk_macos_display_position_surface:
 *
 * Tries to position a window on a screen without landing in edges
 * and other weird areas the user can't use.
 */
void
_gdk_macos_display_position_surface (GdkMacosDisplay *self,
                                     GdkMacosSurface *surface,
                                     int             *x,
                                     int             *y)
{
  GdkSurface *transient_for;

  g_return_if_fail (GDK_IS_MACOS_DISPLAY (self));
  g_return_if_fail (GDK_IS_MACOS_TOPLEVEL_SURFACE (surface));

  transient_for = GDK_SURFACE (surface)->transient_for;

  if (transient_for != NULL)
    _gdk_macos_display_position_toplevel_with_parent (self, surface, GDK_MACOS_SURFACE (transient_for), x, y);
  else
    _gdk_macos_display_position_toplevel (self, surface, x, y);
}