Program.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Avalonia;
  8. using Avalonia.Controls;
  9. using Avalonia.Controls.ApplicationLifetimes;
  10. using Avalonia.Fonts.Inter;
  11. using Avalonia.Headless;
  12. using Avalonia.LogicalTree;
  13. using Avalonia.Threading;
  14. using ControlCatalog.Pages;
  15. namespace ControlCatalog.NetCore
  16. {
  17. static class Program
  18. {
  19. private static bool s_useFramebuffer;
  20. [STAThread]
  21. static int Main(string[] args)
  22. {
  23. if (args.Contains("--fbdev"))
  24. {
  25. s_useFramebuffer = true;
  26. }
  27. if (args.Contains("--wait-for-attach"))
  28. {
  29. Console.WriteLine("Attach debugger and use 'Set next statement'");
  30. while (true)
  31. {
  32. Thread.Sleep(100);
  33. if (Debugger.IsAttached)
  34. break;
  35. }
  36. }
  37. var builder = BuildAvaloniaApp();
  38. double GetScaling()
  39. {
  40. var idx = Array.IndexOf(args, "--scaling");
  41. if (idx != 0 && args.Length > idx + 1 &&
  42. double.TryParse(args[idx + 1], NumberStyles.Any, CultureInfo.InvariantCulture, out var scaling))
  43. return scaling;
  44. return 1;
  45. }
  46. if (s_useFramebuffer)
  47. {
  48. SilenceConsole();
  49. return builder.StartLinuxFbDev(args, scaling: GetScaling());
  50. }
  51. else if (args.Contains("--vnc"))
  52. {
  53. return builder.StartWithHeadlessVncPlatform(null, 5901, args, ShutdownMode.OnMainWindowClose);
  54. }
  55. else if (args.Contains("--full-headless"))
  56. {
  57. return builder
  58. .UseHeadless(new AvaloniaHeadlessPlatformOptions
  59. {
  60. UseHeadlessDrawing = true
  61. })
  62. .AfterSetup(_ =>
  63. {
  64. DispatcherTimer.RunOnce(async () =>
  65. {
  66. var window = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime)
  67. .MainWindow;
  68. var tc = window.GetLogicalDescendants().OfType<TabControl>().First();
  69. foreach (var page in tc.Items.Cast<TabItem>().ToList())
  70. {
  71. if (page.Header.ToString() == "DatePicker" || page.Header.ToString() == "TreeView")
  72. continue;
  73. Console.WriteLine("Selecting " + page.Header);
  74. tc.SelectedItem = page;
  75. await Task.Delay(50);
  76. }
  77. Console.WriteLine("Selecting the first page");
  78. tc.SelectedItem = tc.Items.OfType<object>().First();
  79. await Task.Delay(500);
  80. Console.WriteLine("Clicked through all pages, triggering GC");
  81. for (var c = 0; c < 3; c++)
  82. {
  83. GC.Collect(2, GCCollectionMode.Forced);
  84. await Task.Delay(50);
  85. }
  86. void FormatMem(string metric, long bytes)
  87. {
  88. Console.WriteLine(metric + ": " + bytes / 1024 / 1024 + "MB");
  89. }
  90. FormatMem("GC allocated bytes", GC.GetTotalMemory(true));
  91. FormatMem("WorkingSet64", Process.GetCurrentProcess().WorkingSet64);
  92. }, TimeSpan.FromSeconds(1));
  93. })
  94. .StartWithClassicDesktopLifetime(args);
  95. }
  96. else if (args.Contains("--drm"))
  97. {
  98. SilenceConsole();
  99. return builder.StartLinuxDrm(args, scaling: GetScaling());
  100. }
  101. else if (args.Contains("--dxgi"))
  102. {
  103. builder.With(new Win32PlatformOptions()
  104. {
  105. UseLowLatencyDxgiSwapChain = true,
  106. UseWindowsUIComposition = false
  107. });
  108. return builder.StartWithClassicDesktopLifetime(args);
  109. }
  110. else
  111. return builder.StartWithClassicDesktopLifetime(args);
  112. }
  113. /// <summary>
  114. /// This method is needed for IDE previewer infrastructure
  115. /// </summary>
  116. public static AppBuilder BuildAvaloniaApp()
  117. => AppBuilder.Configure<App>()
  118. .UsePlatformDetect()
  119. .With(new X11PlatformOptions
  120. {
  121. EnableMultiTouch = true,
  122. UseDBusMenu = true,
  123. EnableIme = true
  124. })
  125. .UseSkia()
  126. .WithInterFont()
  127. .AfterSetup(builder =>
  128. {
  129. if (!s_useFramebuffer)
  130. {
  131. builder.Instance!.AttachDevTools(new Avalonia.Diagnostics.DevToolsOptions()
  132. {
  133. StartupScreenIndex = 1,
  134. });
  135. }
  136. EmbedSample.Implementation = OperatingSystem.IsWindows() ? (INativeDemoControl)new EmbedSampleWin()
  137. : OperatingSystem.IsMacOS() ? new EmbedSampleMac()
  138. : OperatingSystem.IsLinux() ? new EmbedSampleGtk()
  139. : null;
  140. })
  141. .LogToTrace();
  142. static void SilenceConsole()
  143. {
  144. new Thread(() =>
  145. {
  146. Console.CursorVisible = false;
  147. while (true)
  148. Console.ReadKey(true);
  149. })
  150. { IsBackground = true }.Start();
  151. }
  152. }
  153. }