AllViewsTester.cs 12 KB

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