Scenario.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 sceanrio belongs to. If you don't specify a category the sceanrio 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)"/> 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 Top level for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/> in most cases.
  50. /// </summary>
  51. public Toplevel Top { get; set; }
  52. /// <summary>
  53. /// The Window for the <see cref="Scenario"/>. This should be set within the <see cref="Terminal.Gui.Application.Top"/> in most cases.
  54. /// </summary>
  55. public Window Win { get; set; }
  56. /// <summary>
  57. /// Helper that provides the default <see cref="Terminal.Gui.Window"/> implementation with a frame and
  58. /// label showing the name of the <see cref="Scenario"/> and logic to exit back to
  59. /// the Scenario picker UI.
  60. /// Override <see cref="Init(Toplevel)"/> to provide any <see cref="Terminal.Gui.Toplevel"/> behavior needed.
  61. /// </summary>
  62. /// <param name="top"></param>
  63. /// <remarks>
  64. /// <para>
  65. /// Thg base implementation calls <see cref="Application.Init"/>, sets <see cref="Top"/> to the passed in <see cref="Toplevel"/>, creates a <see cref="Window"/> for <see cref="Win"/> and adds it to <see cref="Top"/>.
  66. /// </para>
  67. /// <para>
  68. /// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Init "/> before creating any views or calling other Terminal.Gui APIs.
  69. /// </para>
  70. /// </remarks>
  71. public virtual void Init(Toplevel top)
  72. {
  73. Application.Init ();
  74. Top = top;
  75. if (Top == null) {
  76. Top = Application.Top;
  77. }
  78. Win = new Window ($"CTRL-Q to Close - Scenario: {GetName ()}") {
  79. X = 0,
  80. Y = 0,
  81. Width = Dim.Fill (),
  82. Height = Dim.Fill ()
  83. };
  84. Top.Add (Win);
  85. }
  86. /// <summary>
  87. /// Defines the metadata (Name and Description) for a <see cref="Scenario"/>
  88. /// </summary>
  89. [System.AttributeUsage (System.AttributeTargets.Class)]
  90. public class ScenarioMetadata : System.Attribute {
  91. /// <summary>
  92. /// <see cref="Scenario"/> Name
  93. /// </summary>
  94. public string Name { get; set; }
  95. /// <summary>
  96. /// <see cref="Scenario"/> Description
  97. /// </summary>
  98. public string Description { get; set; }
  99. public ScenarioMetadata (string Name, string Description)
  100. {
  101. this.Name = Name;
  102. this.Description = Description;
  103. }
  104. /// <summary>
  105. /// Static helper function to get the <see cref="Scenario"/> Name given a Type
  106. /// </summary>
  107. /// <param name="t"></param>
  108. /// <returns></returns>
  109. public static string GetName (Type t) => ((ScenarioMetadata)System.Attribute.GetCustomAttributes (t) [0]).Name;
  110. /// <summary>
  111. /// Static helper function to get the <see cref="Scenario"/> Description given a Type
  112. /// </summary>
  113. /// <param name="t"></param>
  114. /// <returns></returns>
  115. public static string GetDescription (Type t) => ((ScenarioMetadata)System.Attribute.GetCustomAttributes (t) [0]).Description;
  116. }
  117. /// <summary>
  118. /// Helper to get the <see cref="Scenario"/> Name (defined in <see cref="ScenarioMetadata"/>)
  119. /// </summary>
  120. /// <returns></returns>
  121. public string GetName () => ScenarioMetadata.GetName (this.GetType ());
  122. /// <summary>
  123. /// Helper to get the <see cref="Scenario"/> Description (defined in <see cref="ScenarioMetadata"/>)
  124. /// </summary>
  125. /// <returns></returns>
  126. public string GetDescription () => ScenarioMetadata.GetDescription (this.GetType ());
  127. /// <summary>
  128. /// Defines the category names used to catagorize a <see cref="Scenario"/>
  129. /// </summary>
  130. [System.AttributeUsage (System.AttributeTargets.Class, AllowMultiple = true)]
  131. public class ScenarioCategory : System.Attribute {
  132. /// <summary>
  133. /// Category Name
  134. /// </summary>
  135. public string Name { get; set; }
  136. public ScenarioCategory (string Name) => this.Name = Name;
  137. /// <summary>
  138. /// Static helper function to get the <see cref="Scenario"/> Name given a Type
  139. /// </summary>
  140. /// <param name="t"></param>
  141. /// <returns>Name of the catagory</returns>
  142. public static string GetName (Type t) => ((ScenarioCategory)System.Attribute.GetCustomAttributes (t) [0]).Name;
  143. /// <summary>
  144. /// Static helper function to get the <see cref="Scenario"/> Categories given a Type
  145. /// </summary>
  146. /// <param name="t"></param>
  147. /// <returns>list of catagory names</returns>
  148. public static List<string> GetCategories (Type t) => System.Attribute.GetCustomAttributes (t)
  149. .ToList ()
  150. .Where (a => a is ScenarioCategory)
  151. .Select (a => ((ScenarioCategory)a).Name)
  152. .ToList ();
  153. }
  154. /// <summary>
  155. /// Helper function to get the list of categories a <see cref="Scenario"/> belongs to (defined in <see cref="ScenarioCategory"/>)
  156. /// </summary>
  157. /// <returns>list of catagory names</returns>
  158. public List<string> GetCategories () => ScenarioCategory.GetCategories (this.GetType ());
  159. /// <inheritdoc/>
  160. public override string ToString () => $"{GetName (),-30}{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. // This method already performs a later automatic shutdown.
  178. Application.Run (Top, false);
  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> () { "All" };
  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. return categories;
  199. }
  200. /// <summary>
  201. /// Returns an instance of each <see cref="Scenario"/> defined in the project.
  202. /// https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class
  203. /// </summary>
  204. internal static List<Type> GetDerivedClassesCollection ()
  205. {
  206. List<Type> objects = new List<Type> ();
  207. foreach (Type type in typeof (Scenario).Assembly.GetTypes ()
  208. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf (typeof (Scenario)))) {
  209. objects.Add (type);
  210. }
  211. return objects;
  212. }
  213. protected virtual void Dispose (bool disposing)
  214. {
  215. if (!_disposedValue) {
  216. if (disposing) {
  217. // TODO: dispose managed state (managed objects)
  218. }
  219. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  220. // TODO: set large fields to null
  221. _disposedValue = true;
  222. }
  223. }
  224. public void Dispose ()
  225. {
  226. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  227. Dispose (disposing: true);
  228. GC.SuppressFinalize (this);
  229. }
  230. }
  231. }