AllViewsTester.cs 13 KB

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