Scenario.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Terminal.Gui;
  6. namespace UICatalog {
  7. /// <summary>
  8. /// <para>Base class for each demo/scenario.</para>
  9. /// <para>
  10. /// To define a new scenario:
  11. /// <list type="number">
  12. /// <item><description>Create a new <c>.cs</c> file in the <cs>Scenarios</cs> directory that derives from <see cref="Scenario"/>.</description></item>
  13. /// <item><description>Annotate the <see cref="Scenario"/> derived class with a <see cref="Scenario.ScenarioMetadata"/> attribute specifying the scenario's name and description.</description></item>
  14. /// <item><description>Add one or more <see cref="Scenario.ScenarioCategory"/> attributes to the class specifying which categories the scenario belongs to. If you don't specify a category the scenario will show up in "_All".</description></item>
  15. /// <item><description>Implement the <see cref="Setup"/> override which will be called when a user selects the scenario to run.</description></item>
  16. /// <item><description>Optionally, implement the <see cref="Init(ColorScheme)"/> and/or <see cref="Run"/> overrides to provide a custom implementation.</description></item>
  17. /// </list>
  18. /// </para>
  19. /// <para>
  20. /// The UI Catalog program uses reflection to find all scenarios and adds them to the
  21. /// ListViews. Press ENTER to run the selected scenario. Press the default quit key to quit. /
  22. /// </para>
  23. /// </summary>
  24. /// <example>
  25. /// The example below is provided in the `Scenarios` directory as a generic sample that can be copied and re-named:
  26. /// <code>
  27. /// using Terminal.Gui;
  28. ///
  29. /// namespace UICatalog {
  30. /// [ScenarioMetadata (Name: "Generic", Description: "Generic sample - A template for creating new Scenarios")]
  31. /// [ScenarioCategory ("Controls")]
  32. /// class MyScenario : Scenario {
  33. /// public override void Setup ()
  34. /// {
  35. /// // Put your scenario code here, e.g.
  36. /// Win.Add (new Button ("Press me!") {
  37. /// X = Pos.Center (),
  38. /// Y = Pos.Center (),
  39. /// Clicked = () => MessageBox.Query (20, 7, "Hi", "Neat?", "Yes", "No")
  40. /// });
  41. /// }
  42. /// }
  43. /// }
  44. /// </code>
  45. /// </example>
  46. public class Scenario : IDisposable {
  47. private bool _disposedValue;
  48. public string Theme = "Default";
  49. public string TopLevelColorScheme = "Base";
  50. /// <summary>
  51. /// The Window for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/> in most cases.
  52. /// </summary>
  53. public Window Win { get; set; }
  54. /// <summary>
  55. /// Helper that provides the default <see cref="Terminal.Gui.Window"/> implementation with a frame and
  56. /// label showing the name of the <see cref="Scenario"/> and logic to exit back to
  57. /// the Scenario picker UI.
  58. /// Override <see cref="Init"/> to provide any <see cref="Terminal.Gui.Toplevel"/> behavior needed.
  59. /// </summary>
  60. /// <remarks>
  61. /// <para>
  62. /// The base implementation calls <see cref="Application.Init"/> and creates a <see cref="Window"/> for <see cref="Win"/>
  63. /// and adds it to <see cref="Application.Top"/>.
  64. /// </para>
  65. /// <para>
  66. /// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Init"/>
  67. /// before creating any views or calling other Terminal.Gui APIs.
  68. /// </para>
  69. /// </remarks>
  70. public virtual void Init ()
  71. {
  72. Application.Init ();
  73. ConfigurationManager.Themes.Theme = Theme;
  74. ConfigurationManager.Apply ();
  75. Win = new Window ($"{Application.QuitKey} to Quit - Scenario: {GetName ()}") {
  76. X = 0,
  77. Y = 0,
  78. Width = Dim.Fill (),
  79. Height = Dim.Fill (),
  80. ColorScheme = Colors.ColorSchemes [TopLevelColorScheme],
  81. };
  82. Application.Top.Add (Win);
  83. }
  84. /// <summary>
  85. /// Defines the metadata (Name and Description) for a <see cref="Scenario"/>
  86. /// </summary>
  87. [System.AttributeUsage (System.AttributeTargets.Class)]
  88. public class ScenarioMetadata : System.Attribute {
  89. /// <summary>
  90. /// <see cref="Scenario"/> Name
  91. /// </summary>
  92. public string Name { get; set; }
  93. /// <summary>
  94. /// <see cref="Scenario"/> Description
  95. /// </summary>
  96. public string Description { get; set; }
  97. public ScenarioMetadata (string Name, string Description)
  98. {
  99. this.Name = Name;
  100. this.Description = Description;
  101. }
  102. /// <summary>
  103. /// Static helper function to get the <see cref="Scenario"/> Name given a Type
  104. /// </summary>
  105. /// <param name="t"></param>
  106. /// <returns></returns>
  107. public static string GetName (Type t) => ((ScenarioMetadata)System.Attribute.GetCustomAttributes (t) [0]).Name;
  108. /// <summary>
  109. /// Static helper function to get the <see cref="Scenario"/> Description given a Type
  110. /// </summary>
  111. /// <param name="t"></param>
  112. /// <returns></returns>
  113. public static string GetDescription (Type t) => ((ScenarioMetadata)System.Attribute.GetCustomAttributes (t) [0]).Description;
  114. }
  115. /// <summary>
  116. /// Helper to get the <see cref="Scenario"/> Name (defined in <see cref="ScenarioMetadata"/>)
  117. /// </summary>
  118. /// <returns></returns>
  119. public string GetName () => ScenarioMetadata.GetName (this.GetType ());
  120. /// <summary>
  121. /// Helper to get the <see cref="Scenario"/> Description (defined in <see cref="ScenarioMetadata"/>)
  122. /// </summary>
  123. /// <returns></returns>
  124. public string GetDescription () => ScenarioMetadata.GetDescription (this.GetType ());
  125. /// <summary>
  126. /// Defines the category names used to catagorize a <see cref="Scenario"/>
  127. /// </summary>
  128. [System.AttributeUsage (System.AttributeTargets.Class, AllowMultiple = true)]
  129. public class ScenarioCategory : System.Attribute {
  130. /// <summary>
  131. /// Category Name
  132. /// </summary>
  133. public string Name { get; set; }
  134. public ScenarioCategory (string Name) => this.Name = Name;
  135. /// <summary>
  136. /// Static helper function to get the <see cref="Scenario"/> Name given a Type
  137. /// </summary>
  138. /// <param name="t"></param>
  139. /// <returns>Name of the category</returns>
  140. public static string GetName (Type t) => ((ScenarioCategory)System.Attribute.GetCustomAttributes (t) [0]).Name;
  141. /// <summary>
  142. /// Static helper function to get the <see cref="Scenario"/> Categories given a Type
  143. /// </summary>
  144. /// <param name="t"></param>
  145. /// <returns>list of category names</returns>
  146. public static List<string> GetCategories (Type t) => System.Attribute.GetCustomAttributes (t)
  147. .ToList ()
  148. .Where (a => a is ScenarioCategory)
  149. .Select (a => ((ScenarioCategory)a).Name)
  150. .ToList ();
  151. }
  152. /// <summary>
  153. /// Helper function to get the list of categories a <see cref="Scenario"/> belongs to (defined in <see cref="ScenarioCategory"/>)
  154. /// </summary>
  155. /// <returns>list of category names</returns>
  156. public List<string> GetCategories () => ScenarioCategory.GetCategories (this.GetType ());
  157. private static int _maxScenarioNameLen = 30;
  158. /// <summary>
  159. /// Gets the Scenario Name + Description with the Description padded
  160. /// based on the longest known Scenario name.
  161. /// </summary>
  162. /// <returns></returns>
  163. public override string ToString () => $"{GetName ().PadRight(_maxScenarioNameLen)}{GetDescription ()}";
  164. /// <summary>
  165. /// Override this to implement the <see cref="Scenario"/> setup logic (create controls, etc...).
  166. /// </summary>
  167. /// <remarks>This is typically the best place to put scenario logic code.</remarks>
  168. public virtual void Setup ()
  169. {
  170. }
  171. /// <summary>
  172. /// Runs the <see cref="Scenario"/>. Override to start the <see cref="Scenario"/>
  173. /// using a <see cref="Toplevel"/> different than `Top`.
  174. /// </summary>
  175. /// <remarks>
  176. /// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Shutdown"/> before returning.
  177. /// </remarks>
  178. public virtual void Run ()
  179. {
  180. // Must explicit call Application.Shutdown method to shutdown.
  181. Application.Run (Application.Top);
  182. }
  183. /// <summary>
  184. /// Stops the scenario. Override to change shutdown behavior for the <see cref="Scenario"/>.
  185. /// </summary>
  186. public virtual void RequestStop ()
  187. {
  188. Application.RequestStop ();
  189. }
  190. /// <summary>
  191. /// Returns a list of all Categories set by all of the <see cref="Scenario"/>s defined in the project.
  192. /// </summary>
  193. internal static List<string> GetAllCategories ()
  194. {
  195. List<string> categories = new List<string> ();
  196. foreach (Type type in typeof (Scenario).Assembly.GetTypes ()
  197. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf (typeof (Scenario)))) {
  198. List<System.Attribute> attrs = System.Attribute.GetCustomAttributes (type).ToList ();
  199. categories = categories.Union (attrs.Where (a => a is ScenarioCategory).Select (a => ((ScenarioCategory)a).Name)).ToList ();
  200. }
  201. // Sort
  202. categories = categories.OrderBy (c => c).ToList ();
  203. // Put "All" at the top
  204. categories.Insert (0, "All Scenarios");
  205. return categories;
  206. }
  207. /// <summary>
  208. /// Returns a list of all <see cref="Scenario"/> instanaces defined in the project, sorted by <see cref="ScenarioMetadata.Name"/>.
  209. /// https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class
  210. /// </summary>
  211. public static List<Scenario> GetScenarios ()
  212. {
  213. List<Scenario> objects = new List<Scenario> ();
  214. foreach (Type type in typeof (Scenario).Assembly.ExportedTypes
  215. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf (typeof (Scenario)))) {
  216. var scenario = (Scenario)Activator.CreateInstance (type);
  217. objects.Add (scenario);
  218. _maxScenarioNameLen = Math.Max (_maxScenarioNameLen, scenario.GetName ().Length + 1);
  219. }
  220. return objects.OrderBy (s => s.GetName ()).ToList ();
  221. }
  222. protected virtual void Dispose (bool disposing)
  223. {
  224. if (!_disposedValue) {
  225. if (disposing) {
  226. // TODO: dispose managed state (managed objects)
  227. }
  228. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  229. // TODO: set large fields to null
  230. _disposedValue = true;
  231. }
  232. }
  233. public void Dispose ()
  234. {
  235. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  236. Dispose (disposing: true);
  237. GC.SuppressFinalize (this);
  238. }
  239. }
  240. }