AllViewsTester.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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.Current
  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. // If we are to create a generic Type
  194. if (type.IsGenericType)
  195. {
  196. // For each of the <T> arguments
  197. List<Type> typeArguments = [];
  198. // use <object> or the original type if applicable
  199. foreach (Type arg in type.GetGenericArguments ())
  200. {
  201. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  202. {
  203. typeArguments.Add (arg);
  204. }
  205. else
  206. {
  207. typeArguments.Add (typeof (object));
  208. }
  209. }
  210. // And change what type we are instantiating from MyClass<T> to MyClass<object> or MyClass<T>
  211. type = type.MakeGenericType (typeArguments.ToArray ());
  212. }
  213. // Ensure the type does not contain any generic parameters
  214. if (type.ContainsGenericParameters)
  215. {
  216. Logging.Warning ($"Cannot create an instance of {type} because it contains generic parameters.");
  217. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  218. return;
  219. }
  220. // Instantiate view
  221. var view = (View)Activator.CreateInstance (type)!;
  222. _eventLog!.ViewToLog = view;
  223. if (view is IDesignable designable)
  224. {
  225. string settingsEditorDemoText = _propertiesEditor!.DemoText;
  226. designable.EnableForDesign (ref settingsEditorDemoText);
  227. }
  228. else
  229. {
  230. view.Text = _propertiesEditor!.DemoText;
  231. view.Title = "_Test Title";
  232. }
  233. view.Initialized += CurrentView_Initialized;
  234. view.SubViewsLaidOut += CurrentView_LayoutComplete;
  235. view.Id = "_curView";
  236. _curView = view;
  237. _hostPane!.Add (_curView);
  238. _layoutEditor!.ViewToEdit = _curView;
  239. _viewportSettingsEditor!.ViewToEdit = _curView;
  240. _arrangementEditor!.ViewToEdit = _curView;
  241. _propertiesEditor!.ViewToEdit = _curView;
  242. _curView.SetNeedsLayout ();
  243. }
  244. private void DisposeCurrentView ()
  245. {
  246. if (_curView != null)
  247. {
  248. _curView.Initialized -= CurrentView_Initialized;
  249. _curView.SubViewsLaidOut -= CurrentView_LayoutComplete;
  250. _hostPane!.Remove (_curView);
  251. _layoutEditor!.ViewToEdit = null;
  252. _viewportSettingsEditor!.ViewToEdit = null;
  253. _arrangementEditor!.ViewToEdit = null;
  254. _propertiesEditor!.ViewToEdit = null;
  255. _curView.Dispose ();
  256. _curView = null;
  257. }
  258. }
  259. private static List<Type> GetAllViewClassesCollection ()
  260. {
  261. List<Type> types = typeof (View).Assembly.GetTypes ()
  262. .Where (
  263. myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
  264. && myType.IsSubclassOf (typeof (View)))
  265. .ToList ();
  266. types.Add (typeof (View));
  267. return types;
  268. }
  269. private void CurrentView_LayoutComplete (object? sender, LayoutEventArgs args) { UpdateHostTitle (_curView); }
  270. private void UpdateHostTitle (View? view) { _hostPane!.Title = $"{view!.GetType ().Name} [_0]"; }
  271. private void CurrentView_Initialized (object? sender, EventArgs e)
  272. {
  273. if (sender is not View view)
  274. {
  275. return;
  276. }
  277. if (view.Width == Dim.Absolute (0) || view.Width is null)
  278. {
  279. view.Width = Dim.Fill ();
  280. }
  281. if (view.Height == Dim.Absolute (0) || view.Height is null)
  282. {
  283. view.Height = Dim.Fill ();
  284. }
  285. UpdateHostTitle (view);
  286. }
  287. public override List<Key> GetDemoKeyStrokes ()
  288. {
  289. var keys = new List<Key> ();
  290. for (int i = 0; i < GetAllViewClassesCollection ().Count; i++)
  291. {
  292. keys.Add (Key.CursorDown);
  293. }
  294. return keys;
  295. }
  296. }