Skip to content

Commit

Permalink
Improve popup positioning when using ThemeShadowChrome
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Apr 28, 2020
1 parent 4c023d1 commit 8a3771d
Show file tree
Hide file tree
Showing 6 changed files with 907 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.9.0-preview.200425.2</Version>
<Version>0.9.0-preview.200428.1</Version>
<Authors>Yimeng Wu</Authors>
<Product>ModernWPF UI Library</Product>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
35 changes: 20 additions & 15 deletions ModernWpf.Controls/Common/PopupHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Reflection;
using System.Windows.Controls.Primitives;
using ModernWpf.Controls.Primitives;

namespace ModernWpf.Controls
{
Expand All @@ -13,32 +13,37 @@ public static void Reposition(this Popup popup)
throw new ArgumentNullException(nameof(popup));
}

if (s_repositionDelegate == null && !s_failedToCreateRepositionDelegate)
if (PopupPositioner.GetPositioner(popup) is { } positioner)
{
try
positioner.Reposition();
}
else
{
if (s_reposition.Value is { } reposition)
{
var method = typeof(Popup).GetMethod("Reposition", BindingFlags.Instance | BindingFlags.NonPublic);
s_repositionDelegate = (Action<Popup>)Delegate.CreateDelegate(typeof(Action<Popup>), method);
reposition(popup);
}
catch (Exception)
else
{
s_failedToCreateRepositionDelegate = true;
var offset = popup.HorizontalOffset;
popup.SetCurrentValue(Popup.HorizontalOffsetProperty, offset + 0.1);
popup.InvalidateProperty(Popup.HorizontalOffsetProperty);
}
}
}

if (s_repositionDelegate != null)
private static Action<Popup> CreateRepositionDelegate()
{
try
{
s_repositionDelegate(popup);
return DelegateHelper.CreateDelegate<Action<Popup>>(typeof(Popup), nameof(Reposition), true);
}
else
catch
{
var offset = popup.HorizontalOffset;
popup.SetCurrentValue(Popup.HorizontalOffsetProperty, offset + 0.1);
popup.SetCurrentValue(Popup.HorizontalOffsetProperty, offset);
return null;
}
}

private static Action<Popup> s_repositionDelegate;
private static bool s_failedToCreateRepositionDelegate;
private static readonly Lazy<Action<Popup>> s_reposition = new Lazy<Action<Popup>>(CreateRepositionDelegate);
}
}
Loading

0 comments on commit 8a3771d

Please sign in to comment.