Program.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.Linq;
  8. using Terminal.Gui;
  9. namespace UICatalog {
  10. /// <summary>
  11. /// Main program for the Terminal.gui UI Catalog app. This app provides a chooser that allows
  12. /// for a calalog of UI demos, examples, and tests.
  13. /// </summary>
  14. class Program {
  15. private static Toplevel _top;
  16. private static MenuBar _menu;
  17. private static int _nameColumnWidth;
  18. private static Window _leftPane;
  19. private static List<string> _categories;
  20. private static ListView _categoryListView;
  21. private static Window _rightPane;
  22. private static List<Type> _scenarios;
  23. private static ListView _scenarioListView;
  24. private static StatusBar _statusBar;
  25. private static Scenario _runningScenario = null;
  26. static void Main (string [] args)
  27. {
  28. if (Debugger.IsAttached)
  29. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  30. _scenarios = Scenario.GetDerivedClassesCollection ().ToList ();
  31. if (args.Length > 0) {
  32. var item = _scenarios.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals (args [0], StringComparison.OrdinalIgnoreCase));
  33. _runningScenario = (Scenario)Activator.CreateInstance (_scenarios [item]);
  34. Application.Init ();
  35. _runningScenario.Init (Application.Top);
  36. _runningScenario.Setup ();
  37. _runningScenario.Run ();
  38. _runningScenario = null;
  39. return;
  40. }
  41. Scenario scenario = GetScenarioToRun ();
  42. while (scenario != null) {
  43. Application.Init ();
  44. scenario.Init (Application.Top);
  45. scenario.Setup ();
  46. scenario.Run ();
  47. scenario = GetScenarioToRun ();
  48. }
  49. }
  50. /// <summary>
  51. /// Create all controls. This gets called once and the controls remain with their state between Sceanrio runs.
  52. /// </summary>
  53. private static void Setup ()
  54. {
  55. _menu = new MenuBar (new MenuBarItem [] {
  56. new MenuBarItem ("_File", new MenuItem [] {
  57. new MenuItem ("_Quit", "", () => Application.RequestStop() )
  58. }),
  59. new MenuBarItem ("_About...", "About this app", () => MessageBox.Query (0, 10, "About UI Catalog", "UI Catalog is a comprehensive sample library for Terminal.Gui", "Ok")),
  60. });
  61. _leftPane = new Window ("Categories") {
  62. X = 0,
  63. Y = 1, // for menu
  64. Width = 25,
  65. Height = Dim.Fill (),
  66. CanFocus = false,
  67. };
  68. _categories = Scenario.GetAllCategories ();
  69. _categoryListView = new ListView (_categories) {
  70. X = 1,
  71. Y = 0,
  72. Width = Dim.Fill (0),
  73. Height = Dim.Fill (2),
  74. AllowsMarking = false,
  75. CanFocus = true,
  76. };
  77. _categoryListView.OpenSelectedItem += (o, a) => {
  78. _top.SetFocus (_rightPane);
  79. };
  80. _categoryListView.SelectedChanged += CategoryListView_SelectedChanged;
  81. _leftPane.Add (_categoryListView);
  82. _rightPane = new Window ("Scenarios") {
  83. X = 25,
  84. Y = 1, // for menu
  85. Width = Dim.Fill (),
  86. Height = Dim.Fill (),
  87. CanFocus = false,
  88. };
  89. _nameColumnWidth = Scenario.ScenarioMetadata.GetName (_scenarios.OrderByDescending (t => Scenario.ScenarioMetadata.GetName (t).Length).FirstOrDefault ()).Length;
  90. _scenarioListView = new ListView () {
  91. X = 0,
  92. Y = 0,
  93. Width = Dim.Fill (0),
  94. Height = Dim.Fill (0),
  95. AllowsMarking = false,
  96. CanFocus = true,
  97. };
  98. //_scenarioListView.OnKeyPress += (KeyEvent ke) => {
  99. // if (_top.MostFocused == _scenarioListView && ke.Key == Key.Enter) {
  100. // _scenarioListView_OpenSelectedItem (null, null);
  101. // }
  102. //};
  103. _scenarioListView.OpenSelectedItem += _scenarioListView_OpenSelectedItem;
  104. _rightPane.Add (_scenarioListView);
  105. _categoryListView.SelectedItem = 0;
  106. _categoryListView.OnSelectedChanged ();
  107. _statusBar = new StatusBar (new StatusItem [] {
  108. //new StatusItem(Key.F1, "~F1~ Help", () => Help()),
  109. new StatusItem(Key.ControlQ, "~CTRL-Q~ Quit", () => {
  110. if (_runningScenario is null){
  111. // This causes GetScenarioToRun to return null
  112. _runningScenario = null;
  113. Application.RequestStop();
  114. } else {
  115. _runningScenario.RequestStop();
  116. }
  117. }),
  118. });
  119. }
  120. /// <summary>
  121. /// This shows the selection UI. Each time it is run, it calls Application.Init to reset everything.
  122. /// </summary>
  123. /// <returns></returns>
  124. private static Scenario GetScenarioToRun ()
  125. {
  126. Application.Init ();
  127. if (_menu == null) {
  128. Setup ();
  129. }
  130. _top = Application.Top;
  131. _top.KeyUp += KeyUpHandler;
  132. _top.Add (_menu);
  133. _top.Add (_leftPane);
  134. _top.Add (_rightPane);
  135. _top.Add (_statusBar);
  136. // HACK: There is no other way to SetFocus before Application.Run. See Issue #445
  137. #if false
  138. if (_runningScenario != null)
  139. Application.Iteration += Application_Iteration;
  140. #else
  141. _top.Ready += (o, a) => {
  142. if (_runningScenario != null) {
  143. _top.SetFocus (_rightPane);
  144. _runningScenario = null;
  145. }
  146. };
  147. #endif
  148. Application.Run (_top);
  149. return _runningScenario;
  150. }
  151. #if false
  152. private static void Application_Iteration (object sender, EventArgs e)
  153. {
  154. Application.Iteration -= Application_Iteration;
  155. _top.SetFocus (_rightPane);
  156. }
  157. #endif
  158. private static void _scenarioListView_OpenSelectedItem (object sender, EventArgs e)
  159. {
  160. if (_runningScenario is null) {
  161. var source = _scenarioListView.Source as ScenarioListDataSource;
  162. _runningScenario = (Scenario)Activator.CreateInstance (source.Scenarios [_scenarioListView.SelectedItem]);
  163. Application.RequestStop ();
  164. }
  165. }
  166. internal class ScenarioListDataSource : IListDataSource {
  167. public List<Type> Scenarios { get; set; }
  168. public bool IsMarked (int item) => false;// Scenarios [item].IsMarked;
  169. public int Count => Scenarios.Count;
  170. public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
  171. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width)
  172. {
  173. container.Move (col, line);
  174. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  175. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  176. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width);
  177. }
  178. public void SetMark (int item, bool value)
  179. {
  180. }
  181. // A slightly adapted method from: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  182. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width)
  183. {
  184. int used = 0;
  185. int index = 0;
  186. while (index < ustr.Length) {
  187. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  188. var count = Rune.ColumnWidth (rune);
  189. if (used + count >= width) break;
  190. driver.AddRune (rune);
  191. used += count;
  192. index += size;
  193. }
  194. while (used < width) {
  195. driver.AddRune (' ');
  196. used++;
  197. }
  198. }
  199. public IList ToList ()
  200. {
  201. return Scenarios;
  202. }
  203. }
  204. /// <summary>
  205. /// When Scenarios are running we need to override the behavior of the Menu
  206. /// and Statusbar to enable Scenarios that use those (or related key input)
  207. /// to not be impacted. Same as for tabs.
  208. /// </summary>
  209. /// <param name="ke"></param>
  210. private static void KeyUpHandler (object sender, View.KeyEventEventArgs a)
  211. {
  212. if (_runningScenario != null) {
  213. //switch (ke.Key) {
  214. //case Key.Esc:
  215. // //_runningScenario.RequestStop ();
  216. // break;
  217. //case Key.Enter:
  218. // break;
  219. //}<
  220. } else if (a.KeyEvent.Key == Key.Tab || a.KeyEvent.Key == Key.BackTab) {
  221. // BUGBUG: Work around Issue #434 by implementing our own TAB navigation
  222. if (_top.MostFocused == _categoryListView)
  223. _top.SetFocus (_rightPane);
  224. else
  225. _top.SetFocus (_leftPane);
  226. }
  227. }
  228. private static void CategoryListView_SelectedChanged (object sender, ListViewItemEventArgs e)
  229. {
  230. var item = _categories [_categoryListView.SelectedItem];
  231. List<Type> newlist;
  232. if (item.Equals ("All")) {
  233. newlist = _scenarios;
  234. } else {
  235. newlist = _scenarios.Where (t => Scenario.ScenarioCategory.GetCategories (t).Contains (item)).ToList ();
  236. }
  237. _scenarioListView.Source = new ScenarioListDataSource (newlist);
  238. _scenarioListView.SelectedItem = 0;
  239. }
  240. }
  241. }