Program.cs 5.0 KB

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