Make it more likely for OpenLink target app to be focused

This commit is contained in:
Soreepeong 2025-08-07 08:45:58 +09:00
parent 75a1742af9
commit 5440e9b0b8

View file

@ -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;
/// <summary>
/// Open a link in the default browser.
/// Open a link in the default browser, and attempts to focus the newly launched application.
/// </summary>
/// <param name="url">The link to open.</param>
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<InterfaceManager>.GetNullable() is { } im
? im.GameWindowHandle
: 0,
Verb = "open",
};
if (Service<Framework>.GetNullable() is { } fw)
_ = fw.RunOnFrameworkThread(() => Process.Start(process));
else
Process.Start(process);
}
try
{
var psi = new ProcessStartInfo((string)url!)
{
UseShellExecute = true,
ErrorDialogParentHandle = Service<InterfaceManager>.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);
/// <summary>
/// Perform a "zipper merge" (A, 1, B, 2, C, 3) of multiple enumerables, allowing for lists to end early.