UICatalogRunnable.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. using System.Collections.ObjectModel;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Text.Json.Serialization;
  6. using Microsoft.Extensions.Logging;
  7. using RuntimeEnvironment = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment;
  8. #nullable enable
  9. namespace UICatalog;
  10. /// <summary>
  11. /// This is the main UI Catalog app view. It is run fresh when the app loads (if a Scenario has not been passed on
  12. /// the command line) and each time a Scenario ends.
  13. /// </summary>
  14. public class UICatalogRunnable : Runnable
  15. {
  16. // When a scenario is run, the main app is killed. The static
  17. // members are cached so that when the scenario exits the
  18. // main app UI can be restored to previous state
  19. // Note, we used to pass this to scenarios that run, but it just added complexity
  20. // So that was removed. But we still have this here to demonstrate how changing
  21. // the scheme works.
  22. public static string? CachedRunnableScheme { get; set; }
  23. // Diagnostics
  24. private static ViewDiagnosticFlags _diagnosticFlags;
  25. public UICatalogRunnable ()
  26. {
  27. _diagnosticFlags = Diagnostics;
  28. _menuBar = CreateMenuBar ();
  29. _statusBar = CreateStatusBar ();
  30. _categoryList = CreateCategoryList ();
  31. _scenarioList = CreateScenarioList ();
  32. Add (_menuBar, _categoryList, _scenarioList, _statusBar);
  33. IsModalChanged += IsModalChangedHandler;
  34. IsRunningChanged += IsRunningChangedHandler;
  35. // Restore previous selections
  36. if (_categoryList.Source?.Count > 0)
  37. {
  38. _categoryList.SelectedItem = _cachedCategoryIndex ?? 0;
  39. }
  40. else
  41. {
  42. _categoryList.SelectedItem = null;
  43. }
  44. _scenarioList.SelectedRow = _cachedScenarioIndex;
  45. SchemeName = CachedRunnableScheme = SchemeManager.SchemesToSchemeName (Schemes.Base);
  46. ConfigurationManager.Applied += ConfigAppliedHandler;
  47. }
  48. private static bool _isFirstRunning = true;
  49. private void IsModalChangedHandler (object? sender, EventArgs<bool> args)
  50. {
  51. if (!args.Value)
  52. {
  53. return;
  54. }
  55. if (_disableMouseCb is { })
  56. {
  57. _disableMouseCb.CheckedState = Application.IsMouseDisabled ? CheckState.Checked : CheckState.UnChecked;
  58. }
  59. if (_shVersion is { })
  60. {
  61. _shVersion.Title = $"{RuntimeEnvironment.OperatingSystem} {RuntimeEnvironment.OperatingSystemVersion}, {Application.Driver!.GetVersionInfo ()}";
  62. }
  63. if (CachedSelectedScenario != null)
  64. {
  65. CachedSelectedScenario = null;
  66. _isFirstRunning = false;
  67. }
  68. if (!_isFirstRunning)
  69. {
  70. _scenarioList.SetFocus ();
  71. }
  72. if (_statusBar is { })
  73. {
  74. _statusBar.VisibleChanged += (s, e) => { ShowStatusBar = _statusBar.Visible; };
  75. }
  76. IsModalChanged -= IsModalChangedHandler;
  77. _categoryList!.EnsureSelectedItemVisible ();
  78. _scenarioList.EnsureSelectedCellIsVisible ();
  79. }
  80. private void IsRunningChangedHandler (object? sender, EventArgs<bool> args)
  81. {
  82. if (!args.Value)
  83. {
  84. ConfigurationManager.Applied -= ConfigAppliedHandler;
  85. IsRunningChanged -= IsRunningChangedHandler;
  86. }
  87. }
  88. #region MenuBar
  89. private readonly MenuBar? _menuBar;
  90. private CheckBox? _force16ColorsMenuItemCb;
  91. private OptionSelector? _themesSelector;
  92. private OptionSelector? _topSchemesSelector;
  93. private OptionSelector? _logLevelSelector;
  94. private FlagSelector<ViewDiagnosticFlags>? _diagnosticFlagsSelector;
  95. private CheckBox? _disableMouseCb;
  96. private MenuBar CreateMenuBar ()
  97. {
  98. MenuBar menuBar = new (
  99. [
  100. new (
  101. "_File",
  102. [
  103. new MenuItem ()
  104. {
  105. Title ="_Quit",
  106. HelpText = "Quit UI Catalog",
  107. Key = Application.QuitKey,
  108. // By not specifying TargetView the Key Binding will be Application-level
  109. Command = Command.Quit
  110. }
  111. ]),
  112. new ("_Themes", CreateThemeMenuItems ()),
  113. new ("Diag_nostics", CreateDiagnosticMenuItems ()),
  114. new ("_Logging", CreateLoggingMenuItems ()),
  115. new (
  116. "_Help",
  117. [
  118. new MenuItem (
  119. "_Documentation",
  120. "API docs",
  121. () => OpenUrl ("https://gui-cs.github.io/Terminal.Gui"),
  122. Key.F1
  123. ),
  124. new MenuItem (
  125. "_README",
  126. "Project readme",
  127. () => OpenUrl ("https://github.com/gui-cs/Terminal.Gui"),
  128. Key.F2
  129. ),
  130. new MenuItem (
  131. "_About...",
  132. "About UI Catalog",
  133. () => MessageBox.Query (
  134. App,
  135. "",
  136. GetAboutBoxMessage (),
  137. wrapMessage: false,
  138. buttons: "_Ok"
  139. ),
  140. Key.A.WithCtrl
  141. )
  142. ])
  143. ])
  144. {
  145. Title = "menuBar",
  146. Id = "menuBar"
  147. };
  148. return menuBar;
  149. View [] CreateThemeMenuItems ()
  150. {
  151. List<View> menuItems = [];
  152. _force16ColorsMenuItemCb = new ()
  153. {
  154. Title = "Force _16 Colors",
  155. CheckedState = Application.Driver!.Force16Colors ? CheckState.Checked : CheckState.UnChecked,
  156. // Best practice for CheckBoxes in menus is to disable focus and highlight states
  157. CanFocus = false,
  158. HighlightStates = MouseState.None
  159. };
  160. _force16ColorsMenuItemCb.CheckedStateChanging += (sender, args) =>
  161. {
  162. if (Application.Driver!.Force16Colors
  163. && args.Result == CheckState.UnChecked
  164. && !Application.Driver!.SupportsTrueColor)
  165. {
  166. args.Handled = true;
  167. }
  168. };
  169. _force16ColorsMenuItemCb.CheckedStateChanged += (sender, args) =>
  170. {
  171. Application.Driver!.Force16Colors = args.Value == CheckState.Checked;
  172. _force16ColorsShortcutCb!.CheckedState = args.Value;
  173. SetNeedsDraw ();
  174. };
  175. menuItems.Add (
  176. new MenuItem
  177. {
  178. CommandView = _force16ColorsMenuItemCb
  179. });
  180. menuItems.Add (new Line ());
  181. if (ConfigurationManager.IsEnabled)
  182. {
  183. _themesSelector = new ()
  184. {
  185. // HighlightStates = MouseState.In,
  186. CanFocus = true,
  187. // InvertFocusAttribute = true
  188. };
  189. _themesSelector.ValueChanged += (_, args) =>
  190. {
  191. if (args.Value is null)
  192. {
  193. return;
  194. }
  195. ThemeManager.Theme = ThemeManager.GetThemeNames () [(int)args.Value];
  196. };
  197. var menuItem = new MenuItem
  198. {
  199. CommandView = _themesSelector,
  200. HelpText = "Cycle Through Themes",
  201. Key = Key.T.WithCtrl
  202. };
  203. menuItems.Add (menuItem);
  204. menuItems.Add (new Line ());
  205. _topSchemesSelector = new ()
  206. {
  207. // HighlightStates = MouseState.In,
  208. };
  209. _topSchemesSelector.ValueChanged += (_, args) =>
  210. {
  211. if (args.Value is null)
  212. {
  213. return;
  214. }
  215. CachedRunnableScheme = SchemeManager.GetSchemesForCurrentTheme ()!.Keys.ToArray () [(int)args.Value];
  216. SchemeName = CachedRunnableScheme;
  217. SetNeedsDraw ();
  218. };
  219. menuItem = new ()
  220. {
  221. Title = "Scheme for Runnable",
  222. SubMenu = new (
  223. [
  224. new ()
  225. {
  226. CommandView = _topSchemesSelector,
  227. HelpText = "Cycle Through schemes",
  228. Key = Key.S.WithCtrl
  229. }
  230. ])
  231. };
  232. menuItems.Add (menuItem);
  233. UpdateThemesMenu ();
  234. }
  235. else
  236. {
  237. menuItems.Add (new MenuItem ()
  238. {
  239. Title = "Configuration Manager is not Enabled",
  240. Enabled = false
  241. });
  242. }
  243. return menuItems.ToArray ();
  244. }
  245. View [] CreateDiagnosticMenuItems ()
  246. {
  247. List<View> menuItems = [];
  248. _diagnosticFlagsSelector = new ()
  249. {
  250. Styles = SelectorStyles.ShowNoneFlag,
  251. CanFocus = true
  252. };
  253. _diagnosticFlagsSelector.UsedHotKeys.Add (Key.D);
  254. _diagnosticFlagsSelector.AssignHotKeys = true;
  255. _diagnosticFlagsSelector.Value = Diagnostics;
  256. _diagnosticFlagsSelector.Activating += (sender, args) =>
  257. {
  258. _diagnosticFlags = (ViewDiagnosticFlags)((int)args.Context!.Source!.Data!);// (ViewDiagnosticFlags)_diagnosticFlagsSelector.Value;
  259. Diagnostics = _diagnosticFlags;
  260. };
  261. MenuItem diagFlagMenuItem = new MenuItem ()
  262. {
  263. CommandView = _diagnosticFlagsSelector,
  264. HelpText = "View Diagnostics"
  265. };
  266. diagFlagMenuItem.Accepting += (sender, args) =>
  267. {
  268. //_diagnosticFlags = (ViewDiagnosticFlags)_diagnosticFlagsSelector.Value;
  269. //Diagnostics = _diagnosticFlags;
  270. //args.Handled = true;
  271. };
  272. menuItems.Add (diagFlagMenuItem);
  273. menuItems.Add (new Line ());
  274. _disableMouseCb = new ()
  275. {
  276. Title = "_Disable Mouse",
  277. CheckedState = Application.IsMouseDisabled ? CheckState.Checked : CheckState.UnChecked,
  278. // Best practice for CheckBoxes in menus is to disable focus and highlight states
  279. CanFocus = false,
  280. HighlightStates = MouseState.None
  281. };
  282. //_disableMouseCb.CheckedStateChanged += (_, args) => { Application.IsMouseDisabled = args.Value == CheckState.Checked; };
  283. _disableMouseCb.Activating += (sender, args) =>
  284. {
  285. Application.IsMouseDisabled = !Application.IsMouseDisabled;
  286. _disableMouseCb.CheckedState = Application.IsMouseDisabled ? CheckState.Checked : CheckState.None;
  287. };
  288. menuItems.Add (
  289. new MenuItem
  290. {
  291. CommandView = _disableMouseCb,
  292. HelpText = "Disable Mouse"
  293. });
  294. return menuItems.ToArray ();
  295. }
  296. View [] CreateLoggingMenuItems ()
  297. {
  298. List<View?> menuItems = [];
  299. LogLevel [] logLevels = Enum.GetValues<LogLevel> ();
  300. _logLevelSelector = new ()
  301. {
  302. AssignHotKeys = true,
  303. Labels = Enum.GetNames<LogLevel> (),
  304. Value = logLevels.ToList ().IndexOf (Enum.Parse<LogLevel> (UICatalog.Options.DebugLogLevel)),
  305. // HighlightStates = MouseState.In,
  306. };
  307. _logLevelSelector.ValueChanged += (_, args) =>
  308. {
  309. UICatalog.Options = UICatalog.Options with { DebugLogLevel = Enum.GetName (logLevels [args.Value!.Value])! };
  310. UICatalog.LogLevelSwitch.MinimumLevel =
  311. UICatalog.LogLevelToLogEventLevel (Enum.Parse<LogLevel> (UICatalog.Options.DebugLogLevel));
  312. };
  313. menuItems.Add (
  314. new MenuItem
  315. {
  316. CommandView = _logLevelSelector,
  317. HelpText = "Cycle Through Log Levels",
  318. Key = Key.L.WithCtrl
  319. });
  320. // add a separator
  321. menuItems.Add (new Line ());
  322. menuItems.Add (
  323. new MenuItem (
  324. "_Open Log Folder",
  325. string.Empty,
  326. () => OpenUrl (UICatalog.LOGFILE_LOCATION)
  327. ));
  328. return menuItems.ToArray ()!;
  329. }
  330. }
  331. private void UpdateThemesMenu ()
  332. {
  333. if (_themesSelector is null)
  334. {
  335. return;
  336. }
  337. _themesSelector.Value = null;
  338. _themesSelector.AssignHotKeys = true;
  339. _themesSelector.UsedHotKeys.Clear ();
  340. _themesSelector.Labels = ThemeManager.GetThemeNames ().ToArray ();
  341. _themesSelector.Value = ThemeManager.GetThemeNames ().IndexOf (ThemeManager.GetCurrentThemeName ());
  342. if (_topSchemesSelector is null)
  343. {
  344. return;
  345. }
  346. _topSchemesSelector.AssignHotKeys = true;
  347. _topSchemesSelector.UsedHotKeys.Clear ();
  348. int? selectedScheme = _topSchemesSelector.Value;
  349. _topSchemesSelector.Labels = SchemeManager.GetSchemeNames ().ToArray ();
  350. _topSchemesSelector.Value = selectedScheme;
  351. if (CachedRunnableScheme is null || !SchemeManager.GetSchemeNames ().Contains (CachedRunnableScheme))
  352. {
  353. CachedRunnableScheme = SchemeManager.SchemesToSchemeName (Schemes.Base);
  354. }
  355. int newSelectedItem = SchemeManager.GetSchemeNames ().IndexOf (CachedRunnableScheme!);
  356. // if the item is in bounds then select it
  357. if (newSelectedItem >= 0 && newSelectedItem < SchemeManager.GetSchemeNames ().Count)
  358. {
  359. _topSchemesSelector.Value = newSelectedItem;
  360. }
  361. }
  362. #endregion MenuBar
  363. #region Scenario List
  364. private readonly TableView _scenarioList;
  365. private static int _cachedScenarioIndex;
  366. public static ObservableCollection<Scenario>? CachedScenarios { get; set; }
  367. // If set, holds the scenario the user selected to run
  368. public static Scenario? CachedSelectedScenario { get; set; }
  369. private TableView CreateScenarioList ()
  370. {
  371. // Create the scenario list. The contents of the scenario list changes whenever the
  372. // Category list selection changes (to show just the scenarios that belong to the selected
  373. // category).
  374. TableView scenarioList = new ()
  375. {
  376. X = Pos.Right (_categoryList!) - 1,
  377. Y = Pos.Bottom (_menuBar!),
  378. Width = Dim.Fill (),
  379. Height = Dim.Fill (Dim.Func (v => v!.Frame.Height, _statusBar)),
  380. //AllowsMarking = false,
  381. CanFocus = true,
  382. Title = "_Scenarios",
  383. BorderStyle = _categoryList!.BorderStyle,
  384. SuperViewRendersLineCanvas = true
  385. };
  386. // TableView provides many options for table headers. For simplicity, we turn all
  387. // of these off. By enabling FullRowSelect and turning off headers, TableView looks just
  388. // like a ListView
  389. scenarioList.FullRowSelect = true;
  390. scenarioList.Style.ShowHeaders = false;
  391. scenarioList.Style.ShowHorizontalHeaderOverline = false;
  392. scenarioList.Style.ShowHorizontalHeaderUnderline = false;
  393. scenarioList.Style.ShowHorizontalBottomline = false;
  394. scenarioList.Style.ShowVerticalCellLines = false;
  395. scenarioList.Style.ShowVerticalHeaderLines = false;
  396. /* By default, TableView lays out columns at render time and only
  397. * measures y rows of data at a time. Where y is the height of the
  398. * console. This is for the following reasons:
  399. *
  400. * - Performance, when tables have a large amount of data
  401. * - Defensive, prevents a single wide cell value pushing other
  402. * columns off-screen (requiring horizontal scrolling
  403. *
  404. * In the case of UICatalog here, such an approach is overkill so
  405. * we just measure all the data ourselves and set the appropriate
  406. * max widths as ColumnStyles
  407. */
  408. int longestName = CachedScenarios!.Max (s => s.GetName ().Length);
  409. scenarioList.Style.ColumnStyles.Add (
  410. 0,
  411. new () { MaxWidth = longestName, MinWidth = longestName, MinAcceptableWidth = longestName }
  412. );
  413. scenarioList.Style.ColumnStyles.Add (1, new () { MaxWidth = 1 });
  414. scenarioList.CellActivated += ScenarioView_OpenSelectedItem;
  415. // TableView typically is a grid where nav keys are biased for moving left/right.
  416. scenarioList.KeyBindings.Remove (Key.Home);
  417. scenarioList.KeyBindings.Add (Key.Home, Command.Start);
  418. scenarioList.KeyBindings.Remove (Key.End);
  419. scenarioList.KeyBindings.Add (Key.End, Command.End);
  420. // Ideally, TableView.MultiSelect = false would turn off any keybindings for
  421. // multi-select options. But it currently does not. UI Catalog uses Ctrl-A for
  422. // a shortcut to About.
  423. scenarioList.MultiSelect = false;
  424. scenarioList.KeyBindings.Remove (Key.A.WithCtrl);
  425. return scenarioList;
  426. }
  427. /// <summary>Launches the selected scenario, setting the global _selectedScenario</summary>
  428. /// <param name="e"></param>
  429. private void ScenarioView_OpenSelectedItem (object? sender, EventArgs? e)
  430. {
  431. if (CachedSelectedScenario is null)
  432. {
  433. // Save selected item state
  434. _cachedCategoryIndex = _categoryList!.SelectedItem;
  435. _cachedScenarioIndex = _scenarioList.SelectedRow;
  436. // Create new instance of scenario (even though Scenarios contains instances)
  437. var selectedScenarioName = (string)_scenarioList.Table [_scenarioList.SelectedRow, 0];
  438. CachedSelectedScenario = (Scenario)Activator.CreateInstance (
  439. CachedScenarios!.FirstOrDefault (
  440. s => s.GetName ()
  441. == selectedScenarioName
  442. )!
  443. .GetType ()
  444. )!;
  445. // Tell the main app to stop
  446. Application.RequestStop ();
  447. }
  448. }
  449. #endregion Scenario List
  450. #region Category List
  451. private readonly ListView? _categoryList;
  452. private static int? _cachedCategoryIndex;
  453. public static ObservableCollection<string>? CachedCategories { get; set; }
  454. private ListView CreateCategoryList ()
  455. {
  456. // Create the Category list view. This list never changes.
  457. ListView categoryList = new ()
  458. {
  459. X = 0,
  460. Y = Pos.Bottom (_menuBar!),
  461. Width = Dim.Auto (),
  462. Height = Dim.Fill (Dim.Func (v => v!.Frame.Height, _statusBar)),
  463. AllowsMarking = false,
  464. CanFocus = true,
  465. Title = "_Categories",
  466. BorderStyle = LineStyle.Rounded,
  467. SuperViewRendersLineCanvas = true,
  468. Source = new ListWrapper<string> (CachedCategories)
  469. };
  470. categoryList.OpenSelectedItem += (s, a) => { _scenarioList!.SetFocus (); };
  471. categoryList.SelectedItemChanged += CategoryView_SelectedChanged;
  472. // This enables the scrollbar by causing lazy instantiation to happen
  473. categoryList.VerticalScrollBar.AutoShow = true;
  474. return categoryList;
  475. }
  476. private void CategoryView_SelectedChanged (object? sender, ListViewItemEventArgs? e)
  477. {
  478. if (e is null or { Item: null })
  479. {
  480. return;
  481. }
  482. string item = CachedCategories! [e.Item.Value];
  483. ObservableCollection<Scenario> newScenarioList;
  484. if (e.Item == 0)
  485. {
  486. // First category is "All"
  487. newScenarioList = CachedScenarios!;
  488. }
  489. else
  490. {
  491. newScenarioList = new (CachedScenarios!.Where (s => s.GetCategories ().Contains (item)).ToList ());
  492. }
  493. _scenarioList.Table = new EnumerableTableSource<Scenario> (
  494. newScenarioList,
  495. new ()
  496. {
  497. { "Name", s => s.GetName () }, { "Description", s => s.GetDescription () }
  498. }
  499. );
  500. }
  501. #endregion Category List
  502. #region StatusBar
  503. private readonly StatusBar? _statusBar;
  504. [ConfigurationProperty (Scope = typeof (AppSettingsScope), OmitClassName = true)]
  505. [JsonPropertyName ("UICatalog.StatusBar")]
  506. public static bool ShowStatusBar { get; set; } = true;
  507. private Shortcut? _shQuit;
  508. private Shortcut? _shVersion;
  509. private CheckBox? _force16ColorsShortcutCb;
  510. private StatusBar CreateStatusBar ()
  511. {
  512. StatusBar statusBar = new ()
  513. {
  514. Visible = ShowStatusBar,
  515. AlignmentModes = AlignmentModes.IgnoreFirstOrLast,
  516. CanFocus = false
  517. };
  518. // ReSharper disable All
  519. statusBar.Height = Dim.Auto (
  520. DimAutoStyle.Auto,
  521. minimumContentDim: Dim.Func (_ => statusBar.Visible ? 1 : 0),
  522. maximumContentDim: Dim.Func (_ => statusBar.Visible ? 1 : 0));
  523. // ReSharper restore All
  524. _shQuit = new ()
  525. {
  526. CanFocus = false,
  527. Title = "Quit",
  528. Key = Application.QuitKey
  529. };
  530. _shVersion = new ()
  531. {
  532. Title = "Version Info",
  533. CanFocus = false
  534. };
  535. var statusBarShortcut = new Shortcut
  536. {
  537. Key = Key.F10,
  538. Title = "Show/Hide Status Bar",
  539. CanFocus = false
  540. };
  541. statusBarShortcut.Accepting += (sender, args) =>
  542. {
  543. statusBar.Visible = !_statusBar!.Visible;
  544. args.Handled = true;
  545. };
  546. _force16ColorsShortcutCb = new ()
  547. {
  548. Title = "16 color mode",
  549. CheckedState = Application.Driver!.Force16Colors ? CheckState.Checked : CheckState.UnChecked,
  550. CanFocus = true
  551. };
  552. Shortcut force16ColorsShortcut = new ()
  553. {
  554. CanFocus = false,
  555. CommandView = _force16ColorsShortcutCb,
  556. HelpText = "",
  557. BindKeyToApplication = true,
  558. Key = Key.F7
  559. };
  560. force16ColorsShortcut.Accepting += (sender, args) =>
  561. {
  562. Application.Driver.Force16Colors = !Application.Driver.Force16Colors;
  563. _force16ColorsMenuItemCb!.CheckedState = Application.Driver.Force16Colors ? CheckState.Checked : CheckState.UnChecked;
  564. SetNeedsDraw ();
  565. args.Handled = true;
  566. };
  567. statusBar.Add (
  568. _shQuit,
  569. statusBarShortcut,
  570. force16ColorsShortcut,
  571. _shVersion
  572. );
  573. if (UICatalog.Options.DontEnableConfigurationManagement)
  574. {
  575. statusBar.AddShortcutAt (statusBar.SubViews.ToList ().IndexOf (_shVersion), new Shortcut () { Title = "CM is Disabled" });
  576. }
  577. return statusBar;
  578. }
  579. #endregion StatusBar
  580. #region Configuration Manager
  581. /// <summary>
  582. /// Called when CM has applied changes.
  583. /// </summary>
  584. private void ConfigApplied ()
  585. {
  586. UpdateThemesMenu ();
  587. SchemeName = CachedRunnableScheme;
  588. if (_shQuit is { })
  589. {
  590. _shQuit.Key = Application.QuitKey;
  591. }
  592. if (_statusBar is { })
  593. {
  594. _statusBar.Visible = ShowStatusBar;
  595. }
  596. _disableMouseCb!.CheckedState = Application.IsMouseDisabled ? CheckState.Checked : CheckState.UnChecked;
  597. _force16ColorsShortcutCb!.CheckedState = Application.Driver!.Force16Colors ? CheckState.Checked : CheckState.UnChecked;
  598. Application.TopRunnableView?.SetNeedsDraw ();
  599. }
  600. private void ConfigAppliedHandler (object? sender, ConfigurationManagerEventArgs? a) { ConfigApplied (); }
  601. #endregion Configuration Manager
  602. /// <summary>
  603. /// Gets the message displayed in the About Box. `public` so it can be used from Unit tests.
  604. /// </summary>
  605. /// <returns></returns>
  606. public static string GetAboutBoxMessage ()
  607. {
  608. // NOTE: Do not use multiline verbatim strings here.
  609. // WSL gets all confused.
  610. StringBuilder msg = new ();
  611. msg.AppendLine ("UI Catalog: A comprehensive sample library and test app for");
  612. msg.AppendLine ();
  613. msg.AppendLine (
  614. """
  615. _______ _ _ _____ _
  616. |__ __| (_) | | / ____| (_)
  617. | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _
  618. | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | |
  619. | | __/ | | | | | | | | | | | (_| | || |__| | |_| | |
  620. |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_|
  621. """);
  622. msg.AppendLine ();
  623. msg.AppendLine ("v2 - Pre-Alpha");
  624. msg.AppendLine ();
  625. msg.AppendLine ("https://github.com/gui-cs/Terminal.Gui");
  626. return msg.ToString ();
  627. }
  628. public static void OpenUrl (string url)
  629. {
  630. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows))
  631. {
  632. url = url.Replace ("&", "^&");
  633. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  634. }
  635. else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux))
  636. {
  637. using var process = new Process
  638. {
  639. StartInfo = new ()
  640. {
  641. FileName = "xdg-open",
  642. Arguments = url,
  643. RedirectStandardError = true,
  644. RedirectStandardOutput = true,
  645. CreateNoWindow = true,
  646. UseShellExecute = false
  647. }
  648. };
  649. process.Start ();
  650. }
  651. else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  652. {
  653. Process.Start ("open", url);
  654. }
  655. }
  656. }