@@ -195,7 +195,11 @@ virtual HRESULT Close() override
195195 {
196196 @autoreleasepool
197197 {
198- [Window close ];
198+ if (Window != nullptr )
199+ {
200+ [Window close ];
201+ }
202+
199203 return S_OK;
200204 }
201205 }
@@ -291,7 +295,14 @@ virtual bool TryLock() override
291295 {
292296 @autoreleasepool
293297 {
294- return [View lockFocusIfCanDraw ] == YES ;
298+ @try
299+ {
300+ return [View lockFocusIfCanDraw ] == YES ;
301+ }
302+ @catch (NSException *)
303+ {
304+ return NO ;
305+ }
295306 }
296307 }
297308
@@ -719,15 +730,33 @@ @implementation AvnView
719730 bool _isLeftPressed, _isMiddlePressed, _isRightPressed, _isXButton1Pressed, _isXButton2Pressed, _isMouseOver;
720731 NSEvent * _lastMouseDownEvent;
721732 bool _lastKeyHandled;
733+ AvnPixelSize _lastPixelSize;
722734}
723735
724- - (void )dealloc
736+ - (void )onClosed
725737{
738+ @synchronized (self)
739+ {
740+ _parent = nullptr ;
741+ }
726742}
727743
728- - (void )onClosed
744+ - (BOOL )lockFocusIfCanDraw
745+ {
746+ @synchronized (self)
747+ {
748+ if (_parent == nullptr )
749+ {
750+ return NO ;
751+ }
752+ }
753+
754+ return [super lockFocusIfCanDraw ];
755+ }
756+
757+ -(AvnPixelSize) getPixelSize
729758{
730- _parent = NULL ;
759+ return _lastPixelSize ;
731760}
732761
733762- (NSEvent *) lastMouseDownEvent
@@ -742,6 +771,8 @@ -(AvnView*) initWithParent: (WindowBaseImpl*) parent
742771 [self setWantsLayer: YES ];
743772 _parent = parent;
744773 _area = nullptr ;
774+ _lastPixelSize.Height = 100 ;
775+ _lastPixelSize.Width = 100 ;
745776 return self;
746777}
747778
@@ -783,6 +814,10 @@ -(void)setFrameSize:(NSSize)newSize
783814 [self addTrackingArea: _area];
784815
785816 _parent->UpdateCursor ();
817+
818+ auto fsize = [self convertSizeToBacking: [self frame ].size];
819+ _lastPixelSize.Width = (int )fsize.width ;
820+ _lastPixelSize.Height = (int )fsize.height ;
786821
787822 _parent->BaseEvents ->Resized (AvnSize{newSize.width , newSize.height });
788823}
@@ -812,7 +847,13 @@ - (void) drawFb: (AvnFramebuffer*) fb
812847
813848- (void )drawRect : (NSRect )dirtyRect
814849{
850+ if (_parent == nullptr )
851+ {
852+ return ;
853+ }
854+
815855 _parent->BaseEvents ->RunRenderPriorityJobs ();
856+
816857 @synchronized (self) {
817858 if (_swRenderedFrame != NULL )
818859 {
@@ -879,7 +920,12 @@ - (AvnPoint)toAvnPoint:(CGPoint)p
879920
880921- (void ) viewDidChangeBackingProperties
881922{
923+ auto fsize = [self convertSizeToBacking: [self frame ].size];
924+ _lastPixelSize.Width = (int )fsize.width ;
925+ _lastPixelSize.Height = (int )fsize.height ;
926+
882927 _parent->BaseEvents ->ScalingChanged ([_parent->Window backingScaleFactor ]);
928+
883929 [super viewDidChangeBackingProperties ];
884930}
885931
@@ -1161,6 +1207,12 @@ @implementation AvnWindow
11611207 bool _closed;
11621208 NSMenu * _menu;
11631209 bool _isAppMenuApplied;
1210+ double _lastScaling;
1211+ }
1212+
1213+ -(double ) getScaling
1214+ {
1215+ return _lastScaling;
11641216}
11651217
11661218+(void )closeAll
@@ -1174,10 +1226,6 @@ +(void)closeAll
11741226 }
11751227}
11761228
1177- - (void )dealloc
1178- {
1179- }
1180-
11811229- (void )pollModalSession : (nonnull NSModalSession )session
11821230{
11831231 auto response = [NSApp runModalSession: session];
@@ -1232,6 +1280,9 @@ -(AvnWindow*) initWithParent: (WindowBaseImpl*) parent
12321280 [self setReleasedWhenClosed: false ];
12331281 _parent = parent;
12341282 [self setDelegate: self ];
1283+ _closed = false ;
1284+
1285+ _lastScaling = [self backingScaleFactor ];
12351286 return self;
12361287}
12371288
@@ -1247,6 +1298,11 @@ - (BOOL)windowShouldClose:(NSWindow *)sender
12471298 return true ;
12481299}
12491300
1301+ - (void )windowDidChangeBackingProperties : (NSNotification *)notification
1302+ {
1303+ _lastScaling = [self backingScaleFactor ];
1304+ }
1305+
12501306- (void )windowWillClose : (NSNotification *)notification
12511307{
12521308 _closed = true ;
@@ -1257,9 +1313,6 @@ - (void)windowWillClose:(NSNotification *)notification
12571313 [self restoreParentWindow ];
12581314 parent->BaseEvents ->Closed ();
12591315 [parent->View onClosed ];
1260- dispatch_async (dispatch_get_main_queue (), ^{
1261- [self setContentView: nil ];
1262- });
12631316 }
12641317}
12651318
@@ -1406,18 +1459,6 @@ - (void)windowDidMove:(NSNotification *)notification
14061459 _parent->GetPosition (&position);
14071460 _parent->BaseEvents ->PositionChanged (position);
14081461}
1409-
1410- // TODO this breaks resizing.
1411- /* - (void)windowDidResize:(NSNotification *)notification
1412- {
1413-
1414- auto parent = dynamic_cast<IWindowStateChanged*>(_parent.operator->());
1415-
1416- if(parent != nullptr)
1417- {
1418- parent->WindowStateChanged();
1419- }
1420- }*/
14211462@end
14221463
14231464class PopupImpl : public virtual WindowBaseImpl, public IAvnPopup
0 commit comments