AllViewsTester.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("All Views Tester", "Provides a test UI for all classes derived from View.")]
  9. [ScenarioCategory ("Layout")]
  10. [ScenarioCategory ("Tests")]
  11. [ScenarioCategory ("Controls")]
  12. [ScenarioCategory ("Adornments")]
  13. [ScenarioCategory ("Arrangement")]
  14. public class AllViewsTester : Scenario
  15. {
  16. private Dictionary<string, Type>? _viewClasses;
  17. private ListView? _classListView;
  18. private AdornmentsEditor? _adornmentsEditor;
  19. private ArrangementEditor? _arrangementEditor;
  20. private LayoutEditor? _layoutEditor;
  21. private ViewportSettingsEditor? _viewportSettingsEditor;
  22. private ViewPropertiesEditor? _propertiesEditor;
  23. private FrameView? _hostPane;
  24. private View? _curView;
  25. private EventLog? _eventLog;
  26. public override void Main ()
  27. {
  28. // Don't create a sub-win (Scenario.Win); just use Application.Top
  29. Application.Init ();
  30. var app = new Window
  31. {
  32. Title = GetQuitKeyAndName (),
  33. };
  34. // Set the BorderStyle we use for all subviews, but disable the app border thickness
  35. app.Border!.LineStyle = LineStyle.Heavy;
  36. app.Border.Thickness = new (0);
  37. _viewClasses = GetAllViewClassesCollection ()
  38. .OrderBy (t => t.Name)
  39. .Select (t => new KeyValuePair<string, Type> (t.Name, t))
  40. .ToDictionary (t => t.Key, t => t.Value);
  41. _classListView = new ()
  42. {
  43. Title = "Classes [_1]",
  44. X = 0,
  45. Y = 0,
  46. Width = Dim.Auto (),
  47. Height = Dim.Fill (),
  48. AllowsMarking = false,
  49. SelectedItem = 0,
  50. Source = new ListWrapper<string> (new (_viewClasses.Keys.ToList ())),
  51. SuperViewRendersLineCanvas = true
  52. };
  53. _classListView.Border!.Thickness = new (1);
  54. _classListView.SelectedItemChanged += (s, args) =>
  55. {
  56. // Dispose existing current View, if any
  57. DisposeCurrentView ();
  58. CreateCurrentView (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  59. // Force ViewToEdit to be the view and not a subview
  60. if (_adornmentsEditor is { })
  61. {
  62. _adornmentsEditor.AutoSelectSuperView = _curView;
  63. _adornmentsEditor.ViewToEdit = _curView;
  64. }
  65. };
  66. _classListView.Accepting += (sender, args) =>
  67. {
  68. _curView?.SetFocus ();
  69. args.Handled = true;
  70. };
  71. _adornmentsEditor = new ()
  72. {
  73. Title = "Adornments [_2]",
  74. X = Pos.Right (_classListView) - 1,
  75. Y = 0,
  76. Width = Dim.Auto (),
  77. Height = Dim.Auto (),
  78. AutoSelectViewToEdit = false,
  79. AutoSelectAdornments = false,
  80. SuperViewRendersLineCanvas = true,
  81. };
  82. _adornmentsEditor.Border!.Thickness = new (1);
  83. _adornmentsEditor.ExpanderButton!.Orientation = Orientation.Horizontal;
  84. _adornmentsEditor.ExpanderButton.Enabled = false;
  85. _arrangementEditor = new ()
  86. {
  87. Title = "Arrangement [_3]",
  88. X = Pos.Right (_classListView) - 1,
  89. Y = Pos.Bottom (_adornmentsEditor) - Pos.Func (() => _adornmentsEditor.Frame.Height == 1 ? 0 : 1),
  90. Width = Dim.Width (_adornmentsEditor),
  91. Height = Dim.Fill (),
  92. AutoSelectViewToEdit = false,
  93. AutoSelectAdornments = false,
  94. SuperViewRendersLineCanvas = true
  95. };
  96. _arrangementEditor.ExpanderButton!.Orientation = Orientation.Horizontal;
  97. _arrangementEditor.ExpanderButton.CollapsedChanging += (sender, args) =>
  98. {
  99. _adornmentsEditor.ExpanderButton.Collapsed = args.NewValue;
  100. };
  101. _arrangementEditor.Border!.Thickness = new (1);
  102. _layoutEditor = new ()
  103. {
  104. Title = "Layout [_4]",
  105. X = Pos.Right (_arrangementEditor) - 1,
  106. Y = 0,
  107. //Width = Dim.Fill (), // set below
  108. Height = Dim.Auto (),
  109. CanFocus = true,
  110. AutoSelectViewToEdit = false,
  111. AutoSelectAdornments = false,
  112. SuperViewRendersLineCanvas = true
  113. };
  114. _layoutEditor.Border!.Thickness = new (1, 1, 1, 0);
  115. _viewportSettingsEditor = new ()
  116. {
  117. Title = "ViewportSettings [_5]",
  118. X = Pos.Right (_arrangementEditor) - 1,
  119. Y = Pos.Bottom (_layoutEditor) - Pos.Func (() => _layoutEditor.Frame.Height == 1 ? 0 : 1),
  120. Width = Dim.Width (_layoutEditor),
  121. Height = Dim.Auto (),
  122. CanFocus = true,
  123. AutoSelectViewToEdit = false,
  124. AutoSelectAdornments = false,
  125. SuperViewRendersLineCanvas = true
  126. };
  127. _viewportSettingsEditor.Border!.Thickness = new (1, 1, 1, 1);
  128. _propertiesEditor = new ()
  129. {
  130. Title = "View Properties [_6]",
  131. X = Pos.Right (_adornmentsEditor) - 1,
  132. Y = Pos.Bottom (_viewportSettingsEditor) - Pos.Func (() => _viewportSettingsEditor.Frame.Height == 1 ? 0 : 1),
  133. Width = Dim.Width (_layoutEditor),
  134. Height = Dim.Auto (),
  135. CanFocus = true,
  136. SuperViewRendersLineCanvas = true
  137. };
  138. _propertiesEditor.Border!.Thickness = new (1, 1, 1, 0);
  139. _eventLog = new ()
  140. {
  141. // X = Pos.Right(_layoutEditor),
  142. SuperViewRendersLineCanvas = true
  143. };
  144. _eventLog.Border!.Thickness = new (1);
  145. _eventLog.X = Pos.AnchorEnd () - 1;
  146. _eventLog.Y = 0;
  147. _eventLog.Height = Dim.Height (_classListView);
  148. //_eventLog.Width = 30;
  149. _layoutEditor.Width = Dim.Fill (
  150. Dim.Func (
  151. () =>
  152. {
  153. if (_eventLog.NeedsLayout)
  154. {
  155. // We have two choices:
  156. // 1) Call Layout explicitly
  157. // 2) Throw LayoutException so Layout tries again
  158. _eventLog.Layout ();
  159. //throw new LayoutException ("_eventLog");
  160. }
  161. return _eventLog.Frame.Width;
  162. }));
  163. _hostPane = new ()
  164. {
  165. Id = "_hostPane",
  166. X = Pos.Right (_adornmentsEditor),
  167. Y = Pos.Bottom (_propertiesEditor),
  168. Width = Dim.Width (_layoutEditor) - 2,
  169. Height = Dim.Fill (),
  170. CanFocus = true,
  171. TabStop = TabBehavior.TabStop,
  172. //SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Base),
  173. Arrangement = ViewArrangement.LeftResizable | ViewArrangement.BottomResizable | ViewArrangement.RightResizable,
  174. BorderStyle = LineStyle.Double,
  175. SuperViewRendersLineCanvas = true
  176. };
  177. _hostPane.Border!.SetScheme (app.GetScheme ());
  178. _hostPane.Padding!.Thickness = new (1);
  179. _hostPane.Padding.Diagnostics = ViewDiagnosticFlags.Ruler;
  180. _hostPane.Padding.SetScheme (app.GetScheme ());
  181. app.Add (_classListView, _adornmentsEditor, _arrangementEditor, _layoutEditor, _viewportSettingsEditor, _propertiesEditor, _eventLog, _hostPane);
  182. app.Initialized += App_Initialized;
  183. Application.Run (app);
  184. app.Dispose ();
  185. Application.Shutdown ();
  186. }
  187. private void App_Initialized (object? sender, EventArgs e)
  188. {
  189. _classListView!.SelectedItem = 0;
  190. _classListView.SetFocus ();
  191. }
  192. // TODO: Add Command.HotKey handler (pop a message box?)
  193. private void CreateCurrentView (Type type)
  194. {
  195. Debug.Assert (_curView is null);
  196. // If we are to create a generic Type
  197. if (type.IsGenericType)
  198. {
  199. // For each of the <T> arguments
  200. List<Type> typeArguments = new ();
  201. // use <object> or the original type if applicable
  202. foreach (Type arg in type.GetGenericArguments ())
  203. {
  204. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  205. {
  206. typeArguments.Add (arg);
  207. }
  208. else
  209. {
  210. typeArguments.Add (typeof (object));
  211. }
  212. }
  213. // And change what type we are instantiating from MyClass<T> to MyClass<object> or MyClass<T>
  214. type = type.MakeGenericType (typeArguments.ToArray ());
  215. }
  216. // Ensure the type does not contain any generic parameters
  217. if (type.ContainsGenericParameters)
  218. {
  219. Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
  220. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  221. return;
  222. }
  223. // Instantiate view
  224. var view = (View)Activator.CreateInstance (type)!;
  225. _eventLog!.ViewToLog = view;
  226. if (view is IDesignable designable)
  227. {
  228. string settingsEditorDemoText = _propertiesEditor!.DemoText;
  229. designable.EnableForDesign (ref settingsEditorDemoText);
  230. }
  231. else
  232. {
  233. view.Text = _propertiesEditor!.DemoText;
  234. view.Title = "_Test Title";
  235. }
  236. view.Initialized += CurrentView_Initialized;
  237. view.SubViewsLaidOut += CurrentView_LayoutComplete;
  238. view.Id = "_curView";
  239. _curView = view;
  240. _hostPane!.Add (_curView);
  241. _layoutEditor!.ViewToEdit = _curView;
  242. _viewportSettingsEditor!.ViewToEdit = _curView;
  243. _arrangementEditor!.ViewToEdit = _curView;
  244. _propertiesEditor!.ViewToEdit = _curView;
  245. _curView.SetNeedsLayout ();
  246. }
  247. private void DisposeCurrentView ()
  248. {
  249. if (_curView != null)
  250. {
  251. _curView.Initialized -= CurrentView_Initialized;
  252. _curView.SubViewsLaidOut -= CurrentView_LayoutComplete;
  253. _hostPane!.Remove (_curView);
  254. _layoutEditor!.ViewToEdit = null;
  255. _viewportSettingsEditor!.ViewToEdit = null;
  256. _arrangementEditor!.ViewToEdit = null;
  257. _propertiesEditor!.ViewToEdit = null;
  258. _curView.Dispose ();
  259. _curView = null;
  260. }
  261. }
  262. private static List<Type> GetAllViewClassesCollection ()
  263. {
  264. List<Type> types = typeof (View).Assembly.GetTypes ()
  265. .Where (
  266. myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
  267. && myType.IsSubclassOf (typeof (View)))
  268. .ToList ();
  269. types.Add (typeof (View));
  270. return types;
  271. }
  272. private void CurrentView_LayoutComplete (object? sender, LayoutEventArgs args) { UpdateHostTitle (_curView); }
  273. private void UpdateHostTitle (View? view) { _hostPane!.Title = $"{view!.GetType ().Name} [_0]"; }
  274. private void CurrentView_Initialized (object? sender, EventArgs e)
  275. {
  276. if (sender is not View view)
  277. {
  278. return;
  279. }
  280. if (view.Width == Dim.Absolute (0) || view.Width is null)
  281. {
  282. view.Width = Dim.Fill ();
  283. }
  284. if (view.Height == Dim.Absolute (0) || view.Height is null)
  285. {
  286. view.Height = Dim.Fill ();
  287. }
  288. UpdateHostTitle (view);
  289. }
  290. public override List<Key> GetDemoKeyStrokes ()
  291. {
  292. var keys = new List<Key> ();
  293. for (int i = 0; i < GetAllViewClassesCollection ().Count; i++)
  294. {
  295. keys.Add (Key.CursorDown);
  296. }
  297. return keys;
  298. }
  299. }