Browse Source

Moved platform impls into IPlatformFactory.

pull/4/head
Steven Kirk 12 years ago
parent
commit
971db93002
  1. 34
      Perspex.Direct2D1/Direct2D1Platform.cs
  2. 7
      Perspex.Direct2D1/Renderer.cs
  3. 5
      Perspex.Windows/Window.cs
  4. 16
      Perspex/Application.cs
  5. 3
      Perspex/Controls/TextBlock.cs
  6. 3
      Perspex/Media/RectangleGeometry.cs
  7. 3
      Perspex/Media/StreamGeometry.cs
  8. 1
      Perspex/Perspex.csproj
  9. 19
      Perspex/Platform/IPlatformFactory.cs
  10. 11
      Perspex/Platform/IRenderer.cs
  11. 2
      TestApplication/App.cs
  12. 18
      TestApplication/Program.cs

34
Perspex.Direct2D1/Direct2D1Platform.cs

@ -6,26 +6,42 @@
namespace Perspex.Direct2D1 namespace Perspex.Direct2D1
{ {
using System;
using Perspex.Direct2D1.Media; using Perspex.Direct2D1.Media;
using Perspex.Media;
using Perspex.Platform; using Perspex.Platform;
using Splat; 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() 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; var locator = Locator.CurrentMutable;
locator.Register(() => d2d1Factory, typeof(SharpDX.Direct2D1.Factory)); locator.Register(() => d2d1Factory, typeof(SharpDX.Direct2D1.Factory));
locator.Register(() => dwFactory, typeof(SharpDX.DirectWrite.Factory)); locator.Register(() => dwFactory, typeof(SharpDX.DirectWrite.Factory));
locator.Register(() => textService, typeof(ITextService)); locator.Register(() => instance, typeof(IPlatformFactory));
}
locator.Register(() => new Renderer(), typeof(IRenderer)); public IRenderer CreateRenderer(IntPtr handle, double width, double height)
locator.Register(() => new StreamGeometryImpl(), typeof(IStreamGeometryImpl)); {
return new Renderer(handle, width, height);
}
public IStreamGeometryImpl CreateStreamGeometry()
{
return new StreamGeometryImpl();
}
public ITextService GetTextService()
{
return textService;
} }
} }
} }

7
Perspex.Direct2D1/Renderer.cs

@ -31,13 +31,8 @@ namespace Perspex.Direct2D1
/// <param name="hwnd">The window handle.</param> /// <param name="hwnd">The window handle.</param>
/// <param name="width">The width of the window.</param> /// <param name="width">The width of the window.</param>
/// <param name="height">The height of the window.</param> /// <param name="height">The height of the window.</param>
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<Factory>(); this.Direct2DFactory = Locator.Current.GetService<Factory>();
this.DirectWriteFactory = Locator.Current.GetService<DwFactory>(); this.DirectWriteFactory = Locator.Current.GetService<DwFactory>();

5
Perspex.Windows/Window.cs

@ -41,11 +41,12 @@ namespace Perspex.Windows
public Window() public Window()
{ {
IPlatformFactory factory = Locator.Current.GetService<IPlatformFactory>();
this.CreateWindow(); this.CreateWindow();
Size clientSize = this.ClientSize; Size clientSize = this.ClientSize;
this.LayoutManager = new LayoutManager(); this.LayoutManager = new LayoutManager();
this.renderer = Locator.Current.GetService<IRenderer>(); this.renderer = factory.CreateRenderer(this.Handle, (int)clientSize.Width, (int)clientSize.Height);
this.renderer.Initialize(this.Handle, (int)clientSize.Width, (int)clientSize.Height);
this.inputManager = Locator.Current.GetService<IInputManager>(); this.inputManager = Locator.Current.GetService<IInputManager>();
this.Template = ControlTemplate.Create<Window>(this.DefaultTemplate); this.Template = ControlTemplate.Create<Window>(this.DefaultTemplate);

16
Perspex/Application.cs

@ -6,9 +6,7 @@
namespace Perspex namespace Perspex
{ {
using System.Reflection;
using Perspex.Input; using Perspex.Input;
using Perspex.Platform;
using Perspex.Styling; using Perspex.Styling;
using Splat; using Splat;
@ -19,6 +17,7 @@ namespace Perspex
public Application() public Application()
{ {
Current = this; Current = this;
this.InputManager = new InputManager();
} }
public static Application Current public static Application Current
@ -27,6 +26,12 @@ namespace Perspex
private set; private set;
} }
public InputManager InputManager
{
get;
private set;
}
public Styles Styles public Styles Styles
{ {
get get
@ -45,10 +50,11 @@ namespace Perspex
} }
} }
public static void RegisterPortableServices() public void RegisterServices()
{ {
InputManager inputManager = new InputManager(); Styler styler = new Styler();
Locator.CurrentMutable.Register(() => inputManager, typeof(IInputManager)); Locator.CurrentMutable.Register(() => this.InputManager, typeof(IInputManager));
Locator.CurrentMutable.Register(() => styler, typeof(IStyler));
} }
} }
} }

3
Perspex/Controls/TextBlock.cs

@ -71,7 +71,8 @@ namespace Perspex.Controls
{ {
if (this.Visibility != Visibility.Collapsed) if (this.Visibility != Visibility.Collapsed)
{ {
ITextService service = Locator.Current.GetService<ITextService>(); IPlatformFactory factory = Locator.Current.GetService<IPlatformFactory>();
ITextService service = factory.GetTextService();
if (!string.IsNullOrEmpty(this.Text)) if (!string.IsNullOrEmpty(this.Text))
{ {

3
Perspex/Media/RectangleGeometry.cs

@ -13,7 +13,8 @@ namespace Perspex.Media
{ {
public RectangleGeometry(Rect rect) public RectangleGeometry(Rect rect)
{ {
IStreamGeometryImpl impl = Locator.Current.GetService<IStreamGeometryImpl>(); IPlatformFactory factory = Locator.Current.GetService<IPlatformFactory>();
IStreamGeometryImpl impl = factory.CreateStreamGeometry();
using (IStreamGeometryContextImpl context = impl.Open()) using (IStreamGeometryContextImpl context = impl.Open())
{ {

3
Perspex/Media/StreamGeometry.cs

@ -13,7 +13,8 @@ namespace Perspex.Media
{ {
public StreamGeometry() public StreamGeometry()
{ {
this.PlatformImpl = Locator.Current.GetService<IStreamGeometryImpl>(); IPlatformFactory factory = Locator.Current.GetService<IPlatformFactory>();
this.PlatformImpl = factory.CreateStreamGeometry();
} }
public override Rect Bounds public override Rect Bounds

1
Perspex/Perspex.csproj

@ -90,6 +90,7 @@
<Compile Include="Input\Raw\RawInputEventArgs.cs" /> <Compile Include="Input\Raw\RawInputEventArgs.cs" />
<Compile Include="Input\Raw\RawMouseEventArgs.cs" /> <Compile Include="Input\Raw\RawMouseEventArgs.cs" />
<Compile Include="Media\Brushes.cs" /> <Compile Include="Media\Brushes.cs" />
<Compile Include="Platform\IPlatformFactory.cs" />
<Compile Include="Platform\IStreamGeometryContextImpl.cs" /> <Compile Include="Platform\IStreamGeometryContextImpl.cs" />
<Compile Include="Platform\IGeometryImpl.cs" /> <Compile Include="Platform\IGeometryImpl.cs" />
<Compile Include="Platform\IStreamGeometryImpl.cs" /> <Compile Include="Platform\IStreamGeometryImpl.cs" />

19
Perspex/Platform/IPlatformFactory.cs

@ -0,0 +1,19 @@
// -----------------------------------------------------------------------
// <copyright file="IPlatformFactory.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Platform
{
using System;
public interface IPlatformFactory
{
IStreamGeometryImpl CreateStreamGeometry();
IRenderer CreateRenderer(IntPtr handle, double width, double height);
ITextService GetTextService();
}
}

11
Perspex/Platform/IRenderer.cs

@ -10,17 +10,6 @@ namespace Perspex.Platform
public interface IRenderer public interface IRenderer
{ {
/// <summary>
/// Initializes the renderer to draw to the specified handle.
/// </summary>
/// <param name="handle">The window etc handle</param>
/// <param name="width">The initial viewport width.</param>
/// <param name="height">The initial viewport height.</param>
/// <remarks>
/// TODO: This probably should be somewhere else...
/// </remarks>
void Initialize(IntPtr handle, double width, double height);
/// <summary> /// <summary>
/// Renders the specified visual. /// Renders the specified visual.
/// </summary> /// </summary>

2
TestApplication/App.cs

@ -8,7 +8,7 @@
{ {
public App() public App()
{ {
RegisterPortableServices(); this.RegisterServices();
Direct2D1Platform.Initialize(); Direct2D1Platform.Initialize();
this.Styles = new DefaultTheme(); this.Styles = new DefaultTheme();
} }

18
TestApplication/Program.cs

@ -18,28 +18,10 @@ using Splat;
namespace TestApplication 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 class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Locator.CurrentMutable.Register(() => new Styler(), typeof(IStyler));
Locator.CurrentMutable.Register(() => new TestLogger(), typeof(ILogger));
App application = new App(); App application = new App();
Window window = new Window Window window = new Window

Loading…
Cancel
Save