Application.cs 5.8 KB

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