Scenario.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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(Toplevel, 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 CTRL-Q to exit it. /
  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. /// <summary>
  49. /// The Window for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/> in most cases.
  50. /// </summary>
  51. public Window Win { get; set; }
  52. /// <summary>
  53. /// Helper that provides the default <see cref="Terminal.Gui.Window"/> implementation with a frame and
  54. /// label showing the name of the <see cref="Scenario"/> and logic to exit back to
  55. /// the Scenario picker UI.
  56. /// Override <see cref="Init"/> to provide any <see cref="Terminal.Gui.Toplevel"/> behavior needed.
  57. /// </summary>
  58. /// <param name="colorScheme">The colorscheme to use.</param>
  59. /// <remarks>
  60. /// <para>
  61. /// The base implementation calls <see cref="Application.Init"/> and creates a <see cref="Window"/> for <see cref="Win"/>
  62. /// and adds it to <see cref="Application.Top"/>.
  63. /// </para>
  64. /// <para>
  65. /// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Init"/>
  66. /// before creating any views or calling other Terminal.Gui APIs.
  67. /// </para>
  68. /// </remarks>
  69. public virtual void Init (ColorScheme colorScheme)
  70. {
  71. Application.Init ();
  72. Win = new Window ($"CTRL-Q to Close - Scenario: {GetName ()}") {
  73. X = 0,
  74. Y = 0,
  75. Width = Dim.Fill (),
  76. Height = Dim.Fill (),
  77. ColorScheme = colorScheme,
  78. };
  79. Application.Top.Add (Win);
  80. }
  81. /// <summary>
  82. /// Defines the metadata (Name and Description) for a <see cref="Scenario"/>
  83. /// </summary>
  84. [System.AttributeUsage (System.AttributeTargets.Class)]
  85. public class ScenarioMetadata : System.Attribute {
  86. /// <summary>
  87. /// <see cref="Scenario"/> Name
  88. /// </summary>
  89. public string Name { get; set; }
  90. /// <summary>
  91. /// <see cref="Scenario"/> Description
  92. /// </summary>
  93. public string Description { get; set; }
  94. public ScenarioMetadata (string Name, string Description)
  95. {
  96. this.Name = Name;
  97. this.Description = Description;
  98. }
  99. /// <summary>
  100. /// Static helper function to get the <see cref="Scenario"/> Name given a Type
  101. /// </summary>
  102. /// <param name="t"></param>
  103. /// <returns></returns>
  104. public static string GetName (Type t) => ((ScenarioMetadata)System.Attribute.GetCustomAttributes (t) [0]).Name;
  105. /// <summary>
  106. /// Static helper function to get the <see cref="Scenario"/> Description given a Type
  107. /// </summary>
  108. /// <param name="t"></param>
  109. /// <returns></returns>
  110. public static string GetDescription (Type t) => ((ScenarioMetadata)System.Attribute.GetCustomAttributes (t) [0]).Description;
  111. }
  112. /// <summary>
  113. /// Helper to get the <see cref="Scenario"/> Name (defined in <see cref="ScenarioMetadata"/>)
  114. /// </summary>
  115. /// <returns></returns>
  116. public string GetName () => ScenarioMetadata.GetName (this.GetType ());
  117. /// <summary>
  118. /// Helper to get the <see cref="Scenario"/> Description (defined in <see cref="ScenarioMetadata"/>)
  119. /// </summary>
  120. /// <returns></returns>
  121. public string GetDescription () => ScenarioMetadata.GetDescription (this.GetType ());
  122. /// <summary>
  123. /// Defines the category names used to catagorize a <see cref="Scenario"/>
  124. /// </summary>
  125. [System.AttributeUsage (System.AttributeTargets.Class, AllowMultiple = true)]
  126. public class ScenarioCategory : System.Attribute {
  127. /// <summary>
  128. /// Category Name
  129. /// </summary>
  130. public string Name { get; set; }
  131. public ScenarioCategory (string Name) => this.Name = Name;
  132. /// <summary>
  133. /// Static helper function to get the <see cref="Scenario"/> Name given a Type
  134. /// </summary>
  135. /// <param name="t"></param>
  136. /// <returns>Name of the category</returns>
  137. public static string GetName (Type t) => ((ScenarioCategory)System.Attribute.GetCustomAttributes (t) [0]).Name;
  138. /// <summary>
  139. /// Static helper function to get the <see cref="Scenario"/> Categories given a Type
  140. /// </summary>
  141. /// <param name="t"></param>
  142. /// <returns>list of category names</returns>
  143. public static List<string> GetCategories (Type t) => System.Attribute.GetCustomAttributes (t)
  144. .ToList ()
  145. .Where (a => a is ScenarioCategory)
  146. .Select (a => ((ScenarioCategory)a).Name)
  147. .ToList ();
  148. }
  149. /// <summary>
  150. /// Helper function to get the list of categories a <see cref="Scenario"/> belongs to (defined in <see cref="ScenarioCategory"/>)
  151. /// </summary>
  152. /// <returns>list of category names</returns>
  153. public List<string> GetCategories () => ScenarioCategory.GetCategories (this.GetType ());
  154. private static int _maxScenarioNameLen = 30;
  155. /// <summary>
  156. /// Gets the Scenario Name + Description with the Description padded
  157. /// based on the longest known Scenario name.
  158. /// </summary>
  159. /// <returns></returns>
  160. public override string ToString () => $"{GetName ().PadRight(_maxScenarioNameLen)}{GetDescription ()}";
  161. /// <summary>
  162. /// Override this to implement the <see cref="Scenario"/> setup logic (create controls, etc...).
  163. /// </summary>
  164. /// <remarks>This is typically the best place to put scenario logic code.</remarks>
  165. public virtual void Setup ()
  166. {
  167. }
  168. /// <summary>
  169. /// Runs the <see cref="Scenario"/>. Override to start the <see cref="Scenario"/> using a <see cref="Toplevel"/> different than `Top`.
  170. ///
  171. /// </summary>
  172. /// <remarks>
  173. /// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Shutdown"/> before returning.
  174. /// </remarks>
  175. public virtual void Run ()
  176. {
  177. // Must explicit call Application.Shutdown method to shutdown.
  178. Application.Run (Application.Top);
  179. }
  180. /// <summary>
  181. /// Stops the scenario. Override to change shutdown behavior for the <see cref="Scenario"/>.
  182. /// </summary>
  183. public virtual void RequestStop ()
  184. {
  185. Application.RequestStop ();
  186. }
  187. /// <summary>
  188. /// Returns a list of all Categories set by all of the <see cref="Scenario"/>s defined in the project.
  189. /// </summary>
  190. internal static List<string> GetAllCategories ()
  191. {
  192. List<string> categories = new List<string> ();
  193. foreach (Type type in typeof (Scenario).Assembly.GetTypes ()
  194. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf (typeof (Scenario)))) {
  195. List<System.Attribute> attrs = System.Attribute.GetCustomAttributes (type).ToList ();
  196. categories = categories.Union (attrs.Where (a => a is ScenarioCategory).Select (a => ((ScenarioCategory)a).Name)).ToList ();
  197. }
  198. // Sort
  199. categories = categories.OrderBy (c => c).ToList ();
  200. // Put "All" at the top
  201. categories.Insert (0, "All Scenarios");
  202. return categories;
  203. }
  204. /// <summary>
  205. /// Returns a list of all <see cref="Scenario"/> instanaces defined in the project, sorted by <see cref="ScenarioMetadata.Name"/>.
  206. /// https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class
  207. /// </summary>
  208. public static List<Scenario> GetScenarios ()
  209. {
  210. List<Scenario> objects = new List<Scenario> ();
  211. foreach (Type type in typeof (Scenario).Assembly.ExportedTypes
  212. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf (typeof (Scenario)))) {
  213. var scenario = (Scenario)Activator.CreateInstance (type);
  214. objects.Add (scenario);
  215. _maxScenarioNameLen = Math.Max (_maxScenarioNameLen, scenario.GetName ().Length + 1);
  216. }
  217. return objects.OrderBy (s => s.GetName ()).ToList ();
  218. }
  219. protected virtual void Dispose (bool disposing)
  220. {
  221. if (!_disposedValue) {
  222. if (disposing) {
  223. // TODO: dispose managed state (managed objects)
  224. }
  225. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  226. // TODO: set large fields to null
  227. _disposedValue = true;
  228. }
  229. }
  230. public void Dispose ()
  231. {
  232. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  233. Dispose (disposing: true);
  234. GC.SuppressFinalize (this);
  235. }
  236. }
  237. }