AllViewsTester.cs 12 KB

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