AllViewsTester.cs 13 KB

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