UICatalog.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. using NStack;
  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. #nullable enable
  16. /// <summary>
  17. /// UI Catalog is a comprehensive sample library for Terminal.Gui. It provides a simple UI for adding to the catalog of scenarios.
  18. /// </summary>
  19. /// <remarks>
  20. /// <para>
  21. /// UI Catalog attempts to satisfy the following goals:
  22. /// </para>
  23. /// <para>
  24. /// <list type="number">
  25. /// <item>
  26. /// <description>
  27. /// Be an easy to use showcase for Terminal.Gui concepts and features.
  28. /// </description>
  29. /// </item>
  30. /// <item>
  31. /// <description>
  32. /// Provide sample code that illustrates how to properly implement said concepts & features.
  33. /// </description>
  34. /// </item>
  35. /// <item>
  36. /// <description>
  37. /// Make it easy for contributors to add additional samples in a structured way.
  38. /// </description>
  39. /// </item>
  40. /// </list>
  41. /// </para>
  42. /// <para>
  43. /// See the project README for more details (https://github.com/gui-cs/Terminal.Gui/tree/master/UICatalog/README.md).
  44. /// </para>
  45. /// </remarks>
  46. namespace UICatalog {
  47. /// <summary>
  48. /// UI Catalog is a comprehensive sample app and scenario library for <see cref="Terminal.Gui"/>
  49. /// </summary>
  50. class UICatalogApp {
  51. //[SerializableConfigurationProperty (Scope = typeof (AppScope), OmitClassName = true), JsonPropertyName ("UICatalog.StatusBar")]
  52. //public static bool ShowStatusBar { get; set; } = true;
  53. [SerializableConfigurationProperty (Scope = typeof (AppScope), OmitClassName = true), JsonPropertyName ("UICatalog.StatusBar")]
  54. public static bool ShowStatusBar { get; set; } = true;
  55. static readonly FileSystemWatcher _currentDirWatcher = new FileSystemWatcher ();
  56. static readonly FileSystemWatcher _homeDirWatcher = new FileSystemWatcher ();
  57. static void Main (string [] args)
  58. {
  59. Console.OutputEncoding = Encoding.Default;
  60. if (Debugger.IsAttached) {
  61. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  62. }
  63. _scenarios = Scenario.GetScenarios ();
  64. _categories = Scenario.GetAllCategories ();
  65. if (args.Length > 0 && args.Contains ("-usc")) {
  66. _useSystemConsole = true;
  67. args = args.Where (val => val != "-usc").ToArray ();
  68. }
  69. StartConfigFileWatcher ();
  70. // If a Scenario name has been provided on the commandline
  71. // run it and exit when done.
  72. if (args.Length > 0) {
  73. var item = _scenarios.FindIndex (s => s.GetName ().Equals (args [0], StringComparison.OrdinalIgnoreCase));
  74. _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios [item].GetType ())!;
  75. Application.UseSystemConsole = _useSystemConsole;
  76. Application.Init ();
  77. _selectedScenario.Theme = _cachedTheme;
  78. _selectedScenario.TopLevelColorScheme = _topLevelColorScheme;
  79. _selectedScenario.Init ();
  80. _selectedScenario.Setup ();
  81. _selectedScenario.Run ();
  82. _selectedScenario.Dispose ();
  83. _selectedScenario = null;
  84. Application.Shutdown ();
  85. VerifyObjectsWereDisposed ();
  86. return;
  87. }
  88. _aboutMessage = new StringBuilder ();
  89. _aboutMessage.AppendLine (@"A comprehensive sample library for");
  90. _aboutMessage.AppendLine (@"");
  91. _aboutMessage.AppendLine (@" _______ _ _ _____ _ ");
  92. _aboutMessage.AppendLine (@" |__ __| (_) | | / ____| (_) ");
  93. _aboutMessage.AppendLine (@" | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ");
  94. _aboutMessage.AppendLine (@" | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | | ");
  95. _aboutMessage.AppendLine (@" | | __/ | | | | | | | | | | | (_| | || |__| | |_| | | ");
  96. _aboutMessage.AppendLine (@" |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_| ");
  97. _aboutMessage.AppendLine (@"");
  98. _aboutMessage.AppendLine (@"v2 - Work in Progress");
  99. _aboutMessage.AppendLine (@"");
  100. _aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui");
  101. Scenario scenario;
  102. while ((scenario = RunUICatalogTopLevel ()) != null) {
  103. VerifyObjectsWereDisposed ();
  104. ConfigurationManager.Themes!.Theme = _cachedTheme!;
  105. ConfigurationManager.Apply ();
  106. scenario.Theme = _cachedTheme;
  107. scenario.TopLevelColorScheme = _topLevelColorScheme;
  108. scenario.Init ();
  109. scenario.Setup ();
  110. scenario.Run ();
  111. scenario.Dispose ();
  112. // This call to Application.Shutdown brackets the Application.Init call
  113. // made by Scenario.Init() above
  114. Application.Shutdown ();
  115. VerifyObjectsWereDisposed ();
  116. }
  117. StopConfigFileWatcher ();
  118. VerifyObjectsWereDisposed ();
  119. }
  120. private static void StopConfigFileWatcher ()
  121. {
  122. _currentDirWatcher.EnableRaisingEvents = false;
  123. _currentDirWatcher.Changed -= ConfigFileChanged;
  124. _currentDirWatcher.Created -= ConfigFileChanged;
  125. _homeDirWatcher.EnableRaisingEvents = false;
  126. _homeDirWatcher.Changed -= ConfigFileChanged;
  127. _homeDirWatcher.Created -= ConfigFileChanged;
  128. }
  129. private static void StartConfigFileWatcher ()
  130. {
  131. // Setup a file system watcher for `./.tui/`
  132. _currentDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  133. var f = new FileInfo (Assembly.GetExecutingAssembly ().Location);
  134. var tuiDir = Path.Combine (f.Directory!.FullName, ".tui");
  135. if (!Directory.Exists (tuiDir)) {
  136. Directory.CreateDirectory (tuiDir);
  137. }
  138. _currentDirWatcher.Path = tuiDir;
  139. _currentDirWatcher.Filter = "*config.json";
  140. // Setup a file system watcher for `~/.tui/`
  141. _homeDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  142. f = new FileInfo (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile));
  143. tuiDir = Path.Combine (f.FullName, ".tui");
  144. if (!Directory.Exists (tuiDir)) {
  145. Directory.CreateDirectory (tuiDir);
  146. }
  147. _homeDirWatcher.Path = tuiDir;
  148. _homeDirWatcher.Filter = "*config.json";
  149. _currentDirWatcher.Changed += ConfigFileChanged;
  150. //_currentDirWatcher.Created += ConfigFileChanged;
  151. _currentDirWatcher.EnableRaisingEvents = true;
  152. _homeDirWatcher.Changed += ConfigFileChanged;
  153. //_homeDirWatcher.Created += ConfigFileChanged;
  154. _homeDirWatcher.EnableRaisingEvents = true;
  155. }
  156. private static void ConfigFileChanged (object sender, FileSystemEventArgs e)
  157. {
  158. if (Application.Top == null) {
  159. return;
  160. }
  161. // TOOD: THis is a hack. Figure out how to ensure that the file is fully written before reading it.
  162. Thread.Sleep (500);
  163. ConfigurationManager.Load ();
  164. ConfigurationManager.Apply ();
  165. }
  166. /// <summary>
  167. /// Shows the UI Catalog selection UI. When the user selects a Scenario to run, the
  168. /// UI Catalog main app UI is killed and the Scenario is run as though it were Application.Top.
  169. /// When the Scenario exits, this function exits.
  170. /// </summary>
  171. /// <returns></returns>
  172. static Scenario RunUICatalogTopLevel ()
  173. {
  174. Application.UseSystemConsole = _useSystemConsole;
  175. // Run UI Catalog UI. When it exits, if _selectedScenario is != null then
  176. // a Scenario was selected. Otherwise, the user wants to quit UI Catalog.
  177. Application.Init ();
  178. if (_cachedTheme is null) {
  179. _cachedTheme = ConfigurationManager.Themes?.Theme;
  180. } else {
  181. ConfigurationManager.Themes!.Theme = _cachedTheme;
  182. ConfigurationManager.Apply ();
  183. }
  184. //Application.EnableConsoleScrolling = _enableConsoleScrolling;
  185. Application.Run<UICatalogTopLevel> ();
  186. Application.Shutdown ();
  187. return _selectedScenario!;
  188. }
  189. static List<Scenario>? _scenarios;
  190. static List<string>? _categories;
  191. // When a scenario is run, the main app is killed. These items
  192. // are therefore cached so that when the scenario exits the
  193. // main app UI can be restored to previous state
  194. static int _cachedScenarioIndex = 0;
  195. static int _cachedCategoryIndex = 0;
  196. static string? _cachedTheme = string.Empty;
  197. static StringBuilder? _aboutMessage = null;
  198. // If set, holds the scenario the user selected
  199. static Scenario? _selectedScenario = null;
  200. static bool _useSystemConsole = false;
  201. static ConsoleDriver.DiagnosticFlags _diagnosticFlags;
  202. //static bool _enableConsoleScrolling = false;
  203. static bool _isFirstRunning = true;
  204. static string _topLevelColorScheme = string.Empty;
  205. static MenuItem []? _themeMenuItems;
  206. static MenuBarItem? _themeMenuBarItem;
  207. /// <summary>
  208. /// This is the main UI Catalog app view. It is run fresh when the app loads (if a Scenario has not been passed on
  209. /// the command line) and each time a Scenario ends.
  210. /// </summary>
  211. public class UICatalogTopLevel : Toplevel {
  212. public MenuItem? miIsMouseDisabled;
  213. public MenuItem? miEnableConsoleScrolling;
  214. public TileView ContentPane;
  215. public ListView CategoryListView;
  216. public ListView ScenarioListView;
  217. public StatusItem Capslock;
  218. public StatusItem Numlock;
  219. public StatusItem Scrolllock;
  220. public StatusItem DriverName;
  221. public StatusItem OS;
  222. public UICatalogTopLevel ()
  223. {
  224. _themeMenuItems = CreateThemeMenuItems ();
  225. _themeMenuBarItem = new MenuBarItem ("_Themes", _themeMenuItems);
  226. MenuBar = new MenuBar (new MenuBarItem [] {
  227. new MenuBarItem ("_File", new MenuItem [] {
  228. new MenuItem ("_Quit", "Quit UI Catalog", () => RequestStop(), null, null)
  229. }),
  230. _themeMenuBarItem,
  231. new MenuBarItem ("Diag_nostics", CreateDiagnosticMenuItems()),
  232. new MenuBarItem ("_Help", new MenuItem [] {
  233. new MenuItem ("_gui.cs API Overview", "", () => OpenUrl ("https://gui-cs.github.io/Terminal.Gui/articles/overview.html"), null, null, Key.F1),
  234. new MenuItem ("gui.cs _README", "", () => OpenUrl ("https://github.com/gui-cs/Terminal.Gui"), null, null, Key.F2),
  235. new MenuItem ("_About...",
  236. "About UI Catalog", () => MessageBox.Query ("About UI Catalog", _aboutMessage!.ToString(), 0, false, "_Ok"), null, null, Key.CtrlMask | Key.A),
  237. }),
  238. });
  239. Capslock = new StatusItem (Key.CharMask, "Caps", null);
  240. Numlock = new StatusItem (Key.CharMask, "Num", null);
  241. Scrolllock = new StatusItem (Key.CharMask, "Scroll", null);
  242. DriverName = new StatusItem (Key.CharMask, "Driver:", null);
  243. OS = new StatusItem (Key.CharMask, "OS:", null);
  244. StatusBar = new StatusBar () {
  245. Visible = UICatalogApp.ShowStatusBar
  246. };
  247. StatusBar.Items = new StatusItem [] {
  248. new StatusItem(Application.QuitKey, $"~{Application.QuitKey} to quit", () => {
  249. if (_selectedScenario is null){
  250. // This causes GetScenarioToRun to return null
  251. _selectedScenario = null;
  252. RequestStop();
  253. } else {
  254. _selectedScenario.RequestStop();
  255. }
  256. }),
  257. new StatusItem(Key.F10, "~F10~ Status Bar", () => {
  258. StatusBar.Visible = !StatusBar.Visible;
  259. ContentPane!.Height = Dim.Fill(StatusBar.Visible ? 1 : 0);
  260. LayoutSubviews();
  261. SetSubViewNeedsDisplay();
  262. }),
  263. DriverName,
  264. OS
  265. };
  266. ContentPane = new TileView () {
  267. Id = "ContentPane",
  268. X = 0,
  269. Y = 1, // for menu
  270. Width = Dim.Fill (),
  271. Height = Dim.Fill (1),
  272. CanFocus = true,
  273. Shortcut = Key.CtrlMask | Key.C,
  274. };
  275. ContentPane.BorderStyle = BorderStyle.Single;
  276. ContentPane.SetSplitterPos (0, 25);
  277. ContentPane.ShortcutAction = () => ContentPane.SetFocus ();
  278. CategoryListView = new ListView (_categories) {
  279. X = 0,
  280. Y = 0,
  281. Width = Dim.Fill (0),
  282. Height = Dim.Fill (0),
  283. AllowsMarking = false,
  284. CanFocus = true,
  285. };
  286. CategoryListView.OpenSelectedItem += (s, a) => {
  287. ScenarioListView!.SetFocus ();
  288. };
  289. CategoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
  290. ContentPane.Tiles.ElementAt (0).Title = "Categories";
  291. ContentPane.Tiles.ElementAt (0).MinSize = 2;
  292. ContentPane.Tiles.ElementAt (0).ContentView.Add (CategoryListView);
  293. ScenarioListView = new ListView () {
  294. X = 0,
  295. Y = 0,
  296. Width = Dim.Fill (0),
  297. Height = Dim.Fill (0),
  298. AllowsMarking = false,
  299. CanFocus = true,
  300. };
  301. ScenarioListView.OpenSelectedItem += ScenarioListView_OpenSelectedItem;
  302. ContentPane.Tiles.ElementAt (1).Title = "Scenarios";
  303. ContentPane.Tiles.ElementAt (1).ContentView.Add (ScenarioListView);
  304. ContentPane.Tiles.ElementAt (1).MinSize = 2;
  305. KeyDown += KeyDownHandler;
  306. Add (MenuBar);
  307. Add (ContentPane);
  308. Add (StatusBar);
  309. Loaded += LoadedHandler;
  310. Unloaded += UnloadedHandler;
  311. // Restore previous selections
  312. CategoryListView.SelectedItem = _cachedCategoryIndex;
  313. ScenarioListView.SelectedItem = _cachedScenarioIndex;
  314. ConfigurationManager.Applied += ConfigAppliedHandler;
  315. }
  316. void LoadedHandler (object? sender, EventArgs? args)
  317. {
  318. ConfigChanged ();
  319. miIsMouseDisabled!.Checked = Application.IsMouseDisabled;
  320. miEnableConsoleScrolling!.Checked = Application.EnableConsoleScrolling;
  321. DriverName.Title = $"Driver: {Driver.GetType ().Name}";
  322. OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem} {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion}";
  323. if (_selectedScenario != null) {
  324. _selectedScenario = null;
  325. _isFirstRunning = false;
  326. }
  327. if (!_isFirstRunning) {
  328. ScenarioListView.SetFocus ();
  329. }
  330. StatusBar.VisibleChanged += (s, e) => {
  331. UICatalogApp.ShowStatusBar = StatusBar.Visible;
  332. var height = (StatusBar.Visible ? 1 : 0);
  333. ContentPane.Height = Dim.Fill (height);
  334. LayoutSubviews ();
  335. SetSubViewNeedsDisplay ();
  336. };
  337. Loaded -= LoadedHandler;
  338. }
  339. private void UnloadedHandler (object? sender, EventArgs? args)
  340. {
  341. ConfigurationManager.Applied -= ConfigAppliedHandler;
  342. Unloaded -= UnloadedHandler;
  343. }
  344. void ConfigAppliedHandler (object? sender, ConfigurationManagerEventArgs? a)
  345. {
  346. ConfigChanged ();
  347. }
  348. /// <summary>
  349. /// Launches the selected scenario, setting the global _selectedScenario
  350. /// </summary>
  351. /// <param name="e"></param>
  352. void ScenarioListView_OpenSelectedItem (object? sender, EventArgs? e)
  353. {
  354. if (_selectedScenario is null) {
  355. // Save selected item state
  356. _cachedCategoryIndex = CategoryListView.SelectedItem;
  357. _cachedScenarioIndex = ScenarioListView.SelectedItem;
  358. // Create new instance of scenario (even though Scenarios contains instances)
  359. var sourceList = ScenarioListView.Source.ToList ();
  360. _selectedScenario = (Scenario)Activator.CreateInstance (ScenarioListView.Source.ToList () [ScenarioListView.SelectedItem]!.GetType ())!;
  361. // Tell the main app to stop
  362. Application.RequestStop ();
  363. }
  364. }
  365. List<MenuItem []> CreateDiagnosticMenuItems ()
  366. {
  367. List<MenuItem []> menuItems = new List<MenuItem []> {
  368. CreateDiagnosticFlagsMenuItems (),
  369. new MenuItem [] { },
  370. CreateEnableConsoleScrollingMenuItems (),
  371. CreateDisabledEnabledMouseItems (),
  372. CreateKeybindingsMenuItems ()
  373. };
  374. return menuItems;
  375. }
  376. MenuItem [] CreateDisabledEnabledMouseItems ()
  377. {
  378. List<MenuItem> menuItems = new List<MenuItem> ();
  379. miIsMouseDisabled = new MenuItem {
  380. Title = "_Disable Mouse"
  381. };
  382. miIsMouseDisabled.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miIsMouseDisabled!.Title!.ToString ()!.Substring (1, 1) [0];
  383. miIsMouseDisabled.CheckType |= MenuItemCheckStyle.Checked;
  384. miIsMouseDisabled.Action += () => {
  385. miIsMouseDisabled.Checked = Application.IsMouseDisabled = (bool)!miIsMouseDisabled.Checked!;
  386. };
  387. menuItems.Add (miIsMouseDisabled);
  388. return menuItems.ToArray ();
  389. }
  390. MenuItem [] CreateKeybindingsMenuItems ()
  391. {
  392. List<MenuItem> menuItems = new List<MenuItem> ();
  393. var item = new MenuItem {
  394. Title = "_Key Bindings",
  395. Help = "Change which keys do what"
  396. };
  397. item.Action += () => {
  398. var dlg = new KeyBindingsDialog ();
  399. Application.Run (dlg);
  400. };
  401. menuItems.Add (null!);
  402. menuItems.Add (item);
  403. return menuItems.ToArray ();
  404. }
  405. MenuItem [] CreateEnableConsoleScrollingMenuItems ()
  406. {
  407. List<MenuItem> menuItems = new List<MenuItem> ();
  408. miEnableConsoleScrolling = new MenuItem ();
  409. miEnableConsoleScrolling.Title = "_Enable Console Scrolling";
  410. miEnableConsoleScrolling.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miEnableConsoleScrolling.Title.ToString ()!.Substring (1, 1) [0];
  411. miEnableConsoleScrolling.CheckType |= MenuItemCheckStyle.Checked;
  412. miEnableConsoleScrolling.Action += () => {
  413. miEnableConsoleScrolling.Checked = !miEnableConsoleScrolling.Checked;
  414. Application.EnableConsoleScrolling = (bool)miEnableConsoleScrolling.Checked!;
  415. };
  416. menuItems.Add (miEnableConsoleScrolling);
  417. return menuItems.ToArray ();
  418. }
  419. MenuItem [] CreateDiagnosticFlagsMenuItems ()
  420. {
  421. const string OFF = "Diagnostics: _Off";
  422. const string FRAME_RULER = "Diagnostics: Frame _Ruler";
  423. const string FRAME_PADDING = "Diagnostics: _Frame Padding";
  424. var index = 0;
  425. List<MenuItem> menuItems = new List<MenuItem> ();
  426. foreach (Enum diag in Enum.GetValues (_diagnosticFlags.GetType ())) {
  427. var item = new MenuItem {
  428. Title = GetDiagnosticsTitle (diag),
  429. Shortcut = Key.AltMask + index.ToString () [0]
  430. };
  431. index++;
  432. item.CheckType |= MenuItemCheckStyle.Checked;
  433. if (GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off) == item.Title) {
  434. item.Checked = (_diagnosticFlags & (ConsoleDriver.DiagnosticFlags.FramePadding
  435. | ConsoleDriver.DiagnosticFlags.FrameRuler)) == 0;
  436. } else {
  437. item.Checked = _diagnosticFlags.HasFlag (diag);
  438. }
  439. item.Action += () => {
  440. var t = GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off);
  441. if (item.Title == t && item.Checked == false) {
  442. _diagnosticFlags &= ~(ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  443. item.Checked = true;
  444. } else if (item.Title == t && item.Checked == true) {
  445. _diagnosticFlags |= (ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  446. item.Checked = false;
  447. } else {
  448. var f = GetDiagnosticsEnumValue (item.Title);
  449. if (_diagnosticFlags.HasFlag (f)) {
  450. SetDiagnosticsFlag (f, false);
  451. } else {
  452. SetDiagnosticsFlag (f, true);
  453. }
  454. }
  455. foreach (var menuItem in menuItems) {
  456. if (menuItem.Title == t) {
  457. menuItem.Checked = !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FrameRuler)
  458. && !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FramePadding);
  459. } else if (menuItem.Title != t) {
  460. menuItem.Checked = _diagnosticFlags.HasFlag (GetDiagnosticsEnumValue (menuItem.Title));
  461. }
  462. }
  463. ConsoleDriver.Diagnostics = _diagnosticFlags;
  464. Application.Top.SetNeedsDisplay ();
  465. };
  466. menuItems.Add (item);
  467. }
  468. return menuItems.ToArray ();
  469. string GetDiagnosticsTitle (Enum diag)
  470. {
  471. return Enum.GetName (_diagnosticFlags.GetType (), diag) switch {
  472. "Off" => OFF,
  473. "FrameRuler" => FRAME_RULER,
  474. "FramePadding" => FRAME_PADDING,
  475. _ => "",
  476. };
  477. }
  478. Enum GetDiagnosticsEnumValue (ustring title)
  479. {
  480. return title!.ToString () switch {
  481. FRAME_RULER => ConsoleDriver.DiagnosticFlags.FrameRuler,
  482. FRAME_PADDING => ConsoleDriver.DiagnosticFlags.FramePadding,
  483. _ => null!,
  484. };
  485. }
  486. void SetDiagnosticsFlag (Enum diag, bool add)
  487. {
  488. switch (diag) {
  489. case ConsoleDriver.DiagnosticFlags.FrameRuler:
  490. if (add) {
  491. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FrameRuler;
  492. } else {
  493. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FrameRuler;
  494. }
  495. break;
  496. case ConsoleDriver.DiagnosticFlags.FramePadding:
  497. if (add) {
  498. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FramePadding;
  499. } else {
  500. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FramePadding;
  501. }
  502. break;
  503. default:
  504. _diagnosticFlags = default;
  505. break;
  506. }
  507. }
  508. }
  509. public MenuItem []? CreateThemeMenuItems ()
  510. {
  511. List<MenuItem> menuItems = new List<MenuItem> ();
  512. foreach (var theme in ConfigurationManager.Themes!) {
  513. var item = new MenuItem {
  514. Title = theme.Key,
  515. Shortcut = Key.AltMask + theme.Key [0]
  516. };
  517. item.CheckType |= MenuItemCheckStyle.Checked;
  518. item.Checked = theme.Key == _cachedTheme; // ConfigurationManager.Themes.Theme;
  519. item.Action += () => {
  520. ConfigurationManager.Themes.Theme = _cachedTheme = theme.Key;
  521. ConfigurationManager.Apply ();
  522. };
  523. menuItems.Add (item);
  524. }
  525. var schemeMenuItems = new List<MenuItem> ();
  526. foreach (var sc in Colors.ColorSchemes) {
  527. var item = new MenuItem {
  528. Title = $"_{sc.Key}",
  529. Data = sc.Key,
  530. Shortcut = Key.AltMask | (Key)sc.Key [..1] [0]
  531. };
  532. item.CheckType |= MenuItemCheckStyle.Radio;
  533. item.Checked = sc.Key == _topLevelColorScheme;
  534. item.Action += () => {
  535. _topLevelColorScheme = (string)item.Data;
  536. foreach (var schemeMenuItem in schemeMenuItems) {
  537. schemeMenuItem.Checked = (string)schemeMenuItem.Data == _topLevelColorScheme;
  538. }
  539. ColorScheme = Colors.ColorSchemes [_topLevelColorScheme];
  540. Application.Top.SetNeedsDisplay ();
  541. };
  542. schemeMenuItems.Add (item);
  543. }
  544. menuItems.Add (null!);
  545. var mbi = new MenuBarItem ("_Color Scheme for Application.Top", schemeMenuItems.ToArray ());
  546. menuItems.Add (mbi);
  547. return menuItems.ToArray ();
  548. }
  549. public void ConfigChanged ()
  550. {
  551. if (_topLevelColorScheme == null || !Colors.ColorSchemes.ContainsKey (_topLevelColorScheme)) {
  552. _topLevelColorScheme = "Base";
  553. }
  554. _themeMenuItems = ((UICatalogTopLevel)Application.Top).CreateThemeMenuItems ();
  555. _themeMenuBarItem!.Children = _themeMenuItems;
  556. var checkedThemeMenu = _themeMenuItems?.Where (m => m?.Checked ?? false).FirstOrDefault ();
  557. if (checkedThemeMenu != null) {
  558. checkedThemeMenu.Checked = false;
  559. }
  560. checkedThemeMenu = _themeMenuItems?.Where (m => m != null && m.Title == ConfigurationManager.Themes?.Theme).FirstOrDefault ();
  561. if (checkedThemeMenu != null) {
  562. ConfigurationManager.Themes!.Theme = checkedThemeMenu.Title.ToString ()!;
  563. checkedThemeMenu.Checked = true;
  564. }
  565. var schemeMenuItems = ((MenuBarItem)_themeMenuItems?.Where (i => i is MenuBarItem)!.FirstOrDefault ()!)!.Children;
  566. foreach (var schemeMenuItem in schemeMenuItems) {
  567. schemeMenuItem.Checked = (string)schemeMenuItem.Data == _topLevelColorScheme;
  568. }
  569. ColorScheme = Colors.ColorSchemes [_topLevelColorScheme];
  570. ContentPane.BorderStyle = FrameView.DefaultBorderStyle;
  571. MenuBar.Menus [0].Children [0].Shortcut = Application.QuitKey;
  572. StatusBar.Items [0].Shortcut = Application.QuitKey;
  573. StatusBar.Items [0].Title = $"~{Application.QuitKey} to quit";
  574. miIsMouseDisabled!.Checked = Application.IsMouseDisabled;
  575. miEnableConsoleScrolling!.Checked = Application.EnableConsoleScrolling;
  576. var height = (UICatalogApp.ShowStatusBar ? 1 : 0);// + (MenuBar.Visible ? 1 : 0);
  577. ContentPane.Height = Dim.Fill (height);
  578. StatusBar.Visible = UICatalogApp.ShowStatusBar;
  579. Application.Top.SetNeedsDisplay ();
  580. }
  581. void KeyDownHandler (object? sender, KeyEventEventArgs? a)
  582. {
  583. if (a!.KeyEvent.IsCapslock) {
  584. Capslock.Title = "Caps: On";
  585. StatusBar.SetNeedsDisplay ();
  586. } else {
  587. Capslock.Title = "Caps: Off";
  588. StatusBar.SetNeedsDisplay ();
  589. }
  590. if (a!.KeyEvent.IsNumlock) {
  591. Numlock.Title = "Num: On";
  592. StatusBar.SetNeedsDisplay ();
  593. } else {
  594. Numlock.Title = "Num: Off";
  595. StatusBar.SetNeedsDisplay ();
  596. }
  597. if (a!.KeyEvent.IsScrolllock) {
  598. Scrolllock.Title = "Scroll: On";
  599. StatusBar.SetNeedsDisplay ();
  600. } else {
  601. Scrolllock.Title = "Scroll: Off";
  602. StatusBar.SetNeedsDisplay ();
  603. }
  604. }
  605. void CategoryListView_SelectedChanged (object? sender, ListViewItemEventArgs? e)
  606. {
  607. var item = _categories! [e!.Item];
  608. List<Scenario> newlist;
  609. if (e.Item == 0) {
  610. // First category is "All"
  611. newlist = _scenarios!;
  612. } else {
  613. newlist = _scenarios!.Where (s => s.GetCategories ().Contains (item)).ToList ();
  614. }
  615. ScenarioListView.SetSource (newlist.ToList ());
  616. }
  617. }
  618. static void VerifyObjectsWereDisposed ()
  619. {
  620. #if DEBUG_IDISPOSABLE
  621. // Validate there are no outstanding Responder-based instances
  622. // after a scenario was selected to run. This proves the main UI Catalog
  623. // 'app' closed cleanly.
  624. foreach (var inst in Responder.Instances) {
  625. Debug.Assert (inst.WasDisposed);
  626. }
  627. Responder.Instances.Clear ();
  628. // Validate there are no outstanding Application.RunState-based instances
  629. // after a scenario was selected to run. This proves the main UI Catalog
  630. // 'app' closed cleanly.
  631. foreach (var inst in Application.RunState.Instances) {
  632. Debug.Assert (inst.WasDisposed);
  633. }
  634. Application.RunState.Instances.Clear ();
  635. #endif
  636. }
  637. static void OpenUrl (string url)
  638. {
  639. try {
  640. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  641. url = url.Replace ("&", "^&");
  642. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  643. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  644. using var process = new Process {
  645. StartInfo = new ProcessStartInfo {
  646. FileName = "xdg-open",
  647. Arguments = url,
  648. RedirectStandardError = true,
  649. RedirectStandardOutput = true,
  650. CreateNoWindow = true,
  651. UseShellExecute = false
  652. }
  653. };
  654. process.Start ();
  655. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  656. Process.Start ("open", url);
  657. }
  658. } catch {
  659. throw;
  660. }
  661. }
  662. }
  663. }