UICatalog.cs 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. global using Attribute = Terminal.Gui.Attribute;
  2. global using CM = Terminal.Gui.ConfigurationManager;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.CommandLine;
  7. using System.Diagnostics;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using System.Text.Json.Serialization;
  15. using Terminal.Gui;
  16. using static Terminal.Gui.ConfigurationManager;
  17. using Command = Terminal.Gui.Command;
  18. using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
  19. #nullable enable
  20. namespace UICatalog;
  21. /// <summary>
  22. /// UI Catalog is a comprehensive sample library for Terminal.Gui. It provides a simple UI for adding to the
  23. /// catalog of scenarios.
  24. /// </summary>
  25. /// <remarks>
  26. /// <para>UI Catalog attempts to satisfy the following goals:</para>
  27. /// <para>
  28. /// <list type="number">
  29. /// <item>
  30. /// <description>Be an easy to use showcase for Terminal.Gui concepts and features.</description>
  31. /// </item>
  32. /// <item>
  33. /// <description>Provide sample code that illustrates how to properly implement said concepts & features.</description>
  34. /// </item>
  35. /// <item>
  36. /// <description>Make it easy for contributors to add additional samples in a structured way.</description>
  37. /// </item>
  38. /// </list>
  39. /// </para>
  40. /// <para>
  41. /// See the project README for more details
  42. /// (https://github.com/gui-cs/Terminal.Gui/tree/master/UICatalog/README.md).
  43. /// </para>
  44. /// </remarks>
  45. public class UICatalogApp
  46. {
  47. private static int _cachedCategoryIndex;
  48. // When a scenario is run, the main app is killed. These items
  49. // are therefore cached so that when the scenario exits the
  50. // main app UI can be restored to previous state
  51. private static int _cachedScenarioIndex;
  52. private static string? _cachedTheme = string.Empty;
  53. private static ObservableCollection<string>? _categories;
  54. private static readonly FileSystemWatcher _currentDirWatcher = new ();
  55. private static ViewDiagnosticFlags _diagnosticFlags;
  56. private static string _forceDriver = string.Empty;
  57. private static readonly FileSystemWatcher _homeDirWatcher = new ();
  58. private static bool _isFirstRunning = true;
  59. private static Options _options;
  60. private static ObservableCollection<Scenario>? _scenarios;
  61. // If set, holds the scenario the user selected
  62. private static Scenario? _selectedScenario;
  63. private static MenuBarItem? _themeMenuBarItem;
  64. private static MenuItem []? _themeMenuItems;
  65. private static string _topLevelColorScheme = string.Empty;
  66. [SerializableConfigurationProperty (Scope = typeof (AppScope), OmitClassName = true)]
  67. [JsonPropertyName ("UICatalog.StatusBar")]
  68. public static bool ShowStatusBar { get; set; } = true;
  69. /// <summary>
  70. /// Gets the message displayed in the About Box. `public` so it can be used from Unit tests.
  71. /// </summary>
  72. /// <returns></returns>
  73. public static string GetAboutBoxMessage ()
  74. {
  75. // NOTE: Do not use multiline verbatim strings here.
  76. // WSL gets all confused.
  77. StringBuilder msg = new ();
  78. msg.AppendLine ("UI Catalog: A comprehensive sample library for");
  79. msg.AppendLine ();
  80. msg.AppendLine ("""
  81. _______ _ _ _____ _
  82. |__ __| (_) | | / ____| (_)
  83. | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _
  84. | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | |
  85. | | __/ | | | | | | | | | | | (_| | || |__| | |_| | |
  86. |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_|
  87. """);
  88. msg.AppendLine ();
  89. msg.AppendLine ("v2 - Pre-Alpha");
  90. msg.AppendLine ();
  91. msg.AppendLine ("https://github.com/gui-cs/Terminal.Gui");
  92. return msg.ToString ();
  93. }
  94. private static void ConfigFileChanged (object sender, FileSystemEventArgs e)
  95. {
  96. if (Application.Top == null)
  97. {
  98. return;
  99. }
  100. // TODO: This is a hack. Figure out how to ensure that the file is fully written before reading it.
  101. //Thread.Sleep (500);
  102. Load ();
  103. Apply ();
  104. }
  105. private static int Main (string [] args)
  106. {
  107. Console.OutputEncoding = Encoding.Default;
  108. if (Debugger.IsAttached)
  109. {
  110. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  111. }
  112. _scenarios = Scenario.GetScenarios ();
  113. _categories = Scenario.GetAllCategories ();
  114. // Process command line args
  115. // "UICatalog [-driver <driver>] [scenario name]"
  116. // If no driver is provided, the default driver is used.
  117. Option<string> driverOption = new Option<string> ("--driver", "The ConsoleDriver to use.").FromAmong (
  118. Application.GetDriverTypes ()
  119. .Select (d => d.Name)
  120. .ToArray ()
  121. );
  122. driverOption.AddAlias ("-d");
  123. driverOption.AddAlias ("--d");
  124. Argument<string> scenarioArgument = new Argument<string> (
  125. "scenario",
  126. description: "The name of the scenario to run.",
  127. getDefaultValue: () => "none"
  128. ).FromAmong (
  129. _scenarios.Select (s => s.GetName ())
  130. .Append ("none")
  131. .ToArray ()
  132. );
  133. var rootCommand =
  134. new RootCommand ("A comprehensive sample library for Terminal.Gui") { scenarioArgument, driverOption };
  135. rootCommand.SetHandler (
  136. context =>
  137. {
  138. var options = new Options
  139. {
  140. Driver = context.ParseResult.GetValueForOption (driverOption) ?? string.Empty,
  141. Scenario = context.ParseResult.GetValueForArgument (scenarioArgument)
  142. /* etc. */
  143. };
  144. // See https://github.com/dotnet/command-line-api/issues/796 for the rationale behind this hackery
  145. _options = options;
  146. }
  147. );
  148. rootCommand.Invoke (args);
  149. UICatalogMain (_options);
  150. return 0;
  151. }
  152. private static void OpenUrl (string url)
  153. {
  154. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows))
  155. {
  156. url = url.Replace ("&", "^&");
  157. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  158. }
  159. else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux))
  160. {
  161. using var process = new Process
  162. {
  163. StartInfo = new ()
  164. {
  165. FileName = "xdg-open",
  166. Arguments = url,
  167. RedirectStandardError = true,
  168. RedirectStandardOutput = true,
  169. CreateNoWindow = true,
  170. UseShellExecute = false
  171. }
  172. };
  173. process.Start ();
  174. }
  175. else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  176. {
  177. Process.Start ("open", url);
  178. }
  179. }
  180. /// <summary>
  181. /// Shows the UI Catalog selection UI. When the user selects a Scenario to run, the UI Catalog main app UI is
  182. /// killed and the Scenario is run as though it were Application.Top. When the Scenario exits, this function exits.
  183. /// </summary>
  184. /// <returns></returns>
  185. private static Scenario RunUICatalogTopLevel ()
  186. {
  187. // Run UI Catalog UI. When it exits, if _selectedScenario is != null then
  188. // a Scenario was selected. Otherwise, the user wants to quit UI Catalog.
  189. // If the user specified a driver on the command line then use it,
  190. // ignoring Config files.
  191. Application.Init (driverName: _forceDriver);
  192. if (_cachedTheme is null)
  193. {
  194. _cachedTheme = Themes?.Theme;
  195. }
  196. else
  197. {
  198. Themes!.Theme = _cachedTheme;
  199. Apply ();
  200. }
  201. Application.Run<UICatalogTopLevel> ().Dispose ();
  202. Application.Shutdown ();
  203. return _selectedScenario!;
  204. }
  205. private static void StartConfigFileWatcher ()
  206. {
  207. // Setup a file system watcher for `./.tui/`
  208. _currentDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  209. string assemblyLocation = Assembly.GetExecutingAssembly ().Location;
  210. string tuiDir;
  211. if (!string.IsNullOrEmpty (assemblyLocation))
  212. {
  213. var assemblyFile = new FileInfo (assemblyLocation);
  214. tuiDir = Path.Combine (assemblyFile.Directory!.FullName, ".tui");
  215. }
  216. else
  217. {
  218. tuiDir = Path.Combine (AppContext.BaseDirectory, ".tui");
  219. }
  220. if (!Directory.Exists (tuiDir))
  221. {
  222. Directory.CreateDirectory (tuiDir);
  223. }
  224. _currentDirWatcher.Path = tuiDir;
  225. _currentDirWatcher.Filter = "*config.json";
  226. // Setup a file system watcher for `~/.tui/`
  227. _homeDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  228. var f = new FileInfo (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile));
  229. tuiDir = Path.Combine (f.FullName, ".tui");
  230. if (!Directory.Exists (tuiDir))
  231. {
  232. Directory.CreateDirectory (tuiDir);
  233. }
  234. _homeDirWatcher.Path = tuiDir;
  235. _homeDirWatcher.Filter = "*config.json";
  236. _currentDirWatcher.Changed += ConfigFileChanged;
  237. //_currentDirWatcher.Created += ConfigFileChanged;
  238. _currentDirWatcher.EnableRaisingEvents = true;
  239. _homeDirWatcher.Changed += ConfigFileChanged;
  240. //_homeDirWatcher.Created += ConfigFileChanged;
  241. _homeDirWatcher.EnableRaisingEvents = true;
  242. }
  243. private static void StopConfigFileWatcher ()
  244. {
  245. _currentDirWatcher.EnableRaisingEvents = false;
  246. _currentDirWatcher.Changed -= ConfigFileChanged;
  247. _currentDirWatcher.Created -= ConfigFileChanged;
  248. _homeDirWatcher.EnableRaisingEvents = false;
  249. _homeDirWatcher.Changed -= ConfigFileChanged;
  250. _homeDirWatcher.Created -= ConfigFileChanged;
  251. }
  252. private static void UICatalogMain (Options options)
  253. {
  254. StartConfigFileWatcher ();
  255. // By setting _forceDriver we ensure that if the user has specified a driver on the command line, it will be used
  256. // regardless of what's in a config file.
  257. Application.ForceDriver = _forceDriver = options.Driver;
  258. // If a Scenario name has been provided on the commandline
  259. // run it and exit when done.
  260. if (options.Scenario != "none")
  261. {
  262. _topLevelColorScheme = "Base";
  263. int item = _scenarios!.IndexOf (
  264. _scenarios!.FirstOrDefault (
  265. s =>
  266. s.GetName ()
  267. .Equals (options.Scenario, StringComparison.OrdinalIgnoreCase)
  268. )!);
  269. _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios [item].GetType ())!;
  270. Application.Init (driverName: _forceDriver);
  271. _selectedScenario.Theme = _cachedTheme;
  272. _selectedScenario.TopLevelColorScheme = _topLevelColorScheme;
  273. _selectedScenario.Main ();
  274. _selectedScenario.Dispose ();
  275. _selectedScenario = null;
  276. // TODO: Throw if shutdown was not called already
  277. Application.Shutdown ();
  278. VerifyObjectsWereDisposed ();
  279. return;
  280. }
  281. while (RunUICatalogTopLevel () is { } scenario)
  282. {
  283. VerifyObjectsWereDisposed ();
  284. Themes!.Theme = _cachedTheme!;
  285. Apply ();
  286. scenario.Theme = _cachedTheme;
  287. scenario.TopLevelColorScheme = _topLevelColorScheme;
  288. scenario.Main ();
  289. scenario.Dispose ();
  290. // This call to Application.Shutdown brackets the Application.Init call
  291. // made by Scenario.Init() above
  292. // TODO: Throw if shutdown was not called already
  293. Application.Shutdown ();
  294. VerifyObjectsWereDisposed ();
  295. }
  296. StopConfigFileWatcher ();
  297. VerifyObjectsWereDisposed ();
  298. }
  299. private static void VerifyObjectsWereDisposed ()
  300. {
  301. #if DEBUG_IDISPOSABLE
  302. // Validate there are no outstanding Responder-based instances
  303. // after a scenario was selected to run. This proves the main UI Catalog
  304. // 'app' closed cleanly.
  305. foreach (Responder? inst in Responder.Instances)
  306. {
  307. Debug.Assert (inst.WasDisposed);
  308. }
  309. Responder.Instances.Clear ();
  310. // Validate there are no outstanding Application.RunState-based instances
  311. // after a scenario was selected to run. This proves the main UI Catalog
  312. // 'app' closed cleanly.
  313. foreach (RunState? inst in RunState.Instances)
  314. {
  315. Debug.Assert (inst.WasDisposed);
  316. }
  317. RunState.Instances.Clear ();
  318. #endif
  319. }
  320. /// <summary>
  321. /// This is the main UI Catalog app view. It is run fresh when the app loads (if a Scenario has not been passed on
  322. /// the command line) and each time a Scenario ends.
  323. /// </summary>
  324. public class UICatalogTopLevel : Toplevel
  325. {
  326. public ListView CategoryList;
  327. public MenuItem? MiForce16Colors;
  328. public MenuItem? MiIsMenuBorderDisabled;
  329. public MenuItem? MiIsMouseDisabled;
  330. public MenuItem? MiUseSubMenusSingleFrame;
  331. public Shortcut? ShForce16Colors;
  332. //public Shortcut? ShDiagnostics;
  333. public Shortcut? ShVersion;
  334. // UI Catalog uses TableView for the scenario list instead of a ListView to demonstate how
  335. // TableView works. There's no real reason not to use ListView. Because we use TableView, and TableView
  336. // doesn't (currently) have CollectionNavigator support built in, we implement it here, within the app.
  337. public TableView ScenarioList;
  338. private readonly CollectionNavigator _scenarioCollectionNav = new ();
  339. public UICatalogTopLevel ()
  340. {
  341. _diagnosticFlags = Diagnostics;
  342. _themeMenuItems = CreateThemeMenuItems ();
  343. _themeMenuBarItem = new ("_Themes", _themeMenuItems);
  344. MenuBar = new ()
  345. {
  346. Menus =
  347. [
  348. new (
  349. "_File",
  350. new MenuItem []
  351. {
  352. new (
  353. "_Quit",
  354. "Quit UI Catalog",
  355. RequestStop
  356. )
  357. }
  358. ),
  359. _themeMenuBarItem,
  360. new ("Diag_nostics", CreateDiagnosticMenuItems ()),
  361. new (
  362. "_Help",
  363. new MenuItem []
  364. {
  365. new (
  366. "_Documentation",
  367. "",
  368. () => OpenUrl ("https://gui-cs.github.io/Terminal.GuiV2Docs"),
  369. null,
  370. null,
  371. (KeyCode)Key.F1
  372. ),
  373. new (
  374. "_README",
  375. "",
  376. () => OpenUrl ("https://github.com/gui-cs/Terminal.Gui"),
  377. null,
  378. null,
  379. (KeyCode)Key.F2
  380. ),
  381. new (
  382. "_About...",
  383. "About UI Catalog",
  384. () => MessageBox.Query (
  385. title: "",
  386. message: GetAboutBoxMessage (),
  387. wrapMessage: false,
  388. buttons: "_Ok"
  389. ),
  390. null,
  391. null,
  392. (KeyCode)Key.A.WithCtrl
  393. )
  394. }
  395. )
  396. ]
  397. };
  398. StatusBar = new ()
  399. {
  400. Visible = ShowStatusBar,
  401. AlignmentModes = AlignmentModes.IgnoreFirstOrLast
  402. };
  403. if (StatusBar is { })
  404. {
  405. ShVersion = new ()
  406. {
  407. Title = "Version Info",
  408. CanFocus = false
  409. };
  410. var statusBarShortcut = new Shortcut
  411. {
  412. Key = Key.F10,
  413. Title = "Show/Hide Status Bar"
  414. };
  415. statusBarShortcut.Accept += (sender, args) => { StatusBar.Visible = !StatusBar.Visible; };
  416. ShForce16Colors = new ()
  417. {
  418. CommandView = new CheckBox
  419. {
  420. Title = "16 color mode",
  421. State = Application.Force16Colors ? CheckState.Checked : CheckState.UnChecked,
  422. CanFocus = false
  423. },
  424. HelpText = "",
  425. Key = Key.F6
  426. };
  427. ((CheckBox)ShForce16Colors.CommandView).Toggle += (sender, args) =>
  428. {
  429. Application.Force16Colors = args.NewValue == CheckState.Checked;
  430. MiForce16Colors!.Checked = Application.Force16Colors;
  431. Application.Refresh ();
  432. };
  433. //ShDiagnostics = new Shortcut ()
  434. //{
  435. // HelpText = "Diagnostic flags",
  436. // CommandView = new RadioGroup()
  437. // {
  438. // RadioLabels = ["Off", "Ruler", "Padding", "MouseEnter"],
  439. // CanFocus = false,
  440. // Orientation = Orientation.Vertical,
  441. // }
  442. //};
  443. StatusBar.Add (
  444. new Shortcut
  445. {
  446. Title = "Quit",
  447. Key = Application.QuitKey
  448. },
  449. statusBarShortcut,
  450. ShForce16Colors,
  451. //ShDiagnostics,
  452. ShVersion
  453. );
  454. }
  455. // Create the Category list view. This list never changes.
  456. CategoryList = new ()
  457. {
  458. X = 0,
  459. Y = 1,
  460. Width = Dim.Auto (),
  461. Height = Dim.Fill (1),
  462. AllowsMarking = false,
  463. CanFocus = true,
  464. Title = "_Categories",
  465. BorderStyle = LineStyle.Rounded,
  466. SuperViewRendersLineCanvas = true,
  467. Source = new ListWrapper<string> (_categories)
  468. };
  469. CategoryList.OpenSelectedItem += (s, a) => { ScenarioList!.SetFocus (); };
  470. CategoryList.SelectedItemChanged += CategoryView_SelectedChanged;
  471. // Create the scenario list. The contents of the scenario list changes whenever the
  472. // Category list selection changes (to show just the scenarios that belong to the selected
  473. // category).
  474. ScenarioList = new ()
  475. {
  476. X = Pos.Right (CategoryList) - 1,
  477. Y = 1,
  478. Width = Dim.Fill (),
  479. Height = Dim.Height (CategoryList),
  480. //AllowsMarking = false,
  481. CanFocus = true,
  482. Title = "_Scenarios",
  483. BorderStyle = CategoryList.BorderStyle,
  484. SuperViewRendersLineCanvas = true
  485. };
  486. // TableView provides many options for table headers. For simplicity we turn all
  487. // of these off. By enabling FullRowSelect and turning off headers, TableView looks just
  488. // like a ListView
  489. ScenarioList.FullRowSelect = true;
  490. ScenarioList.Style.ShowHeaders = false;
  491. ScenarioList.Style.ShowHorizontalHeaderOverline = false;
  492. ScenarioList.Style.ShowHorizontalHeaderUnderline = false;
  493. ScenarioList.Style.ShowHorizontalBottomline = false;
  494. ScenarioList.Style.ShowVerticalCellLines = false;
  495. ScenarioList.Style.ShowVerticalHeaderLines = false;
  496. /* By default TableView lays out columns at render time and only
  497. * measures y rows of data at a time. Where y is the height of the
  498. * console. This is for the following reasons:
  499. *
  500. * - Performance, when tables have a large amount of data
  501. * - Defensive, prevents a single wide cell value pushing other
  502. * columns off screen (requiring horizontal scrolling
  503. *
  504. * In the case of UICatalog here, such an approach is overkill so
  505. * we just measure all the data ourselves and set the appropriate
  506. * max widths as ColumnStyles
  507. */
  508. int longestName = _scenarios!.Max (s => s.GetName ().Length);
  509. ScenarioList.Style.ColumnStyles.Add (
  510. 0,
  511. new () { MaxWidth = longestName, MinWidth = longestName, MinAcceptableWidth = longestName }
  512. );
  513. ScenarioList.Style.ColumnStyles.Add (1, new () { MaxWidth = 1 });
  514. // Enable user to find & select a scenario by typing text
  515. // TableView does not (currently) have built-in CollectionNavigator support (the ability for the
  516. // user to type and the items that match get selected). We implement it in the app instead.
  517. ScenarioList.KeyDown += (s, a) =>
  518. {
  519. if (CollectionNavigatorBase.IsCompatibleKey (a))
  520. {
  521. int? newItem =
  522. _scenarioCollectionNav?.GetNextMatchingItem (
  523. ScenarioList.SelectedRow,
  524. (char)a
  525. );
  526. if (newItem is int v && newItem != -1)
  527. {
  528. ScenarioList.SelectedRow = v;
  529. ScenarioList.EnsureSelectedCellIsVisible ();
  530. ScenarioList.SetNeedsDisplay ();
  531. a.Handled = true;
  532. }
  533. }
  534. };
  535. ScenarioList.CellActivated += ScenarioView_OpenSelectedItem;
  536. // TableView typically is a grid where nav keys are biased for moving left/right.
  537. ScenarioList.KeyBindings.Add (Key.Home, Command.TopHome);
  538. ScenarioList.KeyBindings.Add (Key.End, Command.BottomEnd);
  539. // Ideally, TableView.MultiSelect = false would turn off any keybindings for
  540. // multi-select options. But it currently does not. UI Catalog uses Ctrl-A for
  541. // a shortcut to About.
  542. ScenarioList.MultiSelect = false;
  543. ScenarioList.KeyBindings.Remove (Key.A.WithCtrl);
  544. Add (CategoryList);
  545. Add (ScenarioList);
  546. Add (MenuBar);
  547. if (StatusBar is { })
  548. {
  549. Add (StatusBar);
  550. }
  551. Loaded += LoadedHandler;
  552. Unloaded += UnloadedHandler;
  553. // Restore previous selections
  554. CategoryList.SelectedItem = _cachedCategoryIndex;
  555. ScenarioList.SelectedRow = _cachedScenarioIndex;
  556. Applied += ConfigAppliedHandler;
  557. }
  558. public void ConfigChanged ()
  559. {
  560. if (_topLevelColorScheme == null || !Colors.ColorSchemes.ContainsKey (_topLevelColorScheme))
  561. {
  562. _topLevelColorScheme = "Base";
  563. }
  564. _cachedTheme = Themes?.Theme;
  565. _themeMenuItems = CreateThemeMenuItems ();
  566. _themeMenuBarItem!.Children = _themeMenuItems;
  567. foreach (MenuItem mi in _themeMenuItems!)
  568. {
  569. if (mi is { Parent: null })
  570. {
  571. mi.Parent = _themeMenuBarItem;
  572. }
  573. }
  574. ColorScheme = Colors.ColorSchemes [_topLevelColorScheme];
  575. MenuBar.Menus [0].Children [0].Shortcut = (KeyCode)Application.QuitKey;
  576. if (StatusBar is { })
  577. {
  578. ((Shortcut)StatusBar.Subviews [0]).Key = Application.QuitKey;
  579. StatusBar.Visible = ShowStatusBar;
  580. }
  581. MiIsMouseDisabled!.Checked = Application.IsMouseDisabled;
  582. Application.Top.SetNeedsDisplay ();
  583. }
  584. public MenuItem []? CreateThemeMenuItems ()
  585. {
  586. List<MenuItem> menuItems = CreateForce16ColorItems ().ToList ();
  587. menuItems.Add (null!);
  588. var schemeCount = 0;
  589. foreach (KeyValuePair<string, ThemeScope> theme in Themes!)
  590. {
  591. var item = new MenuItem
  592. {
  593. Title = $"_{theme.Key}",
  594. Shortcut = (KeyCode)new Key ((KeyCode)((uint)KeyCode.D1 + schemeCount++))
  595. .WithCtrl
  596. };
  597. item.CheckType |= MenuItemCheckStyle.Checked;
  598. item.Checked = theme.Key == _cachedTheme; // CM.Themes.Theme;
  599. item.Action += () =>
  600. {
  601. Themes.Theme = _cachedTheme = theme.Key;
  602. Apply ();
  603. };
  604. menuItems.Add (item);
  605. }
  606. List<MenuItem> schemeMenuItems = new ();
  607. foreach (KeyValuePair<string, ColorScheme?> sc in Colors.ColorSchemes)
  608. {
  609. var item = new MenuItem { Title = $"_{sc.Key}", Data = sc.Key };
  610. item.CheckType |= MenuItemCheckStyle.Radio;
  611. item.Checked = sc.Key == _topLevelColorScheme;
  612. item.Action += () =>
  613. {
  614. _topLevelColorScheme = (string)item.Data;
  615. foreach (MenuItem schemeMenuItem in schemeMenuItems)
  616. {
  617. schemeMenuItem.Checked = (string)schemeMenuItem.Data == _topLevelColorScheme;
  618. }
  619. ColorScheme = Colors.ColorSchemes [_topLevelColorScheme];
  620. Application.Top.SetNeedsDisplay ();
  621. };
  622. schemeMenuItems.Add (item);
  623. }
  624. menuItems.Add (null!);
  625. var mbi = new MenuBarItem ("_Color Scheme for Application.Top", schemeMenuItems.ToArray ());
  626. menuItems.Add (mbi);
  627. return menuItems.ToArray ();
  628. }
  629. private void CategoryView_SelectedChanged (object? sender, ListViewItemEventArgs? e)
  630. {
  631. string item = _categories! [e!.Item];
  632. ObservableCollection<Scenario> newlist;
  633. if (e.Item == 0)
  634. {
  635. // First category is "All"
  636. newlist = _scenarios!;
  637. }
  638. else
  639. {
  640. newlist = new (_scenarios!.Where (s => s.GetCategories ().Contains (item)).ToList ());
  641. }
  642. ScenarioList.Table = new EnumerableTableSource<Scenario> (
  643. newlist,
  644. new ()
  645. {
  646. { "Name", s => s.GetName () }, { "Description", s => s.GetDescription () }
  647. }
  648. );
  649. // Create a collection of just the scenario names (the 1st column in our TableView)
  650. // for CollectionNavigator.
  651. List<object> firstColumnList = new ();
  652. for (var i = 0; i < ScenarioList.Table.Rows; i++)
  653. {
  654. firstColumnList.Add (ScenarioList.Table [i, 0]);
  655. }
  656. _scenarioCollectionNav.Collection = firstColumnList;
  657. }
  658. private void ConfigAppliedHandler (object? sender, ConfigurationManagerEventArgs? a) { ConfigChanged (); }
  659. private MenuItem [] CreateDiagnosticFlagsMenuItems ()
  660. {
  661. const string OFF = "View Diagnostics: _Off";
  662. const string RULER = "View Diagnostics: _Ruler";
  663. const string PADDING = "View Diagnostics: _Padding";
  664. const string MOUSEENTER = "View Diagnostics: _MouseEnter";
  665. var index = 0;
  666. List<MenuItem> menuItems = new ();
  667. foreach (Enum diag in Enum.GetValues (_diagnosticFlags.GetType ()))
  668. {
  669. var item = new MenuItem
  670. {
  671. Title = GetDiagnosticsTitle (diag), Shortcut = (KeyCode)new Key (index.ToString () [0]).WithAlt
  672. };
  673. index++;
  674. item.CheckType |= MenuItemCheckStyle.Checked;
  675. if (GetDiagnosticsTitle (ViewDiagnosticFlags.Off) == item.Title)
  676. {
  677. item.Checked = !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Padding)
  678. && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Ruler)
  679. && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.MouseEnter);
  680. }
  681. else
  682. {
  683. item.Checked = _diagnosticFlags.HasFlag (diag);
  684. }
  685. item.Action += () =>
  686. {
  687. string t = GetDiagnosticsTitle (ViewDiagnosticFlags.Off);
  688. if (item.Title == t && item.Checked == false)
  689. {
  690. _diagnosticFlags &= ~(ViewDiagnosticFlags.Padding | ViewDiagnosticFlags.Ruler | ViewDiagnosticFlags.MouseEnter);
  691. item.Checked = true;
  692. }
  693. else if (item.Title == t && item.Checked == true)
  694. {
  695. _diagnosticFlags |= ViewDiagnosticFlags.Padding | ViewDiagnosticFlags.Ruler | ViewDiagnosticFlags.MouseEnter;
  696. item.Checked = false;
  697. }
  698. else
  699. {
  700. Enum f = GetDiagnosticsEnumValue (item.Title);
  701. if (_diagnosticFlags.HasFlag (f))
  702. {
  703. SetDiagnosticsFlag (f, false);
  704. }
  705. else
  706. {
  707. SetDiagnosticsFlag (f, true);
  708. }
  709. }
  710. foreach (MenuItem menuItem in menuItems)
  711. {
  712. if (menuItem.Title == t)
  713. {
  714. menuItem.Checked = !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Ruler)
  715. && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.Padding)
  716. && !_diagnosticFlags.HasFlag (ViewDiagnosticFlags.MouseEnter);
  717. }
  718. else if (menuItem.Title != t)
  719. {
  720. menuItem.Checked = _diagnosticFlags.HasFlag (GetDiagnosticsEnumValue (menuItem.Title));
  721. }
  722. }
  723. Diagnostics = _diagnosticFlags;
  724. Application.Top.SetNeedsDisplay ();
  725. };
  726. menuItems.Add (item);
  727. }
  728. return menuItems.ToArray ();
  729. string GetDiagnosticsTitle (Enum diag)
  730. {
  731. return Enum.GetName (_diagnosticFlags.GetType (), diag) switch
  732. {
  733. "Off" => OFF,
  734. "Ruler" => RULER,
  735. "Padding" => PADDING,
  736. "MouseEnter" => MOUSEENTER,
  737. _ => ""
  738. };
  739. }
  740. Enum GetDiagnosticsEnumValue (string title)
  741. {
  742. return title switch
  743. {
  744. RULER => ViewDiagnosticFlags.Ruler,
  745. PADDING => ViewDiagnosticFlags.Padding,
  746. MOUSEENTER => ViewDiagnosticFlags.MouseEnter,
  747. _ => null!
  748. };
  749. }
  750. void SetDiagnosticsFlag (Enum diag, bool add)
  751. {
  752. switch (diag)
  753. {
  754. case ViewDiagnosticFlags.Ruler:
  755. if (add)
  756. {
  757. _diagnosticFlags |= ViewDiagnosticFlags.Ruler;
  758. }
  759. else
  760. {
  761. _diagnosticFlags &= ~ViewDiagnosticFlags.Ruler;
  762. }
  763. break;
  764. case ViewDiagnosticFlags.Padding:
  765. if (add)
  766. {
  767. _diagnosticFlags |= ViewDiagnosticFlags.Padding;
  768. }
  769. else
  770. {
  771. _diagnosticFlags &= ~ViewDiagnosticFlags.Padding;
  772. }
  773. break;
  774. case ViewDiagnosticFlags.MouseEnter:
  775. if (add)
  776. {
  777. _diagnosticFlags |= ViewDiagnosticFlags.MouseEnter;
  778. }
  779. else
  780. {
  781. _diagnosticFlags &= ~ViewDiagnosticFlags.MouseEnter;
  782. }
  783. break;
  784. default:
  785. _diagnosticFlags = default (ViewDiagnosticFlags);
  786. break;
  787. }
  788. }
  789. }
  790. private List<MenuItem []> CreateDiagnosticMenuItems ()
  791. {
  792. List<MenuItem []> menuItems = new ()
  793. {
  794. CreateDiagnosticFlagsMenuItems (),
  795. new MenuItem [] { null! },
  796. CreateDisabledEnabledMouseItems (),
  797. CreateDisabledEnabledMenuBorder (),
  798. CreateDisabledEnableUseSubMenusSingleFrame (),
  799. CreateKeyBindingsMenuItems ()
  800. };
  801. return menuItems;
  802. }
  803. // TODO: This should be an ConfigurationManager setting
  804. private MenuItem [] CreateDisabledEnabledMenuBorder ()
  805. {
  806. List<MenuItem> menuItems = new ();
  807. MiIsMenuBorderDisabled = new () { Title = "Disable Menu _Border" };
  808. MiIsMenuBorderDisabled.Shortcut =
  809. (KeyCode)new Key (MiIsMenuBorderDisabled!.Title!.Substring (14, 1) [0]).WithAlt
  810. .WithCtrl.NoShift;
  811. MiIsMenuBorderDisabled.CheckType |= MenuItemCheckStyle.Checked;
  812. MiIsMenuBorderDisabled.Action += () =>
  813. {
  814. MiIsMenuBorderDisabled.Checked = (bool)!MiIsMenuBorderDisabled.Checked!;
  815. MenuBar.MenusBorderStyle = !(bool)MiIsMenuBorderDisabled.Checked
  816. ? LineStyle.Single
  817. : LineStyle.None;
  818. };
  819. menuItems.Add (MiIsMenuBorderDisabled);
  820. return menuItems.ToArray ();
  821. }
  822. private MenuItem [] CreateDisabledEnabledMouseItems ()
  823. {
  824. List<MenuItem> menuItems = new ();
  825. MiIsMouseDisabled = new () { Title = "_Disable Mouse" };
  826. MiIsMouseDisabled.Shortcut =
  827. (KeyCode)new Key (MiIsMouseDisabled!.Title!.Substring (1, 1) [0]).WithAlt.WithCtrl.NoShift;
  828. MiIsMouseDisabled.CheckType |= MenuItemCheckStyle.Checked;
  829. MiIsMouseDisabled.Action += () =>
  830. {
  831. MiIsMouseDisabled.Checked =
  832. Application.IsMouseDisabled = (bool)!MiIsMouseDisabled.Checked!;
  833. };
  834. menuItems.Add (MiIsMouseDisabled);
  835. return menuItems.ToArray ();
  836. }
  837. // TODO: This should be an ConfigurationManager setting
  838. private MenuItem [] CreateDisabledEnableUseSubMenusSingleFrame ()
  839. {
  840. List<MenuItem> menuItems = new ();
  841. MiUseSubMenusSingleFrame = new () { Title = "Enable _Sub-Menus Single Frame" };
  842. MiUseSubMenusSingleFrame.Shortcut = KeyCode.CtrlMask
  843. | KeyCode.AltMask
  844. | (KeyCode)MiUseSubMenusSingleFrame!.Title!.Substring (8, 1) [
  845. 0];
  846. MiUseSubMenusSingleFrame.CheckType |= MenuItemCheckStyle.Checked;
  847. MiUseSubMenusSingleFrame.Action += () =>
  848. {
  849. MiUseSubMenusSingleFrame.Checked = (bool)!MiUseSubMenusSingleFrame.Checked!;
  850. MenuBar.UseSubMenusSingleFrame = (bool)MiUseSubMenusSingleFrame.Checked;
  851. };
  852. menuItems.Add (MiUseSubMenusSingleFrame);
  853. return menuItems.ToArray ();
  854. }
  855. private MenuItem [] CreateForce16ColorItems ()
  856. {
  857. List<MenuItem> menuItems = new ();
  858. MiForce16Colors = new ()
  859. {
  860. Title = "Force _16 Colors",
  861. Shortcut = (KeyCode)Key.F6,
  862. Checked = Application.Force16Colors,
  863. CanExecute = () => Application.Driver.SupportsTrueColor
  864. };
  865. MiForce16Colors.CheckType |= MenuItemCheckStyle.Checked;
  866. MiForce16Colors.Action += () =>
  867. {
  868. MiForce16Colors.Checked = Application.Force16Colors = (bool)!MiForce16Colors.Checked!;
  869. ((CheckBox)ShForce16Colors!.CommandView!).State =
  870. Application.Force16Colors ? CheckState.Checked : CheckState.UnChecked;
  871. Application.Refresh ();
  872. };
  873. menuItems.Add (MiForce16Colors);
  874. return menuItems.ToArray ();
  875. }
  876. private MenuItem [] CreateKeyBindingsMenuItems ()
  877. {
  878. List<MenuItem> menuItems = new ();
  879. var item = new MenuItem { Title = "_Key Bindings", Help = "Change which keys do what" };
  880. item.Action += () =>
  881. {
  882. var dlg = new KeyBindingsDialog ();
  883. Application.Run (dlg);
  884. dlg.Dispose ();
  885. };
  886. menuItems.Add (null!);
  887. menuItems.Add (item);
  888. return menuItems.ToArray ();
  889. }
  890. private void LoadedHandler (object? sender, EventArgs? args)
  891. {
  892. ConfigChanged ();
  893. MiIsMouseDisabled!.Checked = Application.IsMouseDisabled;
  894. if (ShVersion is { })
  895. {
  896. ShVersion.Title = $"{RuntimeEnvironment.OperatingSystem} {RuntimeEnvironment.OperatingSystemVersion}, {Driver.GetVersionInfo ()}";
  897. }
  898. if (_selectedScenario != null)
  899. {
  900. _selectedScenario = null;
  901. _isFirstRunning = false;
  902. }
  903. if (!_isFirstRunning)
  904. {
  905. ScenarioList.SetFocus ();
  906. }
  907. if (StatusBar is { })
  908. {
  909. StatusBar.VisibleChanged += (s, e) =>
  910. {
  911. ShowStatusBar = StatusBar.Visible;
  912. int height = StatusBar.Visible ? 1 : 0;
  913. CategoryList.Height = Dim.Fill (height);
  914. ScenarioList.Height = Dim.Fill (height);
  915. // ContentPane.Height = Dim.Fill (height);
  916. LayoutSubviews ();
  917. SetSubViewNeedsDisplay ();
  918. };
  919. }
  920. Loaded -= LoadedHandler;
  921. CategoryList.EnsureSelectedItemVisible ();
  922. ScenarioList.EnsureSelectedCellIsVisible ();
  923. }
  924. /// <summary>Launches the selected scenario, setting the global _selectedScenario</summary>
  925. /// <param name="e"></param>
  926. private void ScenarioView_OpenSelectedItem (object? sender, EventArgs? e)
  927. {
  928. if (_selectedScenario is null)
  929. {
  930. // Save selected item state
  931. _cachedCategoryIndex = CategoryList.SelectedItem;
  932. _cachedScenarioIndex = ScenarioList.SelectedRow;
  933. // Create new instance of scenario (even though Scenarios contains instances)
  934. var selectedScenarioName = (string)ScenarioList.Table [ScenarioList.SelectedRow, 0];
  935. _selectedScenario = (Scenario)Activator.CreateInstance (
  936. _scenarios!.FirstOrDefault (
  937. s => s.GetName ()
  938. == selectedScenarioName
  939. )!
  940. .GetType ()
  941. )!;
  942. // Tell the main app to stop
  943. Application.RequestStop ();
  944. }
  945. }
  946. private void UnloadedHandler (object? sender, EventArgs? args)
  947. {
  948. Applied -= ConfigAppliedHandler;
  949. Unloaded -= UnloadedHandler;
  950. Dispose ();
  951. }
  952. }
  953. private struct Options
  954. {
  955. public string Driver;
  956. public string Scenario;
  957. /* etc. */
  958. }
  959. }