From 2d30eb0321fcb882dd806a2f44dfe88f4a4adfdc Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Mon, 29 Jul 2019 23:00:14 +0200 Subject: [PATCH] Adds way to open URIs in the user's browser. --- Robust.Client/ClientIoC.cs | 8 ++ Robust.Client/UserInterface/UriOpener.cs | 112 +++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 Robust.Client/UserInterface/UriOpener.cs diff --git a/Robust.Client/ClientIoC.cs b/Robust.Client/ClientIoC.cs index 2f8f9598e..b57fbed4b 100644 --- a/Robust.Client/ClientIoC.cs +++ b/Robust.Client/ClientIoC.cs @@ -88,6 +88,7 @@ namespace Robust.Client IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); + IoCManager.Register(); break; case GameController.DisplayMode.Clyde: IoCManager.Register(); @@ -96,6 +97,13 @@ namespace Robust.Client IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); +#if LINUX + IoCManager.Register(); +#elif MACOS + IoCManager.Register(); +#elif WINDOWS + IoCManager.Register(); +#endif break; default: throw new ArgumentOutOfRangeException(); diff --git a/Robust.Client/UserInterface/UriOpener.cs b/Robust.Client/UserInterface/UriOpener.cs new file mode 100644 index 000000000..74e096801 --- /dev/null +++ b/Robust.Client/UserInterface/UriOpener.cs @@ -0,0 +1,112 @@ +using System; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace Robust.Client.UserInterface +{ + /// + /// Helper for opening s on the user's machine. + /// + public interface IUriOpener + { + /// + /// Open a in the user's web browser. + /// + /// + /// The URI must be an absolute http:// or https:// URI. + /// + /// The uri to open. + /// + /// Thrown if the URI is not absolute or for HTTP or HTTPS. + /// + void OpenUri(Uri uri); + + /// + /// Open a URI in the user's web browser. + /// + /// + /// The URI must be an absolute http:// or https:// URI. + /// + /// The uri to open. + /// + /// Thrown if the URI is not absolute or for HTTP or HTTPS. + /// + void OpenUri(string uriString); + } + + internal abstract class UriOpenerBase : IUriOpener + { + public void OpenUri(Uri uri) + { + if (!uri.IsAbsoluteUri) + { + throw new ArgumentException("URI must be absolute."); + } + + if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps) + { + throw new ArgumentException("URI must be for HTTP or HTTPS."); + } + + Task.Run(() => DoOpen(uri)); + } + + public void OpenUri(string uriString) + { + if (!Uri.IsWellFormedUriString(uriString, UriKind.Absolute)) + { + throw new ArgumentException("URI must be well formed & absolute."); + } + + var uri = new Uri(uriString, UriKind.Absolute); + OpenUri(uri); + } + + protected abstract void DoOpen(Uri uri); + } + +#if LINUX + internal sealed class UriOpenerLinux : UriOpenerBase + { + protected override void DoOpen(Uri uri) + { + // Expect xdg-open to exist. + Process.Start(new ProcessStartInfo + { + FileName = "xdg-open", + Arguments = $"'{uri}'" + }); + } + } + +#elif MACOS + internal sealed class UriOpenerMacOS : UriOpenerBase + { + protected override void DoOpen(Uri uri) + { + Process.Start(new ProcessStartInfo + { + FileName = "open", + Arguments = $"'{uri}'" + }); + } + } + +#elif WINDOWS + internal sealed class UriOpenerWindows : UriOpenerBase + { + protected override void DoOpen(Uri uri) + { + Process.Start(uri.ToString()); + } + } +#endif + + internal sealed class UriOpenerDummy : UriOpenerBase + { + protected override void DoOpen(Uri uri) + { + // Literally nothing. + } + } +}