Repository: microsoft/PowerToys
File: src/common/ManagedCommon/WindowHelpers.cs (C#)

Code before:
﻿// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace ManagedCommon
{
    public class WindowHelpers
    {
        public static void BringToForeground(IntPtr handle)
        {
            NativeMethods.INPUT input = new NativeMethods.INPUT { type = NativeMethods.INPUTTYPE.INPUT_MOUSE, data = { } };
            NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[] { input };
            _ = NativeMethods.SendInput(1, inputs, NativeMethods.INPUT.Size);

            var fgHandle = NativeMethods.GetForegroundWindow();

            var threadId1 = NativeMethods.GetWindowThreadProcessId(handle, System.IntPtr.Zero);
            var threadId2 = NativeMethods.GetWindowThreadProcessId(fgHandle, System.IntPtr.Zero);

            if (threadId1 != threadId2)
            {
                NativeMethods.AttachThreadInput(threadId1, threadId2, true);
                NativeMethods.SetForegroundWindow(handle);
                NativeMethods.AttachThreadInput(threadId1, threadId2, false);
            }
            else
            {
                NativeMethods.SetForegroundWindow(handle);
            }
        }
    }
}


Review comment (bug):
Refactoring looks good but unfortunately doesn't fix the issue.
I debugged the code and I think the problem is that the `fgHandle` we get here is the flyout, window that gets hidden immediately when the started utility steal its focus.

Implementing this may be a fix: https://github.com/microsoft/PowerToys/issues/26745

https://github.com/microsoft/PowerToys/blob/652bc353af8b64b82838d23ac8645405119eb587/src/settings-ui/Settings.UI/SettingsXAML/Flyout/LaunchPage.xaml.cs#L146-L147C43


Code after:
﻿// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace ManagedCommon
{
    public class WindowHelpers
    {
        public static void BringToForeground(IntPtr handle)
        {
            NativeMethods.INPUT input = new NativeMethods.INPUT { type = NativeMethods.INPUTTYPE.INPUT_MOUSE, data = { } };
            NativeMethods.INPUT[] inputs = new NativeMethods.INPUT[] { input };
            _ = NativeMethods.SendInput(1, inputs, NativeMethods.INPUT.Size);

            NativeMethods.SetForegroundWindow(handle);

            var fgHandle = NativeMethods.GetForegroundWindow();

            if (fgHandle != handle)
            {
                var threadId1 = NativeMethods.GetWindowThreadProcessId(handle, System.IntPtr.Zero);
                var threadId2 = NativeMethods.GetWindowThreadProcessId(fgHandle, System.IntPtr.Zero);

                if (threadId1 != threadId2)
                {
                    NativeMethods.AttachThreadInput(threadId1, threadId2, true);
                    NativeMethods.SetForegroundWindow(handle);
                    NativeMethods.AttachThreadInput(threadId1, threadId2, false);
                }
                else
                {
                    NativeMethods.SetForegroundWindow(handle);
                }
            }
        }
    }
}