UICatalogRunnable.cs 31 KB

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