1
0

Program.cs 4.2 KB

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