Program.cs 8.3 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. internal 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 _selectedScenario = null;
  26. static void Main (string [] args)
  27. {
  28. if (Debugger.IsAttached)
  29. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  30. _scenarios = Scenario.GetDerivedClassesCollection ().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList();
  31. if (args.Length > 0) {
  32. var item = _scenarios.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals (args [0], StringComparison.OrdinalIgnoreCase));
  33. _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios [item]);
  34. _selectedScenario.Init (Application.Top);
  35. _selectedScenario.Setup ();
  36. _selectedScenario.Run ();
  37. _selectedScenario = null;
  38. return;
  39. }
  40. Scenario scenario = GetScenarioToRun ();
  41. while (scenario != null) {
  42. scenario.Init (Application.Top);
  43. scenario.Setup ();
  44. scenario.Run ();
  45. scenario = GetScenarioToRun ();
  46. }
  47. }
  48. /// <summary>
  49. /// Create all controls. This gets called once and the controls remain with their state between Sceanrio runs.
  50. /// </summary>
  51. private static void Setup ()
  52. {
  53. _menu = new MenuBar (new MenuBarItem [] {
  54. new MenuBarItem ("_File", new MenuItem [] {
  55. new MenuItem ("_Quit", "", () => Application.RequestStop() )
  56. }),
  57. new MenuBarItem ("_About...", "About this app", () => MessageBox.Query (0, 10, "About UI Catalog", "UI Catalog is a comprehensive sample library for Terminal.Gui", "Ok")),
  58. });
  59. _leftPane = new Window ("Categories") {
  60. X = 0,
  61. Y = 1, // for menu
  62. Width = 25,
  63. Height = Dim.Fill (),
  64. CanFocus = false,
  65. };
  66. _categories = Scenario.GetAllCategories ().OrderBy(c => c).ToList();
  67. _categoryListView = new ListView (_categories) {
  68. X = 1,
  69. Y = 0,
  70. Width = Dim.Fill (0),
  71. Height = Dim.Fill (2),
  72. AllowsMarking = false,
  73. CanFocus = true,
  74. };
  75. _categoryListView.OpenSelectedItem += (o, a) => {
  76. _top.SetFocus (_rightPane);
  77. };
  78. _categoryListView.SelectedChanged += CategoryListView_SelectedChanged;
  79. _leftPane.Add (_categoryListView);
  80. _rightPane = new Window ("Scenarios") {
  81. X = 25,
  82. Y = 1, // for menu
  83. Width = Dim.Fill (),
  84. Height = Dim.Fill (),
  85. CanFocus = false,
  86. };
  87. _nameColumnWidth = Scenario.ScenarioMetadata.GetName (_scenarios.OrderByDescending (t => Scenario.ScenarioMetadata.GetName (t).Length).FirstOrDefault ()).Length;
  88. _scenarioListView = new ListView () {
  89. X = 0,
  90. Y = 0,
  91. Width = Dim.Fill (0),
  92. Height = Dim.Fill (0),
  93. AllowsMarking = false,
  94. CanFocus = true,
  95. };
  96. //_scenarioListView.OnKeyPress += (KeyEvent ke) => {
  97. // if (_top.MostFocused == _scenarioListView && ke.Key == Key.Enter) {
  98. // _scenarioListView_OpenSelectedItem (null, null);
  99. // }
  100. //};
  101. _scenarioListView.OpenSelectedItem += _scenarioListView_OpenSelectedItem;
  102. _rightPane.Add (_scenarioListView);
  103. _categoryListView.SelectedItem = 0;
  104. _categoryListView.OnSelectedChanged ();
  105. _statusBar = new StatusBar (new StatusItem [] {
  106. //new StatusItem(Key.F1, "~F1~ Help", () => Help()),
  107. new StatusItem(Key.ControlQ, "~CTRL-Q~ Quit", () => {
  108. if (_selectedScenario is null){
  109. // This causes GetScenarioToRun to return null
  110. _selectedScenario = null;
  111. Application.RequestStop();
  112. } else {
  113. _selectedScenario.RequestStop();
  114. }
  115. }),
  116. });
  117. }
  118. /// <summary>
  119. /// This shows the selection UI. Each time it is run, it calls Application.Init to reset everything.
  120. /// </summary>
  121. /// <returns></returns>
  122. private static Scenario GetScenarioToRun ()
  123. {
  124. Application.Init ();
  125. if (_menu == null) {
  126. Setup ();
  127. }
  128. _top = Application.Top;
  129. _top.KeyUp += KeyUpHandler;
  130. _top.Add (_menu);
  131. _top.Add (_leftPane);
  132. _top.Add (_rightPane);
  133. _top.Add (_statusBar);
  134. // HACK: There is no other way to SetFocus before Application.Run. See Issue #445
  135. #if false
  136. if (_runningScenario != null)
  137. Application.Iteration += Application_Iteration;
  138. #else
  139. _top.Ready += (o, a) => {
  140. if (_selectedScenario != null) {
  141. _top.SetFocus (_rightPane);
  142. _selectedScenario = null;
  143. }
  144. };
  145. #endif
  146. Application.Run (_top);
  147. Application.Shutdown ();
  148. return _selectedScenario;
  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 (_selectedScenario is null) {
  160. var source = _scenarioListView.Source as ScenarioListDataSource;
  161. _selectedScenario = (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. public IList ToList ()
  199. {
  200. return Scenarios;
  201. }
  202. }
  203. /// <summary>
  204. /// When Scenarios are running we need to override the behavior of the Menu
  205. /// and Statusbar to enable Scenarios that use those (or related key input)
  206. /// to not be impacted. Same as for tabs.
  207. /// </summary>
  208. /// <param name="ke"></param>
  209. private static void KeyUpHandler (object sender, View.KeyEventEventArgs a)
  210. {
  211. if (_selectedScenario != null) {
  212. //switch (ke.Key) {
  213. //case Key.Esc:
  214. // //_runningScenario.RequestStop ();
  215. // break;
  216. //case Key.Enter:
  217. // break;
  218. //}<
  219. } else if (a.KeyEvent.Key == Key.Tab || a.KeyEvent.Key == Key.BackTab) {
  220. // BUGBUG: Work around Issue #434 by implementing our own TAB navigation
  221. if (_top.MostFocused == _categoryListView)
  222. _top.SetFocus (_rightPane);
  223. else
  224. _top.SetFocus (_leftPane);
  225. }
  226. }
  227. private static void CategoryListView_SelectedChanged (object sender, ListViewItemEventArgs e)
  228. {
  229. var item = _categories [_categoryListView.SelectedItem];
  230. List<Type> newlist;
  231. if (item.Equals ("All")) {
  232. newlist = _scenarios;
  233. } else {
  234. newlist = _scenarios.Where (t => Scenario.ScenarioCategory.GetCategories (t).Contains (item)).ToList ();
  235. }
  236. _scenarioListView.Source = new ScenarioListDataSource (newlist);
  237. _scenarioListView.SelectedItem = 0;
  238. }
  239. }
  240. }