UICatalog.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 FrameView _leftPane;
  48. private static List<string> _categories;
  49. private static ListView _categoryListView;
  50. private static FrameView _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 int _categoryListViewItem;
  58. private static int _scenarioListViewItem;
  59. private static Scenario _runningScenario = null;
  60. private static bool _useSystemConsole = false;
  61. static void Main (string [] args)
  62. {
  63. if (Debugger.IsAttached)
  64. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  65. _scenarios = Scenario.GetDerivedClasses<Scenario> ().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList ();
  66. if (args.Length > 0) {
  67. var item = _scenarios.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals (args [0], StringComparison.OrdinalIgnoreCase));
  68. _runningScenario = (Scenario)Activator.CreateInstance (_scenarios [item]);
  69. Application.Init ();
  70. _runningScenario.Init (Application.Top, _baseColorScheme);
  71. _runningScenario.Setup ();
  72. _runningScenario.Run ();
  73. _runningScenario = null;
  74. return;
  75. }
  76. Scenario scenario;
  77. while ((scenario = GetScenarioToRun ()) != null) {
  78. #if DEBUG_IDISPOSABLE
  79. // Validate there are no outstanding Responder-based instances
  80. // after a sceanario was selected to run. This proves the main UI Catalog
  81. // 'app' closed cleanly.
  82. foreach (var inst in Responder.Instances) {
  83. Debug.Assert (inst.WasDisposed);
  84. }
  85. Responder.Instances.Clear ();
  86. #endif
  87. Application.UseSystemConsole = _useSystemConsole;
  88. scenario.Init (Application.Top, _baseColorScheme);
  89. scenario.Setup ();
  90. scenario.Run ();
  91. static void ReadyHandler ()
  92. {
  93. _rightPane.SetFocus ();
  94. _top.Ready -= ReadyHandler;
  95. }
  96. _top.Ready += ReadyHandler;
  97. #if DEBUG_IDISPOSABLE
  98. // After the scenario runs, validate all Responder-based instances
  99. // were disposed. This proves the scenario 'app' closed cleanly.
  100. foreach (var inst in Responder.Instances) {
  101. Debug.Assert (inst.WasDisposed);
  102. }
  103. Responder.Instances.Clear();
  104. #endif
  105. }
  106. Application.Shutdown ();
  107. #if DEBUG_IDISPOSABLE
  108. // This proves that when the user exited the UI Catalog app
  109. // it cleaned up properly.
  110. foreach (var inst in Responder.Instances) {
  111. Debug.Assert (inst.WasDisposed);
  112. }
  113. Responder.Instances.Clear ();
  114. #endif
  115. }
  116. /// <summary>
  117. /// This shows the selection UI. Each time it is run, it calls Application.Init to reset everything.
  118. /// </summary>
  119. /// <returns></returns>
  120. private static Scenario GetScenarioToRun ()
  121. {
  122. Application.UseSystemConsole = false;
  123. Application.Init ();
  124. // Set this here because not initialized until driver is loaded
  125. _baseColorScheme = Colors.Base;
  126. StringBuilder aboutMessage = new StringBuilder ();
  127. aboutMessage.AppendLine ("UI Catalog is a comprehensive sample library for Terminal.Gui");
  128. aboutMessage.AppendLine (@" _ ");
  129. aboutMessage.AppendLine (@" __ _ _ _(_) ___ ___ ");
  130. aboutMessage.AppendLine (@" / _` | | | | | / __/ __|");
  131. aboutMessage.AppendLine (@"| (_| | |_| | || (__\__ \");
  132. aboutMessage.AppendLine (@" \__, |\__,_|_(_)___|___/");
  133. aboutMessage.AppendLine (@" |___/ ");
  134. aboutMessage.AppendLine ("");
  135. aboutMessage.AppendLine ($"Version: {typeof (UICatalogApp).Assembly.GetName ().Version}");
  136. aboutMessage.AppendLine ($"Using Terminal.Gui Version: {typeof (Terminal.Gui.Application).Assembly.GetName ().Version}");
  137. aboutMessage.AppendLine ("");
  138. _menu = new MenuBar (new MenuBarItem [] {
  139. new MenuBarItem ("_File", new MenuItem [] {
  140. new MenuItem ("_Quit", "", () => Application.RequestStop() )
  141. }),
  142. new MenuBarItem ("_Color Scheme", CreateColorSchemeMenuItems()),
  143. new MenuBarItem ("_Diagostics", CreateDiagnosticMenuItems()),
  144. new MenuBarItem ("_About...", "About this app", () => MessageBox.Query ("About UI Catalog", aboutMessage.ToString(), "_Ok")),
  145. });
  146. _leftPane = new FrameView ("Categories") {
  147. X = 0,
  148. Y = 1, // for menu
  149. Width = 25,
  150. Height = Dim.Fill (1),
  151. CanFocus = false,
  152. };
  153. _categories = Scenario.GetAllCategories ().OrderBy (c => c).ToList ();
  154. _categoryListView = new ListView (_categories) {
  155. X = 0,
  156. Y = 0,
  157. Width = Dim.Fill (0),
  158. Height = Dim.Fill (0),
  159. AllowsMarking = false,
  160. CanFocus = true,
  161. };
  162. _categoryListView.OpenSelectedItem += (a) => {
  163. _rightPane.SetFocus ();
  164. };
  165. _categoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
  166. _leftPane.Add (_categoryListView);
  167. _rightPane = new FrameView ("Scenarios") {
  168. X = 25,
  169. Y = 1, // for menu
  170. Width = Dim.Fill (),
  171. Height = Dim.Fill (1),
  172. CanFocus = true,
  173. };
  174. _nameColumnWidth = Scenario.ScenarioMetadata.GetName (_scenarios.OrderByDescending (t => Scenario.ScenarioMetadata.GetName (t).Length).FirstOrDefault ()).Length;
  175. _scenarioListView = new ListView () {
  176. X = 0,
  177. Y = 0,
  178. Width = Dim.Fill (0),
  179. Height = Dim.Fill (0),
  180. AllowsMarking = false,
  181. CanFocus = true,
  182. };
  183. _scenarioListView.OpenSelectedItem += _scenarioListView_OpenSelectedItem;
  184. _rightPane.Add (_scenarioListView);
  185. _categoryListView.SelectedItem = _categoryListViewItem;
  186. _categoryListView.OnSelectedChanged ();
  187. _capslock = new StatusItem (Key.CharMask, "Caps", null);
  188. _numlock = new StatusItem (Key.CharMask, "Num", null);
  189. _scrolllock = new StatusItem (Key.CharMask, "Scroll", null);
  190. _statusBar = new StatusBar (new StatusItem [] {
  191. _capslock,
  192. _numlock,
  193. _scrolllock,
  194. new StatusItem(Key.ControlQ, "~CTRL-Q~ Quit", () => {
  195. if (_runningScenario is null){
  196. // This causes GetScenarioToRun to return null
  197. _runningScenario = null;
  198. Application.RequestStop();
  199. } else {
  200. _runningScenario.RequestStop();
  201. }
  202. }),
  203. });
  204. SetColorScheme ();
  205. _top = Application.Top;
  206. _top.KeyDown += KeyDownHandler;
  207. _top.Add (_menu);
  208. _top.Add (_leftPane);
  209. _top.Add (_rightPane);
  210. _top.Add (_statusBar);
  211. _top.Ready += () => {
  212. if (_runningScenario != null) {
  213. _runningScenario = null;
  214. }
  215. };
  216. Application.Run (_top);
  217. return _runningScenario;
  218. }
  219. static MenuItem [] CreateDiagnosticMenuItems ()
  220. {
  221. MenuItem CheckedMenuMenuItem (ustring menuItem, Action action, Func<bool> checkFunction)
  222. {
  223. var mi = new MenuItem ();
  224. mi.Title = menuItem;
  225. mi.CheckType |= MenuItemCheckStyle.Checked;
  226. mi.Checked = checkFunction ();
  227. mi.Action = () => {
  228. action?.Invoke ();
  229. mi.Title = menuItem;
  230. mi.Checked = checkFunction ();
  231. };
  232. return mi;
  233. }
  234. return new MenuItem [] {
  235. CheckedMenuMenuItem ("Use _System Console",
  236. () => {
  237. _useSystemConsole = !_useSystemConsole;
  238. },
  239. () => _useSystemConsole),
  240. CheckedMenuMenuItem ("Diagnostics: _Frame Padding",
  241. () => {
  242. ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FramePadding;
  243. _top.SetNeedsDisplay ();
  244. },
  245. () => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding),
  246. CheckedMenuMenuItem ("Diagnostics: Frame _Ruler",
  247. () => {
  248. ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FrameRuler;
  249. _top.SetNeedsDisplay ();
  250. },
  251. () => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler),
  252. };
  253. }
  254. static void SetColorScheme ()
  255. {
  256. _leftPane.ColorScheme = _baseColorScheme;
  257. _rightPane.ColorScheme = _baseColorScheme;
  258. _top?.SetNeedsDisplay ();
  259. }
  260. static ColorScheme _baseColorScheme;
  261. static MenuItem [] CreateColorSchemeMenuItems ()
  262. {
  263. List<MenuItem> menuItems = new List<MenuItem> ();
  264. foreach (var sc in Colors.ColorSchemes) {
  265. var item = new MenuItem ();
  266. item.Title = sc.Key;
  267. item.CheckType |= MenuItemCheckStyle.Radio;
  268. item.Checked = sc.Value == _baseColorScheme;
  269. item.Action += () => {
  270. _baseColorScheme = sc.Value;
  271. SetColorScheme ();
  272. foreach (var menuItem in menuItems) {
  273. menuItem.Checked = menuItem.Title.Equals (sc.Key) && sc.Value == _baseColorScheme;
  274. }
  275. };
  276. menuItems.Add (item);
  277. }
  278. return menuItems.ToArray ();
  279. }
  280. private static void _scenarioListView_OpenSelectedItem (EventArgs e)
  281. {
  282. if (_runningScenario is null) {
  283. _scenarioListViewItem = _scenarioListView.SelectedItem;
  284. var source = _scenarioListView.Source as ScenarioListDataSource;
  285. _runningScenario = (Scenario)Activator.CreateInstance (source.Scenarios [_scenarioListView.SelectedItem]);
  286. Application.RequestStop ();
  287. }
  288. }
  289. internal class ScenarioListDataSource : IListDataSource {
  290. public List<Type> Scenarios { get; set; }
  291. public bool IsMarked (int item) => false;
  292. public int Count => Scenarios.Count;
  293. public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
  294. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width)
  295. {
  296. container.Move (col, line);
  297. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  298. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  299. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width);
  300. }
  301. public void SetMark (int item, bool value)
  302. {
  303. }
  304. // A slightly adapted method from: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  305. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width)
  306. {
  307. int used = 0;
  308. int index = 0;
  309. while (index < ustr.Length) {
  310. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  311. var count = Rune.ColumnWidth (rune);
  312. if (used + count >= width) break;
  313. driver.AddRune (rune);
  314. used += count;
  315. index += size;
  316. }
  317. while (used < width) {
  318. driver.AddRune (' ');
  319. used++;
  320. }
  321. }
  322. public IList ToList ()
  323. {
  324. return Scenarios;
  325. }
  326. }
  327. /// <summary>
  328. /// When Scenarios are running we need to override the behavior of the Menu
  329. /// and Statusbar to enable Scenarios that use those (or related key input)
  330. /// to not be impacted. Same as for tabs.
  331. /// </summary>
  332. /// <param name="ke"></param>
  333. private static void KeyDownHandler (View.KeyEventEventArgs a)
  334. {
  335. //if (a.KeyEvent.Key == Key.Tab || a.KeyEvent.Key == Key.BackTab) {
  336. // // BUGBUG: Work around Issue #434 by implementing our own TAB navigation
  337. // if (_top.MostFocused == _categoryListView)
  338. // _top.SetFocus (_rightPane);
  339. // else
  340. // _top.SetFocus (_leftPane);
  341. //}
  342. if (a.KeyEvent.IsCapslock) {
  343. _capslock.Title = "Caps: On";
  344. _statusBar.SetNeedsDisplay ();
  345. } else {
  346. _capslock.Title = "Caps: Off";
  347. _statusBar.SetNeedsDisplay ();
  348. }
  349. if (a.KeyEvent.IsNumlock) {
  350. _numlock.Title = "Num: On";
  351. _statusBar.SetNeedsDisplay ();
  352. } else {
  353. _numlock.Title = "Num: Off";
  354. _statusBar.SetNeedsDisplay ();
  355. }
  356. if (a.KeyEvent.IsScrolllock) {
  357. _scrolllock.Title = "Scroll: On";
  358. _statusBar.SetNeedsDisplay ();
  359. } else {
  360. _scrolllock.Title = "Scroll: Off";
  361. _statusBar.SetNeedsDisplay ();
  362. }
  363. }
  364. private static void CategoryListView_SelectedChanged (ListViewItemEventArgs e)
  365. {
  366. if (_categoryListViewItem != _categoryListView.SelectedItem) {
  367. _scenarioListViewItem = 0;
  368. }
  369. _categoryListViewItem = _categoryListView.SelectedItem;
  370. var item = _categories [_categoryListView.SelectedItem];
  371. List<Type> newlist;
  372. if (item.Equals ("All")) {
  373. newlist = _scenarios;
  374. } else {
  375. newlist = _scenarios.Where (t => Scenario.ScenarioCategory.GetCategories (t).Contains (item)).ToList ();
  376. }
  377. _scenarioListView.Source = new ScenarioListDataSource (newlist);
  378. _scenarioListView.SelectedItem = _scenarioListViewItem;
  379. }
  380. }
  381. }