UICatalog.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. /// <remarks>
  10. /// <para>
  11. /// UI Catalog attempts to satisfy the following goals:
  12. /// </para>
  13. /// <para>
  14. /// <list type="number">
  15. /// <item>
  16. /// <description>
  17. /// Be an easy to use showcase for Terminal.Gui concepts and features.
  18. /// </description>
  19. /// </item>
  20. /// <item>
  21. /// <description>
  22. /// Provide sample code that illustrates how to properly implement said concepts & features.
  23. /// </description>
  24. /// </item>
  25. /// <item>
  26. /// <description>
  27. /// Make it easy for contributors to add additional samples in a structured way.
  28. /// </description>
  29. /// </item>
  30. /// </list>
  31. /// </para>
  32. /// <para>
  33. /// See the project README for more details (https://github.com/migueldeicaza/gui.cs/tree/master/UICatalog/README.md).
  34. /// </para>
  35. /// </remarks>
  36. namespace UICatalog {
  37. /// <summary>
  38. /// UI Catalog is a comprehensive sample app and scenario library for <see cref="Terminal.Gui"/>
  39. /// </summary>
  40. public class UICatalogApp {
  41. private static Toplevel _top;
  42. private static MenuBar _menu;
  43. private static int _nameColumnWidth;
  44. private static Window _leftPane;
  45. private static List<string> _categories;
  46. private static ListView _categoryListView;
  47. private static Window _rightPane;
  48. private static List<Type> _scenarios;
  49. private static ListView _scenarioListView;
  50. private static StatusBar _statusBar;
  51. private static StatusItem _capslock;
  52. private static StatusItem _numlock;
  53. private static StatusItem _scrolllock;
  54. private static Scenario _runningScenario = null;
  55. static void Main (string [] args)
  56. {
  57. if (Debugger.IsAttached)
  58. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  59. _scenarios = Scenario.GetDerivedClassesCollection ().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList ();
  60. if (args.Length > 0) {
  61. var item = _scenarios.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals (args [0], StringComparison.OrdinalIgnoreCase));
  62. _runningScenario = (Scenario)Activator.CreateInstance (_scenarios [item]);
  63. Application.Init ();
  64. _runningScenario.Init (Application.Top);
  65. _runningScenario.Setup ();
  66. _runningScenario.Run ();
  67. _runningScenario = null;
  68. return;
  69. }
  70. Scenario scenario = GetScenarioToRun ();
  71. while (scenario != null) {
  72. Application.Init ();
  73. scenario.Init (Application.Top);
  74. scenario.Setup ();
  75. scenario.Run ();
  76. scenario = GetScenarioToRun ();
  77. }
  78. // Now closes the driver too.
  79. Application.Shutdown ();
  80. }
  81. /// <summary>
  82. /// Create all controls. This gets called once and the controls remain with their state between Sceanrio runs.
  83. /// </summary>
  84. private static void Setup ()
  85. {
  86. _menu = new MenuBar (new MenuBarItem [] {
  87. new MenuBarItem ("_File", new MenuItem [] {
  88. new MenuItem ("_Quit", "", () => Application.RequestStop() )
  89. }),
  90. new MenuBarItem ("_About...", "About this app", () => MessageBox.Query (50, 10, "About UI Catalog", "UI Catalog is a comprehensive sample library for Terminal.Gui", "Ok")),
  91. });
  92. _leftPane = new Window ("Categories") {
  93. X = 0,
  94. Y = 1, // for menu
  95. Width = 25,
  96. Height = Dim.Fill (),
  97. CanFocus = false,
  98. };
  99. _categories = Scenario.GetAllCategories ().OrderBy (c => c).ToList ();
  100. _categoryListView = new ListView (_categories) {
  101. X = 0,
  102. Y = 0,
  103. Width = Dim.Fill (0),
  104. Height = Dim.Fill (0),
  105. AllowsMarking = false,
  106. CanFocus = true,
  107. };
  108. _categoryListView.OpenSelectedItem += (o, a) => {
  109. _top.SetFocus (_rightPane);
  110. };
  111. _categoryListView.SelectedChanged += CategoryListView_SelectedChanged;
  112. _leftPane.Add (_categoryListView);
  113. _rightPane = new Window ("Scenarios") {
  114. X = 25,
  115. Y = 1, // for menu
  116. Width = Dim.Fill (),
  117. Height = Dim.Fill (),
  118. CanFocus = false,
  119. };
  120. _nameColumnWidth = Scenario.ScenarioMetadata.GetName (_scenarios.OrderByDescending (t => Scenario.ScenarioMetadata.GetName (t).Length).FirstOrDefault ()).Length;
  121. _scenarioListView = new ListView () {
  122. X = 0,
  123. Y = 0,
  124. Width = Dim.Fill (0),
  125. Height = Dim.Fill (0),
  126. AllowsMarking = false,
  127. CanFocus = true,
  128. };
  129. //_scenarioListView.OnKeyPress += (KeyEvent ke) => {
  130. // if (_top.MostFocused == _scenarioListView && ke.Key == Key.Enter) {
  131. // _scenarioListView_OpenSelectedItem (null, null);
  132. // }
  133. //};
  134. _scenarioListView.OpenSelectedItem += _scenarioListView_OpenSelectedItem;
  135. _rightPane.Add (_scenarioListView);
  136. _categoryListView.SelectedItem = 0;
  137. _categoryListView.OnSelectedChanged ();
  138. _capslock = new StatusItem (Key.CharMask, "CapslockOff", null);
  139. _numlock = new StatusItem (Key.CharMask, "NumlockOff", null);
  140. _scrolllock = new StatusItem (Key.CharMask, "ScrolllockOff", null);
  141. _statusBar = new StatusBar (new StatusItem [] {
  142. //new StatusItem(Key.F1, "~F1~ Help", () => Help()),
  143. new StatusItem(Key.ControlQ, "~CTRL-Q~ Quit", () => {
  144. if (_runningScenario is null){
  145. // This causes GetScenarioToRun to return null
  146. _runningScenario = null;
  147. Application.RequestStop();
  148. } else {
  149. _runningScenario.RequestStop();
  150. }
  151. }),
  152. _capslock,
  153. _numlock,
  154. _scrolllock
  155. });
  156. }
  157. /// <summary>
  158. /// This shows the selection UI. Each time it is run, it calls Application.Init to reset everything.
  159. /// </summary>
  160. /// <returns></returns>
  161. private static Scenario GetScenarioToRun ()
  162. {
  163. Application.Init ();
  164. if (_menu == null) {
  165. Setup ();
  166. }
  167. _top = Application.Top;
  168. _top.KeyDown += KeyDownHandler;
  169. _top.Add (_menu);
  170. _top.Add (_leftPane);
  171. _top.Add (_rightPane);
  172. _top.Add (_statusBar);
  173. // HACK: There is no other way to SetFocus before Application.Run. See Issue #445
  174. #if false
  175. if (_runningScenario != null)
  176. Application.Iteration += Application_Iteration;
  177. #else
  178. _top.Ready += (o, a) => {
  179. if (_runningScenario != null) {
  180. _top.SetFocus (_rightPane);
  181. _runningScenario = null;
  182. }
  183. };
  184. #endif
  185. Application.Run (_top, false);
  186. return _runningScenario;
  187. }
  188. #if false
  189. private static void Application_Iteration (object sender, EventArgs e)
  190. {
  191. Application.Iteration -= Application_Iteration;
  192. _top.SetFocus (_rightPane);
  193. }
  194. #endif
  195. private static void _scenarioListView_OpenSelectedItem (object sender, EventArgs e)
  196. {
  197. if (_runningScenario is null) {
  198. var source = _scenarioListView.Source as ScenarioListDataSource;
  199. _runningScenario = (Scenario)Activator.CreateInstance (source.Scenarios [_scenarioListView.SelectedItem]);
  200. Application.RequestStop ();
  201. }
  202. }
  203. internal class ScenarioListDataSource : IListDataSource {
  204. public List<Type> Scenarios { get; set; }
  205. public bool IsMarked (int item) => false;// Scenarios [item].IsMarked;
  206. public int Count => Scenarios.Count;
  207. public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
  208. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width)
  209. {
  210. container.Move (col, line);
  211. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  212. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  213. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width);
  214. }
  215. public void SetMark (int item, bool value)
  216. {
  217. }
  218. // A slightly adapted method from: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  219. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width)
  220. {
  221. int used = 0;
  222. int index = 0;
  223. while (index < ustr.Length) {
  224. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  225. var count = Rune.ColumnWidth (rune);
  226. if (used + count >= width) break;
  227. driver.AddRune (rune);
  228. used += count;
  229. index += size;
  230. }
  231. while (used < width) {
  232. driver.AddRune (' ');
  233. used++;
  234. }
  235. }
  236. public IList ToList ()
  237. {
  238. return Scenarios;
  239. }
  240. }
  241. /// <summary>
  242. /// When Scenarios are running we need to override the behavior of the Menu
  243. /// and Statusbar to enable Scenarios that use those (or related key input)
  244. /// to not be impacted. Same as for tabs.
  245. /// </summary>
  246. /// <param name="ke"></param>
  247. private static void KeyDownHandler (object sender, View.KeyEventEventArgs a)
  248. {
  249. if (_runningScenario != null) {
  250. //switch (ke.Key) {
  251. //case Key.Esc:
  252. // //_runningScenario.RequestStop ();
  253. // break;
  254. //case Key.Enter:
  255. // break;
  256. //}<
  257. } else if (a.KeyEvent.Key == Key.Tab || a.KeyEvent.Key == Key.BackTab) {
  258. // BUGBUG: Work around Issue #434 by implementing our own TAB navigation
  259. if (_top.MostFocused == _categoryListView)
  260. _top.SetFocus (_rightPane);
  261. else
  262. _top.SetFocus (_leftPane);
  263. }
  264. if (a.KeyEvent.IsCapslock) {
  265. _capslock.Title = "CapslockOn";
  266. _statusBar.SetNeedsDisplay ();
  267. } else {
  268. _capslock.Title = "CapslockOff";
  269. _statusBar.SetNeedsDisplay ();
  270. }
  271. if (a.KeyEvent.IsNumlock) {
  272. _numlock.Title = "NumlockOn";
  273. _statusBar.SetNeedsDisplay ();
  274. } else {
  275. _numlock.Title = "NumlockOff";
  276. _statusBar.SetNeedsDisplay ();
  277. }
  278. if (a.KeyEvent.IsScrolllock) {
  279. _scrolllock.Title = "ScrolllockOn";
  280. _statusBar.SetNeedsDisplay ();
  281. } else {
  282. _scrolllock.Title = "ScrolllockOff";
  283. _statusBar.SetNeedsDisplay ();
  284. }
  285. }
  286. private static void CategoryListView_SelectedChanged (object sender, ListViewItemEventArgs e)
  287. {
  288. var item = _categories [_categoryListView.SelectedItem];
  289. List<Type> newlist;
  290. if (item.Equals ("All")) {
  291. newlist = _scenarios;
  292. } else {
  293. newlist = _scenarios.Where (t => Scenario.ScenarioCategory.GetCategories (t).Contains (item)).ToList ();
  294. }
  295. _scenarioListView.Source = new ScenarioListDataSource (newlist);
  296. _scenarioListView.SelectedItem = 0;
  297. }
  298. }
  299. }