AllViewsTester.cs 13 KB

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