diff --git a/native/cocos/platform/mac/View.h b/native/cocos/platform/mac/View.h index 2e9cec833c3..adc21ab9d94 100644 --- a/native/cocos/platform/mac/View.h +++ b/native/cocos/platform/mac/View.h @@ -28,7 +28,7 @@ #import @interface View : NSView - +- (int)getWindowId; @property (nonatomic, assign) id device; @end diff --git a/native/cocos/platform/mac/View.mm b/native/cocos/platform/mac/View.mm index 31b631a5e05..160d2fa2410 100644 --- a/native/cocos/platform/mac/View.mm +++ b/native/cocos/platform/mac/View.mm @@ -35,6 +35,20 @@ of this software and associated engine source code (the "Software"), a limited, #import "platform/mac/modules/SystemWindow.h" #import "platform/mac/modules/SystemWindowManager.h" +#include "SDL2/SDL.h" + +static int MetalViewEventWatch(void* userData, SDL_Event*event) { + if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + @autoreleasepool { + auto *view = (__bridge View *)userData; + if ([view getWindowId] == event->window.windowID) { + [view viewDidChangeBackingProperties]; + } + } + } + return 0; +} + @implementation View { cc::MouseEvent _mouseEvent; cc::KeyboardEvent _keyboardEvent; @@ -50,6 +64,8 @@ - (CALayer *)makeBackingLayer { - (instancetype)initWithFrame:(NSRect)frameRect { if (self = [super initWithFrame:frameRect]) { + // View is used as a subview, so the resize message needs to be handled manually. + SDL_AddEventWatch(MetalViewEventWatch, (__bridge void*)(self)); [self.window makeFirstResponder:self]; int pixelRatio = [[NSScreen mainScreen] backingScaleFactor]; CGSize size = CGSizeMake(frameRect.size.width * pixelRatio, frameRect.size.height * pixelRatio); diff --git a/native/cocos/platform/mac/modules/SystemWindow.mm b/native/cocos/platform/mac/modules/SystemWindow.mm index 42a4ed8ad8b..310082a1bfd 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.mm +++ b/native/cocos/platform/mac/modules/SystemWindow.mm @@ -24,6 +24,7 @@ of this software and associated documentation files (the "Software"), to deal #include "platform/mac/modules/SystemWindow.h" #include "platform/mac/View.h" + #include "base/Log.h" #include "base/Macros.h" @@ -57,15 +58,14 @@ of this software and associated documentation files (the "Software"), to deal void SystemWindow::initWindowProperty(SDL_Window* window, const char *title, int x, int y, int w, int h) { CC_ASSERT(window != nullptr); auto* nsWindow = reinterpret_cast(SDLHelper::getWindowHandle(window)); - NSRect rect = NSMakeRect(x, y, w, h); NSString *astring = [NSString stringWithUTF8String:title]; nsWindow.title = astring; + // contentView is created internally by sdl. NSView *view = nsWindow.contentView; - auto* newView = [[View alloc] initWithFrame:rect]; + auto* newView = [[View alloc] initWithFrame:view.frame]; [view addSubview:newView]; [nsWindow.contentView setWantsBestResolutionOpenGLSurface:YES]; [nsWindow makeKeyAndOrderFront:nil]; - _windowHandle = reinterpret_cast(nsWindow.contentView) ; auto dpr = [nsWindow backingScaleFactor];