Repository: microsoft/PowerToys
File: src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs (C#)

Code before:
        {
            HideWindow();
        });
    }

    public void Receive(QuitMessage message) =>

        // This might come in on a background thread
        DispatcherQueue.TryEnqueue(() => Close());

    public void Receive(DismissMessage message) =>
        HideWindow();

    private void HideWindow()
    {
        // Cloak our HWND to avoid all animations.
        Cloak();

        // Then hide our HWND, to make sure that the OS gives the FG / focus back to another app
        // (there's no way for us to guess what the right hwnd might be, only the OS can do it right)
        this.Hide();

        // TRICKY: show our HWND again. This will trick XAML into painting our
        // HWND again, so that we avoid the "flicker" caused by a WinUI3 app
        // window being first shown
        this.Show();

        // Intentionally leave the window cloaked. So our window is "visible",
        // but also cloaked, so you can't see it.
    }

    private void Cloak()
    {
        unsafe
        {
            BOOL value = true;
            PInvoke.DwmSetWindowAttribute(_hwnd, DWMWINDOWATTRIBUTE.DWMWA_CLOAK, &value, (uint)sizeof(BOOL));
        }
    }

    private void Uncloak()
    {
        unsafe
        {
            BOOL value = false;
            PInvoke.DwmSetWindowAttribute(_hwnd, DWMWINDOWATTRIBUTE.DWMWA_CLOAK, &value, (uint)sizeof(BOOL));
        }
    }

    internal void MainWindow_Closed(object sender, WindowEventArgs args)
    {

Review comment (suggestion):
```suggestion
        PInvoke.ShowWindow(hwnd, SHOW_WINDOW_CMD.SW_SHOWNA);
```

SW_SHOWNA will prevent us for trying to fight the focus back.

Code after:
        {
            HideWindow();
        });
    }

    public void Receive(QuitMessage message) =>

        // This might come in on a background thread
        DispatcherQueue.TryEnqueue(() => Close());

    public void Receive(DismissMessage message) =>
        HideWindow();

    private void HideWindow()
    {
        // Cloak our HWND to avoid all animations.
        Cloak();

        // Then hide our HWND, to make sure that the OS gives the FG / focus back to another app
        // (there's no way for us to guess what the right hwnd might be, only the OS can do it right)
        PInvoke.ShowWindow(_hwnd, SHOW_WINDOW_CMD.SW_HIDE);

        // TRICKY: show our HWND again. This will trick XAML into painting our
        // HWND again, so that we avoid the "flicker" caused by a WinUI3 app
        // window being first shown
        // SW_SHOWNA will prevent us for trying to fight the focus back
        PInvoke.ShowWindow(_hwnd, SHOW_WINDOW_CMD.SW_SHOWNA);

        // Intentionally leave the window cloaked. So our window is "visible",
        // but also cloaked, so you can't see it.
    }

    private void Cloak()
    {
        unsafe
        {
            BOOL value = true;
            PInvoke.DwmSetWindowAttribute(_hwnd, DWMWINDOWATTRIBUTE.DWMWA_CLOAK, &value, (uint)sizeof(BOOL));
        }
    }

    private void Uncloak()
    {
        unsafe
        {
            BOOL value = false;
            PInvoke.DwmSetWindowAttribute(_hwnd, DWMWINDOWATTRIBUTE.DWMWA_CLOAK, &value, (uint)sizeof(BOOL));
        }
    }

    internal void MainWindow_Closed(object sender, WindowEventArgs args)