Bläddra i källkod

Added UsePlatformDetect

Nikita Tsukanov 9 år sedan
förälder
incheckning
ee37ab8df4

+ 1 - 2
samples/BindingTest/App.xaml.cs

@@ -19,8 +19,7 @@ namespace BindingTest
             InitializeLogging();
             InitializeLogging();
 
 
             AppBuilder.Configure<App>()
             AppBuilder.Configure<App>()
-                .UseWin32()
-                .UseDirect2D1()
+                .UsePlatformDetect()
                 .Start<MainWindow>();
                 .Start<MainWindow>();
         }
         }
 
 

+ 1 - 2
samples/ControlCatalog.Desktop/Program.cs

@@ -17,8 +17,7 @@ namespace ControlCatalog
             // TODO: Make this work with GTK/Skia/Cairo depending on command-line args
             // TODO: Make this work with GTK/Skia/Cairo depending on command-line args
             // again.
             // again.
             AppBuilder.Configure<App>()
             AppBuilder.Configure<App>()
-                .UseWin32()
-                .UseDirect2D1()
+                .UsePlatformDetect()
                 .Start<MainWindow>();
                 .Start<MainWindow>();
         }
         }
 
 

+ 1 - 2
samples/TestApplication/Program.cs

@@ -35,8 +35,7 @@ namespace TestApplication
             var app = new App();
             var app = new App();
 
 
             AppBuilder.Configure(app)
             AppBuilder.Configure(app)
-                .UseWin32()
-                .UseDirect2D1()
+                .UsePlatformDetect()
                 .SetupWithoutStarting();
                 .SetupWithoutStarting();
 
 
             app.Run();
             app.Run();

+ 1 - 2
samples/XamlTestApplication/Program.cs

@@ -21,8 +21,7 @@ namespace XamlTestApplication
             InitializeLogging();
             InitializeLogging();
 
 
             AppBuilder.Configure<XamlTestApp>()
             AppBuilder.Configure<XamlTestApp>()
-                .UseWin32()
-                .UseDirect2D1()
+                .UsePlatformDetect()
                 .Start<Views.MainWindow>();
                 .Start<Views.MainWindow>();
         }
         }
 
 

+ 0 - 1
src/Android/Avalonia.Android/AndroidPlatform.cs

@@ -38,7 +38,6 @@ namespace Avalonia.Android
                 .Bind<IWindowingPlatform>().ToConstant(this);
                 .Bind<IWindowingPlatform>().ToConstant(this);
 
 
             SkiaPlatform.Initialize();
             SkiaPlatform.Initialize();
-            Application.RegisterPlatformCallback(() => { });
 
 
             _scalingFactor = global::Android.App.Application.Context.Resources.DisplayMetrics.ScaledDensity;
             _scalingFactor = global::Android.App.Application.Context.Resources.DisplayMetrics.ScaledDensity;
 
 

+ 44 - 2
src/Avalonia.Controls/AppBuilder.cs

@@ -2,6 +2,7 @@
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
 
 using System;
 using System;
+using System.Reflection;
 
 
 namespace Avalonia.Controls
 namespace Avalonia.Controls
 {
 {
@@ -98,23 +99,64 @@ namespace Avalonia.Controls
         /// </summary>
         /// </summary>
         /// <param name="initializer">The method to call to initialize the windowing subsystem.</param>
         /// <param name="initializer">The method to call to initialize the windowing subsystem.</param>
         /// <returns>An <see cref="AppBuilder"/> instance.</returns>
         /// <returns>An <see cref="AppBuilder"/> instance.</returns>
-        public AppBuilder WithWindowingSubsystem(Action initializer)
+        public AppBuilder UseWindowingSubsystem(Action initializer)
         {
         {
             WindowingSubsystem = initializer;
             WindowingSubsystem = initializer;
             return this;
             return this;
         }
         }
 
 
+        /// <summary>
+        /// Specifies a windowing subsystem to use.
+        /// </summary>
+        /// <param name="dll">The dll in which to look for subsystem.</param>
+        /// <returns>An <see cref="AppBuilder"/> instance.</returns>
+        public AppBuilder UseWindowingSubsystem(string dll) => UseWindowingSubsystem(GetInitializer(dll));
+
         /// <summary>
         /// <summary>
         /// Specifies a rendering subsystem to use.
         /// Specifies a rendering subsystem to use.
         /// </summary>
         /// </summary>
         /// <param name="initializer">The method to call to initialize the rendering subsystem.</param>
         /// <param name="initializer">The method to call to initialize the rendering subsystem.</param>
         /// <returns>An <see cref="AppBuilder"/> instance.</returns>
         /// <returns>An <see cref="AppBuilder"/> instance.</returns>
-        public AppBuilder WithRenderingSubsystem(Action initializer)
+        public AppBuilder UseRenderingSubsystem(Action initializer)
         {
         {
             RenderingSubsystem = initializer;
             RenderingSubsystem = initializer;
             return this;
             return this;
         }
         }
 
 
+        /// <summary>
+        /// Specifies a rendering subsystem to use.
+        /// </summary>
+        /// <param name="dll">The dll in which to look for subsystem.</param>
+        /// <returns>An <see cref="AppBuilder"/> instance.</returns>
+        public AppBuilder UseRenderingSubsystem(string dll) => UseRenderingSubsystem(GetInitializer(dll));
+
+        static Action GetInitializer(string assemblyName) => () =>
+        {
+            var assembly = Assembly.Load(new AssemblyName(assemblyName));
+            var platformClassName = assemblyName.Replace("Avalonia.", string.Empty) + "Platform";
+            var platformClassFullName = assemblyName + "." + platformClassName;
+            var platformClass = assembly.GetType(platformClassFullName);
+            var init = platformClass.GetRuntimeMethod("Initialize", new Type[0]);
+            init.Invoke(null, null);
+        };
+
+        public AppBuilder UsePlatformDetect()
+        {
+            var platformId = (int)
+                ((dynamic) Type.GetType("System.Environment").GetRuntimeProperty("OSVersion").GetValue(null)).Platform;
+            if (platformId == 4 || platformId == 6)
+            {
+                UseRenderingSubsystem("Avalonia.Cairo");
+                UseWindowingSubsystem("Avalonia.Gtk");
+            }
+            else
+            {
+                UseRenderingSubsystem("Avalonia.Direct2D1");
+                UseWindowingSubsystem("Avalonia.Win32");
+            }
+            return this;
+        }
+
         /// <summary>
         /// <summary>
         /// Sets up the platform-speciic services for the <see cref="Application"/>.
         /// Sets up the platform-speciic services for the <see cref="Application"/>.
         /// </summary>
         /// </summary>

+ 0 - 51
src/Avalonia.Controls/Application.cs

@@ -26,16 +26,12 @@ namespace Avalonia
     /// - A global set of <see cref="Styles"/>.
     /// - A global set of <see cref="Styles"/>.
     /// - A <see cref="FocusManager"/>.
     /// - A <see cref="FocusManager"/>.
     /// - An <see cref="InputManager"/>.
     /// - An <see cref="InputManager"/>.
-    /// - Loads and initializes rendering and windowing subsystems with
-    /// <see cref="InitializeSubsystems(int)"/> and <see cref="InitializeSubsystem(string)"/>.
     /// - Registers services needed by the rest of Avalonia in the <see cref="RegisterServices"/>
     /// - Registers services needed by the rest of Avalonia in the <see cref="RegisterServices"/>
     /// method.
     /// method.
     /// - Tracks the lifetime of the application.
     /// - Tracks the lifetime of the application.
     /// </remarks>
     /// </remarks>
     public class Application : IGlobalDataTemplates, IGlobalStyles, IStyleRoot, IApplicationLifecycle
     public class Application : IGlobalDataTemplates, IGlobalStyles, IStyleRoot, IApplicationLifecycle
     {
     {
-        static Action _platformInitializationCallback;
-
         /// <summary>
         /// <summary>
         /// The application-global data templates.
         /// The application-global data templates.
         /// </summary>
         /// </summary>
@@ -121,11 +117,6 @@ namespace Avalonia
         /// </summary>
         /// </summary>
         IStyleHost IStyleHost.StylingParent => null;
         IStyleHost IStyleHost.StylingParent => null;
 
 
-        public static void RegisterPlatformCallback(Action cb)
-        {
-            _platformInitializationCallback = cb;
-        }
-
         /// <summary>
         /// <summary>
         /// Initializes the application by loading XAML etc.
         /// Initializes the application by loading XAML etc.
         /// </summary>
         /// </summary>
@@ -189,47 +180,5 @@ namespace Avalonia
                 .Bind<IRenderQueueManager>().ToTransient<RenderQueueManager>()
                 .Bind<IRenderQueueManager>().ToTransient<RenderQueueManager>()
                 .Bind<IApplicationLifecycle>().ToConstant(this);
                 .Bind<IApplicationLifecycle>().ToConstant(this);
         }
         }
-
-        /// <summary>
-        /// Initializes the rendering and windowing subsystems according to platform.
-        /// </summary>
-        /// <param name="platformID">The value of Environment.OSVersion.Platform.</param>
-        protected void InitializeSubsystems(int platformID)
-        {
-            if (_platformInitializationCallback != null)
-            {
-                _platformInitializationCallback();
-            }
-            else if (platformID == 4 || platformID == 6)
-            {
-                InitializeSubsystem("Avalonia.Cairo");
-                InitializeSubsystem("Avalonia.Gtk");
-            }
-            else
-            {
-                InitializeSubsystem("Avalonia.Direct2D1");
-                InitializeSubsystem("Avalonia.Win32");
-            }
-        }
-
-        /// <summary>
-        /// Initializes the rendering or windowing subsystem defined by the specified assemblt.
-        /// </summary>
-        /// <param name="assemblyName">The name of the assembly.</param>
-        protected static void InitializeSubsystem(string assemblyName)
-        {
-            var assembly = Assembly.Load(new AssemblyName(assemblyName));
-            var platformClassName = assemblyName.Replace("Avalonia.", string.Empty) + "Platform";
-            var platformClassFullName = assemblyName + "." + platformClassName;
-            var platformClass = assembly.GetType(platformClassFullName);
-            var init = platformClass.GetRuntimeMethod("Initialize", new Type[0]);
-            init.Invoke(null, null);
-        }
-
-        internal static void InitializeWin32Subsystem()
-        {
-            InitializeSubsystem("Avalonia.Direct2D1");
-            InitializeSubsystem("Avalonia.Win32");
-        }
     }
     }
 }
 }

+ 2 - 2
src/Avalonia.DesignerSupport/DesignerAssist.cs

@@ -60,8 +60,8 @@ namespace Avalonia.DesignerSupport
             }
             }
 
 
             AppBuilder.Configure(app == null ? new DesignerApp() : (Application) Activator.CreateInstance(app.AsType()))
             AppBuilder.Configure(app == null ? new DesignerApp() : (Application) Activator.CreateInstance(app.AsType()))
-                .WithWindowingSubsystem(Application.InitializeWin32Subsystem)
-                .WithRenderingSubsystem(() => { })
+                .UseWindowingSubsystem("Avalonia.Win32")
+                .UseRenderingSubsystem("Avalonia.Direct2D1")
                 .SetupWithoutStarting();
                 .SetupWithoutStarting();
         }
         }