Application.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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>Gets all cultures supported by the application without the invariant language.</summary>
  40. public static List<CultureInfo>? SupportedCultures => ApplicationImpl.Instance.SupportedCultures;
  41. /// <summary>
  42. /// <para>
  43. /// Handles recurring events. These are invoked on the main UI thread - allowing for
  44. /// safe updates to <see cref="View"/> instances.
  45. /// </para>
  46. /// </summary>
  47. public static ITimedEvents? TimedEvents => ApplicationImpl.Instance?.TimedEvents;
  48. /// <summary>
  49. /// Maximum number of iterations of the main loop (and hence draws)
  50. /// to allow to occur per second. Defaults to <see cref="DEFAULT_MAXIMUM_ITERATIONS_PER_SECOND"/>> which is a 40ms sleep
  51. /// after iteration (factoring in how long iteration took to run).
  52. /// <remarks>Note that not every iteration draws (see <see cref="View.NeedsDraw"/>).
  53. /// Only affects v2 drivers.</remarks>
  54. /// </summary>
  55. public static ushort MaximumIterationsPerSecond
  56. {
  57. get => ApplicationImpl.Instance.MaximumIterationsPerSecond;
  58. set => ApplicationImpl.Instance.MaximumIterationsPerSecond = value;
  59. }
  60. /// <summary>
  61. /// Default value for <see cref="MaximumIterationsPerSecond"/>
  62. /// </summary>
  63. public const ushort DEFAULT_MAXIMUM_ITERATIONS_PER_SECOND = 25;
  64. /// <summary>
  65. /// Gets a string representation of the Application as rendered by <see cref="Driver"/>.
  66. /// </summary>
  67. /// <returns>A string representation of the Application </returns>
  68. public new static string ToString ()
  69. {
  70. IConsoleDriver? driver = Driver;
  71. if (driver is null)
  72. {
  73. return string.Empty;
  74. }
  75. return ToString (driver);
  76. }
  77. /// <summary>
  78. /// Gets a string representation of the Application rendered by the provided <see cref="IConsoleDriver"/>.
  79. /// </summary>
  80. /// <param name="driver">The driver to use to render the contents.</param>
  81. /// <returns>A string representation of the Application </returns>
  82. public static string ToString (IConsoleDriver? driver)
  83. {
  84. if (driver is null)
  85. {
  86. return string.Empty;
  87. }
  88. var sb = new StringBuilder ();
  89. Cell [,] contents = driver?.Contents!;
  90. for (var r = 0; r < driver!.Rows; r++)
  91. {
  92. for (var c = 0; c < driver.Cols; c++)
  93. {
  94. Rune rune = contents [r, c].Rune;
  95. if (rune.DecodeSurrogatePair (out char []? sp))
  96. {
  97. sb.Append (sp);
  98. }
  99. else
  100. {
  101. sb.Append ((char)rune.Value);
  102. }
  103. if (rune.GetColumns () > 1)
  104. {
  105. c++;
  106. }
  107. // See Issue #2616
  108. //foreach (var combMark in contents [r, c].CombiningMarks) {
  109. // sb.Append ((char)combMark.Value);
  110. //}
  111. }
  112. sb.AppendLine ();
  113. }
  114. return sb.ToString ();
  115. }
  116. internal static List<CultureInfo> GetAvailableCulturesFromEmbeddedResources ()
  117. {
  118. ResourceManager rm = new (typeof (Strings));
  119. CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
  120. return cultures.Where (
  121. cultureInfo =>
  122. !cultureInfo.Equals (CultureInfo.InvariantCulture)
  123. && rm.GetResourceSet (cultureInfo, true, false) is { }
  124. )
  125. .ToList ();
  126. }
  127. // BUGBUG: This does not return en-US even though it's supported by default
  128. internal static List<CultureInfo> GetSupportedCultures ()
  129. {
  130. CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
  131. // Get the assembly
  132. var assembly = Assembly.GetExecutingAssembly ();
  133. //Find the location of the assembly
  134. string assemblyLocation = AppDomain.CurrentDomain.BaseDirectory;
  135. // Find the resource file name of the assembly
  136. var resourceFilename = $"{assembly.GetName ().Name}.resources.dll";
  137. if (cultures.Length > 1 && Directory.Exists (Path.Combine (assemblyLocation, "pt-PT")))
  138. {
  139. // Return all culture for which satellite folder found with culture code.
  140. return cultures.Where (
  141. cultureInfo =>
  142. Directory.Exists (Path.Combine (assemblyLocation, cultureInfo.Name))
  143. && File.Exists (Path.Combine (assemblyLocation, cultureInfo.Name, resourceFilename))
  144. )
  145. .ToList ();
  146. }
  147. // It's called from a self-contained single-file and get available cultures from the embedded resources strings.
  148. return GetAvailableCulturesFromEmbeddedResources ();
  149. }
  150. // IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
  151. // Encapsulate all setting of initial state for Application; Having
  152. // this in a function like this ensures we don't make mistakes in
  153. // guaranteeing that the state of this singleton is deterministic when Init
  154. // starts running and after Shutdown returns.
  155. internal static void ResetState (bool ignoreDisposed = false)
  156. {
  157. if (ApplicationImpl.Instance is ApplicationImpl impl)
  158. {
  159. impl.ResetState (ignoreDisposed);
  160. }
  161. }
  162. }