Application.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #nullable enable
  2. // We use global using directives to simplify the code and avoid repetitive namespace declarations.
  3. // Put them here so they are available throughout the application.
  4. // Do not put them in AssemblyInfo.cs as it will break GitVersion's /updateassemblyinfo
  5. global using Attribute = Terminal.Gui.Drawing.Attribute;
  6. global using Color = Terminal.Gui.Drawing.Color;
  7. global using CM = Terminal.Gui.Configuration.ConfigurationManager;
  8. global using Terminal.Gui.App;
  9. global using Terminal.Gui.Drivers;
  10. global using Terminal.Gui.Input;
  11. global using Terminal.Gui.Configuration;
  12. global using Terminal.Gui.ViewBase;
  13. global using Terminal.Gui.Views;
  14. global using Terminal.Gui.Drawing;
  15. global using Terminal.Gui.Text;
  16. global using Terminal.Gui.Resources;
  17. global using Terminal.Gui.FileServices;
  18. using System.Diagnostics;
  19. using System.Globalization;
  20. using System.Reflection;
  21. using System.Resources;
  22. namespace Terminal.Gui.App;
  23. /// <summary>A static, singleton class representing the application. This class is the entry point for the application.</summary>
  24. /// <example>
  25. /// <code>
  26. /// Application.Init();
  27. /// var win = new Window()
  28. /// {
  29. /// Title = $"Example App ({Application.QuitKey} to quit)"
  30. /// };
  31. /// Application.Run(win);
  32. /// win.Dispose();
  33. /// Application.Shutdown();
  34. /// </code>
  35. /// </example>
  36. /// <remarks></remarks>
  37. public static partial class Application
  38. {
  39. /// <summary>
  40. /// Maximum number of iterations of the main loop (and hence draws)
  41. /// to allow to occur per second. Defaults to <see cref="DefaultMaximumIterationsPerSecond"/>> which is a 40ms sleep
  42. /// after iteration (factoring in how long iteration took to run).
  43. /// <remarks>Note that not every iteration draws (see <see cref="View.NeedsDraw"/>).
  44. /// Only affects v2 drivers.</remarks>
  45. /// </summary>
  46. public static ushort MaximumIterationsPerSecond = DefaultMaximumIterationsPerSecond;
  47. /// <summary>
  48. /// Default value for <see cref="MaximumIterationsPerSecond"/>
  49. /// </summary>
  50. public const ushort DefaultMaximumIterationsPerSecond = 25;
  51. /// <summary>
  52. /// Gets a string representation of the Application as rendered by <see cref="Driver"/>.
  53. /// </summary>
  54. /// <returns>A string representation of the Application </returns>
  55. public new static string ToString ()
  56. {
  57. IDriver? driver = Driver;
  58. if (driver is null)
  59. {
  60. return string.Empty;
  61. }
  62. return ToString (driver);
  63. }
  64. /// <summary>
  65. /// Gets a string representation of the Application rendered by the provided <see cref="IDriver"/>.
  66. /// </summary>
  67. /// <param name="driver">The driver to use to render the contents.</param>
  68. /// <returns>A string representation of the Application </returns>
  69. public static string ToString (IDriver? driver)
  70. {
  71. if (driver is null)
  72. {
  73. return string.Empty;
  74. }
  75. var sb = new StringBuilder ();
  76. Cell [,] contents = driver?.Contents!;
  77. for (var r = 0; r < driver!.Rows; r++)
  78. {
  79. for (var c = 0; c < driver.Cols; c++)
  80. {
  81. Rune rune = contents [r, c].Rune;
  82. if (rune.DecodeSurrogatePair (out char []? sp))
  83. {
  84. sb.Append (sp);
  85. }
  86. else
  87. {
  88. sb.Append ((char)rune.Value);
  89. }
  90. if (rune.GetColumns () > 1)
  91. {
  92. c++;
  93. }
  94. // See Issue #2616
  95. //foreach (var combMark in contents [r, c].CombiningMarks) {
  96. // sb.Append ((char)combMark.Value);
  97. //}
  98. }
  99. sb.AppendLine ();
  100. }
  101. return sb.ToString ();
  102. }
  103. /// <summary>Gets all cultures supported by the application without the invariant language.</summary>
  104. public static List<CultureInfo>? SupportedCultures { get; private set; } = GetSupportedCultures ();
  105. internal static List<CultureInfo> GetAvailableCulturesFromEmbeddedResources ()
  106. {
  107. ResourceManager rm = new (typeof (Strings));
  108. CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
  109. return cultures.Where (
  110. cultureInfo =>
  111. !cultureInfo.Equals (CultureInfo.InvariantCulture)
  112. && rm.GetResourceSet (cultureInfo, true, false) is { }
  113. )
  114. .ToList ();
  115. }
  116. // BUGBUG: This does not return en-US even though it's supported by default
  117. internal static List<CultureInfo> GetSupportedCultures ()
  118. {
  119. CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
  120. // Get the assembly
  121. var assembly = Assembly.GetExecutingAssembly ();
  122. //Find the location of the assembly
  123. string assemblyLocation = AppDomain.CurrentDomain.BaseDirectory;
  124. // Find the resource file name of the assembly
  125. var resourceFilename = $"{assembly.GetName ().Name}.resources.dll";
  126. if (cultures.Length > 1 && Directory.Exists (Path.Combine (assemblyLocation, "pt-PT")))
  127. {
  128. // Return all culture for which satellite folder found with culture code.
  129. return cultures.Where (
  130. cultureInfo =>
  131. Directory.Exists (Path.Combine (assemblyLocation, cultureInfo.Name))
  132. && File.Exists (Path.Combine (assemblyLocation, cultureInfo.Name, resourceFilename))
  133. )
  134. .ToList ();
  135. }
  136. // It's called from a self-contained single-file and get available cultures from the embedded resources strings.
  137. return GetAvailableCulturesFromEmbeddedResources ();
  138. }
  139. }