diff --git a/Perspex.Direct2D1/Direct2D1Platform.cs b/Perspex.Direct2D1/Direct2D1Platform.cs index 5c2fd912dc..c7c5b5e263 100644 --- a/Perspex.Direct2D1/Direct2D1Platform.cs +++ b/Perspex.Direct2D1/Direct2D1Platform.cs @@ -6,26 +6,42 @@ namespace Perspex.Direct2D1 { + using System; using Perspex.Direct2D1.Media; - using Perspex.Media; using Perspex.Platform; using Splat; - public static class Direct2D1Platform + public class Direct2D1Platform : IPlatformFactory { + private static Direct2D1Platform instance = new Direct2D1Platform(); + + private static SharpDX.Direct2D1.Factory d2d1Factory = new SharpDX.Direct2D1.Factory(); + + private static SharpDX.DirectWrite.Factory dwFactory = new SharpDX.DirectWrite.Factory(); + + private static TextService textService = new TextService(dwFactory); + public static void Initialize() { - SharpDX.Direct2D1.Factory d2d1Factory = new SharpDX.Direct2D1.Factory(); - SharpDX.DirectWrite.Factory dwFactory = new SharpDX.DirectWrite.Factory(); - TextService textService = new TextService(dwFactory); - var locator = Locator.CurrentMutable; locator.Register(() => d2d1Factory, typeof(SharpDX.Direct2D1.Factory)); locator.Register(() => dwFactory, typeof(SharpDX.DirectWrite.Factory)); - locator.Register(() => textService, typeof(ITextService)); + locator.Register(() => instance, typeof(IPlatformFactory)); + } - locator.Register(() => new Renderer(), typeof(IRenderer)); - locator.Register(() => new StreamGeometryImpl(), typeof(IStreamGeometryImpl)); + public IRenderer CreateRenderer(IntPtr handle, double width, double height) + { + return new Renderer(handle, width, height); + } + + public IStreamGeometryImpl CreateStreamGeometry() + { + return new StreamGeometryImpl(); + } + + public ITextService GetTextService() + { + return textService; } } } diff --git a/Perspex.Direct2D1/Renderer.cs b/Perspex.Direct2D1/Renderer.cs index f0f936fc40..337fb294f6 100644 --- a/Perspex.Direct2D1/Renderer.cs +++ b/Perspex.Direct2D1/Renderer.cs @@ -31,13 +31,8 @@ namespace Perspex.Direct2D1 /// The window handle. /// The width of the window. /// The height of the window. - public void Initialize(IntPtr hwnd, double width, double height) + public Renderer(IntPtr hwnd, double width, double height) { - if (this.renderTarget != null) - { - throw new InvalidOperationException("Cannot initialize Renderer more than once."); - } - this.Direct2DFactory = Locator.Current.GetService(); this.DirectWriteFactory = Locator.Current.GetService(); diff --git a/Perspex.Windows/Window.cs b/Perspex.Windows/Window.cs index 8a65c93720..527447bd56 100644 --- a/Perspex.Windows/Window.cs +++ b/Perspex.Windows/Window.cs @@ -41,11 +41,12 @@ namespace Perspex.Windows public Window() { + IPlatformFactory factory = Locator.Current.GetService(); + this.CreateWindow(); Size clientSize = this.ClientSize; this.LayoutManager = new LayoutManager(); - this.renderer = Locator.Current.GetService(); - this.renderer.Initialize(this.Handle, (int)clientSize.Width, (int)clientSize.Height); + this.renderer = factory.CreateRenderer(this.Handle, (int)clientSize.Width, (int)clientSize.Height); this.inputManager = Locator.Current.GetService(); this.Template = ControlTemplate.Create(this.DefaultTemplate); diff --git a/Perspex/Application.cs b/Perspex/Application.cs index 96cbadbb3e..18a300c086 100644 --- a/Perspex/Application.cs +++ b/Perspex/Application.cs @@ -6,9 +6,7 @@ namespace Perspex { - using System.Reflection; using Perspex.Input; - using Perspex.Platform; using Perspex.Styling; using Splat; @@ -19,6 +17,7 @@ namespace Perspex public Application() { Current = this; + this.InputManager = new InputManager(); } public static Application Current @@ -27,6 +26,12 @@ namespace Perspex private set; } + public InputManager InputManager + { + get; + private set; + } + public Styles Styles { get @@ -45,10 +50,11 @@ namespace Perspex } } - public static void RegisterPortableServices() + public void RegisterServices() { - InputManager inputManager = new InputManager(); - Locator.CurrentMutable.Register(() => inputManager, typeof(IInputManager)); + Styler styler = new Styler(); + Locator.CurrentMutable.Register(() => this.InputManager, typeof(IInputManager)); + Locator.CurrentMutable.Register(() => styler, typeof(IStyler)); } } } diff --git a/Perspex/Controls/TextBlock.cs b/Perspex/Controls/TextBlock.cs index 93758091f1..98848c479b 100644 --- a/Perspex/Controls/TextBlock.cs +++ b/Perspex/Controls/TextBlock.cs @@ -71,7 +71,8 @@ namespace Perspex.Controls { if (this.Visibility != Visibility.Collapsed) { - ITextService service = Locator.Current.GetService(); + IPlatformFactory factory = Locator.Current.GetService(); + ITextService service = factory.GetTextService(); if (!string.IsNullOrEmpty(this.Text)) { diff --git a/Perspex/Media/RectangleGeometry.cs b/Perspex/Media/RectangleGeometry.cs index d4f13d56a8..9ebc3d39d4 100644 --- a/Perspex/Media/RectangleGeometry.cs +++ b/Perspex/Media/RectangleGeometry.cs @@ -13,7 +13,8 @@ namespace Perspex.Media { public RectangleGeometry(Rect rect) { - IStreamGeometryImpl impl = Locator.Current.GetService(); + IPlatformFactory factory = Locator.Current.GetService(); + IStreamGeometryImpl impl = factory.CreateStreamGeometry(); using (IStreamGeometryContextImpl context = impl.Open()) { diff --git a/Perspex/Media/StreamGeometry.cs b/Perspex/Media/StreamGeometry.cs index 8e114f049d..b249fdbd8f 100644 --- a/Perspex/Media/StreamGeometry.cs +++ b/Perspex/Media/StreamGeometry.cs @@ -13,7 +13,8 @@ namespace Perspex.Media { public StreamGeometry() { - this.PlatformImpl = Locator.Current.GetService(); + IPlatformFactory factory = Locator.Current.GetService(); + this.PlatformImpl = factory.CreateStreamGeometry(); } public override Rect Bounds diff --git a/Perspex/Perspex.csproj b/Perspex/Perspex.csproj index 80527bacfd..65259fd5bd 100644 --- a/Perspex/Perspex.csproj +++ b/Perspex/Perspex.csproj @@ -90,6 +90,7 @@ + diff --git a/Perspex/Platform/IPlatformFactory.cs b/Perspex/Platform/IPlatformFactory.cs new file mode 100644 index 0000000000..26ec83855b --- /dev/null +++ b/Perspex/Platform/IPlatformFactory.cs @@ -0,0 +1,19 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Platform +{ + using System; + + public interface IPlatformFactory + { + IStreamGeometryImpl CreateStreamGeometry(); + + IRenderer CreateRenderer(IntPtr handle, double width, double height); + + ITextService GetTextService(); + } +} diff --git a/Perspex/Platform/IRenderer.cs b/Perspex/Platform/IRenderer.cs index 9d69b77ed8..e251fe0f6e 100644 --- a/Perspex/Platform/IRenderer.cs +++ b/Perspex/Platform/IRenderer.cs @@ -10,17 +10,6 @@ namespace Perspex.Platform public interface IRenderer { - /// - /// Initializes the renderer to draw to the specified handle. - /// - /// The window etc handle - /// The initial viewport width. - /// The initial viewport height. - /// - /// TODO: This probably should be somewhere else... - /// - void Initialize(IntPtr handle, double width, double height); - /// /// Renders the specified visual. /// diff --git a/TestApplication/App.cs b/TestApplication/App.cs index ff715409fd..9826cb1c92 100644 --- a/TestApplication/App.cs +++ b/TestApplication/App.cs @@ -8,7 +8,7 @@ { public App() { - RegisterPortableServices(); + this.RegisterServices(); Direct2D1Platform.Initialize(); this.Styles = new DefaultTheme(); } diff --git a/TestApplication/Program.cs b/TestApplication/Program.cs index 014a2e59ab..482b903d0f 100644 --- a/TestApplication/Program.cs +++ b/TestApplication/Program.cs @@ -18,28 +18,10 @@ using Splat; namespace TestApplication { - class TestLogger : ILogger - { - public LogLevel Level - { - get; - set; - } - - public void Write(string message, LogLevel logLevel) - { - if ((int)logLevel < (int)Level) return; - System.Diagnostics.Debug.WriteLine(message); - } - } - class Program { static void Main(string[] args) { - Locator.CurrentMutable.Register(() => new Styler(), typeof(IStyler)); - Locator.CurrentMutable.Register(() => new TestLogger(), typeof(ILogger)); - App application = new App(); Window window = new Window