AllViewsTester.cs 12 KB

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