UICatalog.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 Terminal.Gui.Configuration;
  14. using static Terminal.Gui.Configuration.ConfigurationManager;
  15. using System.Text.Json.Serialization;
  16. #nullable enable
  17. /// <summary>
  18. /// UI Catalog is a comprehensive sample library for Terminal.Gui. It provides a simple UI for adding to the catalog of scenarios.
  19. /// </summary>
  20. /// <remarks>
  21. /// <para>
  22. /// UI Catalog attempts to satisfy the following goals:
  23. /// </para>
  24. /// <para>
  25. /// <list type="number">
  26. /// <item>
  27. /// <description>
  28. /// Be an easy to use showcase for Terminal.Gui concepts and features.
  29. /// </description>
  30. /// </item>
  31. /// <item>
  32. /// <description>
  33. /// Provide sample code that illustrates how to properly implement said concepts & features.
  34. /// </description>
  35. /// </item>
  36. /// <item>
  37. /// <description>
  38. /// Make it easy for contributors to add additional samples in a structured way.
  39. /// </description>
  40. /// </item>
  41. /// </list>
  42. /// </para>
  43. /// <para>
  44. /// See the project README for more details (https://github.com/gui-cs/Terminal.Gui/tree/master/UICatalog/README.md).
  45. /// </para>
  46. /// </remarks>
  47. namespace UICatalog {
  48. /// <summary>
  49. /// UI Catalog is a comprehensive sample app and scenario library for <see cref="Terminal.Gui"/>
  50. /// </summary>
  51. class UICatalogApp {
  52. //[SerializableConfigurationProperty (Scope = typeof (AppScope), OmitClassName = true), JsonPropertyName ("UICatalog.StatusBar")]
  53. //public static bool ShowStatusBar { get; set; } = true;
  54. [SerializableConfigurationProperty (Scope = typeof (AppScope), OmitClassName = true), JsonPropertyName ("UICatalog.StatusBar")]
  55. public static bool ShowStatusBar { get; set; } = true;
  56. static readonly FileSystemWatcher _currentDirWatcher = new FileSystemWatcher ();
  57. static readonly FileSystemWatcher _homeDirWatcher = new FileSystemWatcher ();
  58. static void Main (string [] args)
  59. {
  60. Console.OutputEncoding = Encoding.Default;
  61. if (Debugger.IsAttached) {
  62. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  63. }
  64. _scenarios = Scenario.GetScenarios ();
  65. _categories = Scenario.GetAllCategories ();
  66. _nameColumnWidth = _scenarios.OrderByDescending (s => s.GetName ().Length).FirstOrDefault ().GetName ().Length;
  67. if (args.Length > 0 && args.Contains ("-usc")) {
  68. _useSystemConsole = true;
  69. args = args.Where (val => val != "-usc").ToArray ();
  70. }
  71. StartConfigFileWatcher ();
  72. // If a Scenario name has been provided on the commandline
  73. // run it and exit when done.
  74. if (args.Length > 0) {
  75. var item = _scenarios.FindIndex (s => s.GetName ().Equals (args [0], StringComparison.OrdinalIgnoreCase));
  76. _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios [item].GetType ());
  77. Application.UseSystemConsole = _useSystemConsole;
  78. Application.Init ();
  79. _selectedScenario.Theme = _cachedTheme;
  80. _selectedScenario.TopLevelColorScheme = _topLevelColorScheme;
  81. _selectedScenario.Init ();
  82. _selectedScenario.Setup ();
  83. _selectedScenario.Run ();
  84. _selectedScenario.Dispose ();
  85. _selectedScenario = null;
  86. Application.Shutdown ();
  87. VerifyObjectsWereDisposed ();
  88. return;
  89. }
  90. _aboutMessage = new StringBuilder ();
  91. _aboutMessage.AppendLine (@"A comprehensive sample library for");
  92. _aboutMessage.AppendLine (@"");
  93. _aboutMessage.AppendLine (@" _______ _ _ _____ _ ");
  94. _aboutMessage.AppendLine (@" |__ __| (_) | | / ____| (_) ");
  95. _aboutMessage.AppendLine (@" | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ");
  96. _aboutMessage.AppendLine (@" | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | | ");
  97. _aboutMessage.AppendLine (@" | | __/ | | | | | | | | | | | (_| | || |__| | |_| | | ");
  98. _aboutMessage.AppendLine (@" |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_| ");
  99. _aboutMessage.AppendLine (@"");
  100. _aboutMessage.AppendLine (@"v2 - Work in Progress");
  101. _aboutMessage.AppendLine (@"");
  102. _aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui");
  103. Scenario scenario;
  104. while ((scenario = RunUICatalogTopLevel ()) != null) {
  105. VerifyObjectsWereDisposed ();
  106. ConfigurationManager.Themes.Theme = _cachedTheme;
  107. ConfigurationManager.Apply ();
  108. scenario.Theme = _cachedTheme;
  109. scenario.TopLevelColorScheme = _topLevelColorScheme;
  110. scenario.Init ();
  111. scenario.Setup ();
  112. scenario.Run ();
  113. scenario.Dispose ();
  114. // This call to Application.Shutdown brackets the Application.Init call
  115. // made by Scenario.Init() above
  116. Application.Shutdown ();
  117. VerifyObjectsWereDisposed ();
  118. }
  119. StopConfigFileWatcher ();
  120. VerifyObjectsWereDisposed ();
  121. }
  122. private static void StopConfigFileWatcher ()
  123. {
  124. _currentDirWatcher.EnableRaisingEvents = false;
  125. _currentDirWatcher.Changed -= ConfigFileChanged;
  126. _currentDirWatcher.Created -= ConfigFileChanged;
  127. _homeDirWatcher.EnableRaisingEvents = false;
  128. _homeDirWatcher.Changed -= ConfigFileChanged;
  129. _homeDirWatcher.Created -= ConfigFileChanged;
  130. }
  131. private static void StartConfigFileWatcher ()
  132. {
  133. // Setup a file system watcher for `./.tui/`
  134. _currentDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  135. var f = new FileInfo (Assembly.GetExecutingAssembly ().Location);
  136. var tuiDir = Path.Combine (f.Directory.FullName, ".tui");
  137. if (!Directory.Exists (tuiDir)) {
  138. Directory.CreateDirectory (tuiDir);
  139. }
  140. _currentDirWatcher.Path = tuiDir;
  141. _currentDirWatcher.Filter = "*config.json";
  142. // Setup a file system watcher for `~/.tui/`
  143. _homeDirWatcher.NotifyFilter = NotifyFilters.LastWrite;
  144. f = new FileInfo (Environment.GetFolderPath (Environment.SpecialFolder.UserProfile));
  145. tuiDir = Path.Combine (f.FullName, ".tui");
  146. if (!Directory.Exists (tuiDir)) {
  147. Directory.CreateDirectory (tuiDir);
  148. }
  149. _homeDirWatcher.Path = tuiDir;
  150. _homeDirWatcher.Filter = "*config.json";
  151. _currentDirWatcher.Changed += ConfigFileChanged;
  152. //_currentDirWatcher.Created += ConfigFileChanged;
  153. _currentDirWatcher.EnableRaisingEvents = true;
  154. _homeDirWatcher.Changed += ConfigFileChanged;
  155. //_homeDirWatcher.Created += ConfigFileChanged;
  156. _homeDirWatcher.EnableRaisingEvents = true;
  157. }
  158. private static void ConfigFileChanged (object sender, FileSystemEventArgs e)
  159. {
  160. if (Application.Top == null) {
  161. return;
  162. }
  163. // TOOD: THis is a hack. Figure out how to ensure that the file is fully written before reading it.
  164. Thread.Sleep (500);
  165. ConfigurationManager.Load ();
  166. ConfigurationManager.Apply ();
  167. }
  168. /// <summary>
  169. /// Shows the UI Catalog selection UI. When the user selects a Scenario to run, the
  170. /// UI Catalog main app UI is killed and the Scenario is run as though it were Application.Top.
  171. /// When the Scenario exits, this function exits.
  172. /// </summary>
  173. /// <returns></returns>
  174. static Scenario RunUICatalogTopLevel ()
  175. {
  176. Application.UseSystemConsole = _useSystemConsole;
  177. // Run UI Catalog UI. When it exits, if _selectedScenario is != null then
  178. // a Scenario was selected. Otherwise, the user wants to quit UI Catalog.
  179. Application.Init ();
  180. if (_cachedTheme is null) {
  181. _cachedTheme = ConfigurationManager.Themes.Theme;
  182. } else {
  183. ConfigurationManager.Themes.Theme = _cachedTheme;
  184. ConfigurationManager.Apply ();
  185. }
  186. //Application.EnableConsoleScrolling = _enableConsoleScrolling;
  187. Application.Run<UICatalogTopLevel> ();
  188. Application.Shutdown ();
  189. return _selectedScenario;
  190. }
  191. static List<Scenario> _scenarios;
  192. static List<string> _categories;
  193. static int _nameColumnWidth;
  194. // When a scenario is run, the main app is killed. These items
  195. // are therefore cached so that when the scenario exits the
  196. // main app UI can be restored to previous state
  197. static int _cachedScenarioIndex = 0;
  198. static int _cachedCategoryIndex = 0;
  199. static string? _cachedTheme;
  200. static StringBuilder _aboutMessage;
  201. // If set, holds the scenario the user selected
  202. static Scenario _selectedScenario = null;
  203. static bool _useSystemConsole = false;
  204. static ConsoleDriver.DiagnosticFlags _diagnosticFlags;
  205. static bool _enableConsoleScrolling = false;
  206. static bool _isFirstRunning = true;
  207. static string _topLevelColorScheme;
  208. static MenuItem [] _themeMenuItems;
  209. static MenuBarItem _themeMenuBarItem;
  210. /// <summary>
  211. /// This is the main UI Catalog app view. It is run fresh when the app loads (if a Scenario has not been passed on
  212. /// the command line) and each time a Scenario ends.
  213. /// </summary>
  214. public class UICatalogTopLevel : Toplevel {
  215. public MenuItem miIsMouseDisabled;
  216. public MenuItem miEnableConsoleScrolling;
  217. public TileView ContentPane;
  218. public ListView CategoryListView;
  219. public ListView ScenarioListView;
  220. public StatusItem Capslock;
  221. public StatusItem Numlock;
  222. public StatusItem Scrolllock;
  223. public StatusItem DriverName;
  224. public StatusItem OS;
  225. public UICatalogTopLevel ()
  226. {
  227. _themeMenuItems = CreateThemeMenuItems ();
  228. _themeMenuBarItem = new MenuBarItem ("_Themes", _themeMenuItems);
  229. MenuBar = new MenuBar (new MenuBarItem [] {
  230. new MenuBarItem ("_File", new MenuItem [] {
  231. new MenuItem ("_Quit", "Quit UI Catalog", () => RequestStop(), null, null)
  232. }),
  233. _themeMenuBarItem,
  234. new MenuBarItem ("Diag_nostics", CreateDiagnosticMenuItems()),
  235. new MenuBarItem ("_Help", new MenuItem [] {
  236. new MenuItem ("_gui.cs API Overview", "", () => OpenUrl ("https://gui-cs.github.io/Terminal.Gui/articles/overview.html"), null, null, Key.F1),
  237. new MenuItem ("gui.cs _README", "", () => OpenUrl ("https://github.com/gui-cs/Terminal.Gui"), null, null, Key.F2),
  238. new MenuItem ("_About...",
  239. "About UI Catalog", () => MessageBox.Query ("About UI Catalog", _aboutMessage.ToString(), 0, null, false, "_Ok"), null, null, Key.CtrlMask | Key.A),
  240. }),
  241. });
  242. Capslock = new StatusItem (Key.CharMask, "Caps", null);
  243. Numlock = new StatusItem (Key.CharMask, "Num", null);
  244. Scrolllock = new StatusItem (Key.CharMask, "Scroll", null);
  245. DriverName = new StatusItem (Key.CharMask, "Driver:", null);
  246. OS = new StatusItem (Key.CharMask, "OS:", null);
  247. StatusBar = new StatusBar () {
  248. Visible = UICatalogApp.ShowStatusBar
  249. };
  250. StatusBar.Items = new StatusItem [] {
  251. new StatusItem(Application.QuitKey, $"~{Application.QuitKey} to quit", () => {
  252. if (_selectedScenario is null){
  253. // This causes GetScenarioToRun to return null
  254. _selectedScenario = null;
  255. RequestStop();
  256. } else {
  257. _selectedScenario.RequestStop();
  258. }
  259. }),
  260. new StatusItem(Key.F10, "~F10~ Status Bar", () => {
  261. StatusBar.Visible = !StatusBar.Visible;
  262. ContentPane.Height = Dim.Fill(StatusBar.Visible ? 1 : 0);
  263. LayoutSubviews();
  264. SetSubViewNeedsDisplay();
  265. }),
  266. DriverName,
  267. OS
  268. };
  269. ContentPane = new TileView () {
  270. X = 0,
  271. Y = 1, // for menu
  272. Width = Dim.Fill (),
  273. Height = Dim.Fill (1),
  274. CanFocus = true,
  275. Shortcut = Key.CtrlMask | Key.C,
  276. };
  277. ContentPane.Border.BorderStyle = BorderStyle.Single;
  278. ContentPane.SetSplitterPos (0, 25);
  279. ContentPane.ShortcutAction = () => ContentPane.SetFocus ();
  280. CategoryListView = new ListView (_categories) {
  281. X = 0,
  282. Y = 0,
  283. Width = Dim.Fill (0),
  284. Height = Dim.Fill (0),
  285. AllowsMarking = false,
  286. CanFocus = true,
  287. };
  288. CategoryListView.OpenSelectedItem += (s,a) => {
  289. ScenarioListView.SetFocus ();
  290. };
  291. CategoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
  292. ContentPane.Tiles.ElementAt (0).Title = "Categories";
  293. ContentPane.Tiles.ElementAt (0).MinSize = 2;
  294. ContentPane.Tiles.ElementAt (0).ContentView.Add (CategoryListView);
  295. ScenarioListView = new ListView () {
  296. X = 0,
  297. Y = 0,
  298. Width = Dim.Fill (0),
  299. Height = Dim.Fill (0),
  300. AllowsMarking = false,
  301. CanFocus = true,
  302. };
  303. ScenarioListView.OpenSelectedItem += ScenarioListView_OpenSelectedItem;
  304. ContentPane.Tiles.ElementAt (1).Title = "Scenarios";
  305. ContentPane.Tiles.ElementAt (1).ContentView.Add (ScenarioListView);
  306. ContentPane.Tiles.ElementAt (1).MinSize = 2;
  307. KeyDown += KeyDownHandler;
  308. Add (MenuBar);
  309. Add (ContentPane);
  310. Add (StatusBar);
  311. Loaded += LoadedHandler;
  312. Unloaded += UnloadedHandler;
  313. // Restore previous selections
  314. CategoryListView.SelectedItem = _cachedCategoryIndex;
  315. ScenarioListView.SelectedItem = _cachedScenarioIndex;
  316. ConfigurationManager.Applied += ConfigAppliedHandler;
  317. }
  318. void LoadedHandler (object sender, EventArgs args)
  319. {
  320. ConfigChanged ();
  321. miIsMouseDisabled.Checked = Application.IsMouseDisabled;
  322. miEnableConsoleScrolling.Checked = Application.EnableConsoleScrolling;
  323. DriverName.Title = $"Driver: {Driver.GetType ().Name}";
  324. OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem} {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion}";
  325. if (_selectedScenario != null) {
  326. _selectedScenario = null;
  327. _isFirstRunning = false;
  328. }
  329. if (!_isFirstRunning) {
  330. ScenarioListView.SetFocus ();
  331. }
  332. StatusBar.VisibleChanged += (s, e) => {
  333. UICatalogApp.ShowStatusBar = StatusBar.Visible;
  334. var height = (StatusBar.Visible ? 1 : 0);// + (MenuBar.Visible ? 1 : 0);
  335. ContentPane.Height = Dim.Fill (height);
  336. LayoutSubviews ();
  337. SetSubViewNeedsDisplay ();
  338. };
  339. Loaded -= LoadedHandler;
  340. }
  341. private void UnloadedHandler (object sender, EventArgs args)
  342. {
  343. ConfigurationManager.Applied -= ConfigAppliedHandler;
  344. Unloaded -= UnloadedHandler;
  345. }
  346. void ConfigAppliedHandler (object sender, ConfigurationManagerEventArgs a)
  347. {
  348. ConfigChanged ();
  349. }
  350. /// <summary>
  351. /// Launches the selected scenario, setting the global _selectedScenario
  352. /// </summary>
  353. /// <param name="e"></param>
  354. void ScenarioListView_OpenSelectedItem (object sender, EventArgs e)
  355. {
  356. if (_selectedScenario is null) {
  357. // Save selected item state
  358. _cachedCategoryIndex = CategoryListView.SelectedItem;
  359. _cachedScenarioIndex = ScenarioListView.SelectedItem;
  360. // Create new instance of scenario (even though Scenarios contains instances)
  361. _selectedScenario = (Scenario)Activator.CreateInstance (ScenarioListView.Source.ToList () [ScenarioListView.SelectedItem].GetType ());
  362. // Tell the main app to stop
  363. Application.RequestStop ();
  364. }
  365. }
  366. List<MenuItem []> CreateDiagnosticMenuItems ()
  367. {
  368. List<MenuItem []> menuItems = new List<MenuItem []> ();
  369. menuItems.Add (CreateDiagnosticFlagsMenuItems ());
  370. menuItems.Add (new MenuItem [] { null });
  371. menuItems.Add (CreateEnableConsoleScrollingMenuItems ());
  372. menuItems.Add (CreateDisabledEnabledMouseItems ());
  373. menuItems.Add (CreateKeybindingsMenuItems ());
  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 => (bool)m.Checked).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.Border.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. }