AllViewsTester.cs 10 KB

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