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