Program.cs 4.2 KB

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