Program.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Dialogs;
  11. using Avalonia.Headless;
  12. using Avalonia.LogicalTree;
  13. using Avalonia.Threading;
  14. namespace ControlCatalog.NetCore
  15. {
  16. static class Program
  17. {
  18. [STAThread]
  19. static int Main(string[] args)
  20. {
  21. if (args.Contains("--wait-for-attach"))
  22. {
  23. Console.WriteLine("Attach debugger and use 'Set next statement'");
  24. while (true)
  25. {
  26. Thread.Sleep(100);
  27. if (Debugger.IsAttached)
  28. break;
  29. }
  30. }
  31. var builder = BuildAvaloniaApp();
  32. double GetScaling()
  33. {
  34. var idx = Array.IndexOf(args, "--scaling");
  35. if (idx != 0 && args.Length > idx + 1 &&
  36. double.TryParse(args[idx + 1], NumberStyles.Any, CultureInfo.InvariantCulture, out var scaling))
  37. return scaling;
  38. return 1;
  39. }
  40. if (args.Contains("--fbdev"))
  41. {
  42. SilenceConsole();
  43. return builder.StartLinuxFbDev(args, scaling: GetScaling());
  44. }
  45. else if (args.Contains("--vnc"))
  46. {
  47. return builder.StartWithHeadlessVncPlatform(null, 5901, args, ShutdownMode.OnMainWindowClose);
  48. }
  49. else if (args.Contains("--full-headless"))
  50. {
  51. return builder
  52. .UseHeadless(true)
  53. .AfterSetup(_ =>
  54. {
  55. DispatcherTimer.RunOnce(async () =>
  56. {
  57. var window = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime)
  58. .MainWindow;
  59. var tc = window.GetLogicalDescendants().OfType<TabControl>().First();
  60. foreach (var page in tc.Items.Cast<TabItem>().ToList())
  61. {
  62. // Skip DatePicker because of some layout bug in grid
  63. if (page.Header.ToString() == "DatePicker")
  64. continue;
  65. Console.WriteLine("Selecting " + page.Header);
  66. tc.SelectedItem = page;
  67. await Task.Delay(500);
  68. }
  69. Console.WriteLine("Selecting the first page");
  70. tc.SelectedItem = tc.Items.OfType<object>().First();
  71. await Task.Delay(500);
  72. Console.WriteLine("Clicked through all pages, triggering GC");
  73. for (var c = 0; c < 3; c++)
  74. {
  75. GC.Collect(2, GCCollectionMode.Forced);
  76. await Task.Delay(500);
  77. }
  78. void FormatMem(string metric, long bytes)
  79. {
  80. Console.WriteLine(metric + ": " + bytes / 1024 / 1024 + "MB");
  81. }
  82. FormatMem("GC allocated bytes", GC.GetTotalMemory(true));
  83. FormatMem("WorkingSet64", Process.GetCurrentProcess().WorkingSet64);
  84. }, TimeSpan.FromSeconds(1));
  85. })
  86. .StartWithClassicDesktopLifetime(args);
  87. }
  88. else if (args.Contains("--drm"))
  89. {
  90. SilenceConsole();
  91. return builder.StartLinuxDrm(args, scaling: GetScaling());
  92. }
  93. else
  94. return builder.StartWithClassicDesktopLifetime(args);
  95. }
  96. /// <summary>
  97. /// This method is needed for IDE previewer infrastructure
  98. /// </summary>
  99. public static AppBuilder BuildAvaloniaApp()
  100. => AppBuilder.Configure<App>()
  101. .UsePlatformDetect()
  102. .With(new X11PlatformOptions
  103. {
  104. EnableMultiTouch = true,
  105. UseDBusMenu = true,
  106. EnableIme = true,
  107. })
  108. .With(new Win32PlatformOptions
  109. {
  110. EnableMultitouch = true,
  111. AllowEglInitialization = true
  112. })
  113. .UseSkia()
  114. .UseManagedSystemDialogs()
  115. .LogToTrace();
  116. static void SilenceConsole()
  117. {
  118. new Thread(() =>
  119. {
  120. Console.CursorVisible = false;
  121. while (true)
  122. Console.ReadKey(true);
  123. })
  124. { IsBackground = true }.Start();
  125. }
  126. }
  127. }