AllViewsTester.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Terminal.Gui;
  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 ("Top Level Windows")]
  11. public class AllViewsTester : Scenario
  12. {
  13. private readonly List<string> _dimNames = new () { "Auto", "Factor", "Fill", "Absolute" };
  14. // TODO: This is missing some
  15. private readonly List<string> _posNames = new () { "Factor", "AnchorEnd", "Center", "Absolute" };
  16. private ListView _classListView;
  17. private CheckBox _computedCheckBox;
  18. private View _curView;
  19. private FrameView _hostPane;
  20. private RadioGroup _hRadioGroup;
  21. private TextField _hText;
  22. private int _hVal;
  23. private FrameView _leftPane;
  24. private FrameView _locationFrame;
  25. // Settings
  26. private FrameView _settingsPane;
  27. private FrameView _sizeFrame;
  28. private Dictionary<string, Type> _viewClasses;
  29. private RadioGroup _wRadioGroup;
  30. private TextField _wText;
  31. private int _wVal;
  32. private RadioGroup _xRadioGroup;
  33. private TextField _xText;
  34. private int _xVal;
  35. private RadioGroup _yRadioGroup;
  36. private TextField _yText;
  37. private int _yVal;
  38. public override void Init ()
  39. {
  40. // Don't create a sub-win (Scenario.Win); just use Application.Top
  41. Application.Init ();
  42. ConfigurationManager.Themes.Theme = Theme;
  43. ConfigurationManager.Apply ();
  44. Top = new ();
  45. Top.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];
  46. var statusBar = new StatusBar (
  47. new StatusItem []
  48. {
  49. new (
  50. Application.QuitKey,
  51. $"{Application.QuitKey} to Quit",
  52. () => Quit ()
  53. ),
  54. new (
  55. KeyCode.F2,
  56. "~F2~ Toggle Frame Ruler",
  57. () =>
  58. {
  59. View.Diagnostics ^=
  60. ViewDiagnosticFlags.Ruler;
  61. Top.SetNeedsDisplay ();
  62. }
  63. ),
  64. new (
  65. KeyCode.F3,
  66. "~F3~ Toggle Frame Padding",
  67. () =>
  68. {
  69. View.Diagnostics ^=
  70. ViewDiagnosticFlags.Padding;
  71. Top.SetNeedsDisplay ();
  72. }
  73. )
  74. }
  75. );
  76. Top.Add (statusBar);
  77. _viewClasses = GetAllViewClassesCollection ()
  78. .OrderBy (t => t.Name)
  79. .Select (t => new KeyValuePair<string, Type> (t.Name, t))
  80. .ToDictionary (t => t.Key, t => t.Value);
  81. _leftPane = new FrameView
  82. {
  83. X = 0,
  84. Y = 0,
  85. Width = 15,
  86. Height = Dim.Fill (1), // for status bar
  87. CanFocus = false,
  88. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  89. Title = "Classes"
  90. };
  91. _classListView = new ListView
  92. {
  93. X = 0,
  94. Y = 0,
  95. Width = Dim.Fill (),
  96. Height = Dim.Fill (),
  97. AllowsMarking = false,
  98. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  99. SelectedItem = 0,
  100. Source = new ListWrapper (_viewClasses.Keys.ToList ())
  101. };
  102. _classListView.OpenSelectedItem += (s, a) => { _settingsPane.SetFocus (); };
  103. _classListView.SelectedItemChanged += (s, args) =>
  104. {
  105. // Remove existing class, if any
  106. if (_curView != null)
  107. {
  108. _curView.LayoutComplete -= LayoutCompleteHandler;
  109. _hostPane.Remove (_curView);
  110. _curView.Dispose ();
  111. _curView = null;
  112. _hostPane.Clear ();
  113. }
  114. _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  115. };
  116. _leftPane.Add (_classListView);
  117. _settingsPane = new FrameView
  118. {
  119. X = Pos.Right (_leftPane),
  120. Y = 0, // for menu
  121. Width = Dim.Fill (),
  122. Height = 10,
  123. CanFocus = false,
  124. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  125. Title = "Settings"
  126. };
  127. _computedCheckBox = new CheckBox { X = 0, Y = 0, Text = "_Computed Layout", Checked = true };
  128. _computedCheckBox.Toggled += (s, e) =>
  129. {
  130. if (_curView != null)
  131. {
  132. _hostPane.LayoutSubviews ();
  133. }
  134. };
  135. _settingsPane.Add (_computedCheckBox);
  136. string [] radioItems = { "_Percent(x)", "_AnchorEnd", "_Center", "A_t(x)" };
  137. _locationFrame = new FrameView
  138. {
  139. X = Pos.Left (_computedCheckBox),
  140. Y = Pos.Bottom (_computedCheckBox),
  141. Height = 3 + radioItems.Length,
  142. Width = 36,
  143. Title = "Location (Pos)"
  144. };
  145. _settingsPane.Add (_locationFrame);
  146. var label = new Label { X = 0, Y = 0, Text = "X:" };
  147. _locationFrame.Add (label);
  148. _xRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  149. _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  150. _xText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  151. _xText.TextChanged += (s, args) =>
  152. {
  153. try
  154. {
  155. _xVal = int.Parse (_xText.Text);
  156. DimPosChanged (_curView);
  157. }
  158. catch
  159. { }
  160. };
  161. _locationFrame.Add (_xText);
  162. _locationFrame.Add (_xRadioGroup);
  163. radioItems = new [] { "P_ercent(y)", "A_nchorEnd", "C_enter", "At(_y)" };
  164. label = new Label { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "Y:" };
  165. _locationFrame.Add (label);
  166. _yText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  167. _yText.TextChanged += (s, args) =>
  168. {
  169. try
  170. {
  171. _yVal = int.Parse (_yText.Text);
  172. DimPosChanged (_curView);
  173. }
  174. catch
  175. { }
  176. };
  177. _locationFrame.Add (_yText);
  178. _yRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  179. _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  180. _locationFrame.Add (_yRadioGroup);
  181. _sizeFrame = new FrameView
  182. {
  183. X = Pos.Right (_locationFrame),
  184. Y = Pos.Y (_locationFrame),
  185. Height = 3 + radioItems.Length,
  186. Width = 40,
  187. Title = "Size (Dim)"
  188. };
  189. radioItems = new [] { "Auto (min)", "_Percent(width)", "_Fill(width)", "_Sized(width)" };
  190. label = new Label { X = 0, Y = 0, Text = "Width:" };
  191. _sizeFrame.Add (label);
  192. _wRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  193. _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  194. _wText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  195. _wText.TextChanged += (s, args) =>
  196. {
  197. try
  198. {
  199. switch (_wRadioGroup.SelectedItem)
  200. {
  201. case 1:
  202. _wVal = Math.Min (int.Parse (_wText.Text), 100);
  203. break;
  204. case 0:
  205. case 2:
  206. case 3:
  207. _wVal = int.Parse (_wText.Text);
  208. break;
  209. }
  210. DimPosChanged (_curView);
  211. }
  212. catch
  213. { }
  214. };
  215. _sizeFrame.Add (_wText);
  216. _sizeFrame.Add (_wRadioGroup);
  217. radioItems = new [] { "_Auto (min)", "P_ercent(height)", "F_ill(height)", "Si_zed(height)" };
  218. label = new Label { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "Height:" };
  219. _sizeFrame.Add (label);
  220. _hText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  221. _hText.TextChanged += (s, args) =>
  222. {
  223. try
  224. {
  225. switch (_hRadioGroup.SelectedItem)
  226. {
  227. case 1:
  228. _hVal = Math.Min (int.Parse (_hText.Text), 100);
  229. break;
  230. case 0:
  231. case 2:
  232. case 3:
  233. _hVal = int.Parse (_hText.Text);
  234. break;
  235. }
  236. DimPosChanged (_curView);
  237. }
  238. catch
  239. { }
  240. };
  241. _sizeFrame.Add (_hText);
  242. _hRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  243. _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  244. _sizeFrame.Add (_hRadioGroup);
  245. _settingsPane.Add (_sizeFrame);
  246. _hostPane = new FrameView
  247. {
  248. X = Pos.Right (_leftPane),
  249. Y = Pos.Bottom (_settingsPane),
  250. Width = Dim.Fill (),
  251. Height = Dim.Fill (1), // + 1 for status bar
  252. ColorScheme = Colors.ColorSchemes ["Dialog"]
  253. };
  254. Top.Add (_leftPane, _settingsPane, _hostPane);
  255. _curView = CreateClass (_viewClasses.First ().Value);
  256. }
  257. // TODO: Add Command.HotKey handler (pop a message box?)
  258. private View CreateClass (Type type)
  259. {
  260. // If we are to create a generic Type
  261. if (type.IsGenericType)
  262. {
  263. // For each of the <T> arguments
  264. List<Type> typeArguments = new ();
  265. // use <object>
  266. foreach (Type arg in type.GetGenericArguments ())
  267. {
  268. typeArguments.Add (typeof (object));
  269. }
  270. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  271. type = type.MakeGenericType (typeArguments.ToArray ());
  272. }
  273. // Instantiate view
  274. var view = (View)Activator.CreateInstance (type);
  275. // Set the colorscheme to make it stand out if is null by default
  276. if (view.ColorScheme == null)
  277. {
  278. view.ColorScheme = Colors.ColorSchemes ["Base"];
  279. }
  280. // If the view supports a Text property, set it so we have something to look at
  281. if (view.GetType ().GetProperty ("Text") != null)
  282. {
  283. try
  284. {
  285. view.GetType ()
  286. .GetProperty ("Text")
  287. ?.GetSetMethod ()
  288. ?.Invoke (view, new [] { "Test Text" });
  289. }
  290. catch (TargetInvocationException e)
  291. {
  292. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  293. view = null;
  294. }
  295. }
  296. // If the view supports a Title property, set it so we have something to look at
  297. if (view != null && view.GetType ().GetProperty ("Title") != null)
  298. {
  299. if (view.GetType ().GetProperty ("Title").PropertyType == typeof (string))
  300. {
  301. view?.GetType ()
  302. .GetProperty ("Title")
  303. ?.GetSetMethod ()
  304. ?.Invoke (view, new [] { "Test Title" });
  305. }
  306. else
  307. {
  308. view?.GetType ()
  309. .GetProperty ("Title")
  310. ?.GetSetMethod ()
  311. ?.Invoke (view, new [] { "Test Title" });
  312. }
  313. }
  314. // If the view supports a Source property, set it so we have something to look at
  315. if (view != null && view.GetType ().GetProperty ("Source") != null && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
  316. {
  317. var source = new ListWrapper (new List<string> { "Test Text #1", "Test Text #2", "Test Text #3" });
  318. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  319. }
  320. // Set Settings
  321. _computedCheckBox.Checked = view.LayoutStyle == LayoutStyle.Computed;
  322. view.Initialized += View_Initialized;
  323. // Add
  324. _hostPane.Add (view);
  325. _hostPane.SetNeedsDisplay ();
  326. return view;
  327. }
  328. private void DimPosChanged (View view)
  329. {
  330. if (view == null)
  331. {
  332. return;
  333. }
  334. LayoutStyle layout = view.LayoutStyle;
  335. try
  336. {
  337. //view.LayoutStyle = LayoutStyle.Absolute;
  338. view.X = _xRadioGroup.SelectedItem switch
  339. {
  340. 0 => Pos.Percent (_xVal),
  341. 1 => Pos.AnchorEnd (),
  342. 2 => Pos.Center (),
  343. 3 => Pos.At (_xVal),
  344. _ => view.X
  345. };
  346. view.Y = _yRadioGroup.SelectedItem switch
  347. {
  348. 0 => Pos.Percent (_yVal),
  349. 1 => Pos.AnchorEnd (),
  350. 2 => Pos.Center (),
  351. 3 => Pos.At (_yVal),
  352. _ => view.Y
  353. };
  354. view.Width = _wRadioGroup.SelectedItem switch
  355. {
  356. 0 => Dim.Auto (min: _wVal),
  357. 1 => Dim.Percent (_wVal),
  358. 2 => Dim.Fill (_wVal),
  359. 3 => Dim.Sized (_wVal),
  360. _ => view.Width
  361. };
  362. view.Height = _hRadioGroup.SelectedItem switch
  363. {
  364. 0 => Dim.Auto (min: _hVal),
  365. 1 => Dim.Percent (_hVal),
  366. 2 => Dim.Fill (_hVal),
  367. 3 => Dim.Sized (_hVal),
  368. _ => view.Height
  369. };
  370. }
  371. catch (Exception e)
  372. {
  373. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  374. }
  375. UpdateTitle (view);
  376. }
  377. private List<Type> GetAllViewClassesCollection ()
  378. {
  379. List<Type> types = new ();
  380. foreach (Type type in typeof (View).Assembly.GetTypes ()
  381. .Where (
  382. myType =>
  383. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  384. ))
  385. {
  386. types.Add (type);
  387. }
  388. types.Add (typeof (View));
  389. return types;
  390. }
  391. private void LayoutCompleteHandler (object sender, LayoutEventArgs args)
  392. {
  393. UpdateSettings (_curView);
  394. UpdateTitle (_curView);
  395. }
  396. private void Quit () { Application.RequestStop (); }
  397. private void UpdateSettings (View view)
  398. {
  399. var x = view.X.ToString ();
  400. var y = view.Y.ToString ();
  401. _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => x.Contains (s)).First ());
  402. _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => y.Contains (s)).First ());
  403. _xText.Text = $"{view.Frame.X}";
  404. _yText.Text = $"{view.Frame.Y}";
  405. var w = view.Width.ToString ();
  406. var h = view.Height.ToString ();
  407. _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => w.Contains (s)).First ());
  408. _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => h.Contains (s)).First ());
  409. _wText.Text = $"{view.Frame.Width}";
  410. _hText.Text = $"{view.Frame.Height}";
  411. }
  412. private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  413. private void View_Initialized (object sender, EventArgs e)
  414. {
  415. var view = sender as View;
  416. if (view is null)
  417. {
  418. return;
  419. }
  420. if (view.Width is not Dim.DimAuto && (view.Width is null || view.Frame.Width == 0))
  421. {
  422. view.Width = Dim.Fill ();
  423. }
  424. if (view.Width is not Dim.DimAuto && (view.Height is null || view.Frame.Height == 0))
  425. {
  426. view.Height = Dim.Fill ();
  427. }
  428. UpdateSettings (view);
  429. UpdateTitle (view);
  430. }
  431. }