AllViewsTester.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. Application.Top.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];
  45. }
  46. public override void Setup ()
  47. {
  48. var statusBar = new StatusBar (
  49. new StatusItem []
  50. {
  51. new (
  52. Application.QuitKey,
  53. $"{Application.QuitKey} to Quit",
  54. () => Quit ()
  55. ),
  56. new (
  57. KeyCode.F2,
  58. "~F2~ Toggle Frame Ruler",
  59. () =>
  60. {
  61. View.Diagnostics ^=
  62. ViewDiagnosticFlags.Ruler;
  63. Application.Top.SetNeedsDisplay ();
  64. }
  65. ),
  66. new (
  67. KeyCode.F3,
  68. "~F3~ Toggle Frame Padding",
  69. () =>
  70. {
  71. View.Diagnostics ^=
  72. ViewDiagnosticFlags.Padding;
  73. Application.Top.SetNeedsDisplay ();
  74. }
  75. )
  76. }
  77. );
  78. Application.Top.Add (statusBar);
  79. _viewClasses = GetAllViewClassesCollection ()
  80. .OrderBy (t => t.Name)
  81. .Select (t => new KeyValuePair<string, Type> (t.Name, t))
  82. .ToDictionary (t => t.Key, t => t.Value);
  83. _leftPane = new FrameView
  84. {
  85. X = 0,
  86. Y = 0,
  87. Width = 15,
  88. Height = Dim.Fill (1), // for status bar
  89. CanFocus = false,
  90. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  91. Title = "Classes"
  92. };
  93. _classListView = new ListView
  94. {
  95. X = 0,
  96. Y = 0,
  97. Width = Dim.Fill (),
  98. Height = Dim.Fill (),
  99. AllowsMarking = false,
  100. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  101. SelectedItem = 0,
  102. Source = new ListWrapper (_viewClasses.Keys.ToList ())
  103. };
  104. _classListView.OpenSelectedItem += (s, a) => { _settingsPane.SetFocus (); };
  105. _classListView.SelectedItemChanged += (s, args) =>
  106. {
  107. // Remove existing class, if any
  108. if (_curView != null)
  109. {
  110. _curView.LayoutComplete -= LayoutCompleteHandler;
  111. _hostPane.Remove (_curView);
  112. _curView.Dispose ();
  113. _curView = null;
  114. _hostPane.Clear ();
  115. }
  116. _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  117. };
  118. _leftPane.Add (_classListView);
  119. _settingsPane = new FrameView
  120. {
  121. X = Pos.Right (_leftPane),
  122. Y = 0, // for menu
  123. Width = Dim.Fill (),
  124. Height = 10,
  125. CanFocus = false,
  126. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  127. Title = "Settings"
  128. };
  129. _computedCheckBox = new CheckBox { X = 0, Y = 0, Text = "_Computed Layout", Checked = true };
  130. _computedCheckBox.Toggled += (s, e) =>
  131. {
  132. if (_curView != null)
  133. {
  134. _hostPane.LayoutSubviews ();
  135. }
  136. };
  137. _settingsPane.Add (_computedCheckBox);
  138. string [] radioItems = { "_Percent(x)", "_AnchorEnd(x)", "_Center", "A_t(x)" };
  139. _locationFrame = new FrameView
  140. {
  141. X = Pos.Left (_computedCheckBox),
  142. Y = Pos.Bottom (_computedCheckBox),
  143. Height = 3 + radioItems.Length,
  144. Width = 36,
  145. Title = "Location (Pos)"
  146. };
  147. _settingsPane.Add (_locationFrame);
  148. var label = new Label { X = 0, Y = 0, Text = "X:" };
  149. _locationFrame.Add (label);
  150. _xRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  151. _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  152. _xText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  153. _xText.TextChanged += (s, args) =>
  154. {
  155. try
  156. {
  157. _xVal = int.Parse (_xText.Text);
  158. DimPosChanged (_curView);
  159. }
  160. catch
  161. { }
  162. };
  163. _locationFrame.Add (_xText);
  164. _locationFrame.Add (_xRadioGroup);
  165. radioItems = new [] { "P_ercent(y)", "A_nchorEnd(y)", "C_enter", "At(_y)" };
  166. label = new Label { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "Y:" };
  167. _locationFrame.Add (label);
  168. _yText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  169. _yText.TextChanged += (s, args) =>
  170. {
  171. try
  172. {
  173. _yVal = int.Parse (_yText.Text);
  174. DimPosChanged (_curView);
  175. }
  176. catch
  177. { }
  178. };
  179. _locationFrame.Add (_yText);
  180. _yRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  181. _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  182. _locationFrame.Add (_yRadioGroup);
  183. _sizeFrame = new FrameView
  184. {
  185. X = Pos.Right (_locationFrame),
  186. Y = Pos.Y (_locationFrame),
  187. Height = 3 + radioItems.Length,
  188. Width = 40,
  189. Title = "Size (Dim)"
  190. };
  191. radioItems = new [] { "_Percent(width)", "_Fill(width)", "_Sized(width)" };
  192. label = new Label { X = 0, Y = 0, Text = "Width:" };
  193. _sizeFrame.Add (label);
  194. _wRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  195. _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  196. _wText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  197. _wText.TextChanged += (s, args) =>
  198. {
  199. try
  200. {
  201. switch (_wRadioGroup.SelectedItem)
  202. {
  203. case 0:
  204. _wVal = Math.Min (int.Parse (_wText.Text), 100);
  205. break;
  206. case 1:
  207. case 2:
  208. _wVal = int.Parse (_wText.Text);
  209. break;
  210. }
  211. DimPosChanged (_curView);
  212. }
  213. catch
  214. { }
  215. };
  216. _sizeFrame.Add (_wText);
  217. _sizeFrame.Add (_wRadioGroup);
  218. radioItems = new [] { "P_ercent(height)", "F_ill(height)", "Si_zed(height)" };
  219. label = new Label { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "Height:" };
  220. _sizeFrame.Add (label);
  221. _hText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  222. _hText.TextChanged += (s, args) =>
  223. {
  224. try
  225. {
  226. switch (_hRadioGroup.SelectedItem)
  227. {
  228. case 0:
  229. _hVal = Math.Min (int.Parse (_hText.Text), 100);
  230. break;
  231. case 1:
  232. case 2:
  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. Application.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 (_xVal),
  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 (_yVal),
  350. 2 => Pos.Center (),
  351. 3 => Pos.At (_yVal),
  352. _ => view.Y
  353. };
  354. view.Width = _wRadioGroup.SelectedItem switch
  355. {
  356. 0 => Dim.Percent (_wVal),
  357. 1 => Dim.Fill (_wVal),
  358. 2 => Dim.Sized (_wVal),
  359. _ => view.Width
  360. };
  361. view.Height = _hRadioGroup.SelectedItem switch
  362. {
  363. 0 => Dim.Percent (_hVal),
  364. 1 => Dim.Fill (_hVal),
  365. 2 => Dim.Sized (_hVal),
  366. _ => view.Height
  367. };
  368. }
  369. catch (Exception e)
  370. {
  371. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  372. }
  373. UpdateTitle (view);
  374. }
  375. private List<Type> GetAllViewClassesCollection ()
  376. {
  377. List<Type> types = new ();
  378. foreach (Type type in typeof (View).Assembly.GetTypes ()
  379. .Where (
  380. myType =>
  381. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  382. ))
  383. {
  384. types.Add (type);
  385. }
  386. types.Add (typeof (View));
  387. return types;
  388. }
  389. private void LayoutCompleteHandler (object sender, LayoutEventArgs args)
  390. {
  391. UpdateSettings (_curView);
  392. UpdateTitle (_curView);
  393. }
  394. private void Quit () { Application.RequestStop (); }
  395. private void UpdateSettings (View view)
  396. {
  397. var x = view.X.ToString ();
  398. var y = view.Y.ToString ();
  399. _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => x.Contains (s)).First ());
  400. _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.Where (s => y.Contains (s)).First ());
  401. _xText.Text = $"{view.Frame.X}";
  402. _yText.Text = $"{view.Frame.Y}";
  403. var w = view.Width.ToString ();
  404. var h = view.Height.ToString ();
  405. _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => w.Contains (s)).First ());
  406. _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.Where (s => h.Contains (s)).First ());
  407. _wText.Text = $"{view.Frame.Width}";
  408. _hText.Text = $"{view.Frame.Height}";
  409. }
  410. private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  411. private void View_Initialized (object sender, EventArgs e)
  412. {
  413. var view = sender as View;
  414. //view.X = Pos.Center ();
  415. //view.Y = Pos.Center ();
  416. if (view.Width == null || view.Frame.Width == 0)
  417. {
  418. view.Width = Dim.Fill ();
  419. }
  420. if (view.Height == null || view.Frame.Height == 0)
  421. {
  422. view.Height = Dim.Fill ();
  423. }
  424. UpdateSettings (view);
  425. UpdateTitle (view);
  426. }
  427. }