AllViewsTester.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 () { "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 [] { "_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 0:
  202. _wVal = Math.Min (int.Parse (_wText.Text), 100);
  203. break;
  204. case 1:
  205. case 2:
  206. _wVal = int.Parse (_wText.Text);
  207. break;
  208. }
  209. DimPosChanged (_curView);
  210. }
  211. catch
  212. { }
  213. };
  214. _sizeFrame.Add (_wText);
  215. _sizeFrame.Add (_wRadioGroup);
  216. radioItems = new [] { "P_ercent(height)", "F_ill(height)", "Si_zed(height)" };
  217. label = new Label { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "Height:" };
  218. _sizeFrame.Add (label);
  219. _hText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  220. _hText.TextChanged += (s, args) =>
  221. {
  222. try
  223. {
  224. switch (_hRadioGroup.SelectedItem)
  225. {
  226. case 0:
  227. _hVal = Math.Min (int.Parse (_hText.Text), 100);
  228. break;
  229. case 1:
  230. case 2:
  231. _hVal = int.Parse (_hText.Text);
  232. break;
  233. }
  234. DimPosChanged (_curView);
  235. }
  236. catch
  237. { }
  238. };
  239. _sizeFrame.Add (_hText);
  240. _hRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  241. _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  242. _sizeFrame.Add (_hRadioGroup);
  243. _settingsPane.Add (_sizeFrame);
  244. _hostPane = new FrameView
  245. {
  246. X = Pos.Right (_leftPane),
  247. Y = Pos.Bottom (_settingsPane),
  248. Width = Dim.Fill (),
  249. Height = Dim.Fill (1), // + 1 for status bar
  250. ColorScheme = Colors.ColorSchemes ["Dialog"]
  251. };
  252. Top.Add (_leftPane, _settingsPane, _hostPane);
  253. _curView = CreateClass (_viewClasses.First ().Value);
  254. }
  255. // TODO: Add Command.HotKey handler (pop a message box?)
  256. private View CreateClass (Type type)
  257. {
  258. // If we are to create a generic Type
  259. if (type.IsGenericType)
  260. {
  261. // For each of the <T> arguments
  262. List<Type> typeArguments = new ();
  263. // use <object>
  264. foreach (Type arg in type.GetGenericArguments ())
  265. {
  266. typeArguments.Add (typeof (object));
  267. }
  268. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  269. type = type.MakeGenericType (typeArguments.ToArray ());
  270. }
  271. // Instantiate view
  272. var view = (View)Activator.CreateInstance (type);
  273. // Set the colorscheme to make it stand out if is null by default
  274. if (view.ColorScheme == null)
  275. {
  276. view.ColorScheme = Colors.ColorSchemes ["Base"];
  277. }
  278. // If the view supports a Text property, set it so we have something to look at
  279. if (view.GetType ().GetProperty ("Text") != null)
  280. {
  281. try
  282. {
  283. view.GetType ()
  284. .GetProperty ("Text")
  285. ?.GetSetMethod ()
  286. ?.Invoke (view, new [] { "Test Text" });
  287. }
  288. catch (TargetInvocationException e)
  289. {
  290. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  291. view = null;
  292. }
  293. }
  294. // If the view supports a Title property, set it so we have something to look at
  295. if (view != null && view.GetType ().GetProperty ("Title") != null)
  296. {
  297. if (view.GetType ().GetProperty ("Title").PropertyType == typeof (string))
  298. {
  299. view?.GetType ()
  300. .GetProperty ("Title")
  301. ?.GetSetMethod ()
  302. ?.Invoke (view, new [] { "Test Title" });
  303. }
  304. else
  305. {
  306. view?.GetType ()
  307. .GetProperty ("Title")
  308. ?.GetSetMethod ()
  309. ?.Invoke (view, new [] { "Test Title" });
  310. }
  311. }
  312. // If the view supports a Source property, set it so we have something to look at
  313. if (view != null && view.GetType ().GetProperty ("Source") != null && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
  314. {
  315. var source = new ListWrapper (new List<string> { "Test Text #1", "Test Text #2", "Test Text #3" });
  316. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  317. }
  318. // Set Settings
  319. _computedCheckBox.Checked = view.LayoutStyle == LayoutStyle.Computed;
  320. view.Initialized += View_Initialized;
  321. // Add
  322. _hostPane.Add (view);
  323. _hostPane.SetNeedsDisplay ();
  324. return view;
  325. }
  326. private void DimPosChanged (View view)
  327. {
  328. if (view == null)
  329. {
  330. return;
  331. }
  332. LayoutStyle layout = view.LayoutStyle;
  333. try
  334. {
  335. //view.LayoutStyle = LayoutStyle.Absolute;
  336. view.X = _xRadioGroup.SelectedItem switch
  337. {
  338. 0 => Pos.Percent (_xVal),
  339. 1 => Pos.AnchorEnd (),
  340. 2 => Pos.Center (),
  341. 3 => Pos.At (_xVal),
  342. _ => view.X
  343. };
  344. view.Y = _yRadioGroup.SelectedItem switch
  345. {
  346. 0 => Pos.Percent (_yVal),
  347. 1 => Pos.AnchorEnd (),
  348. 2 => Pos.Center (),
  349. 3 => Pos.At (_yVal),
  350. _ => view.Y
  351. };
  352. view.Width = _wRadioGroup.SelectedItem switch
  353. {
  354. 0 => Dim.Percent (_wVal),
  355. 1 => Dim.Fill (_wVal),
  356. 2 => Dim.Sized (_wVal),
  357. _ => view.Width
  358. };
  359. view.Height = _hRadioGroup.SelectedItem switch
  360. {
  361. 0 => Dim.Percent (_hVal),
  362. 1 => Dim.Fill (_hVal),
  363. 2 => Dim.Sized (_hVal),
  364. _ => view.Height
  365. };
  366. }
  367. catch (Exception e)
  368. {
  369. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  370. }
  371. UpdateTitle (view);
  372. }
  373. private List<Type> GetAllViewClassesCollection ()
  374. {
  375. List<Type> types = new ();
  376. foreach (Type type in typeof (View).Assembly.GetTypes ()
  377. .Where (
  378. myType =>
  379. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  380. ))
  381. {
  382. types.Add (type);
  383. }
  384. types.Add (typeof (View));
  385. return types;
  386. }
  387. private void LayoutCompleteHandler (object sender, LayoutEventArgs args)
  388. {
  389. UpdateSettings (_curView);
  390. UpdateTitle (_curView);
  391. }
  392. private void Quit () { Application.RequestStop (); }
  393. private void UpdateSettings (View view)
  394. {
  395. var x = view.X.ToString ();
  396. var y = view.Y.ToString ();
  397. _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => x.Contains (s)).First ());
  398. _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => y.Contains (s)).First ());
  399. _xText.Text = $"{view.Frame.X}";
  400. _yText.Text = $"{view.Frame.Y}";
  401. var w = view.Width.ToString ();
  402. var h = view.Height.ToString ();
  403. _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => w.Contains (s)).First ());
  404. _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => h.Contains (s)).First ());
  405. _wText.Text = $"{view.Frame.Width}";
  406. _hText.Text = $"{view.Frame.Height}";
  407. }
  408. private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  409. private void View_Initialized (object sender, EventArgs e)
  410. {
  411. var view = sender as View;
  412. //view.X = Pos.Center ();
  413. //view.Y = Pos.Center ();
  414. if (view.Width == null || view.Frame.Width == 0)
  415. {
  416. view.Width = Dim.Fill ();
  417. }
  418. if (view.Height == null || view.Frame.Height == 0)
  419. {
  420. view.Height = Dim.Fill ();
  421. }
  422. UpdateSettings (view);
  423. UpdateTitle (view);
  424. }
  425. }