UICatalog.cs 11 KB

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