AllViewsTester.cs 13 KB

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