From d460ec9fd0f646797821f72d5133c2fb7ec6ffd5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 2 Feb 2016 01:07:31 +0100 Subject: [PATCH] Implemented basic layout scaling. Maybe works... I don't have a high DPI monitor to test on... --- src/Perspex.Input/Perspex.Input.csproj | 1 - src/Perspex.Layout/Layoutable.cs | 33 ++++++++++++------- .../Perspex.SceneGraph.csproj | 1 + src/Windows/Perspex.Win32/Win32Platform.cs | 2 +- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Perspex.Input/Perspex.Input.csproj b/src/Perspex.Input/Perspex.Input.csproj index babd0e034b..4cd3ce4363 100644 --- a/src/Perspex.Input/Perspex.Input.csproj +++ b/src/Perspex.Input/Perspex.Input.csproj @@ -100,7 +100,6 @@ - diff --git a/src/Perspex.Layout/Layoutable.cs b/src/Perspex.Layout/Layoutable.cs index 0866b05659..ce47d68e4c 100644 --- a/src/Perspex.Layout/Layoutable.cs +++ b/src/Perspex.Layout/Layoutable.cs @@ -3,6 +3,7 @@ using System; using System.Linq; +using Perspex.Platform; using Perspex.VisualTree; using Serilog; using Serilog.Core.Enrichers; @@ -466,6 +467,7 @@ namespace Perspex.Layout .Deflate(margin); var measured = MeasureOverride(constrained); + var width = measured.Width; var height = measured.Height; @@ -485,6 +487,13 @@ namespace Perspex.Layout height = Math.Min(height, MaxHeight); height = Math.Max(height, MinHeight); + if (UseLayoutRounding) + { + var scale = GetLayoutScale(); + width = Math.Ceiling(width * scale) / scale; + height = Math.Ceiling(height * scale) / scale; + } + return NonNegative(new Size(width, height).Inflate(margin)); } else @@ -510,12 +519,6 @@ namespace Perspex.Layout height = Math.Max(height, child.DesiredSize.Height); } - if (UseLayoutRounding) - { - width = Math.Ceiling(width); - height = Math.Ceiling(height); - } - return new Size(width, height); } @@ -537,6 +540,7 @@ namespace Perspex.Layout Math.Max(0, finalRect.Width - Margin.Left - Margin.Right), Math.Max(0, finalRect.Height - Margin.Top - Margin.Bottom)); var size = sizeMinusMargins; + var scale = GetLayoutScale(); if (HorizontalAlignment != HorizontalAlignment.Stretch) { @@ -553,11 +557,11 @@ namespace Perspex.Layout if (UseLayoutRounding) { size = new Size( - Math.Ceiling(size.Width), - Math.Ceiling(size.Height)); + Math.Ceiling(size.Width * scale) / scale, + Math.Ceiling(size.Height * scale) / scale); sizeMinusMargins = new Size( - Math.Ceiling(sizeMinusMargins.Width), - Math.Ceiling(sizeMinusMargins.Height)); + Math.Ceiling(sizeMinusMargins.Width * scale) / scale, + Math.Ceiling(sizeMinusMargins.Height * scale) / scale); } size = ArrangeOverride(size).Constrain(size); @@ -586,8 +590,8 @@ namespace Perspex.Layout if (UseLayoutRounding) { - originX = Math.Floor(originX); - originY = Math.Floor(originY); + originX = Math.Floor(originX * scale) / scale; + originY = Math.Floor(originY * scale) / scale; } Bounds = new Rect(originX, originY, size.Width, size.Height); @@ -666,5 +670,10 @@ namespace Perspex.Layout { return new Size(Math.Max(size.Width, 0), Math.Max(size.Height, 0)); } + + private static double GetLayoutScale() + { + return PerspexLocator.Current.GetService()?.LayoutScalingFactor ?? 1.0; + } } } diff --git a/src/Perspex.SceneGraph/Perspex.SceneGraph.csproj b/src/Perspex.SceneGraph/Perspex.SceneGraph.csproj index 6578b9c7b5..af4e85061d 100644 --- a/src/Perspex.SceneGraph/Perspex.SceneGraph.csproj +++ b/src/Perspex.SceneGraph/Perspex.SceneGraph.csproj @@ -106,6 +106,7 @@ + diff --git a/src/Windows/Perspex.Win32/Win32Platform.cs b/src/Windows/Perspex.Win32/Win32Platform.cs index 987127631e..d23a74f911 100644 --- a/src/Windows/Perspex.Win32/Win32Platform.cs +++ b/src/Windows/Perspex.Win32/Win32Platform.cs @@ -188,7 +188,7 @@ namespace Perspex.Win32 UnmanagedMethods.GetDpiForMonitor(hMonitor, UnmanagedMethods.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, out dpix, out dpiy); // Set scale based on x DPI - _scale = dpix / 100.0; + _scale = dpix / 96.0; } } }