AllViewsTester.cs 12 KB

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