AllViewsTester.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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>
  240. foreach (Type arg in type.GetGenericArguments ())
  241. {
  242. typeArguments.Add (typeof (object));
  243. }
  244. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  245. type = type.MakeGenericType (typeArguments.ToArray ());
  246. }
  247. // Instantiate view
  248. var view = (View)Activator.CreateInstance (type)!;
  249. _eventLog!.ViewToLog = view;
  250. if (view is IDesignable designable)
  251. {
  252. designable.EnableForDesign (ref _demoText);
  253. }
  254. else
  255. {
  256. view.Text = _demoText;
  257. view.Title = "_Test Title";
  258. }
  259. if (view is IOrientation orientatedView)
  260. {
  261. _orientation!.SelectedItem = (int)orientatedView.Orientation;
  262. _orientation.Enabled = true;
  263. }
  264. else
  265. {
  266. _orientation!.Enabled = false;
  267. }
  268. view.Initialized += CurrentView_Initialized;
  269. view.SubviewsLaidOut += CurrentView_LayoutComplete;
  270. view.Id = "_curView";
  271. _curView = view;
  272. _curView = view;
  273. _hostPane!.Add (_curView);
  274. _layoutEditor!.ViewToEdit = _curView;
  275. _viewportSettingsEditor!.ViewToEdit = _curView;
  276. _arrangementEditor!.ViewToEdit = _curView;
  277. _curView.SetNeedsLayout ();
  278. }
  279. private void DisposeCurrentView ()
  280. {
  281. if (_curView != null)
  282. {
  283. _curView.Initialized -= CurrentView_Initialized;
  284. _curView.SubviewsLaidOut -= CurrentView_LayoutComplete;
  285. _hostPane!.Remove (_curView);
  286. _layoutEditor!.ViewToEdit = null;
  287. _viewportSettingsEditor!.ViewToEdit = null;
  288. _arrangementEditor!.ViewToEdit = null;
  289. _curView.Dispose ();
  290. _curView = null;
  291. }
  292. }
  293. private static List<Type> GetAllViewClassesCollection ()
  294. {
  295. List<Type> types = typeof (View).Assembly.GetTypes ()
  296. .Where (
  297. myType => myType is { IsClass: true, IsAbstract: false, IsPublic: true }
  298. && myType.IsSubclassOf (typeof (View)))
  299. .ToList ();
  300. types.Add (typeof (View));
  301. return types;
  302. }
  303. private void CurrentView_LayoutComplete (object? sender, LayoutEventArgs args) { UpdateHostTitle (_curView); }
  304. private void UpdateHostTitle (View? view) { _hostPane!.Title = $"{view!.GetType ().Name} [_0]"; }
  305. private void CurrentView_Initialized (object? sender, EventArgs e)
  306. {
  307. if (sender is not View view)
  308. {
  309. return;
  310. }
  311. if (view.Width == Dim.Absolute(0) || view.Width is null)
  312. {
  313. view.Width = Dim.Fill ();
  314. }
  315. if (view.Height == Dim.Absolute (0) || view.Height is null)
  316. {
  317. view.Height = Dim.Fill ();
  318. }
  319. UpdateHostTitle (view);
  320. }
  321. public override List<Key> GetDemoKeyStrokes ()
  322. {
  323. var keys = new List<Key> ();
  324. for (int i = 0; i < GetAllViewClassesCollection ().Count; i++)
  325. {
  326. keys.Add (Key.CursorDown);
  327. }
  328. return keys;
  329. }
  330. }