AllViewsTester.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 = GetQuitKeyAndName (),
  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 (view is IDesignable designable)
  300. {
  301. designable.EnableForDesign (ref _demoText);
  302. }
  303. else
  304. {
  305. view.Text = _demoText;
  306. view.Title = "_Test Title";
  307. }
  308. // TODO: Add IOrientation so this doesn't require reflection
  309. // If the view supports a Title property, set it so we have something to look at
  310. if (view?.GetType ().GetProperty ("Orientation") is { } prop)
  311. {
  312. _orientation.SelectedItem = (int)prop.GetGetMethod ()!.Invoke (view, null)!;
  313. _orientation.Enabled = true;
  314. }
  315. else
  316. {
  317. _orientation.Enabled = false;
  318. }
  319. view.Initialized += View_Initialized;
  320. // Add
  321. _hostPane.Add (view);
  322. _hostPane.SetNeedsDisplay ();
  323. return view;
  324. }
  325. private void DimPosChanged (View view)
  326. {
  327. if (view == null)
  328. {
  329. return;
  330. }
  331. try
  332. {
  333. view.X = _xRadioGroup.SelectedItem switch
  334. {
  335. 0 => Pos.Percent (_xVal),
  336. 1 => Pos.AnchorEnd (),
  337. 2 => Pos.Center (),
  338. 3 => Pos.Absolute (_xVal),
  339. _ => view.X
  340. };
  341. view.Y = _yRadioGroup.SelectedItem switch
  342. {
  343. 0 => Pos.Percent (_yVal),
  344. 1 => Pos.AnchorEnd (),
  345. 2 => Pos.Center (),
  346. 3 => Pos.Absolute (_yVal),
  347. _ => view.Y
  348. };
  349. view.Width = _wRadioGroup.SelectedItem switch
  350. {
  351. 0 => Dim.Auto (),
  352. 1 => Dim.Percent (_wVal),
  353. 2 => Dim.Fill (_wVal),
  354. 3 => Dim.Absolute (_wVal),
  355. _ => view.Width
  356. };
  357. view.Height = _hRadioGroup.SelectedItem switch
  358. {
  359. 0 => Dim.Auto (),
  360. 1 => Dim.Percent (_hVal),
  361. 2 => Dim.Fill (_hVal),
  362. 3 => Dim.Absolute (_hVal),
  363. _ => view.Height
  364. };
  365. }
  366. catch (Exception e)
  367. {
  368. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  369. }
  370. if (view.Width is DimAuto)
  371. {
  372. _wText.Text = "Auto";
  373. _wText.Enabled = false;
  374. }
  375. else
  376. {
  377. _wText.Text = $"{_wVal}";
  378. _wText.Enabled = true;
  379. }
  380. if (view.Height is DimAuto)
  381. {
  382. _hText.Text = "Auto";
  383. _hText.Enabled = false;
  384. }
  385. else
  386. {
  387. _hText.Text = $"{_hVal}";
  388. _hText.Enabled = true;
  389. }
  390. UpdateTitle (view);
  391. }
  392. private List<Type> GetAllViewClassesCollection ()
  393. {
  394. List<Type> types = new ();
  395. foreach (Type type in typeof (View).Assembly.GetTypes ()
  396. .Where (
  397. myType =>
  398. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  399. ))
  400. {
  401. types.Add (type);
  402. }
  403. types.Add (typeof (View));
  404. return types;
  405. }
  406. private void LayoutCompleteHandler (object sender, LayoutEventArgs args)
  407. {
  408. UpdateSettings (_curView);
  409. UpdateTitle (_curView);
  410. }
  411. private void Quit () { Application.RequestStop (); }
  412. private void UpdateSettings (View view)
  413. {
  414. _adornmentsEditor.ViewToEdit = view;
  415. var x = view.X.ToString ();
  416. var y = view.Y.ToString ();
  417. try
  418. {
  419. _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => x.Contains (s)));
  420. _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => y.Contains (s)));
  421. }
  422. catch (InvalidOperationException e)
  423. {
  424. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  425. Debug.WriteLine($"{e}");
  426. }
  427. _xText.Text = $"{view.Frame.X}";
  428. _yText.Text = $"{view.Frame.Y}";
  429. var w = view.Width.ToString ();
  430. var h = view.Height.ToString ();
  431. _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => w.Contains (s)));
  432. _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => h.Contains (s)));
  433. if (view.Width is DimAuto)
  434. {
  435. _wText.Text = "Auto";
  436. _wText.Enabled = false;
  437. }
  438. else
  439. {
  440. _wText.Text = "100";
  441. _wText.Enabled = true;
  442. }
  443. if (view.Height is DimAuto)
  444. {
  445. _hText.Text = "Auto";
  446. _hText.Enabled = false;
  447. }
  448. else
  449. {
  450. _hText.Text = "100";
  451. _hText.Enabled = true;
  452. }
  453. }
  454. private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  455. private void View_Initialized (object sender, EventArgs e)
  456. {
  457. if (sender is not View view)
  458. {
  459. return;
  460. }
  461. if (view.Width is not DimAuto && (view.Width is null || view.Frame.Width == 0))
  462. {
  463. view.Width = Dim.Fill ();
  464. }
  465. if (view.Height is not DimAuto && (view.Height is null || view.Frame.Height == 0))
  466. {
  467. view.Height = Dim.Fill ();
  468. }
  469. UpdateSettings (view);
  470. UpdateTitle (view);
  471. }
  472. }