AllViewsTester.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Reflection;
  7. using Terminal.Gui;
  8. namespace UICatalog.Scenarios;
  9. [ScenarioMetadata ("All Views Tester", "Provides a test UI for all classes derived from View.")]
  10. [ScenarioCategory ("Layout")]
  11. [ScenarioCategory ("Tests")]
  12. [ScenarioCategory ("Top Level Windows")]
  13. public class AllViewsTester : Scenario
  14. {
  15. private readonly List<string> _dimNames = new () { "Auto", "Percent", "Fill", "Absolute" };
  16. // TODO: This is missing some
  17. private readonly List<string> _posNames = new () { "Percent", "AnchorEnd", "Center", "Absolute" };
  18. private ListView _classListView;
  19. private View _curView;
  20. private FrameView _hostPane;
  21. private AdornmentsEditor _adornmentsEditor;
  22. private RadioGroup _hRadioGroup;
  23. private TextField _hText;
  24. private int _hVal;
  25. private FrameView _leftPane;
  26. private FrameView _locationFrame;
  27. // Settings
  28. private FrameView _settingsPane;
  29. private FrameView _sizeFrame;
  30. private Dictionary<string, Type> _viewClasses;
  31. private RadioGroup _wRadioGroup;
  32. private TextField _wText;
  33. private int _wVal;
  34. private RadioGroup _xRadioGroup;
  35. private TextField _xText;
  36. private int _xVal;
  37. private RadioGroup _yRadioGroup;
  38. private TextField _yText;
  39. private int _yVal;
  40. private RadioGroup _orientation;
  41. private string _demoText = "This, that, and the other thing.";
  42. private TextView _demoTextView;
  43. public override void Main ()
  44. {
  45. // Don't create a sub-win (Scenario.Win); just use Application.Top
  46. Application.Init ();
  47. ConfigurationManager.Apply ();
  48. var app = new Window
  49. {
  50. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
  51. ColorScheme = Colors.ColorSchemes ["TopLevel"]
  52. };
  53. _viewClasses = GetAllViewClassesCollection ()
  54. .OrderBy (t => t.Name)
  55. .Select (t => new KeyValuePair<string, Type> (t.Name, t))
  56. .ToDictionary (t => t.Key, t => t.Value);
  57. _leftPane = new ()
  58. {
  59. X = 0,
  60. Y = 0,
  61. Width = Dim.Auto (DimAutoStyle.Content),
  62. Height = Dim.Fill (),
  63. CanFocus = false,
  64. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  65. Title = "Classes"
  66. };
  67. _classListView = new ()
  68. {
  69. X = 0,
  70. Y = 0,
  71. Width = Dim.Auto (),
  72. Height = Dim.Fill (),
  73. AllowsMarking = false,
  74. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  75. SelectedItem = 0,
  76. Source = new ListWrapper<string> (new (_viewClasses.Keys.ToList ()))
  77. };
  78. _classListView.OpenSelectedItem += (s, a) => { _settingsPane.SetFocus (); };
  79. _classListView.SelectedItemChanged += (s, args) =>
  80. {
  81. // Remove existing class, if any
  82. if (_curView != null)
  83. {
  84. _curView.LayoutComplete -= LayoutCompleteHandler;
  85. _hostPane.Remove (_curView);
  86. _curView.Dispose ();
  87. _curView = null;
  88. _hostPane.Clear ();
  89. }
  90. _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  91. };
  92. _leftPane.Add (_classListView);
  93. _adornmentsEditor = new ()
  94. {
  95. X = Pos.Right (_leftPane),
  96. Y = 0,
  97. Width = Dim.Auto (),
  98. Height = Dim.Fill (),
  99. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  100. BorderStyle = LineStyle.Single
  101. };
  102. var expandButton = new ExpanderButton
  103. {
  104. CanFocus = false,
  105. Orientation = Orientation.Horizontal
  106. };
  107. _adornmentsEditor.Border.Add (expandButton);
  108. _settingsPane = new ()
  109. {
  110. X = Pos.Right (_adornmentsEditor),
  111. Y = 0, // for menu
  112. Width = Dim.Fill (),
  113. Height = Dim.Auto (),
  114. CanFocus = false,
  115. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  116. Title = "Settings"
  117. };
  118. string [] radioItems = { "_Percent(x)", "_AnchorEnd", "_Center", "A_bsolute(x)" };
  119. _locationFrame = new ()
  120. {
  121. X = 0,
  122. Y = 0,
  123. Height = Dim.Auto (),
  124. Width = Dim.Auto (),
  125. Title = "Location (Pos)"
  126. };
  127. _settingsPane.Add (_locationFrame);
  128. var label = new Label { X = 0, Y = 0, Text = "X:" };
  129. _locationFrame.Add (label);
  130. _xRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  131. _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  132. _xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  133. _xText.Accept += (s, args) =>
  134. {
  135. try
  136. {
  137. _xVal = int.Parse (_xText.Text);
  138. DimPosChanged (_curView);
  139. }
  140. catch
  141. { }
  142. };
  143. _locationFrame.Add (_xText);
  144. _locationFrame.Add (_xRadioGroup);
  145. radioItems = new [] { "P_ercent(y)", "A_nchorEnd", "C_enter", "Absolute(_y)" };
  146. label = new () { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "Y:" };
  147. _locationFrame.Add (label);
  148. _yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  149. _yText.Accept += (s, args) =>
  150. {
  151. try
  152. {
  153. _yVal = int.Parse (_yText.Text);
  154. DimPosChanged (_curView);
  155. }
  156. catch
  157. { }
  158. };
  159. _locationFrame.Add (_yText);
  160. _yRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  161. _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  162. _locationFrame.Add (_yRadioGroup);
  163. _sizeFrame = new ()
  164. {
  165. X = Pos.Right (_locationFrame),
  166. Y = Pos.Y (_locationFrame),
  167. Height = Dim.Auto (),
  168. Width = Dim.Auto (),
  169. Title = "Size (Dim)"
  170. };
  171. radioItems = new [] { "Auto", "_Percent(width)", "_Fill(width)", "A_bsolute(width)" };
  172. label = new () { X = 0, Y = 0, Text = "Width:" };
  173. _sizeFrame.Add (label);
  174. _wRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  175. _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  176. _wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  177. _wText.Accept += (s, args) =>
  178. {
  179. try
  180. {
  181. switch (_wRadioGroup.SelectedItem)
  182. {
  183. case 1:
  184. _wVal = Math.Min (int.Parse (_wText.Text), 100);
  185. break;
  186. case 0:
  187. case 2:
  188. case 3:
  189. _wVal = int.Parse (_wText.Text);
  190. break;
  191. }
  192. DimPosChanged (_curView);
  193. }
  194. catch
  195. { }
  196. };
  197. _sizeFrame.Add (_wText);
  198. _sizeFrame.Add (_wRadioGroup);
  199. radioItems = new [] { "_Auto", "P_ercent(height)", "F_ill(height)", "Ab_solute(height)" };
  200. label = new () { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "Height:" };
  201. _sizeFrame.Add (label);
  202. _hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  203. _hText.Accept += (s, args) =>
  204. {
  205. try
  206. {
  207. switch (_hRadioGroup.SelectedItem)
  208. {
  209. case 1:
  210. _hVal = Math.Min (int.Parse (_hText.Text), 100);
  211. break;
  212. case 0:
  213. case 2:
  214. case 3:
  215. _hVal = int.Parse (_hText.Text);
  216. break;
  217. }
  218. DimPosChanged (_curView);
  219. }
  220. catch
  221. { }
  222. };
  223. _sizeFrame.Add (_hText);
  224. _hRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  225. _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  226. _sizeFrame.Add (_hRadioGroup);
  227. _settingsPane.Add (_sizeFrame);
  228. label = new () { X = 0, Y = Pos.Bottom (_sizeFrame), Text = "_Orientation:" };
  229. _orientation = new ()
  230. {
  231. X = Pos.Right (label) + 1,
  232. Y = Pos.Top (label),
  233. RadioLabels = new [] { "Horizontal", "Vertical" },
  234. Orientation = Orientation.Horizontal
  235. };
  236. _orientation.SelectedItemChanged += (s, selected) =>
  237. {
  238. if (_curView?.GetType ().GetProperty ("Orientation") is { } prop)
  239. {
  240. prop.GetSetMethod ()?.Invoke (_curView, new object [] { _orientation.SelectedItem });
  241. }
  242. };
  243. _settingsPane.Add (label, _orientation);
  244. label = new () { X = 0, Y = Pos.Bottom (_orientation), Text = "_Text:" };
  245. _demoTextView = new ()
  246. {
  247. X = Pos.Right (label) + 1,
  248. Y = Pos.Top (label),
  249. Width = Dim.Fill (),
  250. Height = Dim.Auto (minimumContentDim: 2),
  251. Text = _demoText
  252. };
  253. _demoTextView.ContentsChanged += (s, e) =>
  254. {
  255. _demoText = _demoTextView.Text;
  256. if (_curView is { })
  257. {
  258. _curView.Text = _demoText;
  259. }
  260. };
  261. _settingsPane.Add (label, _demoTextView);
  262. _hostPane = new ()
  263. {
  264. X = Pos.Right (_adornmentsEditor),
  265. Y = Pos.Bottom (_settingsPane),
  266. Width = Dim.Fill (),
  267. Height = Dim.Fill (), // + 1 for status bar
  268. ColorScheme = Colors.ColorSchemes ["Dialog"]
  269. };
  270. app.Add (_leftPane, _adornmentsEditor, _settingsPane, _hostPane);
  271. _classListView.SelectedItem = 0;
  272. Application.Run (app);
  273. app.Dispose ();
  274. Application.Shutdown ();
  275. }
  276. // TODO: Add Command.HotKey handler (pop a message box?)
  277. private View CreateClass (Type type)
  278. {
  279. // If we are to create a generic Type
  280. if (type.IsGenericType)
  281. {
  282. // For each of the <T> arguments
  283. List<Type> typeArguments = new ();
  284. // use <object>
  285. foreach (Type arg in type.GetGenericArguments ())
  286. {
  287. typeArguments.Add (typeof (object));
  288. }
  289. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  290. type = type.MakeGenericType (typeArguments.ToArray ());
  291. }
  292. // Instantiate view
  293. var view = (View)Activator.CreateInstance (type);
  294. // Set the colorscheme to make it stand out if is null by default
  295. if (view.ColorScheme == null)
  296. {
  297. view.ColorScheme = Colors.ColorSchemes ["Base"];
  298. }
  299. // If the view supports a Text property, set it so we have something to look at
  300. if (view.GetType ().GetProperty ("Text") != null)
  301. {
  302. try
  303. {
  304. view.GetType ()
  305. .GetProperty ("Text")
  306. ?.GetSetMethod ()
  307. ?.Invoke (view, new [] { _demoText });
  308. }
  309. catch (TargetInvocationException e)
  310. {
  311. MessageBox.ErrorQuery ("Exception", e.InnerException.Message, "Ok");
  312. view = null;
  313. }
  314. }
  315. // If the view supports a Title property, set it so we have something to look at
  316. if (view != null && view.GetType ().GetProperty ("Title") != null)
  317. {
  318. if (view.GetType ().GetProperty ("Title")!.PropertyType == typeof (string))
  319. {
  320. view?.GetType ()
  321. .GetProperty ("Title")
  322. ?.GetSetMethod ()
  323. ?.Invoke (view, new [] { "Test Title" });
  324. }
  325. else
  326. {
  327. view?.GetType ()
  328. .GetProperty ("Title")
  329. ?.GetSetMethod ()
  330. ?.Invoke (view, new [] { "Test Title" });
  331. }
  332. }
  333. // If the view supports a Source property, set it so we have something to look at
  334. if (view != null && view.GetType ().GetProperty ("Source") != null && view.GetType ().GetProperty ("Source").PropertyType == typeof (IListDataSource))
  335. {
  336. var source = new ListWrapper<string> (["Test Text #1", "Test Text #2", "Test Text #3"]);
  337. view?.GetType ().GetProperty ("Source")?.GetSetMethod ()?.Invoke (view, [source]);
  338. }
  339. // If the view supports a Title property, set it so we have something to look at
  340. if (view?.GetType ().GetProperty ("Orientation") is { } prop)
  341. {
  342. _orientation.SelectedItem = (int)prop.GetGetMethod ()!.Invoke (view, null)!;
  343. _orientation.Enabled = true;
  344. }
  345. else
  346. {
  347. _orientation.Enabled = false;
  348. }
  349. view.Initialized += View_Initialized;
  350. // Add
  351. _hostPane.Add (view);
  352. _hostPane.SetNeedsDisplay ();
  353. return view;
  354. }
  355. private void DimPosChanged (View view)
  356. {
  357. if (view == null)
  358. {
  359. return;
  360. }
  361. try
  362. {
  363. view.X = _xRadioGroup.SelectedItem switch
  364. {
  365. 0 => Pos.Percent (_xVal),
  366. 1 => Pos.AnchorEnd (),
  367. 2 => Pos.Center (),
  368. 3 => Pos.Absolute (_xVal),
  369. _ => view.X
  370. };
  371. view.Y = _yRadioGroup.SelectedItem switch
  372. {
  373. 0 => Pos.Percent (_yVal),
  374. 1 => Pos.AnchorEnd (),
  375. 2 => Pos.Center (),
  376. 3 => Pos.Absolute (_yVal),
  377. _ => view.Y
  378. };
  379. view.Width = _wRadioGroup.SelectedItem switch
  380. {
  381. 0 => Dim.Auto (),
  382. 1 => Dim.Percent (_wVal),
  383. 2 => Dim.Fill (_wVal),
  384. 3 => Dim.Absolute (_wVal),
  385. _ => view.Width
  386. };
  387. view.Height = _hRadioGroup.SelectedItem switch
  388. {
  389. 0 => Dim.Auto (),
  390. 1 => Dim.Percent (_hVal),
  391. 2 => Dim.Fill (_hVal),
  392. 3 => Dim.Absolute (_hVal),
  393. _ => view.Height
  394. };
  395. }
  396. catch (Exception e)
  397. {
  398. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  399. }
  400. if (view.Width is DimAuto)
  401. {
  402. _wText.Text = "Auto";
  403. _wText.Enabled = false;
  404. }
  405. else
  406. {
  407. _wText.Text = $"{_wVal}";
  408. _wText.Enabled = true;
  409. }
  410. if (view.Height is DimAuto)
  411. {
  412. _hText.Text = "Auto";
  413. _hText.Enabled = false;
  414. }
  415. else
  416. {
  417. _hText.Text = $"{_hVal}";
  418. _hText.Enabled = true;
  419. }
  420. UpdateTitle (view);
  421. }
  422. private List<Type> GetAllViewClassesCollection ()
  423. {
  424. List<Type> types = new ();
  425. foreach (Type type in typeof (View).Assembly.GetTypes ()
  426. .Where (
  427. myType =>
  428. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  429. ))
  430. {
  431. types.Add (type);
  432. }
  433. types.Add (typeof (View));
  434. return types;
  435. }
  436. private void LayoutCompleteHandler (object sender, LayoutEventArgs args)
  437. {
  438. UpdateSettings (_curView);
  439. UpdateTitle (_curView);
  440. }
  441. private void Quit () { Application.RequestStop (); }
  442. private void UpdateSettings (View view)
  443. {
  444. _adornmentsEditor.ViewToEdit = view;
  445. var x = view.X.ToString ();
  446. var y = view.Y.ToString ();
  447. try
  448. {
  449. _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => x.Contains (s)));
  450. _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => y.Contains (s)));
  451. }
  452. catch (InvalidOperationException e)
  453. {
  454. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  455. Debug.WriteLine($"{e}");
  456. }
  457. _xText.Text = $"{view.Frame.X}";
  458. _yText.Text = $"{view.Frame.Y}";
  459. var w = view.Width.ToString ();
  460. var h = view.Height.ToString ();
  461. _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => w.Contains (s)));
  462. _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => h.Contains (s)));
  463. if (view.Width is DimAuto)
  464. {
  465. _wText.Text = "Auto";
  466. _wText.Enabled = false;
  467. }
  468. else
  469. {
  470. _wText.Text = "100";
  471. _wText.Enabled = true;
  472. }
  473. if (view.Height is DimAuto)
  474. {
  475. _hText.Text = "Auto";
  476. _hText.Enabled = false;
  477. }
  478. else
  479. {
  480. _hText.Text = "100";
  481. _hText.Enabled = true;
  482. }
  483. }
  484. private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  485. private void View_Initialized (object sender, EventArgs e)
  486. {
  487. if (sender is not View view)
  488. {
  489. return;
  490. }
  491. if (view.Width is not DimAuto && (view.Width is null || view.Frame.Width == 0))
  492. {
  493. view.Width = Dim.Fill ();
  494. }
  495. if (view.Height is not DimAuto && (view.Height is null || view.Frame.Height == 0))
  496. {
  497. view.Height = Dim.Fill ();
  498. }
  499. UpdateSettings (view);
  500. UpdateTitle (view);
  501. }
  502. }