UICatalog.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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.Runtime.InteropServices;
  10. using System.Text;
  11. using Terminal.Gui;
  12. using Rune = System.Rune;
  13. /// <summary>
  14. /// UI Catalog is a comprehensive sample library for Terminal.Gui. It provides a simple UI for adding to the catalog of scenarios.
  15. /// </summary>
  16. /// <remarks>
  17. /// <para>
  18. /// UI Catalog attempts to satisfy the following goals:
  19. /// </para>
  20. /// <para>
  21. /// <list type="number">
  22. /// <item>
  23. /// <description>
  24. /// Be an easy to use showcase for Terminal.Gui concepts and features.
  25. /// </description>
  26. /// </item>
  27. /// <item>
  28. /// <description>
  29. /// Provide sample code that illustrates how to properly implement said concepts & features.
  30. /// </description>
  31. /// </item>
  32. /// <item>
  33. /// <description>
  34. /// Make it easy for contributors to add additional samples in a structured way.
  35. /// </description>
  36. /// </item>
  37. /// </list>
  38. /// </para>
  39. /// <para>
  40. /// See the project README for more details (https://github.com/migueldeicaza/gui.cs/tree/master/UICatalog/README.md).
  41. /// </para>
  42. /// </remarks>
  43. namespace UICatalog {
  44. /// <summary>
  45. /// UI Catalog is a comprehensive sample app and scenario library for <see cref="Terminal.Gui"/>
  46. /// </summary>
  47. public class UICatalogApp {
  48. private static Toplevel _top;
  49. private static MenuBar _menu;
  50. private static int _nameColumnWidth;
  51. private static FrameView _leftPane;
  52. private static List<string> _categories;
  53. private static ListView _categoryListView;
  54. private static FrameView _rightPane;
  55. private static List<Type> _scenarios;
  56. private static ListView _scenarioListView;
  57. private static StatusBar _statusBar;
  58. private static StatusItem _capslock;
  59. private static StatusItem _numlock;
  60. private static StatusItem _scrolllock;
  61. private static int _categoryListViewItem;
  62. private static int _scenarioListViewItem;
  63. private static Scenario _runningScenario = null;
  64. private static bool _useSystemConsole = false;
  65. private static ConsoleDriver.DiagnosticFlags _diagnosticFlags;
  66. private static bool _heightAsBuffer = false;
  67. private static bool _isFirstRunning = true;
  68. static void Main (string [] args)
  69. {
  70. Console.OutputEncoding = Encoding.Default;
  71. if (Debugger.IsAttached)
  72. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  73. _scenarios = Scenario.GetDerivedClasses<Scenario> ().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList ();
  74. if (args.Length > 0 && args.Contains ("-usc")) {
  75. _useSystemConsole = true;
  76. args = args.Where (val => val != "-usc").ToArray ();
  77. }
  78. if (args.Length > 0) {
  79. var item = _scenarios.FindIndex (t => Scenario.ScenarioMetadata.GetName (t).Equals (args [0], StringComparison.OrdinalIgnoreCase));
  80. _runningScenario = (Scenario)Activator.CreateInstance (_scenarios [item]);
  81. Application.UseSystemConsole = _useSystemConsole;
  82. Application.Init ();
  83. _runningScenario.Init (Application.Top, _baseColorScheme);
  84. _runningScenario.Setup ();
  85. _runningScenario.Run ();
  86. _runningScenario = null;
  87. Application.Shutdown ();
  88. return;
  89. }
  90. Scenario scenario;
  91. while ((scenario = GetScenarioToRun ()) != null) {
  92. #if DEBUG_IDISPOSABLE
  93. // Validate there are no outstanding Responder-based instances
  94. // after a scenario was selected to run. This proves the main UI Catalog
  95. // 'app' closed cleanly.
  96. foreach (var inst in Responder.Instances) {
  97. Debug.Assert (inst.WasDisposed);
  98. }
  99. Responder.Instances.Clear ();
  100. #endif
  101. scenario.Init (Application.Top, _baseColorScheme);
  102. scenario.Setup ();
  103. scenario.Run ();
  104. //static void LoadedHandler ()
  105. //{
  106. // _rightPane.SetFocus ();
  107. // _top.Loaded -= LoadedHandler;
  108. //}
  109. //_top.Loaded += LoadedHandler;
  110. Application.Shutdown ();
  111. #if DEBUG_IDISPOSABLE
  112. // After the scenario runs, validate all Responder-based instances
  113. // were disposed. This proves the scenario 'app' closed cleanly.
  114. foreach (var inst in Responder.Instances) {
  115. Debug.Assert (inst.WasDisposed);
  116. }
  117. Responder.Instances.Clear ();
  118. #endif
  119. }
  120. Application.Shutdown ();
  121. #if DEBUG_IDISPOSABLE
  122. // This proves that when the user exited the UI Catalog app
  123. // it cleaned up properly.
  124. foreach (var inst in Responder.Instances) {
  125. Debug.Assert (inst.WasDisposed);
  126. }
  127. Responder.Instances.Clear ();
  128. #endif
  129. }
  130. /// <summary>
  131. /// This shows the selection UI. Each time it is run, it calls Application.Init to reset everything.
  132. /// </summary>
  133. /// <returns></returns>
  134. private static Scenario GetScenarioToRun ()
  135. {
  136. Application.UseSystemConsole = _useSystemConsole;
  137. Application.Init ();
  138. Application.HeightAsBuffer = _heightAsBuffer;
  139. // Set this here because not initialized until driver is loaded
  140. _baseColorScheme = Colors.Base;
  141. StringBuilder aboutMessage = new StringBuilder ();
  142. aboutMessage.AppendLine (@"");
  143. aboutMessage.AppendLine (@"UI Catalog is a comprehensive sample library for Terminal.Gui");
  144. aboutMessage.AppendLine (@"");
  145. aboutMessage.AppendLine (@" _______ _ _ _____ _ ");
  146. aboutMessage.AppendLine (@" |__ __| (_) | | / ____| (_)");
  147. aboutMessage.AppendLine (@" | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ");
  148. aboutMessage.AppendLine (@" | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | |");
  149. aboutMessage.AppendLine (@" | | __/ | | | | | | | | | | | (_| | || |__| | |_| | |");
  150. aboutMessage.AppendLine (@" |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_|");
  151. aboutMessage.AppendLine (@"");
  152. aboutMessage.AppendLine ($"Using Terminal.Gui Version: {FileVersionInfo.GetVersionInfo (typeof (Terminal.Gui.Application).Assembly.Location).FileVersion}");
  153. aboutMessage.AppendLine (@"");
  154. _menu = new MenuBar (new MenuBarItem [] {
  155. new MenuBarItem ("_File", new MenuItem [] {
  156. new MenuItem ("_Quit", "", () => Application.RequestStop(), null, null, Key.Q | Key.CtrlMask)
  157. }),
  158. new MenuBarItem ("_Color Scheme", CreateColorSchemeMenuItems()),
  159. new MenuBarItem ("Diag_nostics", CreateDiagnosticMenuItems()),
  160. new MenuBarItem ("_Help", new MenuItem [] {
  161. new MenuItem ("_gui.cs API Overview", "", () => OpenUrl ("https://migueldeicaza.github.io/gui.cs/articles/overview.html"), null, null, Key.F1),
  162. new MenuItem ("gui.cs _README", "", () => OpenUrl ("https://github.com/migueldeicaza/gui.cs"), null, null, Key.F2),
  163. new MenuItem ("_About...", "About this app", () => MessageBox.Query (aboutMessage.Length + 2, 15, "About", aboutMessage.ToString(), "_Ok"), null, null, Key.CtrlMask | Key.A),
  164. })
  165. });
  166. _leftPane = new FrameView ("Categories") {
  167. X = 0,
  168. Y = 1, // for menu
  169. Width = 25,
  170. Height = Dim.Fill (1),
  171. CanFocus = false,
  172. Shortcut = Key.CtrlMask | Key.C
  173. };
  174. _leftPane.Title = $"{_leftPane.Title} ({_leftPane.ShortcutTag})";
  175. _leftPane.ShortcutAction = () => _leftPane.SetFocus ();
  176. _categories = Scenario.GetAllCategories ();
  177. _categoryListView = new ListView (_categories) {
  178. X = 0,
  179. Y = 0,
  180. Width = Dim.Fill (0),
  181. Height = Dim.Fill (0),
  182. AllowsMarking = false,
  183. CanFocus = true,
  184. };
  185. _categoryListView.OpenSelectedItem += (a) => {
  186. _rightPane.SetFocus ();
  187. };
  188. _categoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
  189. _leftPane.Add (_categoryListView);
  190. _rightPane = new FrameView ("Scenarios") {
  191. X = 25,
  192. Y = 1, // for menu
  193. Width = Dim.Fill (),
  194. Height = Dim.Fill (1),
  195. CanFocus = true,
  196. Shortcut = Key.CtrlMask | Key.S
  197. };
  198. _rightPane.Title = $"{_rightPane.Title} ({_rightPane.ShortcutTag})";
  199. _rightPane.ShortcutAction = () => _rightPane.SetFocus ();
  200. _nameColumnWidth = Scenario.ScenarioMetadata.GetName (_scenarios.OrderByDescending (t => Scenario.ScenarioMetadata.GetName (t).Length).FirstOrDefault ()).Length;
  201. _scenarioListView = new ListView () {
  202. X = 0,
  203. Y = 0,
  204. Width = Dim.Fill (0),
  205. Height = Dim.Fill (0),
  206. AllowsMarking = false,
  207. CanFocus = true,
  208. };
  209. _scenarioListView.OpenSelectedItem += _scenarioListView_OpenSelectedItem;
  210. _rightPane.Add (_scenarioListView);
  211. _categoryListView.SelectedItem = _categoryListViewItem;
  212. _categoryListView.OnSelectedChanged ();
  213. _capslock = new StatusItem (Key.CharMask, "Caps", null);
  214. _numlock = new StatusItem (Key.CharMask, "Num", null);
  215. _scrolllock = new StatusItem (Key.CharMask, "Scroll", null);
  216. _statusBar = new StatusBar () {
  217. Visible = true,
  218. };
  219. _statusBar.Items = new StatusItem [] {
  220. _capslock,
  221. _numlock,
  222. _scrolllock,
  223. new StatusItem(Key.Q | Key.CtrlMask, "~CTRL-Q~ Quit", () => {
  224. if (_runningScenario is null){
  225. // This causes GetScenarioToRun to return null
  226. _runningScenario = null;
  227. Application.RequestStop();
  228. } else {
  229. _runningScenario.RequestStop();
  230. }
  231. }),
  232. new StatusItem(Key.F10, "~F10~ Hide/Show Status Bar", () => {
  233. _statusBar.Visible = !_statusBar.Visible;
  234. _leftPane.Height = Dim.Fill(_statusBar.Visible ? 1 : 0);
  235. _rightPane.Height = Dim.Fill(_statusBar.Visible ? 1 : 0);
  236. _top.LayoutSubviews();
  237. _top.SetChildNeedsDisplay();
  238. }),
  239. new StatusItem (Key.CharMask, Application.Driver.GetType ().Name, null),
  240. };
  241. SetColorScheme ();
  242. _top = Application.Top;
  243. _top.KeyDown += KeyDownHandler;
  244. _top.Add (_menu);
  245. _top.Add (_leftPane);
  246. _top.Add (_rightPane);
  247. _top.Add (_statusBar);
  248. void TopHandler () {
  249. if (_runningScenario != null) {
  250. _runningScenario = null;
  251. _isFirstRunning = false;
  252. }
  253. if (!_isFirstRunning) {
  254. _rightPane.SetFocus ();
  255. }
  256. _top.Loaded -= TopHandler;
  257. }
  258. _top.Loaded += TopHandler;
  259. // The following code was moved to the TopHandler event
  260. // because in the MainLoop.EventsPending (wait)
  261. // from the Application.RunLoop with the WindowsDriver
  262. // the OnReady event is triggered due the Focus event.
  263. // On CursesDriver and NetDriver the focus event won't be triggered
  264. // and if it's possible I don't know how to do it.
  265. //void ReadyHandler ()
  266. //{
  267. // if (!_isFirstRunning) {
  268. // _rightPane.SetFocus ();
  269. // }
  270. // _top.Ready -= ReadyHandler;
  271. //}
  272. //_top.Ready += ReadyHandler;
  273. Application.Run (_top);
  274. return _runningScenario;
  275. }
  276. static List<MenuItem []> CreateDiagnosticMenuItems ()
  277. {
  278. List<MenuItem []> menuItems = new List<MenuItem []> ();
  279. menuItems.Add (CreateDiagnosticFlagsMenuItems ());
  280. menuItems.Add (new MenuItem [] { null });
  281. menuItems.Add (CreateSizeStyle ());
  282. menuItems.Add (CreateDisabledEnabledMouse ());
  283. menuItems.Add (CreateKeybindings ());
  284. return menuItems;
  285. }
  286. private static MenuItem [] CreateDisabledEnabledMouse ()
  287. {
  288. List<MenuItem> menuItems = new List<MenuItem> ();
  289. var item = new MenuItem ();
  290. item.Title = "_Disable/Enable Mouse";
  291. item.Shortcut = Key.CtrlMask | Key.AltMask | (Key)item.Title.ToString ().Substring (1, 1) [0];
  292. item.CheckType |= MenuItemCheckStyle.Checked;
  293. item.Checked = Application.IsMouseDisabled;
  294. item.Action += () => {
  295. item.Checked = Application.IsMouseDisabled = !item.Checked;
  296. };
  297. menuItems.Add (item);
  298. return menuItems.ToArray ();
  299. }
  300. private static MenuItem[] CreateKeybindings()
  301. {
  302. List<MenuItem> menuItems = new List<MenuItem> ();
  303. var item = new MenuItem ();
  304. item.Title = "Keybindings";
  305. item.Action += () => {
  306. var dlg = new KeyBindingsDialog ();
  307. Application.Run (dlg);
  308. };
  309. menuItems.Add (null);
  310. menuItems.Add (item);
  311. return menuItems.ToArray ();
  312. }
  313. static MenuItem [] CreateSizeStyle ()
  314. {
  315. List<MenuItem> menuItems = new List<MenuItem> ();
  316. var item = new MenuItem ();
  317. item.Title = "_Height As Buffer";
  318. item.Shortcut = Key.CtrlMask | Key.AltMask | (Key)item.Title.ToString ().Substring (1, 1) [0];
  319. item.CheckType |= MenuItemCheckStyle.Checked;
  320. item.Checked = Application.HeightAsBuffer;
  321. item.Action += () => {
  322. item.Checked = !item.Checked;
  323. _heightAsBuffer = item.Checked;
  324. Application.HeightAsBuffer = _heightAsBuffer;
  325. };
  326. menuItems.Add (item);
  327. return menuItems.ToArray ();
  328. }
  329. static MenuItem [] CreateDiagnosticFlagsMenuItems ()
  330. {
  331. const string OFF = "Diagnostics: _Off";
  332. const string FRAME_RULER = "Diagnostics: Frame _Ruler";
  333. const string FRAME_PADDING = "Diagnostics: _Frame Padding";
  334. var index = 0;
  335. List<MenuItem> menuItems = new List<MenuItem> ();
  336. foreach (Enum diag in Enum.GetValues (_diagnosticFlags.GetType ())) {
  337. var item = new MenuItem ();
  338. item.Title = GetDiagnosticsTitle (diag);
  339. item.Shortcut = Key.AltMask + index.ToString () [0];
  340. index++;
  341. item.CheckType |= MenuItemCheckStyle.Checked;
  342. if (GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off) == item.Title) {
  343. item.Checked = (_diagnosticFlags & (ConsoleDriver.DiagnosticFlags.FramePadding
  344. | ConsoleDriver.DiagnosticFlags.FrameRuler)) == 0;
  345. } else {
  346. item.Checked = _diagnosticFlags.HasFlag (diag);
  347. }
  348. item.Action += () => {
  349. var t = GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off);
  350. if (item.Title == t && !item.Checked) {
  351. _diagnosticFlags &= ~(ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  352. item.Checked = true;
  353. } else if (item.Title == t && item.Checked) {
  354. _diagnosticFlags |= (ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  355. item.Checked = false;
  356. } else {
  357. var f = GetDiagnosticsEnumValue (item.Title);
  358. if (_diagnosticFlags.HasFlag (f)) {
  359. SetDiagnosticsFlag (f, false);
  360. } else {
  361. SetDiagnosticsFlag (f, true);
  362. }
  363. }
  364. foreach (var menuItem in menuItems) {
  365. if (menuItem.Title == t) {
  366. menuItem.Checked = !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FrameRuler)
  367. && !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FramePadding);
  368. } else if (menuItem.Title != t) {
  369. menuItem.Checked = _diagnosticFlags.HasFlag (GetDiagnosticsEnumValue (menuItem.Title));
  370. }
  371. }
  372. ConsoleDriver.Diagnostics = _diagnosticFlags;
  373. _top.SetNeedsDisplay ();
  374. };
  375. menuItems.Add (item);
  376. }
  377. return menuItems.ToArray ();
  378. string GetDiagnosticsTitle (Enum diag)
  379. {
  380. switch (Enum.GetName (_diagnosticFlags.GetType (), diag)) {
  381. case "Off":
  382. return OFF;
  383. case "FrameRuler":
  384. return FRAME_RULER;
  385. case "FramePadding":
  386. return FRAME_PADDING;
  387. }
  388. return "";
  389. }
  390. Enum GetDiagnosticsEnumValue (ustring title)
  391. {
  392. switch (title.ToString ()) {
  393. case FRAME_RULER:
  394. return ConsoleDriver.DiagnosticFlags.FrameRuler;
  395. case FRAME_PADDING:
  396. return ConsoleDriver.DiagnosticFlags.FramePadding;
  397. }
  398. return null;
  399. }
  400. void SetDiagnosticsFlag (Enum diag, bool add)
  401. {
  402. switch (diag) {
  403. case ConsoleDriver.DiagnosticFlags.FrameRuler:
  404. if (add) {
  405. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FrameRuler;
  406. } else {
  407. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FrameRuler;
  408. }
  409. break;
  410. case ConsoleDriver.DiagnosticFlags.FramePadding:
  411. if (add) {
  412. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FramePadding;
  413. } else {
  414. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FramePadding;
  415. }
  416. break;
  417. default:
  418. _diagnosticFlags = default;
  419. break;
  420. }
  421. }
  422. //MenuItem CheckedMenuMenuItem (ustring menuItem, Action action, Func<bool> checkFunction)
  423. //{
  424. // var mi = new MenuItem ();
  425. // mi.Title = menuItem;
  426. // mi.Shortcut = Key.AltMask + index.ToString () [0];
  427. // index++;
  428. // mi.CheckType |= MenuItemCheckStyle.Checked;
  429. // mi.Checked = checkFunction ();
  430. // mi.Action = () => {
  431. // action?.Invoke ();
  432. // mi.Title = menuItem;
  433. // mi.Checked = checkFunction ();
  434. // };
  435. // return mi;
  436. //}
  437. //return new MenuItem [] {
  438. // CheckedMenuMenuItem ("Use _System Console",
  439. // () => {
  440. // _useSystemConsole = !_useSystemConsole;
  441. // },
  442. // () => _useSystemConsole),
  443. // CheckedMenuMenuItem ("Diagnostics: _Frame Padding",
  444. // () => {
  445. // ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FramePadding;
  446. // _top.SetNeedsDisplay ();
  447. // },
  448. // () => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding),
  449. // CheckedMenuMenuItem ("Diagnostics: Frame _Ruler",
  450. // () => {
  451. // ConsoleDriver.Diagnostics ^= ConsoleDriver.DiagnosticFlags.FrameRuler;
  452. // _top.SetNeedsDisplay ();
  453. // },
  454. // () => (ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler),
  455. //};
  456. }
  457. static void SetColorScheme ()
  458. {
  459. _leftPane.ColorScheme = _baseColorScheme;
  460. _rightPane.ColorScheme = _baseColorScheme;
  461. _top?.SetNeedsDisplay ();
  462. }
  463. static ColorScheme _baseColorScheme;
  464. static MenuItem [] CreateColorSchemeMenuItems ()
  465. {
  466. List<MenuItem> menuItems = new List<MenuItem> ();
  467. foreach (var sc in Colors.ColorSchemes) {
  468. var item = new MenuItem ();
  469. item.Title = $"_{sc.Key}";
  470. item.Shortcut = Key.AltMask | (Key)sc.Key.Substring (0, 1) [0];
  471. item.CheckType |= MenuItemCheckStyle.Radio;
  472. item.Checked = sc.Value == _baseColorScheme;
  473. item.Action += () => {
  474. _baseColorScheme = sc.Value;
  475. SetColorScheme ();
  476. foreach (var menuItem in menuItems) {
  477. menuItem.Checked = menuItem.Title.Equals ($"_{sc.Key}") && sc.Value == _baseColorScheme;
  478. }
  479. };
  480. menuItems.Add (item);
  481. }
  482. return menuItems.ToArray ();
  483. }
  484. private static void _scenarioListView_OpenSelectedItem (EventArgs e)
  485. {
  486. if (_runningScenario is null) {
  487. _scenarioListViewItem = _scenarioListView.SelectedItem;
  488. var source = _scenarioListView.Source as ScenarioListDataSource;
  489. _runningScenario = (Scenario)Activator.CreateInstance (source.Scenarios [_scenarioListView.SelectedItem]);
  490. Application.RequestStop ();
  491. }
  492. }
  493. internal class ScenarioListDataSource : IListDataSource {
  494. private readonly int len;
  495. public List<Type> Scenarios { get; set; }
  496. public bool IsMarked (int item) => false;
  497. public int Count => Scenarios.Count;
  498. public int Length => len;
  499. public ScenarioListDataSource (List<Type> itemList)
  500. {
  501. Scenarios = itemList;
  502. len = GetMaxLengthItem ();
  503. }
  504. public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0)
  505. {
  506. container.Move (col, line);
  507. // Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
  508. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
  509. RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width, start);
  510. }
  511. public void SetMark (int item, bool value)
  512. {
  513. }
  514. int GetMaxLengthItem ()
  515. {
  516. if (Scenarios?.Count == 0) {
  517. return 0;
  518. }
  519. int maxLength = 0;
  520. for (int i = 0; i < Scenarios.Count; i++) {
  521. var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [i]));
  522. var sc = $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [i])}";
  523. var l = sc.Length;
  524. if (l > maxLength) {
  525. maxLength = l;
  526. }
  527. }
  528. return maxLength;
  529. }
  530. // A slightly adapted method from: https://github.com/migueldeicaza/gui.cs/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
  531. private void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width, int start = 0)
  532. {
  533. int used = 0;
  534. int index = start;
  535. while (index < ustr.Length) {
  536. (var rune, var size) = Utf8.DecodeRune (ustr, index, index - ustr.Length);
  537. var count = Rune.ColumnWidth (rune);
  538. if (used + count >= width) break;
  539. driver.AddRune (rune);
  540. used += count;
  541. index += size;
  542. }
  543. while (used < width) {
  544. driver.AddRune (' ');
  545. used++;
  546. }
  547. }
  548. public IList ToList ()
  549. {
  550. return Scenarios;
  551. }
  552. }
  553. /// <summary>
  554. /// When Scenarios are running we need to override the behavior of the Menu
  555. /// and Statusbar to enable Scenarios that use those (or related key input)
  556. /// to not be impacted. Same as for tabs.
  557. /// </summary>
  558. /// <param name="ke"></param>
  559. private static void KeyDownHandler (View.KeyEventEventArgs a)
  560. {
  561. //if (a.KeyEvent.Key == Key.Tab || a.KeyEvent.Key == Key.BackTab) {
  562. // // BUGBUG: Work around Issue #434 by implementing our own TAB navigation
  563. // if (_top.MostFocused == _categoryListView)
  564. // _top.SetFocus (_rightPane);
  565. // else
  566. // _top.SetFocus (_leftPane);
  567. //}
  568. if (a.KeyEvent.IsCapslock) {
  569. _capslock.Title = "Caps: On";
  570. _statusBar.SetNeedsDisplay ();
  571. } else {
  572. _capslock.Title = "Caps: Off";
  573. _statusBar.SetNeedsDisplay ();
  574. }
  575. if (a.KeyEvent.IsNumlock) {
  576. _numlock.Title = "Num: On";
  577. _statusBar.SetNeedsDisplay ();
  578. } else {
  579. _numlock.Title = "Num: Off";
  580. _statusBar.SetNeedsDisplay ();
  581. }
  582. if (a.KeyEvent.IsScrolllock) {
  583. _scrolllock.Title = "Scroll: On";
  584. _statusBar.SetNeedsDisplay ();
  585. } else {
  586. _scrolllock.Title = "Scroll: Off";
  587. _statusBar.SetNeedsDisplay ();
  588. }
  589. }
  590. private static void CategoryListView_SelectedChanged (ListViewItemEventArgs e)
  591. {
  592. if (_categoryListViewItem != _categoryListView.SelectedItem) {
  593. _scenarioListViewItem = 0;
  594. }
  595. _categoryListViewItem = _categoryListView.SelectedItem;
  596. var item = _categories [_categoryListViewItem];
  597. List<Type> newlist;
  598. if (_categoryListViewItem == 0) {
  599. // First category is "All"
  600. newlist = _scenarios;
  601. } else {
  602. newlist = _scenarios.Where (t => Scenario.ScenarioCategory.GetCategories (t).Contains (item)).ToList ();
  603. }
  604. _scenarioListView.Source = new ScenarioListDataSource (newlist);
  605. _scenarioListView.SelectedItem = _scenarioListViewItem;
  606. }
  607. private static void OpenUrl (string url)
  608. {
  609. try {
  610. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  611. url = url.Replace ("&", "^&");
  612. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  613. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  614. using (var process = new Process {
  615. StartInfo = new ProcessStartInfo {
  616. FileName = "xdg-open",
  617. Arguments = url,
  618. RedirectStandardError = true,
  619. RedirectStandardOutput = true,
  620. CreateNoWindow = true,
  621. UseShellExecute = false
  622. }
  623. }) {
  624. process.Start ();
  625. }
  626. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  627. Process.Start ("open", url);
  628. }
  629. } catch {
  630. throw;
  631. }
  632. }
  633. }
  634. }