Application.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #nullable enable
  2. using System.Diagnostics;
  3. using System.Globalization;
  4. using System.Reflection;
  5. namespace Terminal.Gui;
  6. /// <summary>A static, singleton class representing the application. This class is the entry point for the application.</summary>
  7. /// <example>
  8. /// <code>
  9. /// Application.Init();
  10. /// var win = new Window()
  11. /// {
  12. /// Title = $"Example App ({Application.QuitKey} to quit)"
  13. /// };
  14. /// Application.Run(win);
  15. /// win.Dispose();
  16. /// Application.Shutdown();
  17. /// </code>
  18. /// </example>
  19. /// <remarks></remarks>
  20. public static partial class Application
  21. {
  22. /// <summary>Gets all cultures supported by the application without the invariant language.</summary>
  23. public static List<CultureInfo>? SupportedCultures { get; private set; }
  24. internal static List<CultureInfo> GetSupportedCultures ()
  25. {
  26. CultureInfo [] culture = CultureInfo.GetCultures (CultureTypes.AllCultures);
  27. // Get the assembly
  28. var assembly = Assembly.GetExecutingAssembly ();
  29. //Find the location of the assembly
  30. string assemblyLocation = AppDomain.CurrentDomain.BaseDirectory;
  31. // Find the resource file name of the assembly
  32. var resourceFilename = $"{Path.GetFileNameWithoutExtension (AppContext.BaseDirectory)}.resources.dll";
  33. // Return all culture for which satellite folder found with culture code.
  34. return culture.Where (
  35. cultureInfo =>
  36. Directory.Exists (Path.Combine (assemblyLocation, cultureInfo.Name))
  37. && File.Exists (Path.Combine (assemblyLocation, cultureInfo.Name, resourceFilename))
  38. )
  39. .ToList ();
  40. }
  41. // IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
  42. // Encapsulate all setting of initial state for Application; Having
  43. // this in a function like this ensures we don't make mistakes in
  44. // guaranteeing that the state of this singleton is deterministic when Init
  45. // starts running and after Shutdown returns.
  46. internal static void ResetState (bool ignoreDisposed = false)
  47. {
  48. // Shutdown is the bookend for Init. As such it needs to clean up all resources
  49. // Init created. Apps that do any threading will need to code defensively for this.
  50. // e.g. see Issue #537
  51. foreach (Toplevel? t in TopLevels)
  52. {
  53. t!.Running = false;
  54. }
  55. TopLevels.Clear ();
  56. Current = null;
  57. #if DEBUG_IDISPOSABLE
  58. // Don't dispose the Top. It's up to caller dispose it
  59. if (!ignoreDisposed && Top is { })
  60. {
  61. Debug.Assert (Top.WasDisposed);
  62. // If End wasn't called _cachedRunStateToplevel may be null
  63. if (_cachedRunStateToplevel is { })
  64. {
  65. Debug.Assert (_cachedRunStateToplevel.WasDisposed);
  66. Debug.Assert (_cachedRunStateToplevel == Top);
  67. }
  68. }
  69. #endif
  70. Top = null;
  71. _cachedRunStateToplevel = null;
  72. // MainLoop stuff
  73. MainLoop?.Dispose ();
  74. MainLoop = null;
  75. MainThreadId = -1;
  76. Iteration = null;
  77. EndAfterFirstIteration = false;
  78. // Driver stuff
  79. if (Driver is { })
  80. {
  81. Driver.SizeChanged -= Driver_SizeChanged;
  82. Driver.KeyDown -= Driver_KeyDown;
  83. Driver.KeyUp -= Driver_KeyUp;
  84. Driver.MouseEvent -= Driver_MouseEvent;
  85. Driver?.End ();
  86. Driver = null;
  87. }
  88. // Don't reset ForceDriver; it needs to be set before Init is called.
  89. //ForceDriver = string.Empty;
  90. //Force16Colors = false;
  91. _forceFakeConsole = false;
  92. // Run State stuff
  93. NotifyNewRunState = null;
  94. NotifyStopRunState = null;
  95. MouseGrabView = null;
  96. IsInitialized = false;
  97. // Mouse
  98. MouseEnteredView = null;
  99. WantContinuousButtonPressedView = null;
  100. MouseEvent = null;
  101. GrabbedMouse = null;
  102. UnGrabbingMouse = null;
  103. GrabbedMouse = null;
  104. UnGrabbedMouse = null;
  105. // Keyboard
  106. AlternateBackwardKey = Key.Empty;
  107. AlternateForwardKey = Key.Empty;
  108. QuitKey = Key.Empty;
  109. KeyDown = null;
  110. KeyUp = null;
  111. SizeChanging = null;
  112. AddApplicationKeyBindings ();
  113. Colors.Reset ();
  114. // Reset synchronization context to allow the user to run async/await,
  115. // as the main loop has been ended, the synchronization context from
  116. // gui.cs does no longer process any callbacks. See #1084 for more details:
  117. // (https://github.com/gui-cs/Terminal.Gui/issues/1084).
  118. SynchronizationContext.SetSynchronizationContext (null);
  119. }
  120. #nullable enable
  121. #nullable restore
  122. #nullable enable
  123. // Only return true if the Current has changed.
  124. #nullable restore
  125. /// <summary>
  126. /// Gets a string representation of the Application as rendered by <see cref="Driver"/>.
  127. /// </summary>
  128. /// <returns>A string representation of the Application </returns>
  129. public new static string ToString ()
  130. {
  131. ConsoleDriver driver = Driver;
  132. if (driver is null)
  133. {
  134. return string.Empty;
  135. }
  136. return ToString (driver);
  137. }
  138. /// <summary>
  139. /// Gets a string representation of the Application rendered by the provided <see cref="ConsoleDriver"/>.
  140. /// </summary>
  141. /// <param name="driver">The driver to use to render the contents.</param>
  142. /// <returns>A string representation of the Application </returns>
  143. public static string ToString (ConsoleDriver driver)
  144. {
  145. var sb = new StringBuilder ();
  146. Cell [,] contents = driver.Contents;
  147. for (var r = 0; r < driver.Rows; r++)
  148. {
  149. for (var c = 0; c < driver.Cols; c++)
  150. {
  151. Rune rune = contents [r, c].Rune;
  152. if (rune.DecodeSurrogatePair (out char [] sp))
  153. {
  154. sb.Append (sp);
  155. }
  156. else
  157. {
  158. sb.Append ((char)rune.Value);
  159. }
  160. if (rune.GetColumns () > 1)
  161. {
  162. c++;
  163. }
  164. // See Issue #2616
  165. //foreach (var combMark in contents [r, c].CombiningMarks) {
  166. // sb.Append ((char)combMark.Value);
  167. //}
  168. }
  169. sb.AppendLine ();
  170. }
  171. return sb.ToString ();
  172. }
  173. }