UICatalog.cs 25 KB

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