-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathLinkHelper.cs
More file actions
38 lines (34 loc) · 1.17 KB
/
LinkHelper.cs
File metadata and controls
38 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Diagnostics;
namespace PolyPilot.Models;
public static class LinkHelper
{
/// <summary>
/// Validates that a URL is a safe external URL (http/https only).
/// </summary>
public static bool IsValidExternalUrl(string? url)
{
if (string.IsNullOrWhiteSpace(url)) return false;
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) return false;
return uri.Scheme is "http" or "https";
}
/// <summary>
/// Opens a URL in the default browser without bringing it to the foreground.
/// On macOS, uses 'open -g'. On other platforms, this is a no-op
/// (callers should fall back to Launcher.Default.OpenAsync).
/// </summary>
public static void OpenInBackground(string url)
{
if (!IsValidExternalUrl(url)) return;
if (OperatingSystem.IsMacOS() || OperatingSystem.IsMacCatalyst())
{
try
{
var psi = new ProcessStartInfo("open") { UseShellExecute = false };
psi.ArgumentList.Add("-g");
psi.ArgumentList.Add(url);
Process.Start(psi)?.Dispose();
}
catch { }
}
}
}