Skip to content

Commit 01a4139

Browse files
committed
Add DpiHelper
1 parent 1280c1a commit 01a4139

File tree

4 files changed

+80
-34
lines changed

4 files changed

+80
-34
lines changed

MaterialDesignThemes.Wpf/ComboBoxPopup.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using System.Reflection;
56
using System.Text;
67
using System.Threading.Tasks;
78
using System.Windows;
@@ -105,34 +106,40 @@ private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSi
105106
{
106107
var locationFromScreen = this.PlacementTarget.PointToScreen(new Point(0, 0));
107108

108-
int locationX = (int) locationFromScreen.X%(int) SystemParameters.PrimaryScreenWidth;
109-
int locationY = (int) locationFromScreen.Y%(int) SystemParameters.PrimaryScreenHeight;
109+
int locationX = (int)locationFromScreen.X % (int)DpiHelper.TransformToDeviceX(SystemParameters.PrimaryScreenWidth);
110+
int locationY = (int)locationFromScreen.Y % (int)DpiHelper.TransformToDeviceY(SystemParameters.PrimaryScreenHeight);
111+
double realOffsetX = (popupSize.Width - targetSize.Width) / 2.0;
110112

111-
if (locationX + popupSize.Width > SystemParameters.PrimaryScreenWidth || locationX < 0)
113+
double offsetX = DpiHelper.TransformToDeviceX(offset.X);
114+
double defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(DefaultVerticalOffset);
115+
double upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(UpVerticalOffset);
116+
double downVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(DownVerticalOffset);
117+
118+
if (locationX + popupSize.Width - realOffsetX > SystemParameters.PrimaryScreenWidth || locationX + realOffsetX < 0)
112119
{
113120
SetChildTemplateIfNeed(DefaultContentTemplate);
114121

115122
double newY = locationY + popupSize.Height > SystemParameters.PrimaryScreenHeight
116-
? -(DefaultVerticalOffset + popupSize.Height)
117-
: DefaultVerticalOffset + targetSize.Height;
123+
? -(defaultVerticalOffsetIndepent + popupSize.Height)
124+
: defaultVerticalOffsetIndepent + targetSize.Height;
118125

119-
return new[] { new CustomPopupPlacement(new Point(offset.X, newY), PopupPrimaryAxis.Horizontal) };
126+
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.Horizontal) };
120127
}
121-
if (locationY + popupSize.Height > SystemParameters.PrimaryScreenHeight)
128+
else if (locationY + popupSize.Height > SystemParameters.PrimaryScreenHeight)
122129
{
123130
SetChildTemplateIfNeed(UpContentTemplate);
124131

125-
double newY = UpVerticalOffset - popupSize.Height + targetSize.Height;
132+
double newY = upVerticalOffsetIndepent - popupSize.Height + targetSize.Height;
126133

127-
return new[] { new CustomPopupPlacement(new Point(offset.X, newY), PopupPrimaryAxis.None) };
134+
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.None) };
128135
}
129136
else
130137
{
131138
SetChildTemplateIfNeed(DownContentTemplate);
132139

133-
double newY = DownVerticalOffset;
140+
double newY = downVerticalOffsetIndepent;
134141

135-
return new[] { new CustomPopupPlacement(new Point(offset.X, newY), PopupPrimaryAxis.None) };
142+
return new[] { new CustomPopupPlacement(new Point(offsetX, newY), PopupPrimaryAxis.None) };
136143
}
137144
}
138145
}

MaterialDesignThemes.Wpf/DpiHelper.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace MaterialDesignThemes.Wpf
10+
{
11+
internal static class DpiHelper
12+
{
13+
private static readonly int _dpiX;
14+
private static readonly int _dpiY;
15+
16+
private const double StandartDpiX = 96.0;
17+
private const double StandartDpiY = 96.0;
18+
19+
static DpiHelper()
20+
{
21+
var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
22+
var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);
23+
24+
_dpiX = (int)dpiXProperty.GetValue(null, null);
25+
_dpiY = (int)dpiYProperty.GetValue(null, null);
26+
}
27+
28+
public static double TransformToDeviceY(double y)
29+
{
30+
return y * _dpiY / StandartDpiY;
31+
}
32+
33+
public static double TransformToDeviceX(double x)
34+
{
35+
return x * _dpiX / StandartDpiX;
36+
}
37+
}
38+
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
<Compile Include="DialogOpenedEventHandler.cs" />
244244
<Compile Include="DialogSession.cs" />
245245
<Compile Include="DialogHost.cs" />
246+
<Compile Include="DpiHelper.cs" />
246247
<Compile Include="DrawerHost.cs" />
247248
<Compile Include="Extensions.cs" />
248249
<Compile Include="ListSortDirectionIndicator.cs" />

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.ComboBox.xaml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686

8787
<ControlTemplate x:Key="PopupContentDownTemplate" TargetType="ContentControl">
8888
<Grid MinWidth="{Binding ElementName=templateRoot, Path=ActualWidth, Converter={StaticResource MathAddConverter}, ConverterParameter=32}"
89-
Margin="6">
89+
Margin="6">
9090
<Grid.RowDefinitions>
9191
<RowDefinition Height="*" />
9292
</Grid.RowDefinitions>
@@ -98,7 +98,8 @@
9898
<BlurEffect Radius="6"/>
9999
</Border.Effect>
100100
</Border>
101-
<Grid Margin="1">
101+
<Grid Margin="1"
102+
SnapsToDevicePixels="True">
102103
<Grid.RowDefinitions>
103104
<RowDefinition Height="Auto"/>
104105
<RowDefinition Height="Auto"/>
@@ -107,35 +108,37 @@
107108
<RowDefinition Height="Auto"/>
108109
</Grid.RowDefinitions>
109110
<Rectangle Grid.Row="0"
110-
Fill="{DynamicResource MaterialDesignPaper}"
111-
Height="{StaticResource PopupTopBottomMargin}"/>
112-
111+
Fill="{DynamicResource MaterialDesignPaper}"
112+
Height="{StaticResource PopupTopBottomMargin}"/>
113+
113114
<Grid Grid.Row="1">
114115
<Grid.ColumnDefinitions>
115116
<ColumnDefinition Width="Auto"/>
116117
<ColumnDefinition Width="Auto"/>
117118
<ColumnDefinition Width="*"/>
118119
</Grid.ColumnDefinitions>
119120
<Rectangle Grid.Column="0"
120-
Width="{StaticResource PopupLeftRightMargin}"
121-
Fill="{DynamicResource MaterialDesignPaper}"/>
121+
Width="{StaticResource PopupLeftRightMargin}"
122+
Fill="{DynamicResource MaterialDesignPaper}"
123+
/>
122124
<Grid Grid.Column="1"
123-
Width="{Binding ElementName=templateRoot, Path=ActualWidth}"
124-
Height="{Binding ElementName=templateRoot, Path=ActualHeight}"/>
125+
Width="{Binding ElementName=templateRoot, Path=ActualWidth}"
126+
Height="{Binding ElementName=templateRoot, Path=ActualHeight}"/>
125127
<Rectangle Grid.Column="2"
126-
MinWidth="{StaticResource PopupLeftRightMargin}"
127-
Fill="{DynamicResource MaterialDesignPaper}"/>
128+
MinWidth="{StaticResource PopupLeftRightMargin}"
129+
Fill="{DynamicResource MaterialDesignPaper}"
130+
/>
128131
</Grid>
129132

130133
<Rectangle Grid.Row="2"
131-
Fill="{DynamicResource MaterialDesignPaper}"
132-
Height="{StaticResource PopupContentPresenterExtend}"/>
134+
Fill="{DynamicResource MaterialDesignPaper}"
135+
Height="{StaticResource PopupContentPresenterExtend}"/>
133136

134137
<ContentPresenter Grid.Row="3"/>
135138

136139
<Rectangle Grid.Row="4"
137-
Height="{StaticResource PopupTopBottomMargin}"
138-
Fill="{DynamicResource MaterialDesignPaper}" />
140+
Height="{StaticResource PopupTopBottomMargin}"
141+
Fill="{DynamicResource MaterialDesignPaper}" />
139142
</Grid>
140143
</Grid>
141144
</ControlTemplate>
@@ -385,8 +388,7 @@
385388

386389
<ControlTemplate x:Key="MaterialDesignComboBoxTemplate" TargetType="{x:Type ComboBox}">
387390
<Grid x:Name="templateRoot"
388-
SnapsToDevicePixels="True"
389-
UseLayoutRounding="True">
391+
SnapsToDevicePixels="True">
390392
<Grid.ColumnDefinitions>
391393
<ColumnDefinition Width="*" />
392394
<ColumnDefinition Width="0" MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" />
@@ -434,9 +436,8 @@
434436
HorizontalOffset="-11"
435437
IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
436438
PlacementTarget="{Binding ElementName=templateRoot}"
437-
SnapsToDevicePixels="True"
438-
UseLayoutRounding="True"
439439
Placement="Custom"
440+
SnapsToDevicePixels="True"
440441
PopupAnimation="Fade"
441442
VerticalOffset="0"
442443
DefaultVerticalOffset="5"
@@ -486,7 +487,8 @@
486487
</ControlTemplate>
487488

488489
<ControlTemplate x:Key="MaterialDesignFloatingHintComboBoxTemplate" TargetType="{x:Type ComboBox}">
489-
<Grid x:Name="templateRoot">
490+
<Grid x:Name="templateRoot"
491+
SnapsToDevicePixels="True">
490492
<VisualStateManager.VisualStateGroups>
491493
<VisualStateGroup x:Name="MaterialDesignStates">
492494
<VisualStateGroup.Transitions>
@@ -557,9 +559,7 @@
557559
</VisualState>
558560
</VisualStateGroup>
559561
</VisualStateManager.VisualStateGroups>
560-
<Grid Margin="0 12 0 0"
561-
SnapsToDevicePixels="True"
562-
UseLayoutRounding="True">
562+
<Grid Margin="0 12 0 0">
563563
<Grid.ColumnDefinitions>
564564
<ColumnDefinition Width="*" />
565565
<ColumnDefinition Width="0" MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" />

0 commit comments

Comments
 (0)