AllViewsTester.cs 12 KB

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