macos: Move native window creation to ::constructed()

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.
This commit is contained in:
Benjamin Otte 2023-04-20 17:38:57 +02:00
parent 7e18a1cea7
commit a306401023
7 changed files with 112 additions and 135 deletions

View 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

View 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);
}

View 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

View 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,

View 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

View 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);

View 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