diff --git a/Dalamud/Utility/Util.cs b/Dalamud/Utility/Util.cs index 591479fbd..a1c2eb6b2 100644 --- a/Dalamud/Utility/Util.cs +++ b/Dalamud/Utility/Util.cs @@ -9,6 +9,7 @@ using System.Reflection.Emit; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; +using System.Threading; using System.Threading.Tasks; using Dalamud.Bindings.ImGui; @@ -524,24 +525,36 @@ public static partial class Util public static bool IsWindows11() => Environment.OSVersion.Version.Build >= 22000; /// - /// Open a link in the default browser. + /// Open a link in the default browser, and attempts to focus the newly launched application. /// /// The link to open. - public static void OpenLink(string url) - { - var process = new ProcessStartInfo(url) + public static void OpenLink(string url) => new Thread( + static url => { - UseShellExecute = true, - ErrorDialogParentHandle = Service.GetNullable() is { } im - ? im.GameWindowHandle - : 0, - Verb = "open", - }; - if (Service.GetNullable() is { } fw) - _ = fw.RunOnFrameworkThread(() => Process.Start(process)); - else - Process.Start(process); - } + try + { + var psi = new ProcessStartInfo((string)url!) + { + UseShellExecute = true, + ErrorDialogParentHandle = Service.GetNullable() is { } im + ? im.GameWindowHandle + : 0, + Verb = "open", + }; + if (Process.Start(psi) is not { } process) + return; + + if (process.Id != 0) + TerraFX.Interop.Windows.Windows.AllowSetForegroundWindow((uint)process.Id); + process.WaitForInputIdle(); + TerraFX.Interop.Windows.Windows.SetForegroundWindow( + (TerraFX.Interop.Windows.HWND)process.MainWindowHandle); + } + catch (Exception e) + { + Log.Error(e, "{fn}: failed to open {url}", nameof(OpenLink), url); + } + }).Start(url); /// /// Perform a "zipper merge" (A, 1, B, 2, C, 3) of multiple enumerables, allowing for lists to end early.