UICatalog.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. global using CM = Terminal.Gui.ConfigurationManager;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using Terminal.Gui;
  10. using System.IO;
  11. using System.Reflection;
  12. using System.Threading;
  13. using static Terminal.Gui.ConfigurationManager;
  14. using System.Text.Json.Serialization;
  15. using static Terminal.Gui.TableView;
  16. #nullable enable
  17. namespace UICatalog {
  18. /// <summary>
  19. /// UI Catalog is a comprehensive sample library for Terminal.Gui. It provides a simple UI for adding to the catalog of scenarios.
  20. /// </summary>
  21. /// <remarks>
  22. /// <para>
  23. /// UI Catalog attempts to satisfy the following goals:
  24. /// </para>
  25. /// <para>
  26. /// <list type="number">
  27. /// <item>
  28. /// <description>
  29. /// Be an easy to use showcase for Terminal.Gui concepts and features.
  30. /// </description>
  31. /// </item>
  32. /// <item>
  33. /// <description>
  34. /// Provide sample code that illustrates how to properly implement said concepts & features.
  35. /// </description>
  36. /// </item>
  37. /// <item>
  38. /// <description>
  39. /// Make it easy for contributors to add additional samples in a structured way.
  40. /// </description>
  41. /// </item>
  42. /// </list>
  43. /// </para>
  44. /// <para>
  45. /// See the project README for more details (https://github.com/gui-cs/Terminal.Gui/tree/master/UICatalog/README.md).
  46. /// </para>
  47. /// </remarks>
  48. class UICatalogApp {
  49. //[SerializableConfigurationProperty (Scope = typeof (AppScope), OmitClassName = true), JsonPropertyName ("UICatalog.StatusBar")]
  50. //public static bool ShowStatusBar { get; set; } = true;
  51. [SerializableConfigurationProperty (Scope = typeof (AppScope), OmitClassName = true), JsonPropertyName ("UICatalog.StatusBar")]
  52. public static bool ShowStatusBar { get; set; } = true;
  53. static readonly FileSystemWatcher _currentDirWatcher = new FileSystemWatcher ();
  54. static readonly FileSystemWatcher _homeDirWatcher = new FileSystemWatcher ();
  55. static void Main (string [] args)
  56. {
  57. Console.OutputEncoding = Encoding.Default;
  58. if (Debugger.IsAttached) {
  59. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  60. }
  61. _scenarios = Scenario.GetScenarios ();
  62. _categories = Scenario.GetAllCategories ();
  63. if (args.Length > 0 && args.Contains ("-usc")) {
  64. _useSystemConsole = true;
  65. args = args.Where (val => val != "-usc").ToArray ();
  66. }
  67. StartConfigFileWatcher ();
  68. // If a Scenario name has been provided on the commandline
  69. // run it and exit when done.
  70. if (args.Length > 0) {
  71. _topLevelColorScheme = "Base";
  72. var item = _scenarios.FindIndex (s => s.GetName ().Equals (args [0], StringComparison.OrdinalIgnoreCase));
  73. _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios [item].GetType ())!;
  74. Application.UseSystemConsole = _useSystemConsole;
  75. Application.Init ();
  76. _selectedScenario.Theme = _cachedTheme;
  77. _selectedScenario.TopLevelColorScheme = _topLevelColorScheme;
  78. _selectedScenario.Init ();
  79. _selectedScenario.Setup ();
  80. _selectedScenario.Run ();
  81. _selectedScenario.Dispose ();
  82. _selectedScenario = null;
  83. Application.Shutdown ();
  84. VerifyObjectsWereDisposed ();
  85. return;
  86. }
  87. _aboutMessage = new StringBuilder ();
  88. _aboutMessage.AppendLine (@"A comprehensive sample library for");
  89. _aboutMessage.AppendLine (@"");
  90. _aboutMessage.AppendLine (@" _______ _ _ _____ _ ");
  91. _aboutMessage.AppendLine (@" |__ __| (_) | | / ____| (_) ");
  92. _aboutMessage.AppendLine (@" | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ");
  93. _aboutMessage.AppendLine (@" | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | | ");
  94. _aboutMessage.AppendLine (@" | | __/ | | | | | | | | | | | (_| | || |__| | |_| | | ");
  95. _aboutMessage.AppendLine (@" |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_| ");
  96. _aboutMessage.AppendLine (@"");
  97. _aboutMessage.AppendLine (@"v2 - Work in Progress");
  98. _aboutMessage.AppendLine (@"");
  99. _aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui");
  100. while (RunUICatalogTopLevel () is { } scenario) {
  101. VerifyObjectsWereDisposed ();
  102. CM.Themes!.Theme = _cachedTheme!;
  103. CM.Apply ();
  104. scenario.Theme = _cachedTheme;
  105. scenario.TopLevelColorScheme = _topLevelColorScheme;
  106. scenario.Init ();
  107. scenario.Setup ();
  108. scenario.Run ();
  109. scenario.Dispose ();
  110. // This call to Application.Shutdown brackets the Application.Init call
  111. // made by Scenario.Init() above
  112. Application.Shutdown ();
  113. VerifyObjectsWereDisposed ();
  114. }
  115. StopConfigFileWatcher ();
  116. VerifyObjectsWereDisposed ();
  117. }
  118. private static void StopConfigFileWatcher ()
  119. {
  120. _currentDirWatcher.EnableRaisingEvents = false;
  121. _currentDirWatcher.Changed -= ConfigFileChanged;
  122. _currentDirWatcher.Created -= ConfigFileChanged;
  123. _homeDirWatcher.EnableRaisingEvents = false;
  124. _homeDirWatcher.Changed -= ConfigFileChanged;
  125. _homeDirWatcher.Created -= ConfigFileChanged;
  126. }
  127. private static void StartConfigFileWatcher ()
  128. {
  129. // Setup a file system watcher for `./.tui/`
  130. _currentDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  131. var f = new FileInfo (Assembly.GetExecutingAssembly ().Location);
  132. var tuiDir = Path.Combine (f.Directory!.FullName, ".tui");
  133. if (!Directory.Exists (tuiDir)) {
  134. Directory.CreateDirectory (tuiDir);
  135. }
  136. _currentDirWatcher.Path = tuiDir;
  137. _currentDirWatcher.Filter = "*config.json";
  138. // Setup a file system watcher for `~/.tui/`
  139. _homeDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  140. f = new FileInfo (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile));
  141. tuiDir = Path.Combine (f.FullName, ".tui");
  142. if (!Directory.Exists (tuiDir)) {
  143. Directory.CreateDirectory (tuiDir);
  144. }
  145. _homeDirWatcher.Path = tuiDir;
  146. _homeDirWatcher.Filter = "*config.json";
  147. _currentDirWatcher.Changed += ConfigFileChanged;
  148. //_currentDirWatcher.Created += ConfigFileChanged;
  149. _currentDirWatcher.EnableRaisingEvents = true;
  150. _homeDirWatcher.Changed += ConfigFileChanged;
  151. //_homeDirWatcher.Created += ConfigFileChanged;
  152. _homeDirWatcher.EnableRaisingEvents = true;
  153. }
  154. private static void ConfigFileChanged (object sender, FileSystemEventArgs e)
  155. {
  156. if (Application.Top == null) {
  157. return;
  158. }
  159. // TODO: This is a hack. Figure out how to ensure that the file is fully written before reading it.
  160. Thread.Sleep (500);
  161. CM.Load ();
  162. CM.Apply ();
  163. }
  164. /// <summary>
  165. /// Shows the UI Catalog selection UI. When the user selects a Scenario to run, the
  166. /// UI Catalog main app UI is killed and the Scenario is run as though it were Application.Top.
  167. /// When the Scenario exits, this function exits.
  168. /// </summary>
  169. /// <returns></returns>
  170. static Scenario RunUICatalogTopLevel ()
  171. {
  172. Application.UseSystemConsole = _useSystemConsole;
  173. // Run UI Catalog UI. When it exits, if _selectedScenario is != null then
  174. // a Scenario was selected. Otherwise, the user wants to quit UI Catalog.
  175. Application.Init ();
  176. if (_cachedTheme is null) {
  177. _cachedTheme = CM.Themes?.Theme;
  178. } else {
  179. CM.Themes!.Theme = _cachedTheme;
  180. CM.Apply ();
  181. }
  182. Application.Run<UICatalogTopLevel> ();
  183. Application.Shutdown ();
  184. return _selectedScenario!;
  185. }
  186. static List<Scenario>? _scenarios;
  187. static List<string>? _categories;
  188. // When a scenario is run, the main app is killed. These items
  189. // are therefore cached so that when the scenario exits the
  190. // main app UI can be restored to previous state
  191. static int _cachedScenarioIndex = 0;
  192. static int _cachedCategoryIndex = 0;
  193. static string? _cachedTheme = string.Empty;
  194. static StringBuilder? _aboutMessage = null;
  195. // If set, holds the scenario the user selected
  196. static Scenario? _selectedScenario = null;
  197. static bool _useSystemConsole = false;
  198. static ConsoleDriver.DiagnosticFlags _diagnosticFlags;
  199. static bool _isFirstRunning = true;
  200. static string _topLevelColorScheme = string.Empty;
  201. static MenuItem []? _themeMenuItems;
  202. static MenuBarItem? _themeMenuBarItem;
  203. /// <summary>
  204. /// This is the main UI Catalog app view. It is run fresh when the app loads (if a Scenario has not been passed on
  205. /// the command line) and each time a Scenario ends.
  206. /// </summary>
  207. public class UICatalogTopLevel : Toplevel {
  208. public MenuItem? miUseSubMenusSingleFrame;
  209. public MenuItem? miIsMenuBorderDisabled;
  210. public MenuItem? miForce16Colors;
  211. public MenuItem? miIsMouseDisabled;
  212. public ListView CategoryList;
  213. // UI Catalog uses TableView for the scenario list instead of a ListView to demonstate how
  214. // TableView works. There's no real reason not to use ListView. Because we use TableView, and TableView
  215. // doesn't (currently) have CollectionNavigator support built in, we implement it here, within the app.
  216. public TableView ScenarioList;
  217. private CollectionNavigator _scenarioCollectionNav = new CollectionNavigator ();
  218. public StatusItem Capslock;
  219. public StatusItem Numlock;
  220. public StatusItem Scrolllock;
  221. public StatusItem DriverName;
  222. public StatusItem OS;
  223. public UICatalogTopLevel ()
  224. {
  225. _themeMenuItems = CreateThemeMenuItems ();
  226. _themeMenuBarItem = new MenuBarItem ("_Themes", _themeMenuItems);
  227. MenuBar = new MenuBar (new MenuBarItem [] {
  228. new MenuBarItem ("_File", new MenuItem [] {
  229. new MenuItem ("_Quit", "Quit UI Catalog", RequestStop, null, null)
  230. }),
  231. _themeMenuBarItem,
  232. new MenuBarItem ("Diag_nostics", CreateDiagnosticMenuItems()),
  233. new MenuBarItem ("_Help", new MenuItem [] {
  234. new MenuItem ("_gui.cs API Overview", "", () => OpenUrl ("https://gui-cs.github.io/Terminal.Gui/articles/overview.html"), null, null, Key.F1),
  235. new MenuItem ("gui.cs _README", "", () => OpenUrl ("https://github.com/gui-cs/Terminal.Gui"), null, null, Key.F2),
  236. new MenuItem ("_About...",
  237. "About UI Catalog", () => MessageBox.Query ("About UI Catalog", _aboutMessage!.ToString(), 0, false, "_Ok"), null, null, Key.CtrlMask | Key.A),
  238. }),
  239. });
  240. Capslock = new StatusItem (Key.CharMask, "Caps", null);
  241. Numlock = new StatusItem (Key.CharMask, "Num", null);
  242. Scrolllock = new StatusItem (Key.CharMask, "Scroll", null);
  243. DriverName = new StatusItem (Key.CharMask, "Driver:", null);
  244. OS = new StatusItem (Key.CharMask, "OS:", null);
  245. StatusBar = new StatusBar () {
  246. Visible = UICatalogApp.ShowStatusBar
  247. };
  248. StatusBar.Items = new StatusItem [] {
  249. new StatusItem(Application.QuitKey, $"~{Application.QuitKey} to quit", () => {
  250. if (_selectedScenario is null){
  251. // This causes GetScenarioToRun to return null
  252. _selectedScenario = null;
  253. RequestStop();
  254. } else {
  255. _selectedScenario.RequestStop();
  256. }
  257. }),
  258. new StatusItem(Key.F10, "~F10~ Status Bar", () => {
  259. StatusBar.Visible = !StatusBar.Visible;
  260. //ContentPane!.Height = Dim.Fill(StatusBar.Visible ? 1 : 0);
  261. LayoutSubviews();
  262. SetSubViewNeedsDisplay();
  263. }),
  264. DriverName,
  265. OS
  266. };
  267. // Create the Category list view. This list never changes.
  268. CategoryList = new ListView (_categories) {
  269. X = 0,
  270. Y = 1,
  271. Width = Dim.Percent (30),
  272. Height = Dim.Fill (1),
  273. AllowsMarking = false,
  274. CanFocus = true,
  275. Title = "Categories",
  276. BorderStyle = LineStyle.Single,
  277. SuperViewRendersLineCanvas = true
  278. };
  279. CategoryList.OpenSelectedItem += (s, a) => {
  280. ScenarioList!.SetFocus ();
  281. };
  282. CategoryList.SelectedItemChanged += CategoryView_SelectedChanged;
  283. // Create the scenario list. The contents of the scenario list changes whenever the
  284. // Category list selection changes (to show just the scenarios that belong to the selected
  285. // category).
  286. ScenarioList = new TableView () {
  287. X = Pos.Right (CategoryList) - 1,
  288. Y = 1,
  289. Width = Dim.Fill (0),
  290. Height = Dim.Fill (1),
  291. //AllowsMarking = false,
  292. CanFocus = true,
  293. Title = "Scenarios",
  294. BorderStyle = LineStyle.Single,
  295. SuperViewRendersLineCanvas = true
  296. };
  297. // TableView provides many options for table headers. For simplicity we turn all
  298. // of these off. By enabling FullRowSelect and turning off headers, TableView looks just
  299. // like a ListView
  300. ScenarioList.FullRowSelect = true;
  301. ScenarioList.Style.ShowHeaders = false;
  302. ScenarioList.Style.ShowHorizontalHeaderOverline = false;
  303. ScenarioList.Style.ShowHorizontalHeaderUnderline = false;
  304. ScenarioList.Style.ShowHorizontalBottomline = false;
  305. ScenarioList.Style.ShowVerticalCellLines = false;
  306. ScenarioList.Style.ShowVerticalHeaderLines = false;
  307. /* By default TableView lays out columns at render time and only
  308. * measures y rows of data at a time. Where y is the height of the
  309. * console. This is for the following reasons:
  310. *
  311. * - Performance, when tables have a large amount of data
  312. * - Defensive, prevents a single wide cell value pushing other
  313. * columns off screen (requiring horizontal scrolling
  314. *
  315. * In the case of UICatalog here, such an approach is overkill so
  316. * we just measure all the data ourselves and set the appropriate
  317. * max widths as ColumnStyles
  318. */
  319. var longestName = _scenarios!.Max (s => s.GetName ().Length);
  320. ScenarioList.Style.ColumnStyles.Add (0, new ColumnStyle () { MaxWidth = longestName, MinWidth = longestName, MinAcceptableWidth = longestName });
  321. ScenarioList.Style.ColumnStyles.Add (1, new ColumnStyle () { MaxWidth = 1 });
  322. // Enable user to find & select a scenario by typing text
  323. // TableView does not (currently) have built-in CollectionNavigator support (the ability for the
  324. // user to type and the items that match get selected). We implement it in the app instead.
  325. ScenarioList.KeyDown += (s, a) => {
  326. if (CollectionNavigator.IsCompatibleKey (a.KeyEvent)) {
  327. var newItem = _scenarioCollectionNav?.GetNextMatchingItem (ScenarioList.SelectedRow, (char)a.KeyEvent.KeyValue);
  328. if (newItem is int v && newItem != -1) {
  329. ScenarioList.SelectedRow = v;
  330. ScenarioList.EnsureSelectedCellIsVisible ();
  331. ScenarioList.SetNeedsDisplay ();
  332. a.Handled = true;
  333. }
  334. }
  335. };
  336. ScenarioList.CellActivated += ScenarioView_OpenSelectedItem;
  337. // TableView typically is a grid where nav keys are biased for moving left/right.
  338. ScenarioList.AddKeyBinding (Key.Home, Command.TopHome);
  339. ScenarioList.AddKeyBinding (Key.End, Command.BottomEnd);
  340. // Ideally, TableView.MultiSelect = false would turn off any keybindings for
  341. // multi-select options. But it currently does not. UI Catalog uses Ctrl-A for
  342. // a shortcut to About.
  343. ScenarioList.MultiSelect = false;
  344. ScenarioList.ClearKeyBinding (Key.CtrlMask | Key.A);
  345. KeyDown += KeyDownHandler;
  346. Add (CategoryList);
  347. Add (ScenarioList);
  348. Add (MenuBar);
  349. Add (StatusBar);
  350. Loaded += LoadedHandler;
  351. Unloaded += UnloadedHandler;
  352. // Restore previous selections
  353. CategoryList.SelectedItem = _cachedCategoryIndex;
  354. ScenarioList.SelectedRow = _cachedScenarioIndex;
  355. CM.Applied += ConfigAppliedHandler;
  356. }
  357. void LoadedHandler (object? sender, EventArgs? args)
  358. {
  359. ConfigChanged ();
  360. miIsMouseDisabled!.Checked = Application.IsMouseDisabled;
  361. DriverName.Title = $"Driver: {Driver.GetVersionInfo ()}";
  362. OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem} {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion}";
  363. if (_selectedScenario != null) {
  364. _selectedScenario = null;
  365. _isFirstRunning = false;
  366. }
  367. if (!_isFirstRunning) {
  368. ScenarioList.SetFocus ();
  369. }
  370. StatusBar.VisibleChanged += (s, e) => {
  371. UICatalogApp.ShowStatusBar = StatusBar.Visible;
  372. var height = (StatusBar.Visible ? 1 : 0);
  373. CategoryList.Height = Dim.Fill (height);
  374. ScenarioList.Height = Dim.Fill (height);
  375. // ContentPane.Height = Dim.Fill (height);
  376. LayoutSubviews ();
  377. SetSubViewNeedsDisplay ();
  378. };
  379. Loaded -= LoadedHandler;
  380. CategoryList.EnsureSelectedItemVisible ();
  381. ScenarioList.EnsureSelectedCellIsVisible ();
  382. }
  383. private void UnloadedHandler (object? sender, EventArgs? args)
  384. {
  385. CM.Applied -= ConfigAppliedHandler;
  386. Unloaded -= UnloadedHandler;
  387. }
  388. void ConfigAppliedHandler (object? sender, ConfigurationManagerEventArgs? a)
  389. {
  390. ConfigChanged ();
  391. }
  392. /// <summary>
  393. /// Launches the selected scenario, setting the global _selectedScenario
  394. /// </summary>
  395. /// <param name="e"></param>
  396. void ScenarioView_OpenSelectedItem (object? sender, EventArgs? e)
  397. {
  398. if (_selectedScenario is null) {
  399. // Save selected item state
  400. _cachedCategoryIndex = CategoryList.SelectedItem;
  401. _cachedScenarioIndex = ScenarioList.SelectedRow;
  402. // Create new instance of scenario (even though Scenarios contains instances)
  403. string selectedScenarioName = (string)ScenarioList.Table [ScenarioList.SelectedRow, 0];
  404. _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios!.FirstOrDefault (s => s.GetName () == selectedScenarioName)!.GetType ())!;
  405. // Tell the main app to stop
  406. Application.RequestStop ();
  407. }
  408. }
  409. List<MenuItem []> CreateDiagnosticMenuItems ()
  410. {
  411. List<MenuItem []> menuItems = new List<MenuItem []> {
  412. CreateDiagnosticFlagsMenuItems (),
  413. new MenuItem [] { null! },
  414. CreateForce16ColorItems (),
  415. new MenuItem [] { null! },
  416. CreateDisabledEnabledMouseItems (),
  417. CreateDisabledEnabledMenuBorder (),
  418. CreateDisabledEnableUseSubMenusSingleFrame (),
  419. CreateKeyBindingsMenuItems ()
  420. };
  421. return menuItems;
  422. }
  423. // TODO: This should be an ConfigurationManager setting
  424. MenuItem [] CreateDisabledEnableUseSubMenusSingleFrame ()
  425. {
  426. List<MenuItem> menuItems = new List<MenuItem> ();
  427. miUseSubMenusSingleFrame = new MenuItem {
  428. Title = "Enable _Sub-Menus Single Frame"
  429. };
  430. miUseSubMenusSingleFrame.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miUseSubMenusSingleFrame!.Title!.Substring (8, 1) [0];
  431. miUseSubMenusSingleFrame.CheckType |= MenuItemCheckStyle.Checked;
  432. miUseSubMenusSingleFrame.Action += () => {
  433. miUseSubMenusSingleFrame.Checked = (bool)!miUseSubMenusSingleFrame.Checked!;
  434. MenuBar.UseSubMenusSingleFrame = (bool)miUseSubMenusSingleFrame.Checked;
  435. };
  436. menuItems.Add (miUseSubMenusSingleFrame);
  437. return menuItems.ToArray ();
  438. }
  439. // TODO: This should be an ConfigurationManager setting
  440. MenuItem [] CreateDisabledEnabledMenuBorder ()
  441. {
  442. List<MenuItem> menuItems = new List<MenuItem> ();
  443. miIsMenuBorderDisabled = new MenuItem {
  444. Title = "Disable Menu _Border"
  445. };
  446. miIsMenuBorderDisabled.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miIsMenuBorderDisabled!.Title!.Substring (14, 1) [0];
  447. miIsMenuBorderDisabled.CheckType |= MenuItemCheckStyle.Checked;
  448. miIsMenuBorderDisabled.Action += () => {
  449. miIsMenuBorderDisabled.Checked = (bool)!miIsMenuBorderDisabled.Checked!;
  450. MenuBar.MenusBorderStyle = !(bool)miIsMenuBorderDisabled.Checked ? LineStyle.Single : LineStyle.None;
  451. };
  452. menuItems.Add (miIsMenuBorderDisabled);
  453. return menuItems.ToArray ();
  454. }
  455. MenuItem [] CreateForce16ColorItems ()
  456. {
  457. List<MenuItem> menuItems = new List<MenuItem> ();
  458. miForce16Colors = new MenuItem {
  459. Title = "Force 16 _Colors"
  460. };
  461. miForce16Colors.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miForce16Colors!.Title!.Substring (10, 1) [0];
  462. miForce16Colors.CheckType |= MenuItemCheckStyle.Checked;
  463. miForce16Colors.Action += () => {
  464. miForce16Colors.Checked = Application.Driver.Force16Colors = (bool)!miForce16Colors.Checked!;
  465. Application.Refresh ();
  466. };
  467. menuItems.Add (miForce16Colors);
  468. return menuItems.ToArray ();
  469. }
  470. MenuItem [] CreateDisabledEnabledMouseItems ()
  471. {
  472. List<MenuItem> menuItems = new List<MenuItem> ();
  473. miIsMouseDisabled = new MenuItem {
  474. Title = "_Disable Mouse"
  475. };
  476. miIsMouseDisabled.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miIsMouseDisabled!.Title!.Substring (1, 1) [0];
  477. miIsMouseDisabled.CheckType |= MenuItemCheckStyle.Checked;
  478. miIsMouseDisabled.Action += () => {
  479. miIsMouseDisabled.Checked = Application.IsMouseDisabled = (bool)!miIsMouseDisabled.Checked!;
  480. };
  481. menuItems.Add (miIsMouseDisabled);
  482. return menuItems.ToArray ();
  483. }
  484. MenuItem [] CreateKeyBindingsMenuItems ()
  485. {
  486. List<MenuItem> menuItems = new List<MenuItem> ();
  487. var item = new MenuItem {
  488. Title = "_Key Bindings",
  489. Help = "Change which keys do what"
  490. };
  491. item.Action += () => {
  492. var dlg = new KeyBindingsDialog ();
  493. Application.Run (dlg);
  494. };
  495. menuItems.Add (null!);
  496. menuItems.Add (item);
  497. return menuItems.ToArray ();
  498. }
  499. MenuItem [] CreateDiagnosticFlagsMenuItems ()
  500. {
  501. const string OFF = "Diagnostics: _Off";
  502. const string FRAME_RULER = "Diagnostics: Frame _Ruler";
  503. const string FRAME_PADDING = "Diagnostics: _Frame Padding";
  504. var index = 0;
  505. List<MenuItem> menuItems = new List<MenuItem> ();
  506. foreach (Enum diag in Enum.GetValues (_diagnosticFlags.GetType ())) {
  507. var item = new MenuItem {
  508. Title = GetDiagnosticsTitle (diag),
  509. Shortcut = Key.AltMask + index.ToString () [0]
  510. };
  511. index++;
  512. item.CheckType |= MenuItemCheckStyle.Checked;
  513. if (GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off) == item.Title) {
  514. item.Checked = (_diagnosticFlags & (ConsoleDriver.DiagnosticFlags.FramePadding
  515. | ConsoleDriver.DiagnosticFlags.FrameRuler)) == 0;
  516. } else {
  517. item.Checked = _diagnosticFlags.HasFlag (diag);
  518. }
  519. item.Action += () => {
  520. var t = GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off);
  521. if (item.Title == t && item.Checked == false) {
  522. _diagnosticFlags &= ~(ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  523. item.Checked = true;
  524. } else if (item.Title == t && item.Checked == true) {
  525. _diagnosticFlags |= (ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  526. item.Checked = false;
  527. } else {
  528. var f = GetDiagnosticsEnumValue (item.Title);
  529. if (_diagnosticFlags.HasFlag (f)) {
  530. SetDiagnosticsFlag (f, false);
  531. } else {
  532. SetDiagnosticsFlag (f, true);
  533. }
  534. }
  535. foreach (var menuItem in menuItems) {
  536. if (menuItem.Title == t) {
  537. menuItem.Checked = !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FrameRuler)
  538. && !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FramePadding);
  539. } else if (menuItem.Title != t) {
  540. menuItem.Checked = _diagnosticFlags.HasFlag (GetDiagnosticsEnumValue (menuItem.Title));
  541. }
  542. }
  543. ConsoleDriver.Diagnostics = _diagnosticFlags;
  544. Application.Top.SetNeedsDisplay ();
  545. };
  546. menuItems.Add (item);
  547. }
  548. return menuItems.ToArray ();
  549. string GetDiagnosticsTitle (Enum diag)
  550. {
  551. return Enum.GetName (_diagnosticFlags.GetType (), diag) switch {
  552. "Off" => OFF,
  553. "FrameRuler" => FRAME_RULER,
  554. "FramePadding" => FRAME_PADDING,
  555. _ => "",
  556. };
  557. }
  558. Enum GetDiagnosticsEnumValue (string title)
  559. {
  560. return title switch {
  561. FRAME_RULER => ConsoleDriver.DiagnosticFlags.FrameRuler,
  562. FRAME_PADDING => ConsoleDriver.DiagnosticFlags.FramePadding,
  563. _ => null!,
  564. };
  565. }
  566. void SetDiagnosticsFlag (Enum diag, bool add)
  567. {
  568. switch (diag) {
  569. case ConsoleDriver.DiagnosticFlags.FrameRuler:
  570. if (add) {
  571. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FrameRuler;
  572. } else {
  573. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FrameRuler;
  574. }
  575. break;
  576. case ConsoleDriver.DiagnosticFlags.FramePadding:
  577. if (add) {
  578. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FramePadding;
  579. } else {
  580. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FramePadding;
  581. }
  582. break;
  583. default:
  584. _diagnosticFlags = default;
  585. break;
  586. }
  587. }
  588. }
  589. public MenuItem []? CreateThemeMenuItems ()
  590. {
  591. List<MenuItem> menuItems = new List<MenuItem> ();
  592. foreach (var theme in CM.Themes!) {
  593. var item = new MenuItem {
  594. Title = theme.Key,
  595. Shortcut = Key.AltMask + theme.Key [0]
  596. };
  597. item.CheckType |= MenuItemCheckStyle.Checked;
  598. item.Checked = theme.Key == _cachedTheme; // CM.Themes.Theme;
  599. item.Action += () => {
  600. CM.Themes.Theme = _cachedTheme = theme.Key;
  601. CM.Apply ();
  602. };
  603. menuItems.Add (item);
  604. }
  605. var schemeMenuItems = new List<MenuItem> ();
  606. foreach (var sc in Colors.ColorSchemes) {
  607. var item = new MenuItem {
  608. Title = $"_{sc.Key}",
  609. Data = sc.Key,
  610. Shortcut = Key.AltMask | (Key)sc.Key [..1] [0]
  611. };
  612. item.CheckType |= MenuItemCheckStyle.Radio;
  613. item.Checked = sc.Key == _topLevelColorScheme;
  614. item.Action += () => {
  615. _topLevelColorScheme = (string)item.Data;
  616. foreach (var schemeMenuItem in schemeMenuItems) {
  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. public void ConfigChanged ()
  630. {
  631. if (_topLevelColorScheme == null || !Colors.ColorSchemes.ContainsKey (_topLevelColorScheme)) {
  632. _topLevelColorScheme = "Base";
  633. }
  634. _themeMenuItems = ((UICatalogTopLevel)Application.Top).CreateThemeMenuItems ();
  635. _themeMenuBarItem!.Children = _themeMenuItems;
  636. foreach (var mi in _themeMenuItems!) {
  637. if (mi != null && mi.Parent == null) {
  638. mi.Parent = _themeMenuBarItem;
  639. }
  640. }
  641. var checkedThemeMenu = _themeMenuItems?.Where (m => m?.Checked ?? false).FirstOrDefault ();
  642. if (checkedThemeMenu != null) {
  643. checkedThemeMenu.Checked = false;
  644. }
  645. checkedThemeMenu = _themeMenuItems?.Where (m => m != null && m.Title == CM.Themes?.Theme).FirstOrDefault ();
  646. if (checkedThemeMenu != null) {
  647. CM.Themes!.Theme = checkedThemeMenu.Title!;
  648. checkedThemeMenu.Checked = true;
  649. }
  650. var schemeMenuItems = ((MenuBarItem)_themeMenuItems?.Where (i => i is MenuBarItem)!.FirstOrDefault ()!)!.Children;
  651. foreach (var schemeMenuItem in schemeMenuItems) {
  652. schemeMenuItem.Checked = (string)schemeMenuItem.Data == _topLevelColorScheme;
  653. }
  654. ColorScheme = Colors.ColorSchemes [_topLevelColorScheme];
  655. //ContentPane.LineStyle = FrameView.DefaultBorderStyle;
  656. MenuBar.Menus [0].Children [0].Shortcut = Application.QuitKey;
  657. StatusBar.Items [0].Shortcut = Application.QuitKey;
  658. StatusBar.Items [0].Title = $"~{Application.QuitKey} to quit";
  659. miIsMouseDisabled!.Checked = Application.IsMouseDisabled;
  660. var height = (UICatalogApp.ShowStatusBar ? 1 : 0);// + (MenuBar.Visible ? 1 : 0);
  661. //ContentPane.Height = Dim.Fill (height);
  662. StatusBar.Visible = UICatalogApp.ShowStatusBar;
  663. Application.Top.SetNeedsDisplay ();
  664. }
  665. void KeyDownHandler (object? sender, KeyEventEventArgs? a)
  666. {
  667. if (a!.KeyEvent.IsCapslock) {
  668. Capslock.Title = "Caps: On";
  669. StatusBar.SetNeedsDisplay ();
  670. } else {
  671. Capslock.Title = "Caps: Off";
  672. StatusBar.SetNeedsDisplay ();
  673. }
  674. if (a!.KeyEvent.IsNumlock) {
  675. Numlock.Title = "Num: On";
  676. StatusBar.SetNeedsDisplay ();
  677. } else {
  678. Numlock.Title = "Num: Off";
  679. StatusBar.SetNeedsDisplay ();
  680. }
  681. if (a!.KeyEvent.IsScrolllock) {
  682. Scrolllock.Title = "Scroll: On";
  683. StatusBar.SetNeedsDisplay ();
  684. } else {
  685. Scrolllock.Title = "Scroll: Off";
  686. StatusBar.SetNeedsDisplay ();
  687. }
  688. }
  689. void CategoryView_SelectedChanged (object? sender, ListViewItemEventArgs? e)
  690. {
  691. var item = _categories! [e!.Item];
  692. List<Scenario> newlist;
  693. if (e.Item == 0) {
  694. // First category is "All"
  695. newlist = _scenarios!;
  696. newlist = _scenarios!;
  697. } else {
  698. newlist = _scenarios!.Where (s => s.GetCategories ().Contains (item)).ToList ();
  699. }
  700. ScenarioList.Table = new EnumerableTableSource<Scenario> (newlist, new Dictionary<string, Func<Scenario, object>> () {
  701. { "Name", (s) => s.GetName() },
  702. { "Description", (s) => s.GetDescription() },
  703. });
  704. // Create a collection of just the scenario names (the 1st column in our TableView)
  705. // for CollectionNavigator.
  706. var firstColumnList = new List<object> ();
  707. for (var i = 0; i < ScenarioList.Table.Rows; i++) {
  708. firstColumnList.Add (ScenarioList.Table [i, 0]);
  709. }
  710. _scenarioCollectionNav.Collection = firstColumnList;
  711. }
  712. }
  713. static void VerifyObjectsWereDisposed ()
  714. {
  715. #if DEBUG_IDISPOSABLE
  716. // Validate there are no outstanding Responder-based instances
  717. // after a scenario was selected to run. This proves the main UI Catalog
  718. // 'app' closed cleanly.
  719. foreach (var inst in Responder.Instances) {
  720. Debug.Assert (inst.WasDisposed);
  721. }
  722. Responder.Instances.Clear ();
  723. // Validate there are no outstanding Application.RunState-based instances
  724. // after a scenario was selected to run. This proves the main UI Catalog
  725. // 'app' closed cleanly.
  726. foreach (var inst in RunState.Instances) {
  727. Debug.Assert (inst.WasDisposed);
  728. }
  729. RunState.Instances.Clear ();
  730. #endif
  731. }
  732. static void OpenUrl (string url)
  733. {
  734. try {
  735. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  736. url = url.Replace ("&", "^&");
  737. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  738. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  739. using var process = new Process {
  740. StartInfo = new ProcessStartInfo {
  741. FileName = "xdg-open",
  742. Arguments = url,
  743. RedirectStandardError = true,
  744. RedirectStandardOutput = true,
  745. CreateNoWindow = true,
  746. UseShellExecute = false
  747. }
  748. };
  749. process.Start ();
  750. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  751. Process.Start ("open", url);
  752. }
  753. } catch {
  754. throw;
  755. }
  756. }
  757. }
  758. }