Program.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. [STAThread]
  20. static int Main(string[] args)
  21. {
  22. if (args.Contains("--wait-for-attach"))
  23. {
  24. Console.WriteLine("Attach debugger and use 'Set next statement'");
  25. while (true)
  26. {
  27. Thread.Sleep(100);
  28. if (Debugger.IsAttached)
  29. break;
  30. }
  31. }
  32. var builder = BuildAvaloniaApp();
  33. double GetScaling()
  34. {
  35. var idx = Array.IndexOf(args, "--scaling");
  36. if (idx != 0 && args.Length > idx + 1 &&
  37. double.TryParse(args[idx + 1], NumberStyles.Any, CultureInfo.InvariantCulture, out var scaling))
  38. return scaling;
  39. return 1;
  40. }
  41. if (args.Contains("--fbdev"))
  42. {
  43. SilenceConsole();
  44. return builder.StartLinuxFbDev(args, scaling: GetScaling());
  45. }
  46. else if (args.Contains("--vnc"))
  47. {
  48. return builder.StartWithHeadlessVncPlatform(null, 5901, args, ShutdownMode.OnMainWindowClose);
  49. }
  50. else if (args.Contains("--full-headless"))
  51. {
  52. return builder
  53. .UseHeadless(new AvaloniaHeadlessPlatformOptions
  54. {
  55. UseHeadlessDrawing = true
  56. })
  57. .AfterSetup(_ =>
  58. {
  59. DispatcherTimer.RunOnce(async () =>
  60. {
  61. var window = ((IClassicDesktopStyleApplicationLifetime)Application.Current.ApplicationLifetime)
  62. .MainWindow;
  63. var tc = window.GetLogicalDescendants().OfType<TabControl>().First();
  64. foreach (var page in tc.Items.Cast<TabItem>().ToList())
  65. {
  66. if (page.Header.ToString() == "DatePicker" || page.Header.ToString() == "TreeView")
  67. continue;
  68. Console.WriteLine("Selecting " + page.Header);
  69. tc.SelectedItem = page;
  70. await Task.Delay(50);
  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(50);
  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 if (args.Contains("--dxgi"))
  97. {
  98. builder.With(new Win32PlatformOptions()
  99. {
  100. UseLowLatencyDxgiSwapChain = true,
  101. UseWindowsUIComposition = false
  102. });
  103. return builder.StartWithClassicDesktopLifetime(args);
  104. }
  105. else
  106. return builder.StartWithClassicDesktopLifetime(args);
  107. }
  108. /// <summary>
  109. /// This method is needed for IDE previewer infrastructure
  110. /// </summary>
  111. public static AppBuilder BuildAvaloniaApp()
  112. => AppBuilder.Configure<App>()
  113. .UsePlatformDetect()
  114. .With(new X11PlatformOptions
  115. {
  116. EnableMultiTouch = true,
  117. UseDBusMenu = true,
  118. EnableIme = true
  119. })
  120. .UseSkia()
  121. .WithFonts(new InterFontCollection())
  122. .AfterSetup(builder =>
  123. {
  124. builder.Instance!.AttachDevTools(new Avalonia.Diagnostics.DevToolsOptions()
  125. {
  126. StartupScreenIndex = 1,
  127. });
  128. EmbedSample.Implementation = OperatingSystem.IsWindows() ? (INativeDemoControl)new EmbedSampleWin()
  129. : OperatingSystem.IsMacOS() ? new EmbedSampleMac()
  130. : OperatingSystem.IsLinux() ? new EmbedSampleGtk()
  131. : null;
  132. })
  133. .LogToTrace();
  134. static void SilenceConsole()
  135. {
  136. new Thread(() =>
  137. {
  138. Console.CursorVisible = false;
  139. while (true)
  140. Console.ReadKey(true);
  141. })
  142. { IsBackground = true }.Start();
  143. }
  144. }
  145. }