Program.cs 3.8 KB

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