UICatalog.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 Microsoft.DotNet.PlatformAbstractions;
  11. using Rune = System.Rune;
  12. /// <summary>
  13. /// UI Catalog is a comprehensive sample library for Terminal.Gui. It provides a simple UI for adding to the catalog of scenarios.
  14. /// </summary>
  15. /// <remarks>
  16. /// <para>
  17. /// UI Catalog attempts to satisfy the following goals:
  18. /// </para>
  19. /// <para>
  20. /// <list type="number">
  21. /// <item>
  22. /// <description>
  23. /// Be an easy to use showcase for Terminal.Gui concepts and features.
  24. /// </description>
  25. /// </item>
  26. /// <item>
  27. /// <description>
  28. /// Provide sample code that illustrates how to properly implement said concepts & features.
  29. /// </description>
  30. /// </item>
  31. /// <item>
  32. /// <description>
  33. /// Make it easy for contributors to add additional samples in a structured way.
  34. /// </description>
  35. /// </item>
  36. /// </list>
  37. /// </para>
  38. /// <para>
  39. /// See the project README for more details (https://github.com/gui-cs/Terminal.Gui/tree/master/UICatalog/README.md).
  40. /// </para>
  41. /// </remarks>
  42. namespace UICatalog {
  43. /// <summary>
  44. /// UI Catalog is a comprehensive sample app and scenario library for <see cref="Terminal.Gui"/>
  45. /// </summary>
  46. class UICatalogApp {
  47. static void Main (string [] args)
  48. {
  49. Console.OutputEncoding = Encoding.Default;
  50. if (Debugger.IsAttached) {
  51. CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
  52. }
  53. _scenarios = Scenario.GetScenarios ();
  54. _categories = Scenario.GetAllCategories ();
  55. _nameColumnWidth = _scenarios.OrderByDescending (s => s.GetName ().Length).FirstOrDefault ().GetName ().Length;
  56. if (args.Length > 0 && args.Contains ("-usc")) {
  57. _useSystemConsole = true;
  58. args = args.Where (val => val != "-usc").ToArray ();
  59. }
  60. // If a Scenario name has been provided on the commandline
  61. // run it and exit when done.
  62. if (args.Length > 0) {
  63. var item = _scenarios.FindIndex (s => s.GetName ().Equals (args [0], StringComparison.OrdinalIgnoreCase));
  64. _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios [item].GetType ());
  65. Application.UseSystemConsole = _useSystemConsole;
  66. Application.Init ();
  67. _selectedScenario.Init (_colorScheme);
  68. _selectedScenario.Setup ();
  69. _selectedScenario.Run ();
  70. _selectedScenario.Dispose ();
  71. _selectedScenario = null;
  72. Application.Shutdown ();
  73. return;
  74. }
  75. _aboutMessage = new StringBuilder ();
  76. _aboutMessage.AppendLine (@"A comprehensive sample library for");
  77. _aboutMessage.AppendLine (@"");
  78. _aboutMessage.AppendLine (@" _______ _ _ _____ _ ");
  79. _aboutMessage.AppendLine (@" |__ __| (_) | | / ____| (_) ");
  80. _aboutMessage.AppendLine (@" | | ___ _ __ _ __ ___ _ _ __ __ _| || | __ _ _ _ ");
  81. _aboutMessage.AppendLine (@" | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | || | |_ | | | | | ");
  82. _aboutMessage.AppendLine (@" | | __/ | | | | | | | | | | | (_| | || |__| | |_| | | ");
  83. _aboutMessage.AppendLine (@" |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_(_)_____|\__,_|_| ");
  84. _aboutMessage.AppendLine (@"");
  85. _aboutMessage.AppendLine (@"https://github.com/gui-cs/Terminal.Gui");
  86. Scenario scenario;
  87. while ((scenario = RunUICatalogTopLevel ()) != null) {
  88. VerifyObjectsWereDisposed ();
  89. scenario.Init (_colorScheme);
  90. scenario.Setup ();
  91. scenario.Run ();
  92. scenario.Dispose ();
  93. // This call to Application.Shutdown brackets the Application.Init call
  94. // made by Scenario.Init() above
  95. Application.Shutdown ();
  96. VerifyObjectsWereDisposed ();
  97. }
  98. VerifyObjectsWereDisposed ();
  99. }
  100. /// <summary>
  101. /// Shows the UI Catalog selection UI. When the user selects a Scenario to run, the
  102. /// UI Catalog main app UI is killed and the Scenario is run as though it were Application.Top.
  103. /// When the Scenario exits, this function exits.
  104. /// </summary>
  105. /// <returns></returns>
  106. static Scenario RunUICatalogTopLevel ()
  107. {
  108. Application.UseSystemConsole = _useSystemConsole;
  109. // Run UI Catalog UI. When it exits, if _selectedScenario is != null then
  110. // a Scenario was selected. Otherwise, the user wants to exit UI Catalog.
  111. Application.Init ();
  112. Application.EnableConsoleScrolling = _enableConsoleScrolling;
  113. Application.Run<UICatalogTopLevel> ();
  114. Application.Shutdown ();
  115. return _selectedScenario;
  116. }
  117. static List<Scenario> _scenarios;
  118. static List<string> _categories;
  119. static int _nameColumnWidth;
  120. // When a scenario is run, the main app is killed. These items
  121. // are therefore cached so that when the scenario exits the
  122. // main app UI can be restored to previous state
  123. static int _cachedScenarioIndex = 0;
  124. static int _cachedCategoryIndex = 0;
  125. static StringBuilder _aboutMessage;
  126. // If set, holds the scenario the user selected
  127. static Scenario _selectedScenario = null;
  128. static bool _useSystemConsole = false;
  129. static ConsoleDriver.DiagnosticFlags _diagnosticFlags;
  130. static bool _enableConsoleScrolling = false;
  131. static bool _isFirstRunning = true;
  132. static ColorScheme _colorScheme;
  133. /// <summary>
  134. /// This is the main UI Catalog app view. It is run fresh when the app loads (if a Scenario has not been passed on
  135. /// the command line) and each time a Scenario ends.
  136. /// </summary>
  137. class UICatalogTopLevel : Toplevel {
  138. public MenuItem miIsMouseDisabled;
  139. public MenuItem miEnableConsoleScrolling;
  140. public FrameView LeftPane;
  141. public ListView CategoryListView;
  142. public FrameView RightPane;
  143. public ListView ScenarioListView;
  144. public StatusItem Capslock;
  145. public StatusItem Numlock;
  146. public StatusItem Scrolllock;
  147. public StatusItem DriverName;
  148. public StatusItem OS;
  149. public UICatalogTopLevel ()
  150. {
  151. ColorScheme = _colorScheme = Colors.Base;
  152. MenuBar = new MenuBar (new MenuBarItem [] {
  153. new MenuBarItem ("_File", new MenuItem [] {
  154. new MenuItem ("_Quit", "Quit UI Catalog", () => RequestStop(), null, null, Key.Q | Key.CtrlMask)
  155. }),
  156. new MenuBarItem ("_Color Scheme", CreateColorSchemeMenuItems()),
  157. new MenuBarItem ("Diag_nostics", CreateDiagnosticMenuItems()),
  158. new MenuBarItem ("_Help", new MenuItem [] {
  159. new MenuItem ("_gui.cs API Overview", "", () => OpenUrl ("https://gui-cs.github.io/Terminal.Gui/articles/overview.html"), null, null, Key.F1),
  160. new MenuItem ("gui.cs _README", "", () => OpenUrl ("https://github.com/gui-cs/Terminal.Gui"), null, null, Key.F2),
  161. new MenuItem ("_About...",
  162. "About UI Catalog", () => MessageBox.Query ("About UI Catalog", _aboutMessage.ToString(), "_Ok"), null, null, Key.CtrlMask | Key.A),
  163. }),
  164. });
  165. Capslock = new StatusItem (Key.CharMask, "Caps", null);
  166. Numlock = new StatusItem (Key.CharMask, "Num", null);
  167. Scrolllock = new StatusItem (Key.CharMask, "Scroll", null);
  168. DriverName = new StatusItem (Key.CharMask, "Driver:", null);
  169. OS = new StatusItem (Key.CharMask, "OS:", null);
  170. StatusBar = new StatusBar () {
  171. Visible = true,
  172. };
  173. StatusBar.Items = new StatusItem [] {
  174. new StatusItem(Key.Q | Key.CtrlMask, "~CTRL-Q~ Quit", () => {
  175. if (_selectedScenario is null){
  176. // This causes GetScenarioToRun to return null
  177. _selectedScenario = null;
  178. RequestStop();
  179. } else {
  180. _selectedScenario.RequestStop();
  181. }
  182. }),
  183. new StatusItem(Key.F10, "~F10~ Status Bar", () => {
  184. StatusBar.Visible = !StatusBar.Visible;
  185. LeftPane.Height = Dim.Fill(StatusBar.Visible ? 1 : 0);
  186. RightPane.Height = Dim.Fill(StatusBar.Visible ? 1 : 0);
  187. LayoutSubviews();
  188. SetChildNeedsDisplay();
  189. }),
  190. DriverName,
  191. OS
  192. };
  193. LeftPane = new FrameView ("Categories") {
  194. X = 0,
  195. Y = 1, // for menu
  196. Width = 25,
  197. Height = Dim.Fill (1),
  198. CanFocus = true,
  199. Shortcut = Key.CtrlMask | Key.C
  200. };
  201. LeftPane.Title = $"{LeftPane.Title} ({LeftPane.ShortcutTag})";
  202. LeftPane.ShortcutAction = () => LeftPane.SetFocus ();
  203. CategoryListView = new ListView (_categories) {
  204. X = 0,
  205. Y = 0,
  206. Width = Dim.Fill (0),
  207. Height = Dim.Fill (0),
  208. AllowsMarking = false,
  209. CanFocus = true,
  210. };
  211. CategoryListView.OpenSelectedItem += (a) => {
  212. RightPane.SetFocus ();
  213. };
  214. CategoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;
  215. LeftPane.Add (CategoryListView);
  216. RightPane = new FrameView ("Scenarios") {
  217. X = 25,
  218. Y = 1, // for menu
  219. Width = Dim.Fill (),
  220. Height = Dim.Fill (1),
  221. CanFocus = true,
  222. Shortcut = Key.CtrlMask | Key.S
  223. };
  224. RightPane.Title = $"{RightPane.Title} ({RightPane.ShortcutTag})";
  225. RightPane.ShortcutAction = () => RightPane.SetFocus ();
  226. ScenarioListView = new ListView () {
  227. X = 0,
  228. Y = 0,
  229. Width = Dim.Fill (0),
  230. Height = Dim.Fill (0),
  231. AllowsMarking = false,
  232. CanFocus = true,
  233. };
  234. ScenarioListView.OpenSelectedItem += ScenarioListView_OpenSelectedItem;
  235. RightPane.Add (ScenarioListView);
  236. KeyDown += KeyDownHandler;
  237. Add (MenuBar);
  238. Add (LeftPane);
  239. Add (RightPane);
  240. Add (StatusBar);
  241. Loaded += LoadedHandler;
  242. // Restore previous selections
  243. CategoryListView.SelectedItem = _cachedCategoryIndex;
  244. ScenarioListView.SelectedItem = _cachedScenarioIndex;
  245. }
  246. void LoadedHandler ()
  247. {
  248. if (_colorScheme == null) {
  249. ColorScheme = _colorScheme = Colors.Base;
  250. }
  251. miIsMouseDisabled.Checked = Application.IsMouseDisabled;
  252. miEnableConsoleScrolling.Checked = Application.EnableConsoleScrolling;
  253. DriverName.Title = $"Driver: {Driver.GetType ().Name}";
  254. OS.Title = $"OS: {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystem} {Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.OperatingSystemVersion}";
  255. if (_selectedScenario != null) {
  256. _selectedScenario = null;
  257. _isFirstRunning = false;
  258. }
  259. if (!_isFirstRunning) {
  260. RightPane.SetFocus ();
  261. }
  262. Loaded -= LoadedHandler;
  263. }
  264. /// <summary>
  265. /// Launches the selected scenario, setting the global _selectedScenario
  266. /// </summary>
  267. /// <param name="e"></param>
  268. void ScenarioListView_OpenSelectedItem (EventArgs e)
  269. {
  270. if (_selectedScenario is null) {
  271. // Save selected item state
  272. _cachedCategoryIndex = CategoryListView.SelectedItem;
  273. _cachedScenarioIndex = ScenarioListView.SelectedItem;
  274. // Create new instance of scenario (even though Scenarios contains instances)
  275. _selectedScenario = (Scenario)Activator.CreateInstance (ScenarioListView.Source.ToList () [ScenarioListView.SelectedItem].GetType ());
  276. // Tell the main app to stop
  277. Application.RequestStop ();
  278. }
  279. }
  280. List<MenuItem []> CreateDiagnosticMenuItems ()
  281. {
  282. List<MenuItem []> menuItems = new List<MenuItem []> ();
  283. menuItems.Add (CreateDiagnosticFlagsMenuItems ());
  284. menuItems.Add (new MenuItem [] { null });
  285. menuItems.Add (CreateEnableConsoleScrollingMenuItems ());
  286. menuItems.Add (CreateDisabledEnabledMouseItems ());
  287. menuItems.Add (CreateKeybindingsMenuItems ());
  288. return menuItems;
  289. }
  290. MenuItem [] CreateDisabledEnabledMouseItems ()
  291. {
  292. List<MenuItem> menuItems = new List<MenuItem> ();
  293. miIsMouseDisabled = new MenuItem ();
  294. miIsMouseDisabled.Title = "_Disable Mouse";
  295. miIsMouseDisabled.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miIsMouseDisabled.Title.ToString ().Substring (1, 1) [0];
  296. miIsMouseDisabled.CheckType |= MenuItemCheckStyle.Checked;
  297. miIsMouseDisabled.Action += () => {
  298. miIsMouseDisabled.Checked = Application.IsMouseDisabled = !miIsMouseDisabled.Checked;
  299. };
  300. menuItems.Add (miIsMouseDisabled);
  301. return menuItems.ToArray ();
  302. }
  303. MenuItem [] CreateKeybindingsMenuItems ()
  304. {
  305. List<MenuItem> menuItems = new List<MenuItem> ();
  306. var item = new MenuItem ();
  307. item.Title = "_Key Bindings";
  308. item.Help = "Change which keys do what";
  309. item.Action += () => {
  310. var dlg = new KeyBindingsDialog ();
  311. Application.Run (dlg);
  312. };
  313. menuItems.Add (null);
  314. menuItems.Add (item);
  315. return menuItems.ToArray ();
  316. }
  317. MenuItem [] CreateEnableConsoleScrollingMenuItems ()
  318. {
  319. List<MenuItem> menuItems = new List<MenuItem> ();
  320. miEnableConsoleScrolling = new MenuItem ();
  321. miEnableConsoleScrolling.Title = "_Enable Console Scrolling";
  322. miEnableConsoleScrolling.Shortcut = Key.CtrlMask | Key.AltMask | (Key)miEnableConsoleScrolling.Title.ToString ().Substring (1, 1) [0];
  323. miEnableConsoleScrolling.CheckType |= MenuItemCheckStyle.Checked;
  324. miEnableConsoleScrolling.Action += () => {
  325. miEnableConsoleScrolling.Checked = !miEnableConsoleScrolling.Checked;
  326. Application.EnableConsoleScrolling = miEnableConsoleScrolling.Checked;
  327. };
  328. menuItems.Add (miEnableConsoleScrolling);
  329. return menuItems.ToArray ();
  330. }
  331. MenuItem [] CreateDiagnosticFlagsMenuItems ()
  332. {
  333. const string OFF = "Diagnostics: _Off";
  334. const string FRAME_RULER = "Diagnostics: Frame _Ruler";
  335. const string FRAME_PADDING = "Diagnostics: _Frame Padding";
  336. var index = 0;
  337. List<MenuItem> menuItems = new List<MenuItem> ();
  338. foreach (Enum diag in Enum.GetValues (_diagnosticFlags.GetType ())) {
  339. var item = new MenuItem ();
  340. item.Title = GetDiagnosticsTitle (diag);
  341. item.Shortcut = Key.AltMask + index.ToString () [0];
  342. index++;
  343. item.CheckType |= MenuItemCheckStyle.Checked;
  344. if (GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off) == item.Title) {
  345. item.Checked = (_diagnosticFlags & (ConsoleDriver.DiagnosticFlags.FramePadding
  346. | ConsoleDriver.DiagnosticFlags.FrameRuler)) == 0;
  347. } else {
  348. item.Checked = _diagnosticFlags.HasFlag (diag);
  349. }
  350. item.Action += () => {
  351. var t = GetDiagnosticsTitle (ConsoleDriver.DiagnosticFlags.Off);
  352. if (item.Title == t && !item.Checked) {
  353. _diagnosticFlags &= ~(ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  354. item.Checked = true;
  355. } else if (item.Title == t && item.Checked) {
  356. _diagnosticFlags |= (ConsoleDriver.DiagnosticFlags.FramePadding | ConsoleDriver.DiagnosticFlags.FrameRuler);
  357. item.Checked = false;
  358. } else {
  359. var f = GetDiagnosticsEnumValue (item.Title);
  360. if (_diagnosticFlags.HasFlag (f)) {
  361. SetDiagnosticsFlag (f, false);
  362. } else {
  363. SetDiagnosticsFlag (f, true);
  364. }
  365. }
  366. foreach (var menuItem in menuItems) {
  367. if (menuItem.Title == t) {
  368. menuItem.Checked = !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FrameRuler)
  369. && !_diagnosticFlags.HasFlag (ConsoleDriver.DiagnosticFlags.FramePadding);
  370. } else if (menuItem.Title != t) {
  371. menuItem.Checked = _diagnosticFlags.HasFlag (GetDiagnosticsEnumValue (menuItem.Title));
  372. }
  373. }
  374. ConsoleDriver.Diagnostics = _diagnosticFlags;
  375. Application.Top.SetNeedsDisplay ();
  376. };
  377. menuItems.Add (item);
  378. }
  379. return menuItems.ToArray ();
  380. string GetDiagnosticsTitle (Enum diag)
  381. {
  382. switch (Enum.GetName (_diagnosticFlags.GetType (), diag)) {
  383. case "Off":
  384. return OFF;
  385. case "FrameRuler":
  386. return FRAME_RULER;
  387. case "FramePadding":
  388. return FRAME_PADDING;
  389. }
  390. return "";
  391. }
  392. Enum GetDiagnosticsEnumValue (ustring title)
  393. {
  394. switch (title.ToString ()) {
  395. case FRAME_RULER:
  396. return ConsoleDriver.DiagnosticFlags.FrameRuler;
  397. case FRAME_PADDING:
  398. return ConsoleDriver.DiagnosticFlags.FramePadding;
  399. }
  400. return null;
  401. }
  402. void SetDiagnosticsFlag (Enum diag, bool add)
  403. {
  404. switch (diag) {
  405. case ConsoleDriver.DiagnosticFlags.FrameRuler:
  406. if (add) {
  407. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FrameRuler;
  408. } else {
  409. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FrameRuler;
  410. }
  411. break;
  412. case ConsoleDriver.DiagnosticFlags.FramePadding:
  413. if (add) {
  414. _diagnosticFlags |= ConsoleDriver.DiagnosticFlags.FramePadding;
  415. } else {
  416. _diagnosticFlags &= ~ConsoleDriver.DiagnosticFlags.FramePadding;
  417. }
  418. break;
  419. default:
  420. _diagnosticFlags = default;
  421. break;
  422. }
  423. }
  424. }
  425. MenuItem [] CreateColorSchemeMenuItems ()
  426. {
  427. List<MenuItem> menuItems = new List<MenuItem> ();
  428. foreach (var sc in Colors.ColorSchemes) {
  429. var item = new MenuItem ();
  430. item.Title = $"_{sc.Key}";
  431. item.Shortcut = Key.AltMask | (Key)sc.Key.Substring (0, 1) [0];
  432. item.CheckType |= MenuItemCheckStyle.Radio;
  433. item.Checked = sc.Value == _colorScheme;
  434. item.Action += () => {
  435. ColorScheme = _colorScheme = sc.Value;
  436. SetNeedsDisplay ();
  437. foreach (var menuItem in menuItems) {
  438. menuItem.Checked = menuItem.Title.Equals ($"_{sc.Key}") && sc.Value == _colorScheme;
  439. }
  440. };
  441. menuItems.Add (item);
  442. }
  443. return menuItems.ToArray ();
  444. }
  445. void KeyDownHandler (View.KeyEventEventArgs a)
  446. {
  447. if (a.KeyEvent.IsCapslock) {
  448. Capslock.Title = "Caps: On";
  449. StatusBar.SetNeedsDisplay ();
  450. } else {
  451. Capslock.Title = "Caps: Off";
  452. StatusBar.SetNeedsDisplay ();
  453. }
  454. if (a.KeyEvent.IsNumlock) {
  455. Numlock.Title = "Num: On";
  456. StatusBar.SetNeedsDisplay ();
  457. } else {
  458. Numlock.Title = "Num: Off";
  459. StatusBar.SetNeedsDisplay ();
  460. }
  461. if (a.KeyEvent.IsScrolllock) {
  462. Scrolllock.Title = "Scroll: On";
  463. StatusBar.SetNeedsDisplay ();
  464. } else {
  465. Scrolllock.Title = "Scroll: Off";
  466. StatusBar.SetNeedsDisplay ();
  467. }
  468. }
  469. void CategoryListView_SelectedChanged (ListViewItemEventArgs e)
  470. {
  471. var item = _categories [e.Item];
  472. List<Scenario> newlist;
  473. if (e.Item == 0) {
  474. // First category is "All"
  475. newlist = _scenarios;
  476. } else {
  477. newlist = _scenarios.Where (s => s.GetCategories ().Contains (item)).ToList ();
  478. }
  479. ScenarioListView.SetSource (newlist.ToList ());
  480. }
  481. }
  482. static void VerifyObjectsWereDisposed ()
  483. {
  484. #if DEBUG_IDISPOSABLE
  485. // Validate there are no outstanding Responder-based instances
  486. // after a scenario was selected to run. This proves the main UI Catalog
  487. // 'app' closed cleanly.
  488. foreach (var inst in Responder.Instances) {
  489. Debug.Assert (inst.WasDisposed);
  490. }
  491. Responder.Instances.Clear ();
  492. // Validate there are no outstanding Application.RunState-based instances
  493. // after a scenario was selected to run. This proves the main UI Catalog
  494. // 'app' closed cleanly.
  495. foreach (var inst in Application.RunState.Instances) {
  496. Debug.Assert (inst.WasDisposed);
  497. }
  498. Application.RunState.Instances.Clear ();
  499. #endif
  500. }
  501. static void OpenUrl (string url)
  502. {
  503. try {
  504. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  505. url = url.Replace ("&", "^&");
  506. Process.Start (new ProcessStartInfo ("cmd", $"/c start {url}") { CreateNoWindow = true });
  507. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) {
  508. using (var process = new Process {
  509. StartInfo = new ProcessStartInfo {
  510. FileName = "xdg-open",
  511. Arguments = url,
  512. RedirectStandardError = true,
  513. RedirectStandardOutput = true,
  514. CreateNoWindow = true,
  515. UseShellExecute = false
  516. }
  517. }) {
  518. process.Start ();
  519. }
  520. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  521. Process.Start ("open", url);
  522. }
  523. } catch {
  524. throw;
  525. }
  526. }
  527. }
  528. }