AllViewsTester.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 FrameView? _settingsPane;
  23. private RadioGroup? _orientation;
  24. private string _demoText = "This, that, and the other thing.";
  25. private TextView? _demoTextView;
  26. private FrameView? _hostPane;
  27. private View? _curView;
  28. private EventLog? _eventLog;
  29. public override void Main ()
  30. {
  31. // Don't create a sub-win (Scenario.Win); just use Application.Top
  32. Application.Init ();
  33. var app = new Window
  34. {
  35. Title = GetQuitKeyAndName (),
  36. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  37. };
  38. // Set the BorderStyle we use for all subviews, but disable the app border thickness
  39. app.Border!.LineStyle = LineStyle.Heavy;
  40. app.Border.Thickness = new (0);
  41. _viewClasses = GetAllViewClassesCollection ()
  42. .OrderBy (t => t.Name)
  43. .Select (t => new KeyValuePair<string, Type> (t.Name, t))
  44. .ToDictionary (t => t.Key, t => t.Value);
  45. _classListView = new ()
  46. {
  47. Title = "Classes [_1]",
  48. X = 0,
  49. Y = 0,
  50. Width = Dim.Auto (),
  51. Height = Dim.Fill (),
  52. AllowsMarking = false,
  53. SelectedItem = 0,
  54. Source = new ListWrapper<string> (new (_viewClasses.Keys.ToList ())),
  55. SuperViewRendersLineCanvas = true
  56. };
  57. _classListView.Border!.Thickness = new (1);
  58. _classListView.SelectedItemChanged += (s, args) =>
  59. {
  60. // Dispose existing current View, if any
  61. DisposeCurrentView ();
  62. CreateCurrentView (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  63. // Force ViewToEdit to be the view and not a subview
  64. if (_adornmentsEditor is { })
  65. {
  66. _adornmentsEditor.AutoSelectSuperView = _curView;
  67. _adornmentsEditor.ViewToEdit = _curView;
  68. }
  69. };
  70. _classListView.Accepting += (sender, args) =>
  71. {
  72. _curView?.SetFocus ();
  73. args.Cancel = true;
  74. };
  75. _adornmentsEditor = new ()
  76. {
  77. Title = "Adornments [_2]",
  78. X = Pos.Right (_classListView) - 1,
  79. Y = 0,
  80. Width = Dim.Auto (),
  81. Height = Dim.Auto (),
  82. AutoSelectViewToEdit = false,
  83. AutoSelectAdornments = false,
  84. SuperViewRendersLineCanvas = true,
  85. };
  86. _adornmentsEditor.Border!.Thickness = new (1);
  87. _adornmentsEditor.ExpanderButton!.Orientation = Orientation.Horizontal;
  88. _adornmentsEditor.ExpanderButton.Enabled = false;
  89. _arrangementEditor = new ()
  90. {
  91. Title = "Arrangement [_3]",
  92. X = Pos.Right (_classListView) - 1,
  93. Y = Pos.Bottom (_adornmentsEditor) - Pos.Func (() => _adornmentsEditor.Frame.Height == 1 ? 0 : 1),
  94. Width = Dim.Width (_adornmentsEditor),
  95. Height = Dim.Fill (),
  96. AutoSelectViewToEdit = false,
  97. AutoSelectAdornments = false,
  98. SuperViewRendersLineCanvas = true
  99. };
  100. _arrangementEditor.ExpanderButton!.Orientation = Orientation.Horizontal;
  101. _arrangementEditor.ExpanderButton.CollapsedChanging += (sender, args) =>
  102. {
  103. _adornmentsEditor.ExpanderButton.Collapsed = args.NewValue;
  104. };
  105. _arrangementEditor.Border!.Thickness = new (1);
  106. _layoutEditor = new ()
  107. {
  108. Title = "Layout [_4]",
  109. X = Pos.Right (_arrangementEditor) - 1,
  110. Y = 0,
  111. //Width = Dim.Fill (), // set below
  112. Height = Dim.Auto (),
  113. CanFocus = true,
  114. AutoSelectViewToEdit = false,
  115. AutoSelectAdornments = false,
  116. SuperViewRendersLineCanvas = true
  117. };
  118. _layoutEditor.Border!.Thickness = new (1, 1, 1, 0);
  119. _viewportSettingsEditor = new ()
  120. {
  121. Title = "ViewportSettings [_5]",
  122. X = Pos.Right (_arrangementEditor) - 1,
  123. Y = Pos.Bottom (_layoutEditor) - Pos.Func (() => _layoutEditor.Frame.Height == 1 ? 0 : 1),
  124. Width = Dim.Width (_layoutEditor),
  125. Height = Dim.Auto (),
  126. CanFocus = true,
  127. AutoSelectViewToEdit = false,
  128. AutoSelectAdornments = false,
  129. SuperViewRendersLineCanvas = true
  130. };
  131. _viewportSettingsEditor.Border!.Thickness = new (1, 1, 1, 1);
  132. _settingsPane = new ()
  133. {
  134. Title = "Misc Settings [_6]",
  135. X = Pos.Right (_adornmentsEditor) - 1,
  136. Y = Pos.Bottom (_viewportSettingsEditor) - Pos.Func (() => _viewportSettingsEditor.Frame.Height == 1 ? 0 : 1),
  137. Width = Dim.Width (_layoutEditor),
  138. Height = Dim.Auto (),
  139. CanFocus = true,
  140. SuperViewRendersLineCanvas = true
  141. };
  142. _settingsPane.Border!.Thickness = new (1, 1, 1, 0);
  143. Label label = new () { X = 0, Y = 0, Text = "_Orientation:" };
  144. _orientation = new ()
  145. {
  146. X = Pos.Right (label) + 1,
  147. Y = Pos.Top (label),
  148. RadioLabels = new [] { "Horizontal", "Vertical" },
  149. Orientation = Orientation.Horizontal
  150. };
  151. _orientation.SelectedItemChanged += (s, selected) =>
  152. {
  153. if (_curView is IOrientation orientatedView)
  154. {
  155. orientatedView.Orientation = (Orientation)_orientation.SelectedItem;
  156. }
  157. };
  158. _settingsPane.Add (label, _orientation);
  159. label = new () { X = 0, Y = Pos.Bottom (_orientation), Text = "_Text:" };
  160. _demoTextView = new ()
  161. {
  162. X = Pos.Right (label) + 1,
  163. Y = Pos.Top (label),
  164. Width = Dim.Fill (),
  165. Height = Dim.Auto (minimumContentDim: 2),
  166. Text = _demoText
  167. };
  168. _demoTextView.ContentsChanged += (s, e) =>
  169. {
  170. _demoText = _demoTextView.Text;
  171. if (_curView is { })
  172. {
  173. _curView.Text = _demoText;
  174. }
  175. };
  176. _settingsPane.Add (label, _demoTextView);
  177. _eventLog = new ()
  178. {
  179. // X = Pos.Right(_layoutEditor),
  180. SuperViewRendersLineCanvas = true
  181. };
  182. _eventLog.Border!.Thickness = new (1);
  183. _eventLog.X = Pos.AnchorEnd () - 1;
  184. _eventLog.Y = 0;
  185. _eventLog.Height = Dim.Height (_classListView);
  186. //_eventLog.Width = 30;
  187. _layoutEditor.Width = Dim.Fill (
  188. Dim.Func (
  189. () =>
  190. {
  191. if (_eventLog.NeedsLayout)
  192. {
  193. // We have two choices:
  194. // 1) Call Layout explicitly
  195. // 2) Throw LayoutException so Layout tries again
  196. _eventLog.Layout ();
  197. //throw new LayoutException ("_eventLog");
  198. }
  199. return _eventLog.Frame.Width;
  200. }));
  201. _hostPane = new ()
  202. {
  203. Id = "_hostPane",
  204. X = Pos.Right (_adornmentsEditor),
  205. Y = Pos.Bottom (_settingsPane),
  206. Width = Dim.Width (_layoutEditor) - 2,
  207. Height = Dim.Fill (),
  208. CanFocus = true,
  209. TabStop = TabBehavior.TabStop,
  210. ColorScheme = Colors.ColorSchemes ["Base"],
  211. Arrangement = ViewArrangement.LeftResizable | ViewArrangement.BottomResizable | ViewArrangement.RightResizable,
  212. BorderStyle = LineStyle.Double,
  213. SuperViewRendersLineCanvas = true
  214. };
  215. _hostPane.Border!.ColorScheme = app.ColorScheme;
  216. _hostPane.Padding!.Thickness = new (1);
  217. _hostPane.Padding.Diagnostics = ViewDiagnosticFlags.Ruler;
  218. _hostPane.Padding.ColorScheme = app.ColorScheme;
  219. app.Add (_classListView, _adornmentsEditor, _arrangementEditor, _layoutEditor, _viewportSettingsEditor, _settingsPane, _eventLog, _hostPane);
  220. app.Initialized += App_Initialized;
  221. Application.Run (app);
  222. app.Dispose ();
  223. Application.Shutdown ();
  224. }
  225. private void App_Initialized (object? sender, EventArgs e)
  226. {
  227. _classListView!.SelectedItem = 0;
  228. _classListView.SetFocus ();
  229. }
  230. // TODO: Add Command.HotKey handler (pop a message box?)
  231. private void CreateCurrentView (Type type)
  232. {
  233. Debug.Assert (_curView is null);
  234. // If we are to create a generic Type
  235. if (type.IsGenericType)
  236. {
  237. // For each of the <T> arguments
  238. List<Type> typeArguments = new ();
  239. // use <object> or the original type if applicable
  240. foreach (Type arg in type.GetGenericArguments ())
  241. {
  242. if (arg.IsValueType && Nullable.GetUnderlyingType (arg) == null)
  243. {
  244. typeArguments.Add (arg);
  245. }
  246. else
  247. {
  248. typeArguments.Add (typeof (object));
  249. }
  250. }
  251. // And change what type we are instantiating from MyClass<T> to MyClass<object> or MyClass<T>
  252. type = type.MakeGenericType (typeArguments.ToArray ());
  253. }
  254. // Ensure the type does not contain any generic parameters
  255. if (type.ContainsGenericParameters)
  256. {
  257. Logging.Warning($"Cannot create an instance of {type} because it contains generic parameters.");
  258. //throw new ArgumentException ($"Cannot create an instance of {type} because it contains generic parameters.");
  259. return;
  260. }
  261. // Instantiate view
  262. var view = (View)Activator.CreateInstance (type)!;
  263. _eventLog!.ViewToLog = view;
  264. if (view is IDesignable designable)
  265. {
  266. designable.EnableForDesign (ref _demoText);
  267. }
  268. else
  269. {
  270. view.Text = _demoText;
  271. view.Title = "_Test Title";
  272. }
  273. if (view is IOrientation orientatedView)
  274. {
  275. _orientation!.SelectedItem = (int)orientatedView.Orientation;
  276. _orientation.Enabled = true;
  277. }
  278. else
  279. {
  280. _orientation!.Enabled = false;
  281. }
  282. view.Initialized += CurrentView_Initialized;
  283. view.SubViewsLaidOut += CurrentView_LayoutComplete;
  284. view.Id = "_curView";
  285. _curView = view;
  286. _hostPane!.Add (_curView);
  287. _layoutEditor!.ViewToEdit = _curView;
  288. _viewportSettingsEditor!.ViewToEdit = _curView;
  289. _arrangementEditor!.ViewToEdit = _curView;
  290. _curView.SetNeedsLayout ();
  291. }
  292. private void DisposeCurrentView ()
  293. {
  294. if (_curView != null)
  295. {
  296. _curView.Initialized -= CurrentView_Initialized;
  297. _curView.SubViewsLaidOut -= CurrentView_LayoutComplete;
  298. _hostPane!.Remove (_curView);
  299. _layoutEditor!.ViewToEdit = null;
  300. _viewportSettingsEditor!.ViewToEdit = null;
  301. _arrangementEditor!.ViewToEdit = null;
  302. _curView.Dispose ();
  303. _curView = null;
  304. }
  305. }
  306. private static List<Type> GetAllViewClassesCollection ()
  307. {
  308. List<Type> types = typeof (View).Assembly.GetTypes ()
  309. .Where (
  310. myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
  311. && myType.IsSubclassOf (typeof (View)))
  312. .ToList ();
  313. types.Add (typeof (View));
  314. return types;
  315. }
  316. private void CurrentView_LayoutComplete (object? sender, LayoutEventArgs args) { UpdateHostTitle (_curView); }
  317. private void UpdateHostTitle (View? view) { _hostPane!.Title = $"{view!.GetType ().Name} [_0]"; }
  318. private void CurrentView_Initialized (object? sender, EventArgs e)
  319. {
  320. if (sender is not View view)
  321. {
  322. return;
  323. }
  324. if (view.Width == Dim.Absolute(0) || view.Width is null)
  325. {
  326. view.Width = Dim.Fill ();
  327. }
  328. if (view.Height == Dim.Absolute (0) || view.Height is null)
  329. {
  330. view.Height = Dim.Fill ();
  331. }
  332. UpdateHostTitle (view);
  333. }
  334. public override List<Key> GetDemoKeyStrokes ()
  335. {
  336. var keys = new List<Key> ();
  337. for (int i = 0; i < GetAllViewClassesCollection ().Count; i++)
  338. {
  339. keys.Add (Key.CursorDown);
  340. }
  341. return keys;
  342. }
  343. }