Skip to content

Commit 116cbf4

Browse files
committed
consolidated code and applied standards
1 parent 7d46c1c commit 116cbf4

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

MaterialDesignThemes.Wpf/ComboBoxPopup.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,44 +106,45 @@ private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSi
106106
{
107107
var locationFromScreen = this.PlacementTarget.PointToScreen(new Point(0, 0));
108108

109-
var mainVisual = TreeHelper.FindMainTreeVisual(this.PlacementTarget);
109+
var mainVisual = PlacementTarget.GetVisualAncestory().OfType<System.Windows.Media.Visual>().LastOrDefault();
110+
if (mainVisual == null) return new CustomPopupPlacement[0];
110111

111-
int screenWidth = (int) DpiHelper.TransformToDeviceX(mainVisual, SystemParameters.PrimaryScreenWidth);
112-
int screenHeight = (int) DpiHelper.TransformToDeviceY(mainVisual, SystemParameters.PrimaryScreenHeight);
112+
var screenWidth = (int) DpiHelper.TransformToDeviceX(mainVisual, SystemParameters.PrimaryScreenWidth);
113+
var screenHeight = (int) DpiHelper.TransformToDeviceY(mainVisual, SystemParameters.PrimaryScreenHeight);
113114

114-
int locationX = (int)locationFromScreen.X % screenWidth;
115-
int locationY = (int)locationFromScreen.Y % screenHeight;
115+
var locationX = (int)locationFromScreen.X % screenWidth;
116+
var locationY = (int)locationFromScreen.Y % screenHeight;
116117

117-
double realOffsetX = (popupSize.Width - targetSize.Width) / 2.0;
118-
double offsetX = DpiHelper.TransformToDeviceX(mainVisual, offset.X);
119-
double defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DefaultVerticalOffset);
120-
double upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, UpVerticalOffset);
121-
double downVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DownVerticalOffset);
118+
var realOffsetX = (popupSize.Width - targetSize.Width) / 2.0;
119+
var offsetX = DpiHelper.TransformToDeviceX(mainVisual, offset.X);
120+
var defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DefaultVerticalOffset);
121+
var upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, UpVerticalOffset);
122+
var downVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, DownVerticalOffset);
122123

123124
if (locationX + popupSize.Width - realOffsetX > screenWidth
124125
|| locationX + realOffsetX < 0)
125126
{
126127
SetChildTemplateIfNeed(DefaultContentTemplate);
127128

128-
double newY = locationY + popupSize.Height > screenHeight
129+
var newY = locationY + popupSize.Height > screenHeight
129130
? -(defaultVerticalOffsetIndepent + popupSize.Height)
130131
: defaultVerticalOffsetIndepent + targetSize.Height;
131132

132133
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.Horizontal) };
133134
}
134-
else if (locationY + popupSize.Height > screenHeight)
135+
if (locationY + popupSize.Height > screenHeight)
135136
{
136137
SetChildTemplateIfNeed(UpContentTemplate);
137138

138-
double newY = upVerticalOffsetIndepent - popupSize.Height + targetSize.Height;
139+
var newY = upVerticalOffsetIndepent - popupSize.Height + targetSize.Height;
139140

140141
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.None) };
141142
}
142143
else
143144
{
144145
SetChildTemplateIfNeed(DownContentTemplate);
145146

146-
double newY = downVerticalOffsetIndepent;
147+
var newY = downVerticalOffsetIndepent;
147148

148149
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.None) };
149150
}

MaterialDesignThemes.Wpf/DpiHelper.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace MaterialDesignThemes.Wpf
1111
{
1212
internal static class DpiHelper
1313
{
14-
private static readonly int _dpiX;
15-
private static readonly int _dpiY;
14+
private static readonly int DpiX;
15+
private static readonly int DpiY;
1616

1717
private const double StandartDpiX = 96.0;
1818
private const double StandartDpiY = 96.0;
@@ -22,40 +22,34 @@ static DpiHelper()
2222
var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
2323
var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);
2424

25-
_dpiX = (int)dpiXProperty.GetValue(null, null);
26-
_dpiY = (int)dpiYProperty.GetValue(null, null);
25+
DpiX = (int)dpiXProperty.GetValue(null, null);
26+
DpiY = (int)dpiYProperty.GetValue(null, null);
2727
}
2828

2929
public static double TransformToDeviceY(Visual visual, double y)
3030
{
3131
var source = PresentationSource.FromVisual(visual);
32-
if (source != null)
33-
{
34-
return y * source.CompositionTarget.TransformToDevice.M22;
35-
}
32+
if (source?.CompositionTarget != null) return y * source.CompositionTarget.TransformToDevice.M22;
3633

3734
return TransformToDeviceY(y);
3835
}
3936

4037
public static double TransformToDeviceX(Visual visual, double x)
4138
{
4239
var source = PresentationSource.FromVisual(visual);
43-
if (source != null)
44-
{
45-
return x * source.CompositionTarget.TransformToDevice.M11;
46-
}
40+
if (source?.CompositionTarget != null) return x * source.CompositionTarget.TransformToDevice.M11;
4741

4842
return TransformToDeviceX(x);
4943
}
5044

5145
public static double TransformToDeviceY(double y)
5246
{
53-
return y * _dpiY / StandartDpiY;
47+
return y * DpiY / StandartDpiY;
5448
}
5549

5650
public static double TransformToDeviceX(double x)
5751
{
58-
return x * _dpiX / StandartDpiX;
52+
return x * DpiX / StandartDpiX;
5953
}
6054
}
6155
}

MaterialDesignThemes.Wpf/Extensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,19 @@ public static bool IsDescendant(this DependencyObject parent, DependencyObject n
2828
{
2929
return node != null && parent.VisualDepthFirstTraversal().Contains(node);
3030
}
31+
32+
/// <summary>
33+
/// Returns full visual ancestory, starting at the leaf.
34+
/// </summary>
35+
/// <param name="leaf"></param>
36+
/// <returns></returns>
37+
public static IEnumerable<DependencyObject> GetVisualAncestory(this DependencyObject leaf)
38+
{
39+
while (leaf != null)
40+
{
41+
yield return leaf;
42+
leaf = VisualTreeHelper.GetParent(leaf);
43+
}
44+
}
3145
}
3246
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@
236236
<Compile Include="CustomPopupPlacementCallbackHelper.cs" />
237237
<Compile Include="DataGridAssist.cs" />
238238
<Compile Include="DateTimeEx.cs" />
239-
<Compile Include="DependencyObjectExtensions.cs" />
240239
<Compile Include="DialogClosingEventArgs.cs" />
241240
<Compile Include="DialogClosingEventHandler.cs" />
242241
<Compile Include="DialogOpenedEventArgs.cs" />

0 commit comments

Comments
 (0)