AllViewsTester.cs 12 KB

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