AllViewsTester.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.Top
  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]);
  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.Right(_layoutEditor),
  141. SuperViewRendersLineCanvas = true
  142. };
  143. _eventLog.Border!.Thickness = new (1);
  144. _eventLog.X = Pos.AnchorEnd () - 1;
  145. _eventLog.Y = 0;
  146. _eventLog.Height = Dim.Height (_classListView);
  147. //_eventLog.Width = 30;
  148. _layoutEditor.Width = Dim.Fill (
  149. Dim.Func (
  150. _ =>
  151. {
  152. if (_eventLog.NeedsLayout)
  153. {
  154. // We have two choices:
  155. // 1) Call Layout explicitly
  156. // 2) Throw LayoutException so Layout tries again
  157. _eventLog.Layout ();
  158. //throw new LayoutException ("_eventLog");
  159. }
  160. return _eventLog.Frame.Width;
  161. }));
  162. _hostPane = new ()
  163. {
  164. Id = "_hostPane",
  165. X = Pos.Right (_adornmentsEditor),
  166. Y = Pos.Bottom (_propertiesEditor),
  167. Width = Dim.Width (_layoutEditor) - 2,
  168. Height = Dim.Fill (),
  169. CanFocus = true,
  170. TabStop = TabBehavior.TabStop,
  171. //SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Base),
  172. Arrangement = ViewArrangement.LeftResizable | ViewArrangement.BottomResizable | ViewArrangement.RightResizable,
  173. BorderStyle = LineStyle.Double,
  174. SuperViewRendersLineCanvas = true
  175. };
  176. _hostPane.Border!.SetScheme (app.GetScheme ());
  177. _hostPane.Padding!.Thickness = new (1);
  178. _hostPane.Padding.Diagnostics = ViewDiagnosticFlags.Ruler;
  179. _hostPane.Padding.SetScheme (app.GetScheme ());
  180. app.Add (_classListView, _adornmentsEditor, _arrangementEditor, _layoutEditor, _viewportSettingsEditor, _propertiesEditor, _eventLog, _hostPane);
  181. app.Initialized += App_Initialized;
  182. Application.Run (app);
  183. app.Dispose ();
  184. Application.Shutdown ();
  185. }
  186. private void App_Initialized (object? sender, EventArgs e)
  187. {
  188. _classListView!.SelectedItem = 0;
  189. _classListView.SetFocus ();
  190. }
  191. // TODO: Add Command.HotKey handler (pop a message box?)
  192. private void CreateCurrentView (Type type)
  193. {
  194. Debug.Assert (_curView is null);
  195. // If we are to create a generic Type
  196. if (type.IsGenericType)
  197. {
  198. // For each of the <T> arguments
  199. List<Type> typeArguments = new ();
  200. // use <object> or the original type if applicable
  201. foreach (Type arg in type.GetGenericArguments ())
  202. {
  203. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  204. {
  205. typeArguments.Add (arg);
  206. }
  207. else
  208. {
  209. typeArguments.Add (typeof (object));
  210. }
  211. }
  212. // And change what type we are instantiating from MyClass<T> to MyClass<object> or MyClass<T>
  213. type = type.MakeGenericType (typeArguments.ToArray ());
  214. }
  215. // Ensure the type does not contain any generic parameters
  216. if (type.ContainsGenericParameters)
  217. {
  218. Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
  219. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  220. return;
  221. }
  222. // Instantiate view
  223. var view = (View)Activator.CreateInstance (type)!;
  224. _eventLog!.ViewToLog = view;
  225. if (view is IDesignable designable)
  226. {
  227. string settingsEditorDemoText = _propertiesEditor!.DemoText;
  228. designable.EnableForDesign (ref settingsEditorDemoText);
  229. }
  230. else
  231. {
  232. view.Text = _propertiesEditor!.DemoText;
  233. view.Title = "_Test Title";
  234. }
  235. view.Initialized += CurrentView_Initialized;
  236. view.SubViewsLaidOut += CurrentView_LayoutComplete;
  237. view.Id = "_curView";
  238. _curView = view;
  239. _hostPane!.Add (_curView);
  240. _layoutEditor!.ViewToEdit = _curView;
  241. _viewportSettingsEditor!.ViewToEdit = _curView;
  242. _arrangementEditor!.ViewToEdit = _curView;
  243. _propertiesEditor!.ViewToEdit = _curView;
  244. _curView.SetNeedsLayout ();
  245. }
  246. private void DisposeCurrentView ()
  247. {
  248. if (_curView != null)
  249. {
  250. _curView.Initialized -= CurrentView_Initialized;
  251. _curView.SubViewsLaidOut -= CurrentView_LayoutComplete;
  252. _hostPane!.Remove (_curView);
  253. _layoutEditor!.ViewToEdit = null;
  254. _viewportSettingsEditor!.ViewToEdit = null;
  255. _arrangementEditor!.ViewToEdit = null;
  256. _propertiesEditor!.ViewToEdit = null;
  257. _curView.Dispose ();
  258. _curView = null;
  259. }
  260. }
  261. private static List<Type> GetAllViewClassesCollection ()
  262. {
  263. List<Type> types = typeof (View).Assembly.GetTypes ()
  264. .Where (
  265. myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
  266. && myType.IsSubclassOf (typeof (View)))
  267. .ToList ();
  268. types.Add (typeof (View));
  269. return types;
  270. }
  271. private void CurrentView_LayoutComplete (object? sender, LayoutEventArgs args) { UpdateHostTitle (_curView); }
  272. private void UpdateHostTitle (View? view) { _hostPane!.Title = $"{view!.GetType ().Name} [_0]"; }
  273. private void CurrentView_Initialized (object? sender, EventArgs e)
  274. {
  275. if (sender is not View view)
  276. {
  277. return;
  278. }
  279. if (view.Width == Dim.Absolute (0) || view.Width is null)
  280. {
  281. view.Width = Dim.Fill ();
  282. }
  283. if (view.Height == Dim.Absolute (0) || view.Height is null)
  284. {
  285. view.Height = Dim.Fill ();
  286. }
  287. UpdateHostTitle (view);
  288. }
  289. public override List<Key> GetDemoKeyStrokes ()
  290. {
  291. var keys = new List<Key> ();
  292. for (int i = 0; i < GetAllViewClassesCollection ().Count; i++)
  293. {
  294. keys.Add (Key.CursorDown);
  295. }
  296. return keys;
  297. }
  298. }