AllViewsTester.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 ("Controls")]
  13. [ScenarioCategory ("Adornments")]
  14. public class AllViewsTester : Scenario
  15. {
  16. private readonly List<string> _dimNames = new () { "Auto", "Percent", "Fill", "Absolute" };
  17. // TODO: This is missing some
  18. private readonly List<string> _posNames = new () { "Percent", "AnchorEnd", "Center", "Absolute" };
  19. private ListView _classListView;
  20. private View _curView;
  21. private FrameView _hostPane;
  22. private AdornmentsEditor _adornmentsEditor;
  23. private RadioGroup _hRadioGroup;
  24. private TextField _hText;
  25. private int _hVal;
  26. private FrameView _leftPane;
  27. private FrameView _locationFrame;
  28. // Settings
  29. private FrameView _settingsPane;
  30. private FrameView _sizeFrame;
  31. private Dictionary<string, Type> _viewClasses;
  32. private RadioGroup _wRadioGroup;
  33. private TextField _wText;
  34. private int _wVal;
  35. private RadioGroup _xRadioGroup;
  36. private TextField _xText;
  37. private int _xVal;
  38. private RadioGroup _yRadioGroup;
  39. private TextField _yText;
  40. private int _yVal;
  41. private RadioGroup _orientation;
  42. private string _demoText = "This, that, and the other thing.";
  43. private TextView _demoTextView;
  44. public override void Main ()
  45. {
  46. // Don't create a sub-win (Scenario.Win); just use Application.Top
  47. Application.Init ();
  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 = true,
  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.SelectedItemChanged += (s, args) =>
  79. {
  80. // Dispose existing current View, if any
  81. DisposeCurrentView ();
  82. CreateCurrentView (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  83. // Force ViewToEdit to be the view and not a subview
  84. if (_adornmentsEditor is { })
  85. {
  86. _adornmentsEditor.AutoSelectSuperView = _curView;
  87. _adornmentsEditor.ViewToEdit = _curView;
  88. }
  89. };
  90. _leftPane.Add (_classListView);
  91. _adornmentsEditor = new ()
  92. {
  93. X = Pos.Right (_leftPane),
  94. Y = 0,
  95. Width = Dim.Auto (),
  96. Height = Dim.Fill (),
  97. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  98. BorderStyle = LineStyle.Single,
  99. AutoSelectViewToEdit = true,
  100. AutoSelectAdornments = false,
  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 = true,
  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. TabStop = TabBehavior.TabStop,
  127. };
  128. _settingsPane.Add (_locationFrame);
  129. var label = new Label { X = 0, Y = 0, Text = "X:" };
  130. _locationFrame.Add (label);
  131. _xRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  132. _xRadioGroup.SelectedItemChanged += OnRadioGroupOnSelectedItemChanged;
  133. _xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  134. _xText.Accepting += (s, args) =>
  135. {
  136. try
  137. {
  138. _xVal = int.Parse (_xText.Text);
  139. DimPosChanged (_curView);
  140. }
  141. catch
  142. { }
  143. };
  144. _locationFrame.Add (_xText);
  145. _locationFrame.Add (_xRadioGroup);
  146. radioItems = new [] { "P_ercent(y)", "A_nchorEnd", "C_enter", "Absolute(_y)" };
  147. label = new () { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "Y:" };
  148. _locationFrame.Add (label);
  149. _yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  150. _yText.Accepting += (s, args) =>
  151. {
  152. try
  153. {
  154. _yVal = int.Parse (_yText.Text);
  155. DimPosChanged (_curView);
  156. }
  157. catch
  158. { }
  159. };
  160. _locationFrame.Add (_yText);
  161. _yRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  162. _yRadioGroup.SelectedItemChanged += OnRadioGroupOnSelectedItemChanged;
  163. _locationFrame.Add (_yRadioGroup);
  164. _sizeFrame = new ()
  165. {
  166. X = Pos.Right (_locationFrame),
  167. Y = Pos.Y (_locationFrame),
  168. Height = Dim.Auto (),
  169. Width = Dim.Auto (),
  170. Title = "Size (Dim)",
  171. TabStop = TabBehavior.TabStop,
  172. };
  173. radioItems = new [] { "Auto", "_Percent(width)", "_Fill(width)", "A_bsolute(width)" };
  174. label = new () { X = 0, Y = 0, Text = "Width:" };
  175. _sizeFrame.Add (label);
  176. _wRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  177. _wRadioGroup.SelectedItemChanged += OnRadioGroupOnSelectedItemChanged;
  178. _wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  179. _wText.Accepting += (s, args) =>
  180. {
  181. try
  182. {
  183. switch (_wRadioGroup.SelectedItem)
  184. {
  185. case 1:
  186. _wVal = Math.Min (int.Parse (_wText.Text), 100);
  187. break;
  188. case 0:
  189. case 2:
  190. case 3:
  191. _wVal = int.Parse (_wText.Text);
  192. break;
  193. }
  194. DimPosChanged (_curView);
  195. }
  196. catch
  197. { }
  198. };
  199. _sizeFrame.Add (_wText);
  200. _sizeFrame.Add (_wRadioGroup);
  201. radioItems = new [] { "_Auto", "P_ercent(height)", "F_ill(height)", "Ab_solute(height)" };
  202. label = new () { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "Height:" };
  203. _sizeFrame.Add (label);
  204. _hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  205. _hText.Accepting += (s, args) =>
  206. {
  207. try
  208. {
  209. switch (_hRadioGroup.SelectedItem)
  210. {
  211. case 1:
  212. _hVal = Math.Min (int.Parse (_hText.Text), 100);
  213. break;
  214. case 0:
  215. case 2:
  216. case 3:
  217. _hVal = int.Parse (_hText.Text);
  218. break;
  219. }
  220. DimPosChanged (_curView);
  221. }
  222. catch
  223. { }
  224. };
  225. _sizeFrame.Add (_hText);
  226. _hRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  227. _hRadioGroup.SelectedItemChanged += OnRadioGroupOnSelectedItemChanged;
  228. _sizeFrame.Add (_hRadioGroup);
  229. _settingsPane.Add (_sizeFrame);
  230. label = new () { X = 0, Y = Pos.Bottom (_sizeFrame), Text = "_Orientation:" };
  231. _orientation = new ()
  232. {
  233. X = Pos.Right (label) + 1,
  234. Y = Pos.Top (label),
  235. RadioLabels = new [] { "Horizontal", "Vertical" },
  236. Orientation = Orientation.Horizontal
  237. };
  238. _orientation.SelectedItemChanged += (s, selected) =>
  239. {
  240. if (_curView is IOrientation orientatedView)
  241. {
  242. orientatedView.Orientation = (Orientation)_orientation.SelectedItem;
  243. }
  244. };
  245. _settingsPane.Add (label, _orientation);
  246. label = new () { X = 0, Y = Pos.Bottom (_orientation), Text = "_Text:" };
  247. _demoTextView = new ()
  248. {
  249. X = Pos.Right (label) + 1,
  250. Y = Pos.Top (label),
  251. Width = Dim.Fill (),
  252. Height = Dim.Auto (minimumContentDim: 2),
  253. Text = _demoText
  254. };
  255. _demoTextView.ContentsChanged += (s, e) =>
  256. {
  257. _demoText = _demoTextView.Text;
  258. if (_curView is { })
  259. {
  260. _curView.Text = _demoText;
  261. }
  262. };
  263. _settingsPane.Add (label, _demoTextView);
  264. _hostPane = new ()
  265. {
  266. X = Pos.Right (_adornmentsEditor),
  267. Y = Pos.Bottom (_settingsPane),
  268. Width = Dim.Fill (),
  269. Height = Dim.Fill (), // + 1 for status bar
  270. CanFocus = true,
  271. TabStop = TabBehavior.TabGroup,
  272. ColorScheme = Colors.ColorSchemes ["Base"]
  273. };
  274. _hostPane.Padding.Thickness = new (1);
  275. _hostPane.Padding.Diagnostics = ViewDiagnosticFlags.Ruler;
  276. _hostPane.Padding.ColorScheme = Colors.ColorSchemes ["Error"];
  277. _hostPane.LayoutStarted += (sender, args) =>
  278. {
  279. };
  280. app.Add (_leftPane, _adornmentsEditor, _settingsPane, _hostPane);
  281. _classListView.SelectedItem = 0;
  282. _leftPane.SetFocus ();
  283. Application.Run (app);
  284. app.Dispose ();
  285. Application.Shutdown ();
  286. }
  287. private void OnRadioGroupOnSelectedItemChanged (object s, SelectedItemChangedArgs selected) { DimPosChanged (_curView); }
  288. // TODO: Add Command.HotKey handler (pop a message box?)
  289. private void CreateCurrentView (Type type)
  290. {
  291. Debug.Assert(_curView is null);
  292. // If we are to create a generic Type
  293. if (type.IsGenericType)
  294. {
  295. // For each of the <T> arguments
  296. List<Type> typeArguments = new ();
  297. // use <object>
  298. foreach (Type arg in type.GetGenericArguments ())
  299. {
  300. typeArguments.Add (typeof (object));
  301. }
  302. // And change what type we are instantiating from MyClass<T> to MyClass<object>
  303. type = type.MakeGenericType (typeArguments.ToArray ());
  304. }
  305. // Instantiate view
  306. var view = (View)Activator.CreateInstance (type);
  307. if (view is IDesignable designable)
  308. {
  309. designable.EnableForDesign (ref _demoText);
  310. }
  311. else
  312. {
  313. view.Text = _demoText;
  314. view.Title = "_Test Title";
  315. }
  316. if (view is IOrientation orientatedView)
  317. {
  318. _orientation.SelectedItem = (int)orientatedView.Orientation;
  319. _orientation.Enabled = true;
  320. }
  321. else
  322. {
  323. _orientation.Enabled = false;
  324. }
  325. view.Initialized += CurrentView_Initialized;
  326. view.LayoutComplete += CurrentView_LayoutComplete;
  327. view.Id = "_curView";
  328. _curView = view;
  329. _hostPane.Add (_curView);
  330. }
  331. private void DisposeCurrentView ()
  332. {
  333. if (_curView != null)
  334. {
  335. _curView.Initialized -= CurrentView_Initialized;
  336. _curView.LayoutComplete -= CurrentView_LayoutComplete;
  337. _hostPane.Remove (_curView);
  338. _curView.Dispose ();
  339. _curView = null;
  340. }
  341. }
  342. private void DimPosChanged (View view)
  343. {
  344. if (view == null || _updatingSettings)
  345. {
  346. return;
  347. }
  348. try
  349. {
  350. view.X = _xRadioGroup.SelectedItem switch
  351. {
  352. 0 => Pos.Percent (_xVal),
  353. 1 => Pos.AnchorEnd (),
  354. 2 => Pos.Center (),
  355. 3 => Pos.Absolute (_xVal),
  356. _ => view.X
  357. };
  358. view.Y = _yRadioGroup.SelectedItem switch
  359. {
  360. 0 => Pos.Percent (_yVal),
  361. 1 => Pos.AnchorEnd (),
  362. 2 => Pos.Center (),
  363. 3 => Pos.Absolute (_yVal),
  364. _ => view.Y
  365. };
  366. view.Width = _wRadioGroup.SelectedItem switch
  367. {
  368. 0 => Dim.Auto (),
  369. 1 => Dim.Percent (_wVal),
  370. 2 => Dim.Fill (_wVal),
  371. 3 => Dim.Absolute (_wVal),
  372. _ => view.Width
  373. };
  374. view.Height = _hRadioGroup.SelectedItem switch
  375. {
  376. 0 => Dim.Auto (),
  377. 1 => Dim.Percent (_hVal),
  378. 2 => Dim.Fill (_hVal),
  379. 3 => Dim.Absolute (_hVal),
  380. _ => view.Height
  381. };
  382. }
  383. catch (Exception e)
  384. {
  385. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  386. }
  387. if (view.Width is DimAuto)
  388. {
  389. _wText.Text = "Auto";
  390. _wText.Enabled = false;
  391. }
  392. else
  393. {
  394. _wText.Text = $"{_wVal}";
  395. _wText.Enabled = true;
  396. }
  397. if (view.Height is DimAuto)
  398. {
  399. _hText.Text = "Auto";
  400. _hText.Enabled = false;
  401. }
  402. else
  403. {
  404. _hText.Text = $"{_hVal}";
  405. _hText.Enabled = true;
  406. }
  407. UpdateHostTitle (view);
  408. }
  409. private List<Type> GetAllViewClassesCollection ()
  410. {
  411. List<Type> types = new ();
  412. foreach (Type type in typeof (View).Assembly.GetTypes ()
  413. .Where (
  414. myType =>
  415. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  416. ))
  417. {
  418. types.Add (type);
  419. }
  420. types.Add (typeof (View));
  421. return types;
  422. }
  423. private void CurrentView_LayoutComplete (object sender, LayoutEventArgs args)
  424. {
  425. UpdateSettings (_curView);
  426. UpdateHostTitle (_curView);
  427. }
  428. private bool _updatingSettings = false;
  429. private void UpdateSettings (View view)
  430. {
  431. _updatingSettings = true;
  432. var x = view.X.ToString ();
  433. var y = view.Y.ToString ();
  434. try
  435. {
  436. _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => x.Contains (s)));
  437. _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => y.Contains (s)));
  438. }
  439. catch (InvalidOperationException e)
  440. {
  441. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  442. Debug.WriteLine ($"{e}");
  443. }
  444. _xText.Text = $"{view.Frame.X}";
  445. _yText.Text = $"{view.Frame.Y}";
  446. var w = view.Width.ToString ();
  447. var h = view.Height.ToString ();
  448. _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => w.Contains (s)));
  449. _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => h.Contains (s)));
  450. if (view.Width.Has<DimAuto> (out _))
  451. {
  452. _wText.Text = "Auto";
  453. _wText.Enabled = false;
  454. }
  455. else
  456. {
  457. _wText.Text = $"{view.Frame.Width}";
  458. _wText.Enabled = true;
  459. }
  460. if (view.Height.Has<DimAuto> (out _))
  461. {
  462. _hText.Text = "Auto";
  463. _hText.Enabled = false;
  464. }
  465. else
  466. {
  467. _hText.Text = $"{view.Frame.Height}";
  468. _hText.Enabled = true;
  469. }
  470. _updatingSettings = false;
  471. }
  472. private void UpdateHostTitle (View view) { _hostPane.Title = $"_Demo of {view.GetType ().Name}"; }
  473. private void CurrentView_Initialized (object sender, EventArgs e)
  474. {
  475. if (sender is not View view)
  476. {
  477. return;
  478. }
  479. if (!view.Width!.Has<DimAuto> (out _) || (view.Width is null))
  480. {
  481. view.Width = Dim.Fill ();
  482. }
  483. if (!view.Height!.Has<DimAuto> (out _) || (view.Height is null))
  484. {
  485. view.Height = Dim.Fill ();
  486. }
  487. UpdateSettings (view);
  488. UpdateHostTitle (view);
  489. }
  490. }