Program.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Threading;
  13. using ControlCatalog.Pages;
  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(new AvaloniaHeadlessPlatformOptions
  53. {
  54. UseHeadlessDrawing = true,
  55. UseComposition = true
  56. })
  57. .AfterSetup(_ =>
  58. {
  59. static Task LowPriorityDelay(int ms)
  60. {
  61. return Task.Delay(ms).ContinueWith(_ =>
  62. Dispatcher.UIThread.InvokeAsync(() => { }, DispatcherPriority.Background))
  63. .Unwrap();
  64. }
  65. DispatcherTimer.RunOnce(async () =>
  66. {
  67. var window = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime)
  68. .MainWindow;
  69. var tc = window.GetLogicalDescendants().OfType<TabControl>().First();
  70. foreach (var page in tc.Items.Cast<TabItem>().ToList())
  71. {
  72. if (page.Header.ToString() == "DatePicker" || page.Header.ToString() == "TreeView")
  73. continue;
  74. Console.WriteLine("Selecting " + page.Header);
  75. tc.SelectedItem = page;
  76. await LowPriorityDelay(20);
  77. }
  78. Console.WriteLine("Selecting the first page");
  79. tc.SelectedItem = tc.Items.OfType<object>().First();
  80. await Task.Delay(500);
  81. Console.WriteLine("Clicked through all pages, triggering GC");
  82. for (var c = 0; c < 3; c++)
  83. {
  84. GC.Collect(2, GCCollectionMode.Forced);
  85. await Task.Delay(00);
  86. }
  87. void FormatMem(string metric, long bytes)
  88. {
  89. Console.WriteLine(metric + ": " + bytes / 1024 / 1024 + "MB");
  90. }
  91. FormatMem("GC allocated bytes", GC.GetTotalMemory(true));
  92. FormatMem("WorkingSet64", Process.GetCurrentProcess().WorkingSet64);
  93. }, TimeSpan.FromSeconds(1));
  94. })
  95. .StartWithClassicDesktopLifetime(args);
  96. }
  97. else if (args.Contains("--drm"))
  98. {
  99. SilenceConsole();
  100. return builder.StartLinuxDrm(args, scaling: GetScaling());
  101. }
  102. else
  103. return builder.StartWithClassicDesktopLifetime(args);
  104. }
  105. /// <summary>
  106. /// This method is needed for IDE previewer infrastructure
  107. /// </summary>
  108. public static AppBuilder BuildAvaloniaApp()
  109. => AppBuilder.Configure<App>()
  110. .UsePlatformDetect()
  111. .With(new X11PlatformOptions
  112. {
  113. EnableMultiTouch = true,
  114. UseDBusMenu = true,
  115. EnableIme = true,
  116. UseCompositor = true
  117. })
  118. .With(new Win32PlatformOptions
  119. {
  120. EnableMultitouch = true,
  121. UseCompositor = true
  122. })
  123. .UseSkia()
  124. .AfterSetup(builder =>
  125. {
  126. builder.Instance!.AttachDevTools(new Avalonia.Diagnostics.DevToolsOptions()
  127. {
  128. StartupScreenIndex = 1,
  129. });
  130. EmbedSample.Implementation = OperatingSystem.IsWindows() ? (INativeDemoControl)new EmbedSampleWin()
  131. : OperatingSystem.IsMacOS() ? new EmbedSampleMac()
  132. : OperatingSystem.IsLinux() ? new EmbedSampleGtk()
  133. : null;
  134. })
  135. .LogToTrace();
  136. static void SilenceConsole()
  137. {
  138. new Thread(() =>
  139. {
  140. Console.CursorVisible = false;
  141. while (true)
  142. Console.ReadKey(true);
  143. })
  144. { IsBackground = true }.Start();
  145. }
  146. }
  147. }