Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Avalonia.Logging.Serilog;
  2. using Serilog;
  3. using System;
  4. using System.Linq;
  5. using Avalonia;
  6. using System.Reflection;
  7. using Avalonia.Platform;
  8. namespace ControlCatalog
  9. {
  10. internal class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. InitializeLogging();
  15. new App()
  16. .ConfigureRenderSystem(args)
  17. .LoadFromXaml()
  18. .RunWithMainWindow<MainWindow>();
  19. }
  20. // This will be made into a runtime configuration extension soon!
  21. private static void InitializeLogging()
  22. {
  23. #if DEBUG
  24. SerilogLogger.Initialize(new LoggerConfiguration()
  25. .MinimumLevel.Warning()
  26. .WriteTo.Trace(outputTemplate: "{Area}: {Message}")
  27. .CreateLogger());
  28. #endif
  29. }
  30. }
  31. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32. // Experimental: Would like to move this into a shared location once I figure out the best place for it
  33. // considering all common libraries are PCL and do not have access to Environment.OSVersion.Platform
  34. // nor do they have access to the platform specific render/subsystem extensions.
  35. //
  36. // Perhaps via DI we register each system with a priority/rank
  37. //
  38. public static class RenderSystemExtensions
  39. {
  40. [Flags]
  41. enum RenderSystem
  42. {
  43. None = 0,
  44. GTK = 1,
  45. Skia = 2,
  46. Direct2D = 4
  47. };
  48. /// <summary>
  49. /// Default (Optimal) render system for a particular platform
  50. /// </summary>
  51. /// <returns></returns>
  52. private static RenderSystem DefaultRenderSystem()
  53. {
  54. switch (Environment.OSVersion.Platform)
  55. {
  56. case PlatformID.MacOSX:
  57. return RenderSystem.GTK;
  58. case PlatformID.Unix:
  59. return RenderSystem.GTK;
  60. case PlatformID.Win32Windows:
  61. return RenderSystem.Direct2D;
  62. }
  63. return RenderSystem.None;
  64. }
  65. /// <summary>
  66. /// Returns an array of avalidable rendering systems in priority order
  67. /// </summary>
  68. /// <returns></returns>
  69. private static RenderSystem[] AvailableRenderSystems()
  70. {
  71. switch (Environment.OSVersion.Platform)
  72. {
  73. case PlatformID.MacOSX:
  74. return new RenderSystem[] { RenderSystem.GTK, RenderSystem.Skia };
  75. case PlatformID.Unix:
  76. return new RenderSystem[] { RenderSystem.GTK, RenderSystem.Skia };
  77. case PlatformID.Win32Windows:
  78. return new RenderSystem[] { RenderSystem.Direct2D, RenderSystem.Skia, RenderSystem.GTK };
  79. }
  80. return new RenderSystem[0];
  81. }
  82. /// <summary>
  83. /// Selects the optimal render system for desktop platforms. Supports cmd line overrides
  84. /// </summary>
  85. /// <param name="app"></param>
  86. /// <param name="args"></param>
  87. public static TApp ConfigureRenderSystem<TApp>(this TApp app, string[] args) where TApp : Application
  88. {
  89. // So this all works great under Windows where it can support
  90. // ALL configurations. But on OSX/Unix we cannot use Direct2D
  91. //
  92. if (args.Contains("--gtk") || DefaultRenderSystem() == RenderSystem.GTK)
  93. {
  94. app.UseGtk();
  95. app.UseCairo();
  96. }
  97. else
  98. {
  99. app.UseWin32();
  100. if (args.Contains("--skia") || DefaultRenderSystem() == RenderSystem.Skia)
  101. {
  102. app.UseSkia();
  103. }
  104. else
  105. {
  106. app.UseDirect2D();
  107. }
  108. }
  109. return app;
  110. }
  111. }
  112. }