Application.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. ApplicationNavigation.ResetState ();
  49. // Shutdown is the bookend for Init. As such it needs to clean up all resources
  50. // Init created. Apps that do any threading will need to code defensively for this.
  51. // e.g. see Issue #537
  52. foreach (Toplevel? t in TopLevels)
  53. {
  54. t!.Running = false;
  55. }
  56. TopLevels.Clear ();
  57. Current = null;
  58. #if DEBUG_IDISPOSABLE
  59. // Don't dispose the Top. It's up to caller dispose it
  60. if (!ignoreDisposed && Top is { })
  61. {
  62. Debug.Assert (Top.WasDisposed);
  63. // If End wasn't called _cachedRunStateToplevel may be null
  64. if (_cachedRunStateToplevel is { })
  65. {
  66. Debug.Assert (_cachedRunStateToplevel.WasDisposed);
  67. Debug.Assert (_cachedRunStateToplevel == Top);
  68. }
  69. }
  70. #endif
  71. Top = null;
  72. _cachedRunStateToplevel = null;
  73. // MainLoop stuff
  74. MainLoop?.Dispose ();
  75. MainLoop = null;
  76. MainThreadId = -1;
  77. Iteration = null;
  78. EndAfterFirstIteration = false;
  79. // Driver stuff
  80. if (Driver is { })
  81. {
  82. Driver.SizeChanged -= Driver_SizeChanged;
  83. Driver.KeyDown -= Driver_KeyDown;
  84. Driver.KeyUp -= Driver_KeyUp;
  85. Driver.MouseEvent -= Driver_MouseEvent;
  86. Driver?.End ();
  87. Driver = null;
  88. }
  89. // Don't reset ForceDriver; it needs to be set before Init is called.
  90. //ForceDriver = string.Empty;
  91. //Force16Colors = false;
  92. _forceFakeConsole = false;
  93. // Run State stuff
  94. NotifyNewRunState = null;
  95. NotifyStopRunState = null;
  96. MouseGrabView = null;
  97. IsInitialized = false;
  98. // Mouse
  99. MouseEnteredView = null;
  100. WantContinuousButtonPressedView = null;
  101. MouseEvent = null;
  102. GrabbedMouse = null;
  103. UnGrabbingMouse = null;
  104. GrabbedMouse = null;
  105. UnGrabbedMouse = null;
  106. // Keyboard
  107. AlternateBackwardKey = Key.Empty;
  108. AlternateForwardKey = Key.Empty;
  109. QuitKey = Key.Empty;
  110. KeyDown = null;
  111. KeyUp = null;
  112. SizeChanging = null;
  113. AddApplicationKeyBindings ();
  114. Colors.Reset ();
  115. // Reset synchronization context to allow the user to run async/await,
  116. // as the main loop has been ended, the synchronization context from
  117. // gui.cs does no longer process any callbacks. See #1084 for more details:
  118. // (https://github.com/gui-cs/Terminal.Gui/issues/1084).
  119. SynchronizationContext.SetSynchronizationContext (null);
  120. }
  121. #nullable enable
  122. #nullable restore
  123. #nullable enable
  124. // Only return true if the Current has changed.
  125. #nullable restore
  126. /// <summary>
  127. /// Gets a string representation of the Application as rendered by <see cref="Driver"/>.
  128. /// </summary>
  129. /// <returns>A string representation of the Application </returns>
  130. public new static string ToString ()
  131. {
  132. ConsoleDriver driver = Driver;
  133. if (driver is null)
  134. {
  135. return string.Empty;
  136. }
  137. return ToString (driver);
  138. }
  139. /// <summary>
  140. /// Gets a string representation of the Application rendered by the provided <see cref="ConsoleDriver"/>.
  141. /// </summary>
  142. /// <param name="driver">The driver to use to render the contents.</param>
  143. /// <returns>A string representation of the Application </returns>
  144. public static string ToString (ConsoleDriver driver)
  145. {
  146. var sb = new StringBuilder ();
  147. Cell [,] contents = driver.Contents;
  148. for (var r = 0; r < driver.Rows; r++)
  149. {
  150. for (var c = 0; c < driver.Cols; c++)
  151. {
  152. Rune rune = contents [r, c].Rune;
  153. if (rune.DecodeSurrogatePair (out char [] sp))
  154. {
  155. sb.Append (sp);
  156. }
  157. else
  158. {
  159. sb.Append ((char)rune.Value);
  160. }
  161. if (rune.GetColumns () > 1)
  162. {
  163. c++;
  164. }
  165. // See Issue #2616
  166. //foreach (var combMark in contents [r, c].CombiningMarks) {
  167. // sb.Append ((char)combMark.Value);
  168. //}
  169. }
  170. sb.AppendLine ();
  171. }
  172. return sb.ToString ();
  173. }
  174. }