Skip to content

Commit

Permalink
Wpf: Clear owner before setting Visible to false
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Oct 2, 2024
1 parent 156c07e commit 203852e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/Eto.WinForms/Forms/HwndFormHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ System.Windows.Window IWpfWindow.Control
{
get { throw new NotImplementedException(); }
}
public void SetOwnerFor(System.Windows.Window child)
public void SetOwnerFor(sw.Window child, sw.Interop.WindowInteropHelper interop)
{
new swin.WindowInteropHelper(child).Owner = Control;
if (interop != null)
interop.Owner = Control;
}
#elif WINFORMS

Expand Down
44 changes: 27 additions & 17 deletions src/Eto.Wpf/Forms/WpfWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IWpfWindow
{
sw.Window Control { get; }

void SetOwnerFor(sw.Window child);
void SetOwnerFor(sw.Window child, sw.Interop.WindowInteropHelper interop);
}

public interface IWpfValidateBinding
Expand Down Expand Up @@ -55,17 +55,16 @@ public abstract class WpfWindow<TControl, TWidget, TCallback> : WpfPanel<TContro
protected virtual bool IsAttached => false;

sw.Interop.WindowInteropHelper windowInterop;


public sw.Interop.WindowInteropHelper WindowInterop => windowInterop ??= new sw.Interop.WindowInteropHelper(Control);

public override IntPtr NativeHandle
{
get
{
if (windowInterop == null)
{
windowInterop = new sw.Interop.WindowInteropHelper(Control);
windowInterop.EnsureHandle();
}
return windowInterop.Handle;
// only call EnsureHandle if we haven't been created before
// otherwise, getting the handle after closed will throw an exception.
return windowInterop == null ? WindowInterop.EnsureHandle() : WindowInterop.Handle;
}
}

Expand Down Expand Up @@ -1070,7 +1069,7 @@ public override void SetContainerContent(sw.FrameworkElement content)
}


public void SetOwnerFor(sw.Window child)
public void SetOwnerFor(sw.Window child, sw.Interop.WindowInteropHelper interop)
{
child.Owner = Control;
}
Expand All @@ -1079,18 +1078,21 @@ public void SetOwner(Window owner)
{
if (owner == null)
{
// Clear out WPF owner
Control.Owner = null;
return;

// If the owner was set to an HWND clear that out too.
if (windowInterop != null)
windowInterop.Owner = IntPtr.Zero;
}
else if (owner.Handler is IWpfWindow wpfWindow)
{
// Owner could be an HwndWindowHandler (or other) which sets owner differently
wpfWindow.SetOwnerFor(Control, WindowInterop);
}
var wpfWindow = owner.Handler as IWpfWindow;
if (wpfWindow != null)
wpfWindow.SetOwnerFor(Control);
}

public double WpfScale
{
get { return dpiHelper?.WpfScale ?? 1f; }
}
public double WpfScale => dpiHelper?.WpfScale ?? 1f;

public float LogicalPixelSize
{
Expand Down Expand Up @@ -1119,9 +1121,17 @@ public override bool Visible
set
{
if (value)
{
// set owner back if we weren't visible beforehand
SetOwner(Widget.Owner);
Control.Show();
}
else
{
// hiding will hide entire owner chain, so clear that out before making it invisible.
SetOwner(null);
Control.Hide();
}
}
}
}
Expand Down

0 comments on commit 203852e

Please sign in to comment.