UICatalog.cs 19 KB

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