AllViewsTester.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 = 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. // Remove existing class, if any
  81. if (_curView != null)
  82. {
  83. _curView.LayoutComplete -= LayoutCompleteHandler;
  84. _hostPane.Remove (_curView);
  85. _curView.Dispose ();
  86. _curView = null;
  87. }
  88. _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
  89. // Add
  90. _hostPane.Add (_curView);
  91. // Force ViewToEdit to be the view and not a subview
  92. if (_adornmentsEditor is { })
  93. {
  94. _adornmentsEditor.AutoSelectSuperView = _curView;
  95. _adornmentsEditor.ViewToEdit = _curView;
  96. }
  97. };
  98. _leftPane.Add (_classListView);
  99. _adornmentsEditor = new ()
  100. {
  101. X = Pos.Right (_leftPane),
  102. Y = 0,
  103. Width = Dim.Auto (),
  104. Height = Dim.Fill (),
  105. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  106. BorderStyle = LineStyle.Single,
  107. AutoSelectViewToEdit = true,
  108. AutoSelectAdornments = false,
  109. };
  110. var expandButton = new ExpanderButton
  111. {
  112. CanFocus = false,
  113. Orientation = Orientation.Horizontal
  114. };
  115. _adornmentsEditor.Border.Add (expandButton);
  116. _settingsPane = new ()
  117. {
  118. X = Pos.Right (_adornmentsEditor),
  119. Y = 0, // for menu
  120. Width = Dim.Fill (),
  121. Height = Dim.Auto (),
  122. CanFocus = true,
  123. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  124. Title = "Settings"
  125. };
  126. string [] radioItems = { "_Percent(x)", "_AnchorEnd", "_Center", "A_bsolute(x)" };
  127. _locationFrame = new ()
  128. {
  129. X = 0,
  130. Y = 0,
  131. Height = Dim.Auto (),
  132. Width = Dim.Auto (),
  133. Title = "Location (Pos)",
  134. TabStop = TabBehavior.TabStop,
  135. };
  136. _settingsPane.Add (_locationFrame);
  137. var label = new Label { X = 0, Y = 0, Text = "X:" };
  138. _locationFrame.Add (label);
  139. _xRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  140. _xRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  141. _xText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
  142. _xText.Accept += (s, args) =>
  143. {
  144. try
  145. {
  146. _xVal = int.Parse (_xText.Text);
  147. DimPosChanged (_curView);
  148. }
  149. catch
  150. { }
  151. };
  152. _locationFrame.Add (_xText);
  153. _locationFrame.Add (_xRadioGroup);
  154. radioItems = new [] { "P_ercent(y)", "A_nchorEnd", "C_enter", "Absolute(_y)" };
  155. label = new () { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "Y:" };
  156. _locationFrame.Add (label);
  157. _yText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
  158. _yText.Accept += (s, args) =>
  159. {
  160. try
  161. {
  162. _yVal = int.Parse (_yText.Text);
  163. DimPosChanged (_curView);
  164. }
  165. catch
  166. { }
  167. };
  168. _locationFrame.Add (_yText);
  169. _yRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  170. _yRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  171. _locationFrame.Add (_yRadioGroup);
  172. _sizeFrame = new ()
  173. {
  174. X = Pos.Right (_locationFrame),
  175. Y = Pos.Y (_locationFrame),
  176. Height = Dim.Auto (),
  177. Width = Dim.Auto (),
  178. Title = "Size (Dim)",
  179. TabStop = TabBehavior.TabStop,
  180. };
  181. radioItems = new [] { "Auto", "_Percent(width)", "_Fill(width)", "A_bsolute(width)" };
  182. label = new () { X = 0, Y = 0, Text = "Width:" };
  183. _sizeFrame.Add (label);
  184. _wRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
  185. _wRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  186. _wText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
  187. _wText.Accept += (s, args) =>
  188. {
  189. try
  190. {
  191. switch (_wRadioGroup.SelectedItem)
  192. {
  193. case 1:
  194. _wVal = Math.Min (int.Parse (_wText.Text), 100);
  195. break;
  196. case 0:
  197. case 2:
  198. case 3:
  199. _wVal = int.Parse (_wText.Text);
  200. break;
  201. }
  202. DimPosChanged (_curView);
  203. }
  204. catch
  205. { }
  206. };
  207. _sizeFrame.Add (_wText);
  208. _sizeFrame.Add (_wRadioGroup);
  209. radioItems = new [] { "_Auto", "P_ercent(height)", "F_ill(height)", "Ab_solute(height)" };
  210. label = new () { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "Height:" };
  211. _sizeFrame.Add (label);
  212. _hText = new () { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
  213. _hText.Accept += (s, args) =>
  214. {
  215. try
  216. {
  217. switch (_hRadioGroup.SelectedItem)
  218. {
  219. case 1:
  220. _hVal = Math.Min (int.Parse (_hText.Text), 100);
  221. break;
  222. case 0:
  223. case 2:
  224. case 3:
  225. _hVal = int.Parse (_hText.Text);
  226. break;
  227. }
  228. DimPosChanged (_curView);
  229. }
  230. catch
  231. { }
  232. };
  233. _sizeFrame.Add (_hText);
  234. _hRadioGroup = new () { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
  235. _hRadioGroup.SelectedItemChanged += (s, selected) => DimPosChanged (_curView);
  236. _sizeFrame.Add (_hRadioGroup);
  237. _settingsPane.Add (_sizeFrame);
  238. label = new () { X = 0, Y = Pos.Bottom (_sizeFrame), Text = "_Orientation:" };
  239. _orientation = new ()
  240. {
  241. X = Pos.Right (label) + 1,
  242. Y = Pos.Top (label),
  243. RadioLabels = new [] { "Horizontal", "Vertical" },
  244. Orientation = Orientation.Horizontal
  245. };
  246. _orientation.SelectedItemChanged += (s, selected) =>
  247. {
  248. if (_curView is IOrientation orientatedView)
  249. {
  250. orientatedView.Orientation = (Orientation)_orientation.SelectedItem;
  251. }
  252. };
  253. _settingsPane.Add (label, _orientation);
  254. label = new () { X = 0, Y = Pos.Bottom (_orientation), Text = "_Text:" };
  255. _demoTextView = new ()
  256. {
  257. X = Pos.Right (label) + 1,
  258. Y = Pos.Top (label),
  259. Width = Dim.Fill (),
  260. Height = Dim.Auto (minimumContentDim: 2),
  261. Text = _demoText
  262. };
  263. _demoTextView.ContentsChanged += (s, e) =>
  264. {
  265. _demoText = _demoTextView.Text;
  266. if (_curView is { })
  267. {
  268. _curView.Text = _demoText;
  269. }
  270. };
  271. _settingsPane.Add (label, _demoTextView);
  272. _hostPane = new ()
  273. {
  274. X = Pos.Right (_adornmentsEditor),
  275. Y = Pos.Bottom (_settingsPane),
  276. Width = Dim.Fill (),
  277. Height = Dim.Fill (), // + 1 for status bar
  278. CanFocus = true,
  279. TabStop = TabBehavior.TabGroup,
  280. ColorScheme = Colors.ColorSchemes ["Dialog"]
  281. };
  282. app.Add (_leftPane, _adornmentsEditor, _settingsPane, _hostPane);
  283. _classListView.SelectedItem = 0;
  284. _leftPane.SetFocus ();
  285. Application.Run (app);
  286. app.Dispose ();
  287. Application.Shutdown ();
  288. }
  289. // TODO: Add Command.HotKey handler (pop a message box?)
  290. private View CreateClass (Type type)
  291. {
  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. // Set the colorscheme to make it stand out if is null by default
  308. if (view.ColorScheme == null)
  309. {
  310. view.ColorScheme = Colors.ColorSchemes ["Base"];
  311. }
  312. if (view is IDesignable designable)
  313. {
  314. designable.EnableForDesign (ref _demoText);
  315. }
  316. else
  317. {
  318. view.Text = _demoText;
  319. view.Title = "_Test Title";
  320. }
  321. if (view is IOrientation orientatedView)
  322. {
  323. _orientation.SelectedItem = (int)orientatedView.Orientation;
  324. _orientation.Enabled = true;
  325. }
  326. else
  327. {
  328. _orientation.Enabled = false;
  329. }
  330. view.Initialized += View_Initialized;
  331. return view;
  332. }
  333. private void DimPosChanged (View view)
  334. {
  335. if (view == null)
  336. {
  337. return;
  338. }
  339. try
  340. {
  341. view.X = _xRadioGroup.SelectedItem switch
  342. {
  343. 0 => Pos.Percent (_xVal),
  344. 1 => Pos.AnchorEnd (),
  345. 2 => Pos.Center (),
  346. 3 => Pos.Absolute (_xVal),
  347. _ => view.X
  348. };
  349. view.Y = _yRadioGroup.SelectedItem switch
  350. {
  351. 0 => Pos.Percent (_yVal),
  352. 1 => Pos.AnchorEnd (),
  353. 2 => Pos.Center (),
  354. 3 => Pos.Absolute (_yVal),
  355. _ => view.Y
  356. };
  357. view.Width = _wRadioGroup.SelectedItem switch
  358. {
  359. 0 => Dim.Auto (),
  360. 1 => Dim.Percent (_wVal),
  361. 2 => Dim.Fill (_wVal),
  362. 3 => Dim.Absolute (_wVal),
  363. _ => view.Width
  364. };
  365. view.Height = _hRadioGroup.SelectedItem switch
  366. {
  367. 0 => Dim.Auto (),
  368. 1 => Dim.Percent (_hVal),
  369. 2 => Dim.Fill (_hVal),
  370. 3 => Dim.Absolute (_hVal),
  371. _ => view.Height
  372. };
  373. }
  374. catch (Exception e)
  375. {
  376. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  377. }
  378. if (view.Width is DimAuto)
  379. {
  380. _wText.Text = "Auto";
  381. _wText.Enabled = false;
  382. }
  383. else
  384. {
  385. _wText.Text = $"{_wVal}";
  386. _wText.Enabled = true;
  387. }
  388. if (view.Height is DimAuto)
  389. {
  390. _hText.Text = "Auto";
  391. _hText.Enabled = false;
  392. }
  393. else
  394. {
  395. _hText.Text = $"{_hVal}";
  396. _hText.Enabled = true;
  397. }
  398. UpdateTitle (view);
  399. }
  400. private List<Type> GetAllViewClassesCollection ()
  401. {
  402. List<Type> types = new ();
  403. foreach (Type type in typeof (View).Assembly.GetTypes ()
  404. .Where (
  405. myType =>
  406. myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View))
  407. ))
  408. {
  409. types.Add (type);
  410. }
  411. types.Add (typeof (View));
  412. return types;
  413. }
  414. private void LayoutCompleteHandler (object sender, LayoutEventArgs args)
  415. {
  416. UpdateSettings (_curView);
  417. UpdateTitle (_curView);
  418. }
  419. private void UpdateSettings (View view)
  420. {
  421. var x = view.X.ToString ();
  422. var y = view.Y.ToString ();
  423. try
  424. {
  425. _xRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => x.Contains (s)));
  426. _yRadioGroup.SelectedItem = _posNames.IndexOf (_posNames.First (s => y.Contains (s)));
  427. }
  428. catch (InvalidOperationException e)
  429. {
  430. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  431. Debug.WriteLine($"{e}");
  432. }
  433. _xText.Text = $"{view.Frame.X}";
  434. _yText.Text = $"{view.Frame.Y}";
  435. var w = view.Width.ToString ();
  436. var h = view.Height.ToString ();
  437. _wRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => w.Contains (s)));
  438. _hRadioGroup.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => h.Contains (s)));
  439. if (view.Width is DimAuto)
  440. {
  441. _wText.Text = "Auto";
  442. _wText.Enabled = false;
  443. }
  444. else
  445. {
  446. _wText.Text = "100";
  447. _wText.Enabled = true;
  448. }
  449. if (view.Height is DimAuto)
  450. {
  451. _hText.Text = "Auto";
  452. _hText.Enabled = false;
  453. }
  454. else
  455. {
  456. _hText.Text = "100";
  457. _hText.Enabled = true;
  458. }
  459. }
  460. private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; }
  461. private void View_Initialized (object sender, EventArgs e)
  462. {
  463. if (sender is not View view)
  464. {
  465. return;
  466. }
  467. if (view.Width is not DimAuto && (view.Width is null || view.Frame.Width == 0))
  468. {
  469. view.Width = Dim.Fill ();
  470. }
  471. if (view.Height is not DimAuto && (view.Height is null || view.Frame.Height == 0))
  472. {
  473. view.Height = Dim.Fill ();
  474. }
  475. UpdateSettings (view);
  476. UpdateTitle (view);
  477. }
  478. }