From 553fbb95fc2cc4482642140d2568cb2fcfef1887 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 26 Mar 2016 15:01:53 +0100 Subject: [PATCH] Removed RenderTargetDecorator. Handle scaling in D2D's RenderTarget itself. --- .../Platform/PlatformManager.cs | 36 +----------- src/Windows/Perspex.Direct2D1/RenderTarget.cs | 58 ++++++++++++++----- src/Windows/Perspex.Win32/WindowImpl.cs | 9 +-- 3 files changed, 53 insertions(+), 50 deletions(-) diff --git a/src/Perspex.Controls/Platform/PlatformManager.cs b/src/Perspex.Controls/Platform/PlatformManager.cs index b9e7aef72c..2f0695aaa5 100644 --- a/src/Perspex.Controls/Platform/PlatformManager.cs +++ b/src/Perspex.Controls/Platform/PlatformManager.cs @@ -1,7 +1,5 @@ using System; using System.Reactive.Disposables; -using Perspex.Input; -using Perspex.Input.Raw; using Perspex.Media; using Perspex.Platform; @@ -9,7 +7,6 @@ namespace Perspex.Controls.Platform { public static partial class PlatformManager { - static IPlatformSettings GetSettings() => PerspexLocator.Current.GetService(); @@ -18,9 +15,9 @@ namespace Perspex.Controls.Platform public static IRenderTarget CreateRenderTarget(ITopLevelImpl window) { - return - new RenderTargetDecorator( - PerspexLocator.Current.GetService().CreateRenderer(window.Handle), window); + return PerspexLocator.Current + .GetService() + .CreateRenderer(window.Handle); } public static IDisposable DesignerMode() @@ -34,33 +31,6 @@ namespace Perspex.Controls.Platform _designerScalingFactor = factor; } - class RenderTargetDecorator : IRenderTarget - { - private readonly IRenderTarget _target; - private readonly ITopLevelImpl _window; - - public RenderTargetDecorator(IRenderTarget target, ITopLevelImpl window) - { - _target = target; - _window = window; - } - - public void Dispose() => _target.Dispose(); - - public DrawingContext CreateDrawingContext() - { - var cs = _window.ClientSize; - var ctx = _target.CreateDrawingContext(); - var factor = _window.Scaling; - if (factor != 1) - { - ctx.PushPostTransform(Matrix.CreateScale(factor, factor)); - ctx.PushTransformContainer(); - } - return ctx; - } - } - public static IWindowImpl CreateWindow() { var platform = PerspexLocator.Current.GetService(); diff --git a/src/Windows/Perspex.Direct2D1/RenderTarget.cs b/src/Windows/Perspex.Direct2D1/RenderTarget.cs index c835fc03e6..0d66ba99bd 100644 --- a/src/Windows/Perspex.Direct2D1/RenderTarget.cs +++ b/src/Windows/Perspex.Direct2D1/RenderTarget.cs @@ -2,10 +2,7 @@ // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; -using Perspex.Direct2D1.Media; -using Perspex.Media; using Perspex.Platform; -using Perspex.Rendering; using Perspex.Win32.Interop; using SharpDX; using SharpDX.Direct2D1; @@ -24,8 +21,6 @@ namespace Perspex.Direct2D1 /// private readonly SharpDX.Direct2D1.RenderTarget _renderTarget; - - /// /// Initializes a new instance of the class. /// @@ -53,13 +48,6 @@ namespace Perspex.Direct2D1 hwndProperties); } - Size2 GetWindowSize() - { - UnmanagedMethods.RECT rc; - UnmanagedMethods.GetClientRect(_hwnd, out rc); - return new Size2(rc.right - rc.left, rc.bottom - rc.top); - } - /// /// Initializes a new instance of the class. /// @@ -94,19 +82,63 @@ namespace Perspex.Direct2D1 public DrawingContext CreateDrawingContext() { var window = _renderTarget as WindowRenderTarget; + var factor = 1.0; + if (window != null) { var size = GetWindowSize(); + factor = GetWindowScaling(); + if (size != _savedSize) + { window.Resize(_savedSize = size); + } + } + + var ctx = new DrawingContext(new Media.DrawingContext(_renderTarget, DirectWriteFactory)); + + if (factor != 1) + { + ctx.PushPostTransform(Matrix.CreateScale(factor, factor)); + ctx.PushTransformContainer(); } - return new DrawingContext(new Media.DrawingContext(_renderTarget, DirectWriteFactory)); + return ctx; } public void Dispose() { _renderTarget.Dispose(); } + + private double GetWindowScaling() + { + if (UnmanagedMethods.ShCoreAvailable) + { + uint dpix, dpiy; + + var monitor = UnmanagedMethods.MonitorFromWindow( + _hwnd, + UnmanagedMethods.MONITOR.MONITOR_DEFAULTTONEAREST); + + if (UnmanagedMethods.GetDpiForMonitor( + monitor, + UnmanagedMethods.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI, + out dpix, + out dpiy) == 0) + { + return dpix / 96.0; + } + } + + return 1; + } + + private Size2 GetWindowSize() + { + UnmanagedMethods.RECT rc; + UnmanagedMethods.GetClientRect(_hwnd, out rc); + return new Size2(rc.right - rc.left, rc.bottom - rc.top); + } } } diff --git a/src/Windows/Perspex.Win32/WindowImpl.cs b/src/Windows/Perspex.Win32/WindowImpl.cs index c3e694e23c..c30aed9aa5 100644 --- a/src/Windows/Perspex.Win32/WindowImpl.cs +++ b/src/Windows/Perspex.Win32/WindowImpl.cs @@ -608,13 +608,14 @@ namespace Perspex.Win32 Handle = new PlatformHandle(_hwnd, PlatformConstants.WindowHandleType); - var monitor = UnmanagedMethods.MonitorFromWindow( - _hwnd, - UnmanagedMethods.MONITOR.MONITOR_DEFAULTTONEAREST); - if (UnmanagedMethods.ShCoreAvailable) { uint dpix, dpiy; + + var monitor = UnmanagedMethods.MonitorFromWindow( + _hwnd, + UnmanagedMethods.MONITOR.MONITOR_DEFAULTTONEAREST); + if (UnmanagedMethods.GetDpiForMonitor( monitor, UnmanagedMethods.MONITOR_DPI_TYPE.MDT_EFFECTIVE_DPI,