UICatalogTop.cs 30 KB

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