Application.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.Globalization;
  18. using System.Reflection;
  19. using System.Resources;
  20. namespace Terminal.Gui.App;
  21. /// <summary>A static, singleton class representing the application. This class is the entry point for the application.</summary>
  22. /// <example>
  23. /// <code>
  24. /// Application.Init();
  25. /// var win = new Window()
  26. /// {
  27. /// Title = $"Example App ({Application.QuitKey} to quit)"
  28. /// };
  29. /// Application.Run(win);
  30. /// win.Dispose();
  31. /// Application.Shutdown();
  32. /// </code>
  33. /// </example>
  34. /// <remarks></remarks>
  35. public static partial class Application
  36. {
  37. /// <summary>
  38. /// Maximum number of iterations of the main loop (and hence draws)
  39. /// to allow to occur per second. Defaults to <see cref="DefaultMaximumIterationsPerSecond"/>> which is a 40ms sleep
  40. /// after iteration (factoring in how long iteration took to run).
  41. /// <remarks>
  42. /// Note that not every iteration draws (see <see cref="View.NeedsDraw"/>).
  43. /// Only affects v2 drivers.
  44. /// </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>Gets all cultures supported by the application without the invariant language.</summary>
  52. public static List<CultureInfo>? SupportedCultures { get; private set; } = GetSupportedCultures ();
  53. internal static List<CultureInfo> GetAvailableCulturesFromEmbeddedResources ()
  54. {
  55. ResourceManager rm = new (typeof (Strings));
  56. CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
  57. return cultures.Where (cultureInfo =>
  58. !cultureInfo.Equals (CultureInfo.InvariantCulture)
  59. && rm.GetResourceSet (cultureInfo, true, false) is { }
  60. )
  61. .ToList ();
  62. }
  63. // BUGBUG: This does not return en-US even though it's supported by default
  64. internal static List<CultureInfo> GetSupportedCultures ()
  65. {
  66. CultureInfo [] cultures = CultureInfo.GetCultures (CultureTypes.AllCultures);
  67. // Get the assembly
  68. var assembly = Assembly.GetExecutingAssembly ();
  69. //Find the location of the assembly
  70. string assemblyLocation = AppDomain.CurrentDomain.BaseDirectory;
  71. // Find the resource file name of the assembly
  72. var resourceFilename = $"{assembly.GetName ().Name}.resources.dll";
  73. if (cultures.Length > 1 && Directory.Exists (Path.Combine (assemblyLocation, "pt-PT")))
  74. {
  75. // Return all culture for which satellite folder found with culture code.
  76. return cultures.Where (cultureInfo =>
  77. Directory.Exists (Path.Combine (assemblyLocation, cultureInfo.Name))
  78. && File.Exists (Path.Combine (assemblyLocation, cultureInfo.Name, resourceFilename))
  79. )
  80. .ToList ();
  81. }
  82. // It's called from a self-contained single-file and get available cultures from the embedded resources strings.
  83. return GetAvailableCulturesFromEmbeddedResources ();
  84. }
  85. }