Skip to content

Commit f1053d1

Browse files
committed
Merge pull request AvaloniaUI#8154 from AvaloniaUI/fixes/osx-nswindow-refactor-fix-issues
1 parent 1c82eb4 commit f1053d1

File tree

8 files changed

+63
-22
lines changed

8 files changed

+63
-22
lines changed

native/Avalonia.Native/src/OSX/AutoFitContentView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ -(void)setFrameSize:(NSSize)newSize
8585
_settingSize = true;
8686
[super setFrameSize:newSize];
8787

88-
auto window = static_cast<id <AvnWindowProtocol>>([self window]);
88+
auto window = (id <AvnWindowProtocol>) [self window];
8989

9090
// TODO get actual titlebar size
9191

native/Avalonia.Native/src/OSX/AvnWindow.mm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ -(CLASS_NAME*) initWithParent: (WindowBaseImpl*) parent contentRect: (NSRect)co
173173
[self setBackgroundColor: [NSColor clearColor]];
174174

175175
_isExtended = false;
176+
177+
#ifdef IS_NSPANEL
178+
[self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces|NSWindowCollectionBehaviorFullScreenAuxiliary];
179+
#endif
180+
176181
return self;
177182
}
178183

@@ -193,6 +198,8 @@ - (void)windowDidChangeBackingProperties:(NSNotification *)notification
193198
[self backingScaleFactor];
194199
}
195200

201+
202+
196203
- (void)windowWillClose:(NSNotification *)notification
197204
{
198205
_closed = true;
@@ -211,11 +218,14 @@ -(BOOL)canBecomeKeyWindow
211218
// If the window has a child window being shown as a dialog then don't allow it to become the key window.
212219
for(NSWindow* uch in [self childWindows])
213220
{
214-
auto ch = static_cast<id <AvnWindowProtocol>>(uch);
215-
if(ch == nil)
221+
if (![uch conformsToProtocol:@protocol(AvnWindowProtocol)])
222+
{
216223
continue;
217-
if (ch.isDialog)
218-
return false;
224+
}
225+
226+
id <AvnWindowProtocol> ch = (id <AvnWindowProtocol>) uch;
227+
228+
return !ch.isDialog;
219229
}
220230

221231
return true;
@@ -371,6 +381,11 @@ - (void)windowDidMove:(NSNotification *)notification
371381

372382
if(cparent != nullptr)
373383
{
384+
if(!cparent->IsShown())
385+
{
386+
return;
387+
}
388+
374389
if(cparent->WindowState() == Maximized)
375390
{
376391
cparent->SetWindowState(Normal);

native/Avalonia.Native/src/OSX/PopupImpl.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ virtual HRESULT Resize(double x, double y, AvnPlatformResizeReason reason) overr
4949
return S_OK;
5050
}
5151
}
52+
5253
public:
5354
virtual bool ShouldTakeFocusOnShow() override
5455
{
5556
return false;
5657
}
58+
59+
virtual HRESULT Show(bool activate, bool isDialog) override
60+
{
61+
return WindowBaseImpl::Show(activate, true);
62+
}
5763
};
5864

5965

native/Avalonia.Native/src/OSX/WindowBaseImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ BEGIN_INTERFACE_MAP()
6060

6161
virtual HRESULT Show(bool activate, bool isDialog) override;
6262

63+
virtual bool IsShown ();
64+
6365
virtual bool ShouldTakeFocusOnShow();
6466

6567
virtual HRESULT Hide() override;

native/Avalonia.Native/src/OSX/WindowBaseImpl.mm

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
View = [[AvnView alloc] initWithParent:this];
3030
StandardContainer = [[AutoFitContentView new] initWithContent:View];
3131

32-
lastPositionSet.X = 100;
33-
lastPositionSet.Y = 100;
32+
lastPositionSet.X = -1;
33+
lastPositionSet.Y = -1;
3434
lastSize = NSSize { 100, 100 };
3535
lastMaxSize = NSSize { CGFLOAT_MAX, CGFLOAT_MAX};
3636
lastMinSize = NSSize { 0, 0 };
@@ -91,7 +91,11 @@
9191
CreateNSWindow(isDialog);
9292
InitialiseNSWindow();
9393

94-
SetPosition(lastPositionSet);
94+
if(lastPositionSet.X >= 0 && lastPositionSet.Y >= 0)
95+
{
96+
SetPosition(lastPositionSet);
97+
}
98+
9599
UpdateStyle();
96100

97101
[Window setTitle:_lastTitle];
@@ -111,6 +115,11 @@
111115
}
112116
}
113117

118+
bool WindowBaseImpl::IsShown ()
119+
{
120+
return _shown;
121+
}
122+
114123
bool WindowBaseImpl::ShouldTakeFocusOnShow() {
115124
return true;
116125
}
@@ -190,9 +199,8 @@
190199
if (ret == nullptr)
191200
return E_POINTER;
192201

193-
auto frame = [View frame];
194-
ret->Width = frame.size.width;
195-
ret->Height = frame.size.height;
202+
ret->Width = lastSize.width;
203+
ret->Height = lastSize.height;
196204

197205
return S_OK;
198206
}
@@ -205,9 +213,11 @@
205213
if (ret == nullptr)
206214
return E_POINTER;
207215

208-
auto frame = [Window frame];
209-
ret->Width = frame.size.width;
210-
ret->Height = frame.size.height;
216+
if(Window != nullptr){
217+
auto frame = [Window frame];
218+
ret->Width = frame.size.width;
219+
ret->Height = frame.size.height;
220+
}
211221

212222
return S_OK;
213223
}
@@ -369,7 +379,10 @@
369379

370380
@autoreleasepool {
371381
lastPositionSet = point;
372-
[Window setFrameTopLeftPoint:ToNSPoint(ConvertPointY(point))];
382+
383+
if(Window != nullptr) {
384+
[Window setFrameTopLeftPoint:ToNSPoint(ConvertPointY(point))];
385+
}
373386

374387
return S_OK;
375388
}
@@ -558,6 +571,7 @@
558571
[Window setContentMaxSize:lastMaxSize];
559572

560573
[Window setOpaque:false];
574+
[Window center];
561575

562576
if (lastMenu != nullptr) {
563577
[GetWindowProtocol() applyMenu:lastMenu];
@@ -575,7 +589,7 @@
575589
return nullptr;
576590
}
577591

578-
return static_cast<id <AvnWindowProtocol>>(Window);
592+
return (id <AvnWindowProtocol>) Window;
579593
}
580594

581595
extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events, IAvnGlContext* gl)

native/Avalonia.Native/src/OSX/WindowImpl.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@
429429
START_COM_CALL;
430430

431431
@autoreleasepool {
432+
auto currentState = _actualWindowState;
433+
_lastWindowState = state;
434+
432435
if (Window == nullptr) {
433436
return S_OK;
434437
}
@@ -439,9 +442,6 @@
439442

440443
_inSetWindowState = true;
441444

442-
auto currentState = _actualWindowState;
443-
_lastWindowState = state;
444-
445445
if (currentState == Normal) {
446446
_preZoomSize = [Window frame];
447447
}

src/Avalonia.Native/WindowImplBase.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ public Size? FrameSize
117117
{
118118
if (_native != null)
119119
{
120-
var s = _native.FrameSize;
121-
return new Size(s.Width, s.Height);
120+
unsafe
121+
{
122+
var s = new AvnSize { Width = -1, Height = -1 };
123+
_native.GetFrameSize(&s);
124+
return s.Width < 0 && s.Height < 0 ? null : new Size(s.Width, s.Height);
125+
}
122126
}
123127

124128
return default;

src/Avalonia.Native/avn.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ interface IAvnWindowBase : IUnknown
448448
HRESULT Close();
449449
HRESULT Activate();
450450
HRESULT GetClientSize(AvnSize*ret);
451-
HRESULT GetFrameSize(AvnSize*ret);
451+
HRESULT GetFrameSize(AvnSize*result);
452452
HRESULT GetScaling(double*ret);
453453
HRESULT SetMinMaxSize(AvnSize minSize, AvnSize maxSize);
454454
HRESULT Resize(double width, double height, AvnPlatformResizeReason reason);

0 commit comments

Comments
 (0)