AllViewsTester.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using Terminal.Gui;
  9. namespace UICatalog {
  10. [ScenarioMetadata (Name: "All Views Tester", Description: "Provides a test UI for all classes derived from View")]
  11. [ScenarioCategory ("Layout")]
  12. class AllViewsTester : Scenario {
  13. Window _leftPane;
  14. ListView _classListView;
  15. FrameView _hostPane;
  16. Dictionary<string, Type> _viewClasses;
  17. View _curView = null;
  18. // Settings
  19. FrameView _settingsPane;
  20. CheckBox _computedCheckBox;
  21. FrameView _locationFrame;
  22. RadioGroup _xRadioGroup;
  23. TextField _xText;
  24. int _xVal = 0;
  25. RadioGroup _yRadioGroup;
  26. TextField _yText;
  27. int _yVal = 0;
  28. FrameView _sizeFrame;
  29. RadioGroup _wRadioGroup;
  30. TextField _wText;
  31. int _wVal = 0;
  32. RadioGroup _hRadioGroup;
  33. TextField _hText;
  34. int _hVal = 0;
  35. public override void Init (Toplevel top, ColorScheme colorScheme)
  36. {
  37. Application.Init ();
  38. Top = top;
  39. if (Top == null) {
  40. Top = Application.Top;
  41. }
  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.ControlQ, "~^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. Top.SetFocus (_settingsPane);
  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. SelectedItemChanged = (selected) => DimPosChanged (_curView),
  122. };
  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. SelectedItemChanged = (selected) => DimPosChanged (_curView),
  149. };
  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. SelectedItemChanged = (selected) => DimPosChanged (_curView)
  164. };
  165. _wText = new TextField ($"{_wVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 };
  166. _wText.TextChanged += (args) => {
  167. try {
  168. _wVal = int.Parse (_wText.Text.ToString ());
  169. DimPosChanged (_curView);
  170. } catch {
  171. }
  172. };
  173. _sizeFrame.Add (_wText);
  174. _sizeFrame.Add (_wRadioGroup);
  175. radioItems = new ustring [] { "Percent(height)", "Fill(height)", "Sized(height)" };
  176. label = new Label ("height:") { X = Pos.Right (_wRadioGroup) + 1, Y = 0 };
  177. _sizeFrame.Add (label);
  178. _hText = new TextField ($"{_hVal}") { X = Pos.Right (label) + 1, Y = 0, Width = 4 };
  179. _hText.TextChanged += (args) => {
  180. try {
  181. _hVal = int.Parse (_hText.Text.ToString ());
  182. DimPosChanged (_curView);
  183. } catch {
  184. }
  185. };
  186. _sizeFrame.Add (_hText);
  187. _hRadioGroup = new RadioGroup (radioItems) {
  188. X = Pos.X (label),
  189. Y = Pos.Bottom (label),
  190. SelectedItemChanged = (selected) => DimPosChanged (_curView),
  191. };
  192. _sizeFrame.Add (_hRadioGroup);
  193. _settingsPane.Add (_sizeFrame);
  194. _hostPane = new FrameView ("") {
  195. X = Pos.Right (_leftPane),
  196. Y = Pos.Bottom (_settingsPane),
  197. Width = Dim.Fill (),
  198. Height = Dim.Fill (1), // + 1 for status bar
  199. ColorScheme = Colors.Dialog,
  200. };
  201. Top.Add (_leftPane, _settingsPane, _hostPane);
  202. _curView = CreateClass (_viewClasses.First ().Value);
  203. }
  204. void DimPosChanged (View view)
  205. {
  206. if (view == null) {
  207. return;
  208. }
  209. try {
  210. switch (_xRadioGroup.SelectedItem) {
  211. case 0:
  212. view.X = Pos.Percent (_xVal);
  213. break;
  214. case 1:
  215. view.X = Pos.AnchorEnd (_xVal);
  216. break;
  217. case 2:
  218. view.X = Pos.Center ();
  219. break;
  220. case 3:
  221. view.X = Pos.At (_xVal);
  222. break;
  223. }
  224. switch (_yRadioGroup.SelectedItem) {
  225. case 0:
  226. view.Y = Pos.Percent (_yVal);
  227. break;
  228. case 1:
  229. view.Y = Pos.AnchorEnd (_yVal);
  230. break;
  231. case 2:
  232. view.Y = Pos.Center ();
  233. break;
  234. case 3:
  235. view.Y = Pos.At (_yVal);
  236. break;
  237. }
  238. switch (_wRadioGroup.SelectedItem) {
  239. case 0:
  240. view.Width = Dim.Percent (_wVal);
  241. break;
  242. case 1:
  243. view.Width = Dim.Fill (_wVal);
  244. break;
  245. case 2:
  246. view.Width = Dim.Sized (_wVal);
  247. break;
  248. }
  249. switch (_hRadioGroup.SelectedItem) {
  250. case 0:
  251. view.Height = Dim.Percent (_hVal);
  252. break;
  253. case 1:
  254. view.Height = Dim.Fill (_hVal);
  255. break;
  256. case 2:
  257. view.Height = Dim.Sized (_hVal);
  258. break;
  259. }
  260. } catch (Exception e) {
  261. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  262. }
  263. UpdateTitle (view);
  264. }
  265. List<string> posNames = new List<String> { "Factor", "AnchorEnd", "Center", "Absolute" };
  266. List<string> dimNames = new List<String> { "Factor", "Fill", "Absolute" };
  267. void UpdateSettings (View view)
  268. {
  269. var x = view.X.ToString ();
  270. var y = view.Y.ToString ();
  271. _xRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => x.Contains (s)).First ());
  272. _yRadioGroup.SelectedItem = posNames.IndexOf (posNames.Where (s => y.Contains (s)).First ());
  273. _xText.Text = $"{view.Frame.X}";
  274. _yText.Text = $"{view.Frame.Y}";
  275. var w = view.Width.ToString ();
  276. var h = view.Height.ToString ();
  277. _wRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => w.Contains (s)).First ());
  278. _hRadioGroup.SelectedItem = dimNames.IndexOf (dimNames.Where (s => h.Contains (s)).First ());
  279. _wText.Text = $"{view.Frame.Width}";
  280. _hText.Text = $"{view.Frame.Height}";
  281. }
  282. void UpdateTitle (View view)
  283. {
  284. _hostPane.Title = $"{view.GetType ().Name} - {view.X.ToString ()}, {view.Y.ToString ()}, {view.Width.ToString ()}, {view.Height.ToString ()}";
  285. }
  286. List<Type> GetAllViewClassesCollection ()
  287. {
  288. List<Type> types = new List<Type> ();
  289. foreach (Type type in typeof (View).Assembly.GetTypes ()
  290. .Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View)))) {
  291. types.Add (type);
  292. }
  293. return types;
  294. }
  295. void ClearClass (View view)
  296. {
  297. // Remove existing class, if any
  298. if (view != null) {
  299. _hostPane.Remove (view);
  300. _hostPane.Clear ();
  301. }
  302. }
  303. View CreateClass (Type type)
  304. {
  305. // Instantiate view
  306. var view = (View)Activator.CreateInstance (type);
  307. //_curView.X = Pos.Center ();
  308. //_curView.Y = Pos.Center ();
  309. //_curView.Width = Dim.Fill (5);
  310. //_curView.Height = Dim.Fill (5);
  311. // Set the colorscheme to make it stand out
  312. view.ColorScheme = Colors.Base;
  313. // If the view supports a Text property, set it so we have something to look at
  314. if (view.GetType ().GetProperty ("Text") != null) {
  315. try {
  316. view.GetType ().GetProperty ("Text")?.GetSetMethod ()?.Invoke (view, new [] { ustring.Make ("Test Text") });
  317. } catch (TargetInvocationException e) {
  318. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  319. view = null;
  320. }
  321. }
  322. // If the view supports a Title property, set it so we have something to look at
  323. if (view != null && view.GetType ().GetProperty ("Title") != null) {
  324. view?.GetType ().GetProperty ("Title")?.GetSetMethod ()?.Invoke (view, new [] { ustring.Make ("Test Title") });
  325. }
  326. // If the view supports a Source property, set it so we have something to look at
  327. if (view != null && view.GetType ().GetProperty ("Source") != null && view.GetType().GetProperty("Source").PropertyType == typeof(Terminal.Gui.IListDataSource)) {
  328. var source = new ListWrapper (new List<ustring> () { ustring.Make ("List Item #1"), ustring.Make ("List Item #2"), ustring.Make ("List Item #3")});
  329. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, new [] { source });
  330. }
  331. // Set Settings
  332. _computedCheckBox.Checked = view.LayoutStyle == LayoutStyle.Computed;
  333. // Add
  334. _hostPane.Add (view);
  335. //DimPosChanged ();
  336. _hostPane.LayoutSubviews ();
  337. _hostPane.Clear ();
  338. _hostPane.SetNeedsDisplay ();
  339. UpdateSettings (view);
  340. UpdateTitle (view);
  341. return view;
  342. }
  343. public override void Run ()
  344. {
  345. base.Run ();
  346. }
  347. private void Quit ()
  348. {
  349. Application.RequestStop ();
  350. }
  351. }
  352. }