Scenario.cs 10 KB

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