macos: Move native window creation to ::constructed()
authorBenjamin Otte <otte@redhat.com>
Thu, 20 Apr 2023 15:38:57 +0000 (17:38 +0200)
committerBenjamin Otte <otte@redhat.com>
Wed, 26 Apr 2023 19:03:34 +0000 (21:03 +0200)
That way, it doesn't need a specific init function.

Also chain up last, so that the generic initialization code in
GdkSurface::constructed can access a fully initialized macos surface.

gdk/macos/gdkmacosdragsurface-private.h
gdk/macos/gdkmacosdragsurface.c
gdk/macos/gdkmacospopupsurface.c
gdk/macos/gdkmacossurface-private.h
gdk/macos/gdkmacossurface.c
gdk/macos/gdkmacostoplevelsurface-private.h
gdk/macos/gdkmacostoplevelsurface.c

index 516562bc3c12cee8a9987bf8f768f8615ad38163..b960019807cbd7ebdd3e383af63380ec29133b29 100644 (file)
@@ -31,11 +31,7 @@ typedef struct _GdkMacosDragSurfaceClass GdkMacosDragSurfaceClass;
 #define GDK_IS_MACOS_DRAG_SURFACE(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_MACOS_DRAG_SURFACE))
 
 GType            _gdk_macos_drag_surface_get_type (void);
-GdkMacosSurface *_gdk_macos_drag_surface_new      (GdkMacosDisplay *display,
-                                                   int              x,
-                                                   int              y,
-                                                   int              width,
-                                                   int              height);
+GdkMacosSurface *_gdk_macos_drag_surface_new      (GdkMacosDisplay *display);
 
 G_END_DECLS
 
index b5041f7067b446fcf939b36b5ea1c6a5617ccc8d..4c36f277159f4f55854de8bb0b02788efd8c6e3c 100644 (file)
@@ -64,27 +64,13 @@ G_DEFINE_TYPE_WITH_CODE (GdkMacosDragSurface, _gdk_macos_drag_surface, GDK_TYPE_
                          G_IMPLEMENT_INTERFACE (GDK_TYPE_DRAG_SURFACE, drag_surface_iface_init))
 
 static void
-_gdk_macos_drag_surface_class_init (GdkMacosDragSurfaceClass *klass)
-{
-}
-
-static void
-_gdk_macos_drag_surface_init (GdkMacosDragSurface *self)
-{
-}
-
-GdkMacosSurface *
-_gdk_macos_drag_surface_new (GdkMacosDisplay *display,
-                             int              x,
-                             int              y,
-                             int              width,
-                             int              height)
+_gdk_macos_drag_surface_constructed (GObject *object)
 {
   GDK_BEGIN_MACOS_ALLOC_POOL;
 
-  GdkFrameClock *frame_clock;
   GdkMacosWindow *window;
-  GdkMacosSurface *self;
+  GdkMacosSurface *self = GDK_MACOS_SURFACE (object);
+  GdkDisplay *display = gdk_surface_get_display (GDK_SURFACE (self));
   NSScreen *screen;
   NSUInteger style_mask;
   NSRect content_rect;
@@ -92,18 +78,15 @@ _gdk_macos_drag_surface_new (GdkMacosDisplay *display,
   int nx;
   int ny;
 
-  g_return_val_if_fail (GDK_IS_MACOS_DISPLAY (display), NULL);
-  g_return_val_if_fail (!frame_clock || GDK_IS_FRAME_CLOCK (frame_clock), NULL);
-
   style_mask = NSWindowStyleMaskBorderless;
 
-  _gdk_macos_display_to_display_coords (display, x, y, &nx, &ny);
+  _gdk_macos_display_to_display_coords (display, 0, 0, &nx, &ny);
 
   screen = _gdk_macos_display_get_screen_at_display_coords (display, nx, ny);
   screen_rect = [screen frame];
   nx -= screen_rect.origin.x;
   ny -= screen_rect.origin.y;
-  content_rect = NSMakeRect (nx, ny - height, width, height);
+  content_rect = NSMakeRect (nx, ny - 1, 1, 1);
 
   window = [[GdkMacosWindow alloc] initWithContentRect:content_rect
                                              styleMask:style_mask
@@ -115,17 +98,41 @@ _gdk_macos_drag_surface_new (GdkMacosDisplay *display,
   [window setBackgroundColor:[NSColor clearColor]];
   [window setDecorated:NO];
 
+  _gdk_macos_surface_set_native (self, window);
+
+  GDK_END_MACOS_ALLOC_POOL;
+
+  G_OBJECT_CLASS (_gdk_macos_drag_surface_parent_class)->constructed (object);
+}
+
+static void
+_gdk_macos_drag_surface_class_init (GdkMacosDragSurfaceClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = _gdk_macos_drag_surface_constructed;
+}
+
+static void
+_gdk_macos_drag_surface_init (GdkMacosDragSurface *self)
+{
+}
+
+GdkMacosSurface *
+_gdk_macos_drag_surface_new (GdkMacosDisplay *display)
+{
+  GdkFrameClock *frame_clock;
+
+  g_return_val_if_fail (GDK_IS_MACOS_DISPLAY (display), NULL);
+
   frame_clock = _gdk_frame_clock_idle_new ();
 
   self = g_object_new (GDK_TYPE_MACOS_DRAG_SURFACE,
                        "display", display,
                        "frame-clock", frame_clock,
-                       "native", window,
                        NULL);
 
   g_object_unref (frame_clock);
 
-  GDK_END_MACOS_ALLOC_POOL;
-
   return g_steal_pointer (&self);
 }
index 394f783e15ac5d1b43d8f6a6770376d211c3a6f2..e3ab5356549db15e9ffa109323e1bbc12b31cbe0 100644 (file)
@@ -284,34 +284,13 @@ _gdk_macos_popup_surface_set_property (GObject      *object,
 }
 
 static void
-_gdk_macos_popup_surface_class_init (GdkMacosPopupSurfaceClass *klass)
-{
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
-  GdkSurfaceClass *surface_class = GDK_SURFACE_CLASS (klass);
-
-  object_class->finalize = _gdk_macos_popup_surface_finalize;
-  object_class->get_property = _gdk_macos_popup_surface_get_property;
-  object_class->set_property = _gdk_macos_popup_surface_set_property;
-
-  surface_class->hide = _gdk_macos_popup_surface_hide;
-
-  gdk_popup_install_properties (object_class, LAST_PROP);
-}
-
-static void
-_gdk_macos_popup_surface_init (GdkMacosPopupSurface *self)
-{
-}
-
-GdkMacosSurface *
-_gdk_macos_popup_surface_new (GdkMacosDisplay *display,
-                              GdkSurface      *parent,
-                              GdkFrameClock   *frame_clock)
+_gdk_macos_popup_surface_constructed (GObject *object)
 {
   GDK_BEGIN_MACOS_ALLOC_POOL;
 
   GdkMacosWindow *window;
-  GdkMacosSurface *self;
+  GdkMacosPopupSurface *self = GDK_MACOS_POPUP_SURFACE (object);
+  GdkDisplay *display = gdk_surface_get_display (GDK_SURFACE (self));
   NSScreen *screen;
   NSUInteger style_mask;
   NSRect content_rect;
@@ -319,10 +298,6 @@ _gdk_macos_popup_surface_new (GdkMacosDisplay *display,
   int nx;
   int ny;
 
-  g_return_val_if_fail (GDK_IS_MACOS_DISPLAY (display), NULL);
-  g_return_val_if_fail (!frame_clock || GDK_IS_FRAME_CLOCK (frame_clock), NULL);
-  g_return_val_if_fail (!parent || GDK_IS_MACOS_SURFACE (parent), NULL);
-
   style_mask = NSWindowStyleMaskBorderless;
 
   _gdk_macos_display_to_display_coords (display, 0, 0, &nx, &ny);
@@ -345,16 +320,32 @@ _gdk_macos_popup_surface_new (GdkMacosDisplay *display,
   [window setExcludedFromWindowsMenu:YES];
   [window setLevel:NSPopUpMenuWindowLevel];
 
-  self = g_object_new (GDK_TYPE_MACOS_POPUP_SURFACE,
-                       "display", display,
-                       "frame-clock", frame_clock,
-                       "native", window,
-                       "parent", parent,
-                       NULL);
+  _gdk_macos_surface_set_native (GDK_MACOS_SURFACE (self), window);
 
   GDK_END_MACOS_ALLOC_POOL;
 
-  return g_steal_pointer (&self);
+  G_OBJECT_CLASS (_gdk_macos_popup_surface_parent_class)->constructed (object);
+}
+
+static void
+_gdk_macos_popup_surface_class_init (GdkMacosPopupSurfaceClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GdkSurfaceClass *surface_class = GDK_SURFACE_CLASS (klass);
+
+  object_class->constructed = _gdk_macos_popup_surface_constructed;
+  object_class->finalize = _gdk_macos_popup_surface_finalize;
+  object_class->get_property = _gdk_macos_popup_surface_get_property;
+  object_class->set_property = _gdk_macos_popup_surface_set_property;
+
+  surface_class->hide = _gdk_macos_popup_surface_hide;
+
+  gdk_popup_install_properties (object_class, LAST_PROP);
+}
+
+static void
+_gdk_macos_popup_surface_init (GdkMacosPopupSurface *self)
+{
 }
 
 void
index 3c2e8f572c9d7782869c74cef3247ed76a7515f9..bab6e717b6f97e34a2b000463c16e349e3df6e5f 100644 (file)
@@ -88,6 +88,8 @@ GdkMacosSurface   *_gdk_macos_surface_new                     (GdkMacosDisplay
                                                                GdkSurfaceType        surface_type,
                                                                GdkSurface           *parent);
 NSWindow          *_gdk_macos_surface_get_native              (GdkMacosSurface      *self);
+void               _gdk_macos_surface_set_native              (GdkMacosSurface      *self,
+                                                               GdkMacosWindow       *window);
 CGDirectDisplayID  _gdk_macos_surface_get_screen_id           (GdkMacosSurface      *self);
 const char        *_gdk_macos_surface_get_title               (GdkMacosSurface      *self);
 void               _gdk_macos_surface_set_title               (GdkMacosSurface      *self,
index 6da541e6cd871ba509abe790c6b4358ee5207683..226fffe656f63d97c5533f699be20098188c234f 100644 (file)
@@ -43,7 +43,6 @@
 #include "gdkmacosglcontext-private.h"
 #include "gdkmacosmonitor-private.h"
 #include "gdkmacospopupsurface-private.h"
-#include "gdkmacostoplevelsurface-private.h"
 #include "gdkmacosutils-private.h"
 
 G_DEFINE_ABSTRACT_TYPE (GdkMacosSurface, gdk_macos_surface, GDK_TYPE_SURFACE)
@@ -416,10 +415,6 @@ gdk_macos_surface_drag_begin (GdkSurface         *surface,
   GdkMacosSurface *drag_surface;
   GdkMacosDrag *drag;
   GdkCursor *cursor;
-  double px;
-  double py;
-  int sx;
-  int sy;
 
   g_assert (GDK_IS_MACOS_SURFACE (self));
   g_assert (GDK_IS_MACOS_TOPLEVEL_SURFACE (self) ||
@@ -427,10 +422,7 @@ gdk_macos_surface_drag_begin (GdkSurface         *surface,
   g_assert (GDK_IS_MACOS_DEVICE (device));
   g_assert (GDK_IS_CONTENT_PROVIDER (content));
 
-  gdk_macos_device_query_state (device, surface, NULL, &px, &py, NULL);
-  _gdk_macos_surface_get_root_coords (GDK_MACOS_SURFACE (surface), &sx, &sy);
-  drag_surface = _gdk_macos_drag_surface_new (GDK_MACOS_DISPLAY (surface->display),
-                                              sx, sy, 1, 1);
+  drag_surface = _gdk_macos_drag_surface_new (GDK_MACOS_DISPLAY (surface->display));
   drag = g_object_new (GDK_TYPE_MACOS_DRAG,
                        "drag-surface", drag_surface,
                        "surface", surface,
@@ -551,26 +543,6 @@ gdk_macos_surface_get_property (GObject    *object,
     }
 }
 
-static void
-gdk_macos_surface_set_property (GObject      *object,
-                                guint         prop_id,
-                                const GValue *value,
-                                GParamSpec   *pspec)
-{
-  GdkMacosSurface *self = GDK_MACOS_SURFACE (object);
-
-  switch (prop_id)
-    {
-    case PROP_NATIVE:
-      self->window = g_value_get_pointer (value);
-      [self->window setGdkSurface:self];
-      break;
-
-    default:
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
-    }
-}
-
 static void
 gdk_macos_surface_class_init (GdkMacosSurfaceClass *klass)
 {
@@ -579,7 +551,6 @@ gdk_macos_surface_class_init (GdkMacosSurfaceClass *klass)
 
   object_class->constructed = gdk_macos_surface_constructed;
   object_class->get_property = gdk_macos_surface_get_property;
-  object_class->set_property = gdk_macos_surface_set_property;
 
   surface_class->destroy = gdk_macos_surface_destroy;
   surface_class->drag_begin = gdk_macos_surface_drag_begin;
@@ -598,7 +569,7 @@ gdk_macos_surface_class_init (GdkMacosSurfaceClass *klass)
    */
   properties [PROP_NATIVE] =
     g_param_spec_pointer ("native", NULL, NULL,
-                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
 
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 }
@@ -630,11 +601,19 @@ _gdk_macos_surface_new (GdkMacosDisplay *display,
   switch (surface_type)
     {
     case GDK_SURFACE_TOPLEVEL:
-      ret = _gdk_macos_toplevel_surface_new (display, frame_clock);
+      ret = g_object_new (GDK_TYPE_MACOS_TOPLEVEL_SURFACE,
+                          "display", display,
+                          "frame-clock", frame_clock,
+                          NULL);
       break;
 
     case GDK_SURFACE_POPUP:
-      ret = _gdk_macos_popup_surface_new (display, parent, frame_clock);
+      ret = g_object_new (GDK_TYPE_MACOS_POPUP_SURFACE,
+                          "display", display,
+                          "frame-clock", frame_clock,
+                          "parent", parent,
+                          NULL);
+
       break;
 
     case GDK_SURFACE_DRAG:
@@ -743,6 +722,16 @@ _gdk_macos_surface_get_native (GdkMacosSurface *self)
   return (NSWindow *)self->window;
 }
 
+void
+_gdk_macos_surface_set_native (GdkMacosSurface *self,
+                               GdkMacosWindow  *window)
+{
+  g_assert (self->window == NULL);
+
+  self->window = window;
+  [self->window setGdkSurface:self];
+}
+
 /**
  * gdk_macos_surface_get_native_window: (attributes org.gtk.Method.get_property=native)
  * @self: a #GdkMacosSurface
index febec5550108c268d76971fe636db16dcec1432a..1e0b73e13c9083e125d526ace489033db8f55191 100644 (file)
@@ -45,8 +45,6 @@ struct _GdkMacosToplevelSurfaceClass
 };
 
 GType            _gdk_macos_toplevel_surface_get_type           (void);
-GdkMacosSurface *_gdk_macos_toplevel_surface_new                (GdkMacosDisplay         *display,
-                                                                 GdkFrameClock           *frame_clock);
 void             _gdk_macos_toplevel_surface_attach_to_parent   (GdkMacosToplevelSurface *self);
 void             _gdk_macos_toplevel_surface_detach_from_parent (GdkMacosToplevelSurface *self);
 
index 7d4d943ea7a4915686da9690e19003b965a8f1f7..202e7367cb4bf6c0443b1ed287a2858933aa5487 100644 (file)
@@ -626,36 +626,13 @@ _gdk_macos_toplevel_surface_set_property (GObject      *object,
 }
 
 static void
-_gdk_macos_toplevel_surface_class_init (GdkMacosToplevelSurfaceClass *klass)
-{
-  GObjectClass *object_class = G_OBJECT_CLASS (klass);
-  GdkSurfaceClass *surface_class = GDK_SURFACE_CLASS (klass);
-
-  object_class->get_property = _gdk_macos_toplevel_surface_get_property;
-  object_class->set_property = _gdk_macos_toplevel_surface_set_property;
-
-  surface_class->destroy = _gdk_macos_toplevel_surface_destroy;
-  surface_class->hide = _gdk_macos_toplevel_surface_hide;
-  surface_class->compute_size = _gdk_macos_toplevel_surface_compute_size;
-  surface_class->request_layout = _gdk_macos_toplevel_surface_request_layout;
-
-  gdk_toplevel_install_properties (object_class, LAST_PROP);
-}
-
-static void
-_gdk_macos_toplevel_surface_init (GdkMacosToplevelSurface *self)
-{
-  self->decorated = TRUE;
-}
-
-GdkMacosSurface *
-_gdk_macos_toplevel_surface_new (GdkMacosDisplay *display,
-                                 GdkFrameClock   *frame_clock)
+_gdk_macos_toplevel_surface_constructed (GObject *object)
 {
   GDK_BEGIN_MACOS_ALLOC_POOL;
 
   GdkMacosWindow *window;
-  GdkMacosSurface *self;
+  GdkMacosToplevelSurface *self = GDK_MACOS_TOPLEVEL_SURFACE (object);
+  GdkDisplay *display = gdk_surface_get_display (GDK_SURFACE (self));
   NSUInteger style_mask;
   NSRect content_rect;
   NSRect visible_frame;
@@ -663,9 +640,6 @@ _gdk_macos_toplevel_surface_new (GdkMacosDisplay *display,
   int nx;
   int ny;
 
-  g_return_val_if_fail (GDK_IS_MACOS_DISPLAY (display), NULL);
-  g_return_val_if_fail (!frame_clock || GDK_IS_FRAME_CLOCK (frame_clock), NULL);
-
   style_mask = (NSWindowStyleMaskTitled |
                 NSWindowStyleMaskClosable |
                 NSWindowStyleMaskMiniaturizable |
@@ -685,15 +659,35 @@ _gdk_macos_toplevel_surface_new (GdkMacosDisplay *display,
   /* Allow NSWindow to go fullscreen */
   [window setCollectionBehavior:NSWindowCollectionBehaviorFullScreenPrimary];
 
-  self = g_object_new (GDK_TYPE_MACOS_TOPLEVEL_SURFACE,
-                       "display", display,
-                       "frame-clock", frame_clock,
-                       "native", window,
-                       NULL);
+  _gdk_macos_surface_set_native (GDK_MACOS_SURFACE (self), window);
 
   GDK_END_MACOS_ALLOC_POOL;
 
-  return g_steal_pointer (&self);
+  G_OBJECT_CLASS (_gdk_macos_toplevel_surface_parent_class)->constructed (object);
+}
+
+static void
+_gdk_macos_toplevel_surface_class_init (GdkMacosToplevelSurfaceClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GdkSurfaceClass *surface_class = GDK_SURFACE_CLASS (klass);
+
+  object_class->constructed = _gdk_macos_toplevel_surface_constructed;
+  object_class->get_property = _gdk_macos_toplevel_surface_get_property;
+  object_class->set_property = _gdk_macos_toplevel_surface_set_property;
+
+  surface_class->destroy = _gdk_macos_toplevel_surface_destroy;
+  surface_class->hide = _gdk_macos_toplevel_surface_hide;
+  surface_class->compute_size = _gdk_macos_toplevel_surface_compute_size;
+  surface_class->request_layout = _gdk_macos_toplevel_surface_request_layout;
+
+  gdk_toplevel_install_properties (object_class, LAST_PROP);
+}
+
+static void
+_gdk_macos_toplevel_surface_init (GdkMacosToplevelSurface *self)
+{
+  self->decorated = TRUE;
 }
 
 void