Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#8154 from AvaloniaUI/fixes/osx-nswindow…
Browse files Browse the repository at this point in the history
…-refactor-fix-issues
  • Loading branch information
danwalmsley committed May 18, 2022
1 parent 1c82eb4 commit f1053d1
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 22 deletions.
2 changes: 1 addition & 1 deletion native/Avalonia.Native/src/OSX/AutoFitContentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ -(void)setFrameSize:(NSSize)newSize
_settingSize = true;
[super setFrameSize:newSize];

auto window = static_cast<id <AvnWindowProtocol>>([self window]);
auto window = (id <AvnWindowProtocol>) [self window];

// TODO get actual titlebar size

Expand Down
23 changes: 19 additions & 4 deletions native/Avalonia.Native/src/OSX/AvnWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ -(CLASS_NAME*) initWithParent: (WindowBaseImpl*) parent contentRect: (NSRect)co
[self setBackgroundColor: [NSColor clearColor]];

_isExtended = false;

#ifdef IS_NSPANEL
[self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces|NSWindowCollectionBehaviorFullScreenAuxiliary];
#endif

return self;
}

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



- (void)windowWillClose:(NSNotification *)notification
{
_closed = true;
Expand All @@ -211,11 +218,14 @@ -(BOOL)canBecomeKeyWindow
// If the window has a child window being shown as a dialog then don't allow it to become the key window.
for(NSWindow* uch in [self childWindows])
{
auto ch = static_cast<id <AvnWindowProtocol>>(uch);
if(ch == nil)
if (![uch conformsToProtocol:@protocol(AvnWindowProtocol)])
{
continue;
if (ch.isDialog)
return false;
}

id <AvnWindowProtocol> ch = (id <AvnWindowProtocol>) uch;

return !ch.isDialog;
}

return true;
Expand Down Expand Up @@ -371,6 +381,11 @@ - (void)windowDidMove:(NSNotification *)notification

if(cparent != nullptr)
{
if(!cparent->IsShown())
{
return;
}

if(cparent->WindowState() == Maximized)
{
cparent->SetWindowState(Normal);
Expand Down
6 changes: 6 additions & 0 deletions native/Avalonia.Native/src/OSX/PopupImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ virtual HRESULT Resize(double x, double y, AvnPlatformResizeReason reason) overr
return S_OK;
}
}

public:
virtual bool ShouldTakeFocusOnShow() override
{
return false;
}

virtual HRESULT Show(bool activate, bool isDialog) override
{
return WindowBaseImpl::Show(activate, true);
}
};


Expand Down
2 changes: 2 additions & 0 deletions native/Avalonia.Native/src/OSX/WindowBaseImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ BEGIN_INTERFACE_MAP()

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

virtual bool IsShown ();

virtual bool ShouldTakeFocusOnShow();

virtual HRESULT Hide() override;
Expand Down
36 changes: 25 additions & 11 deletions native/Avalonia.Native/src/OSX/WindowBaseImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
View = [[AvnView alloc] initWithParent:this];
StandardContainer = [[AutoFitContentView new] initWithContent:View];

lastPositionSet.X = 100;
lastPositionSet.Y = 100;
lastPositionSet.X = -1;
lastPositionSet.Y = -1;
lastSize = NSSize { 100, 100 };
lastMaxSize = NSSize { CGFLOAT_MAX, CGFLOAT_MAX};
lastMinSize = NSSize { 0, 0 };
Expand Down Expand Up @@ -91,7 +91,11 @@
CreateNSWindow(isDialog);
InitialiseNSWindow();

SetPosition(lastPositionSet);
if(lastPositionSet.X >= 0 && lastPositionSet.Y >= 0)
{
SetPosition(lastPositionSet);
}

UpdateStyle();

[Window setTitle:_lastTitle];
Expand All @@ -111,6 +115,11 @@
}
}

bool WindowBaseImpl::IsShown ()
{
return _shown;
}

bool WindowBaseImpl::ShouldTakeFocusOnShow() {
return true;
}
Expand Down Expand Up @@ -190,9 +199,8 @@
if (ret == nullptr)
return E_POINTER;

auto frame = [View frame];
ret->Width = frame.size.width;
ret->Height = frame.size.height;
ret->Width = lastSize.width;
ret->Height = lastSize.height;

return S_OK;
}
Expand All @@ -205,9 +213,11 @@
if (ret == nullptr)
return E_POINTER;

auto frame = [Window frame];
ret->Width = frame.size.width;
ret->Height = frame.size.height;
if(Window != nullptr){
auto frame = [Window frame];
ret->Width = frame.size.width;
ret->Height = frame.size.height;
}

return S_OK;
}
Expand Down Expand Up @@ -369,7 +379,10 @@

@autoreleasepool {
lastPositionSet = point;
[Window setFrameTopLeftPoint:ToNSPoint(ConvertPointY(point))];

if(Window != nullptr) {
[Window setFrameTopLeftPoint:ToNSPoint(ConvertPointY(point))];
}

return S_OK;
}
Expand Down Expand Up @@ -558,6 +571,7 @@
[Window setContentMaxSize:lastMaxSize];

[Window setOpaque:false];
[Window center];

if (lastMenu != nullptr) {
[GetWindowProtocol() applyMenu:lastMenu];
Expand All @@ -575,7 +589,7 @@
return nullptr;
}

return static_cast<id <AvnWindowProtocol>>(Window);
return (id <AvnWindowProtocol>) Window;
}

extern IAvnWindow* CreateAvnWindow(IAvnWindowEvents*events, IAvnGlContext* gl)
Expand Down
6 changes: 3 additions & 3 deletions native/Avalonia.Native/src/OSX/WindowImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@
START_COM_CALL;

@autoreleasepool {
auto currentState = _actualWindowState;
_lastWindowState = state;

if (Window == nullptr) {
return S_OK;
}
Expand All @@ -439,9 +442,6 @@

_inSetWindowState = true;

auto currentState = _actualWindowState;
_lastWindowState = state;

if (currentState == Normal) {
_preZoomSize = [Window frame];
}
Expand Down
8 changes: 6 additions & 2 deletions src/Avalonia.Native/WindowImplBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ public Size? FrameSize
{
if (_native != null)
{
var s = _native.FrameSize;
return new Size(s.Width, s.Height);
unsafe
{
var s = new AvnSize { Width = -1, Height = -1 };
_native.GetFrameSize(&s);
return s.Width < 0 && s.Height < 0 ? null : new Size(s.Width, s.Height);
}
}

return default;
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Native/avn.idl
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ interface IAvnWindowBase : IUnknown
HRESULT Close();
HRESULT Activate();
HRESULT GetClientSize(AvnSize*ret);
HRESULT GetFrameSize(AvnSize*ret);
HRESULT GetFrameSize(AvnSize*result);
HRESULT GetScaling(double*ret);
HRESULT SetMinMaxSize(AvnSize minSize, AvnSize maxSize);
HRESULT Resize(double width, double height, AvnPlatformResizeReason reason);
Expand Down

0 comments on commit f1053d1

Please sign in to comment.