Skip to content

Commit

Permalink
Mac: More FloatingForm/NativeForm fixes
Browse files Browse the repository at this point in the history
- When FloatingForm is shown active it didn't set level properly when losing focus
- new NativeForm observed events were not set specific to that window
  • Loading branch information
cwensley committed Jun 3, 2022
1 parent 9d2d005 commit 57544a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/Eto.Mac/Forms/FloatingFormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ void SetLevelAdjustment()
{
lastOwner.GotFocus -= Owner_GotFocus;
lastOwner.LostFocus -= Owner_LostFocus;
Widget.GotFocus -= Owner_GotFocus;
Widget.LostFocus -= Owner_LostFocus;
Widget.Closed -= Widget_Closed;
}
if (owner != null)
{
owner.GotFocus += Owner_GotFocus;
owner.LostFocus += Owner_LostFocus;
Widget.GotFocus += Owner_GotFocus;
Widget.LostFocus += Owner_LostFocus;
Widget.Closed += Widget_Closed;
}
}
Expand All @@ -87,6 +91,8 @@ private void Widget_Closed(object sender, EventArgs e)
// when closed we need to disconnect from owner to prevent leaks
lastOwner.GotFocus -= Owner_GotFocus;
lastOwner.LostFocus -= Owner_LostFocus;
Widget.GotFocus -= Owner_GotFocus;
Widget.LostFocus -= Owner_LostFocus;
}
}

Expand Down Expand Up @@ -125,9 +131,15 @@ void SetAsTopmost()

private void Owner_LostFocus(object sender, EventArgs e)
{
if (HasFocus || Widget.Owner.HasFocus)
return;
Control.Level = NSWindowLevel.Normal;

// Window will still be topmost until we order the window explicitly.
// If there are multiple app windows, we use this instead of OrderFront otherwise
// the original parent window steals focus again.
if (Control.IsVisible)
Control.OrderFront(Control);
Control.OrderWindow(NSWindowOrderingMode.Above, Control.ParentWindow?.WindowNumber ?? 0);
}

protected override NSPanel CreateControl()
Expand Down
10 changes: 5 additions & 5 deletions src/Eto.Mac/Forms/NativeFormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public NativeFormHandler(NSWindowController windowController)

public override void AttachEvent(string id)
{
// native window, so attach notifications instead of using the delegate so we don't clobber existing functionality
// native window, so attach observers instead of using the delegate so we don't clobber existing functionality
switch (id)
{
case Window.ClosedEvent:
NSNotificationCenter.DefaultCenter.AddObserver(NSWindow.WillCloseNotification, n => Callback.OnClosed(Widget, EventArgs.Empty));
AddObserver(NSWindow.WillCloseNotification, n => Callback.OnClosed(Widget, EventArgs.Empty));
break;
case Window.SizeChangedEvent:
NSNotificationCenter.DefaultCenter.AddObserver(NSWindow.DidResizeNotification, n => Callback.OnSizeChanged(Widget, EventArgs.Empty));
AddObserver(NSWindow.DidResizeNotification, n => Callback.OnSizeChanged(Widget, EventArgs.Empty));
break;
case Window.GotFocusEvent:
NSNotificationCenter.DefaultCenter.AddObserver(NSWindow.DidBecomeKeyNotification, n => Callback.OnGotFocus(Widget, EventArgs.Empty));
AddObserver(NSWindow.DidBecomeKeyNotification, n => Callback.OnGotFocus(Widget, EventArgs.Empty));
break;
case Window.LostFocusEvent:
NSNotificationCenter.DefaultCenter.AddObserver(NSWindow.DidResignKeyNotification, n => Callback.OnLostFocus(Widget, EventArgs.Empty));
AddObserver(NSWindow.DidResignKeyNotification, n => Callback.OnLostFocus(Widget, EventArgs.Empty));
break;
}
return;
Expand Down

0 comments on commit 57544a3

Please sign in to comment.