UICatalog.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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, 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. Id = "ContentPane",
  271. X = 0,
  272. Y = 1, // for menu
  273. Width = Dim.Fill (),
  274. Height = Dim.Fill (1),
  275. CanFocus = true,
  276. Shortcut = Key.CtrlMask | Key.C,
  277. };
  278. ContentPane.BorderStyle = BorderStyle.Single;
  279. ContentPane.SetSplitterPos (0, 25);
  280. ContentPane.ShortcutAction = () => ContentPane.SetFocus ();
  281. CategoryListView = new ListView (_categories) {
  282. X = 0,
  283. Y = 0,
  284. Width = Dim.Fill (0),
  285. Height = Dim.Fill (0),
  286. AllowsMarking = false,
  287. CanFocus = true,
  288. };
  289. CategoryListView.OpenSelectedItem += (s,a) => {
  290. ScenarioListView.SetFocus ();
  291. };
  292. CategoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
  293. ContentPane.Tiles.ElementAt (0).Title = "Categories";
  294. ContentPane.Tiles.ElementAt (0).MinSize = 2;
  295. ContentPane.Tiles.ElementAt (0).ContentView.Add (CategoryListView);
  296. ScenarioListView = new ListView () {
  297. X = 0,
  298. Y = 0,
  299. Width = Dim.Fill (0),
  300. Height = Dim.Fill (0),
  301. AllowsMarking = false,
  302. CanFocus = true,
  303. };
  304. ScenarioListView.OpenSelectedItem += ScenarioListView_OpenSelectedItem;
  305. ContentPane.Tiles.ElementAt (1).Title = "Scenarios";
  306. ContentPane.Tiles.ElementAt (1).ContentView.Add (ScenarioListView);
  307. ContentPane.Tiles.ElementAt (1).MinSize = 2;
  308. KeyDown += KeyDownHandler;
  309. Add (MenuBar);
  310. Add (ContentPane);
  311. Add (StatusBar);
  312. Loaded += LoadedHandler;
  313. Unloaded += UnloadedHandler;
  314. // Restore previous selections
  315. CategoryListView.SelectedItem = _cachedCategoryIndex;
  316. ScenarioListView.SelectedItem = _cachedScenarioIndex;
  317. ConfigurationManager.Applied += ConfigAppliedHandler;
  318. }
  319. void LoadedHandler (object sender, EventArgs args)
  320. {
  321. ConfigChanged ();
  322. miIsMouseDisabled.Checked = Application.IsMouseDisabled;
  323. miEnableConsoleScrolling.Checked = Application.EnableConsoleScrolling;
  324. DriverName.Title = $"Driver: {Driver.GetType ().Name}";
  325. OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem} {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion}";
  326. if (_selectedScenario != null) {
  327. _selectedScenario = null;
  328. _isFirstRunning = false;
  329. }
  330. if (!_isFirstRunning) {
  331. ScenarioListView.SetFocus ();
  332. }
  333. StatusBar.VisibleChanged += (s, e) => {
  334. UICatalogApp.ShowStatusBar = StatusBar.Visible;
  335. var height = (StatusBar.Visible ? 1 : 0);// + (MenuBar.Visible ? 1 : 0);
  336. ContentPane.Height = Dim.Fill (height);
  337. LayoutSubviews ();
  338. SetSubViewNeedsDisplay ();
  339. };
  340. Loaded -= LoadedHandler;
  341. }
  342. private void UnloadedHandler (object sender, EventArgs args)
  343. {
  344. ConfigurationManager.Applied -= ConfigAppliedHandler;
  345. Unloaded -= UnloadedHandler;
  346. }
  347. void ConfigAppliedHandler (object sender, ConfigurationManagerEventArgs a)
  348. {
  349. ConfigChanged ();
  350. }
  351. /// <summary>
  352. /// Launches the selected scenario, setting the global _selectedScenario
  353. /// </summary>
  354. /// <param name="e"></param>
  355. void ScenarioListView_OpenSelectedItem (object sender, EventArgs e)
  356. {
  357. if (_selectedScenario is null) {
  358. // Save selected item state
  359. _cachedCategoryIndex = CategoryListView.SelectedItem;
  360. _cachedScenarioIndex = ScenarioListView.SelectedItem;
  361. // Create new instance of scenario (even though Scenarios contains instances)
  362. _selectedScenario = (Scenario)Activator.CreateInstance (ScenarioListView.Source.ToList () [ScenarioListView.SelectedItem].GetType ());
  363. // Tell the main app to stop
  364. Application.RequestStop ();
  365. }
  366. }
  367. List<MenuItem []> CreateDiagnosticMenuItems ()
  368. {
  369. List<MenuItem []> menuItems = new List<MenuItem []> ();
  370. menuItems.Add (CreateDiagnosticFlagsMenuItems ());
  371. menuItems.Add (new MenuItem [] { null });
  372. menuItems.Add (CreateEnableConsoleScrollingMenuItems ());
  373. menuItems.Add (CreateDisabledEnabledMouseItems ());
  374. menuItems.Add (CreateKeybindingsMenuItems ());
  375. return menuItems;
  376. }
  377. MenuItem [] CreateDisabledEnabledMouseItems ()
  378. {
  379. List<MenuItem> menuItems = new List<MenuItem> ();
  380. miIsMouseDisabled = new MenuItem {
  381. Title = "_Disable Mouse"
  382. };
  383. miIsMouseDisabled.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miIsMouseDisabled.Title.ToString ().Substring (1, 1) [0];
  384. miIsMouseDisabled.CheckType |= MenuItemCheckStyle.Checked;
  385. miIsMouseDisabled.Action += () => {
  386. miIsMouseDisabled.Checked = Application.IsMouseDisabled = (bool)!miIsMouseDisabled.Checked;
  387. };
  388. menuItems.Add (miIsMouseDisabled);
  389. return menuItems.ToArray ();
  390. }
  391. MenuItem [] CreateKeybindingsMenuItems ()
  392. {
  393. List<MenuItem> menuItems = new List<MenuItem> ();
  394. var item = new MenuItem {
  395. Title = "_Key Bindings",
  396. Help = "Change which keys do what"
  397. };
  398. item.Action += () => {
  399. var dlg = new KeyBindingsDialog ();
  400. Application.Run (dlg);
  401. };
  402. menuItems.Add (null);
  403. menuItems.Add (item);
  404. return menuItems.ToArray ();
  405. }
  406. MenuItem [] CreateEnableConsoleScrollingMenuItems ()
  407. {
  408. List<MenuItem> menuItems = new List<MenuItem> ();
  409. miEnableConsoleScrolling = new MenuItem ();
  410. miEnableConsoleScrolling.Title = "_Enable Console Scrolling";
  411. miEnableConsoleScrolling.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miEnableConsoleScrolling.Title.ToString ().Substring (1, 1) [0];
  412. miEnableConsoleScrolling.CheckType |= MenuItemCheckStyle.Checked;
  413. miEnableConsoleScrolling.Action += () => {
  414. miEnableConsoleScrolling.Checked = !miEnableConsoleScrolling.Checked;
  415. Application.EnableConsoleScrolling = (bool)miEnableConsoleScrolling.Checked;
  416. };
  417. menuItems.Add (miEnableConsoleScrolling);
  418. return menuItems.ToArray ();
  419. }
  420. MenuItem [] CreateDiagnosticFlagsMenuItems ()
  421. {
  422. const string OFF = "Diagnostics: _Off";
  423. const string FRAME_RULER = "Diagnostics: Frame _Ruler";
  424. const string FRAME_PADDING = "Diagnostics: _Frame Padding";
  425. var index = 0;
  426. List<MenuItem> menuItems = new List<MenuItem> ();
  427. foreach (Enum diag in Enum.GetValues (_diagnosticFlags.GetType ())) {
  428. var item = new MenuItem {
  429. Title = GetDiagnosticsTitle (diag),
  430. Shortcut = Key.AltMask + index.ToString () [0]
  431. };
  432. index++;
  433. item.CheckType |= MenuItemCheckStyle.Checked;
  434. if (GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off) == item.Title) {
  435. item.Checked = (_diagnosticFlags & (ConsoleDriver.DiagnosticFlags.FramePadding
  436. | ConsoleDriver.DiagnosticFlags.FrameRuler)) == 0;
  437. } else {
  438. item.Checked = _diagnosticFlags.HasFlag (diag);
  439. }
  440. item.Action += () => {
  441. var t = GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off);
  442. if (item.Title == t && item.Checked == false) {
  443. _diagnosticFlags &= ~(ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  444. item.Checked = true;
  445. } else if (item.Title == t && item.Checked == true) {
  446. _diagnosticFlags |= (ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  447. item.Checked = false;
  448. } else {
  449. var f = GetDiagnosticsEnumValue (item.Title);
  450. if (_diagnosticFlags.HasFlag (f)) {
  451. SetDiagnosticsFlag (f, false);
  452. } else {
  453. SetDiagnosticsFlag (f, true);
  454. }
  455. }
  456. foreach (var menuItem in menuItems) {
  457. if (menuItem.Title == t) {
  458. menuItem.Checked = !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FrameRuler)
  459. && !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FramePadding);
  460. } else if (menuItem.Title != t) {
  461. menuItem.Checked = _diagnosticFlags.HasFlag (GetDiagnosticsEnumValue (menuItem.Title));
  462. }
  463. }
  464. ConsoleDriver.Diagnostics = _diagnosticFlags;
  465. Application.Top.SetNeedsDisplay ();
  466. };
  467. menuItems.Add (item);
  468. }
  469. return menuItems.ToArray ();
  470. string GetDiagnosticsTitle (Enum diag)
  471. {
  472. return Enum.GetName (_diagnosticFlags.GetType (), diag) switch {
  473. "Off" => OFF,
  474. "FrameRuler" => FRAME_RULER,
  475. "FramePadding" => FRAME_PADDING,
  476. _ => "",
  477. };
  478. }
  479. Enum GetDiagnosticsEnumValue (ustring title)
  480. {
  481. return title.ToString () switch {
  482. FRAME_RULER => ConsoleDriver.DiagnosticFlags.FrameRuler,
  483. FRAME_PADDING => ConsoleDriver.DiagnosticFlags.FramePadding,
  484. _ => null,
  485. };
  486. }
  487. void SetDiagnosticsFlag (Enum diag, bool add)
  488. {
  489. switch (diag) {
  490. case ConsoleDriver.DiagnosticFlags.FrameRuler:
  491. if (add) {
  492. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FrameRuler;
  493. } else {
  494. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FrameRuler;
  495. }
  496. break;
  497. case ConsoleDriver.DiagnosticFlags.FramePadding:
  498. if (add) {
  499. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FramePadding;
  500. } else {
  501. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FramePadding;
  502. }
  503. break;
  504. default:
  505. _diagnosticFlags = default;
  506. break;
  507. }
  508. }
  509. }
  510. public MenuItem [] CreateThemeMenuItems ()
  511. {
  512. List<MenuItem> menuItems = new List<MenuItem> ();
  513. foreach (var theme in ConfigurationManager.Themes) {
  514. var item = new MenuItem {
  515. Title = theme.Key,
  516. Shortcut = Key.AltMask + theme.Key [0]
  517. };
  518. item.CheckType |= MenuItemCheckStyle.Checked;
  519. item.Checked = theme.Key == _cachedTheme; // ConfigurationManager.Themes.Theme;
  520. item.Action += () => {
  521. ConfigurationManager.Themes.Theme = _cachedTheme = theme.Key;
  522. ConfigurationManager.Apply ();
  523. };
  524. menuItems.Add (item);
  525. }
  526. var schemeMenuItems = new List<MenuItem> ();
  527. foreach (var sc in Colors.ColorSchemes) {
  528. var item = new MenuItem {
  529. Title = $"_{sc.Key}",
  530. Data = sc.Key,
  531. Shortcut = Key.AltMask | (Key)sc.Key [..1] [0]
  532. };
  533. item.CheckType |= MenuItemCheckStyle.Radio;
  534. item.Checked = sc.Key == _topLevelColorScheme;
  535. item.Action += () => {
  536. _topLevelColorScheme = (string)item.Data;
  537. foreach (var schemeMenuItem in schemeMenuItems) {
  538. schemeMenuItem.Checked = (string)schemeMenuItem.Data == _topLevelColorScheme;
  539. }
  540. ColorScheme = Colors.ColorSchemes [_topLevelColorScheme];
  541. Application.Top.SetNeedsDisplay ();
  542. };
  543. schemeMenuItems.Add (item);
  544. }
  545. menuItems.Add (null);
  546. var mbi = new MenuBarItem ("_Color Scheme for Application.Top", schemeMenuItems.ToArray ());
  547. menuItems.Add (mbi);
  548. return menuItems.ToArray ();
  549. }
  550. public void ConfigChanged ()
  551. {
  552. if (_topLevelColorScheme == null || !Colors.ColorSchemes.ContainsKey (_topLevelColorScheme)) {
  553. _topLevelColorScheme = "Base";
  554. }
  555. _themeMenuItems = ((UICatalogTopLevel)Application.Top).CreateThemeMenuItems ();
  556. _themeMenuBarItem.Children = _themeMenuItems;
  557. var checkedThemeMenu = _themeMenuItems.Where (m => (bool)m.Checked).FirstOrDefault ();
  558. if (checkedThemeMenu != null) {
  559. checkedThemeMenu.Checked = false;
  560. }
  561. checkedThemeMenu = _themeMenuItems.Where (m => m != null && m.Title == ConfigurationManager.Themes.Theme).FirstOrDefault ();
  562. if (checkedThemeMenu != null) {
  563. ConfigurationManager.Themes.Theme = checkedThemeMenu.Title.ToString ();
  564. checkedThemeMenu.Checked = true;
  565. }
  566. var schemeMenuItems = ((MenuBarItem)_themeMenuItems.Where (i => i is MenuBarItem).FirstOrDefault ()).Children;
  567. foreach (var schemeMenuItem in schemeMenuItems) {
  568. schemeMenuItem.Checked = (string)schemeMenuItem.Data == _topLevelColorScheme;
  569. }
  570. ColorScheme = Colors.ColorSchemes [_topLevelColorScheme];
  571. ContentPane.BorderStyle = FrameView.DefaultBorderStyle;
  572. MenuBar.Menus [0].Children [0].Shortcut = Application.QuitKey;
  573. StatusBar.Items [0].Shortcut = Application.QuitKey;
  574. StatusBar.Items [0].Title = $"~{Application.QuitKey} to quit";
  575. miIsMouseDisabled.Checked = Application.IsMouseDisabled;
  576. miEnableConsoleScrolling.Checked = Application.EnableConsoleScrolling;
  577. var height = (UICatalogApp.ShowStatusBar ? 1 : 0);// + (MenuBar.Visible ? 1 : 0);
  578. ContentPane.Height = Dim.Fill (height);
  579. StatusBar.Visible = UICatalogApp.ShowStatusBar;
  580. Application.Top.SetNeedsDisplay ();
  581. }
  582. void KeyDownHandler (object sender, KeyEventEventArgs a)
  583. {
  584. if (a.KeyEvent.IsCapslock) {
  585. Capslock.Title = "Caps: On";
  586. StatusBar.SetNeedsDisplay ();
  587. } else {
  588. Capslock.Title = "Caps: Off";
  589. StatusBar.SetNeedsDisplay ();
  590. }
  591. if (a.KeyEvent.IsNumlock) {
  592. Numlock.Title = "Num: On";
  593. StatusBar.SetNeedsDisplay ();
  594. } else {
  595. Numlock.Title = "Num: Off";
  596. StatusBar.SetNeedsDisplay ();
  597. }
  598. if (a.KeyEvent.IsScrolllock) {
  599. Scrolllock.Title = "Scroll: On";
  600. StatusBar.SetNeedsDisplay ();
  601. } else {
  602. Scrolllock.Title = "Scroll: Off";
  603. StatusBar.SetNeedsDisplay ();
  604. }
  605. }
  606. void CategoryListView_SelectedChanged (object sender, ListViewItemEventArgs e)
  607. {
  608. var item = _categories [e.Item];
  609. List<Scenario> newlist;
  610. if (e.Item == 0) {
  611. // First category is "All"
  612. newlist = _scenarios;
  613. } else {
  614. newlist = _scenarios.Where (s => s.GetCategories ().Contains (item)).ToList ();
  615. }
  616. ScenarioListView.SetSource (newlist.ToList ());
  617. }
  618. }
  619. static void VerifyObjectsWereDisposed ()
  620. {
  621. #if DEBUG_IDISPOSABLE
  622. // Validate there are no outstanding Responder-based instances
  623. // after a scenario was selected to run. This proves the main UI Catalog
  624. // 'app' closed cleanly.
  625. foreach (var inst in Responder.Instances) {
  626. Debug.Assert (inst.WasDisposed);
  627. }
  628. Responder.Instances.Clear ();
  629. // Validate there are no outstanding Application.RunState-based instances
  630. // after a scenario was selected to run. This proves the main UI Catalog
  631. // 'app' closed cleanly.
  632. foreach (var inst in Application.RunState.Instances) {
  633. Debug.Assert (inst.WasDisposed);
  634. }
  635. Application.RunState.Instances.Clear ();
  636. #endif
  637. }
  638. static void OpenUrl (string url)
  639. {
  640. try {
  641. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  642. url = url.Replace ("&", "^&");
  643. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  644. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  645. using var process = new Process {
  646. StartInfo = new ProcessStartInfo {
  647. FileName = "xdg-open",
  648. Arguments = url,
  649. RedirectStandardError = true,
  650. RedirectStandardOutput = true,
  651. CreateNoWindow = true,
  652. UseShellExecute = false
  653. }
  654. };
  655. process.Start ();
  656. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  657. Process.Start ("open", url);
  658. }
  659. } catch {
  660. throw;
  661. }
  662. }
  663. }
  664. }