Program.cs 8.0 KB

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