Repository: microsoft/PowerToys
File: src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.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;
using System.Runtime.InteropServices;
using System.Text;
using Peek.UI.Native;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace Peek.UI.Helpers
{
    public static class FileExplorerHelper
    {
        public static unsafe Shell32.IShellFolderViewDual2? GetCurrentFolderView()
        {
            var foregroundWindowHandle = NativeMethods.GetForegroundWindow();

            int capacity = PInvoke.GetWindowTextLength(new HWND(foregroundWindowHandle)) * 2;
            StringBuilder foregroundWindowTitleBuffer = new StringBuilder(capacity);
            NativeMethods.GetWindowText(new HWND(foregroundWindowHandle), foregroundWindowTitleBuffer, foregroundWindowTitleBuffer.Capacity);

            string foregroundWindowTitle = foregroundWindowTitleBuffer.ToString();

            var shell = new Shell32.Shell();
            foreach (SHDocVw.InternetExplorer window in shell.Windows())
            {
                var shellFolderView = (Shell32.IShellFolderViewDual2)window.Document;
                var folderTitle = shellFolderView.Folder.Title;

                if (window.HWND == (int)foregroundWindowHandle && folderTitle == foregroundWindowTitle)
                {
                    return shellFolderView;
                }
            }

            return null;
        }
    }
}


Review comment (bug):
If we have two tabs open with the same name, this could get the wrong tab right? Since I assume this can only happen if it's the same folder opened twice, it's only problematic if selected items or the sort option is different between them. 

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;
using System.Runtime.InteropServices;
using System.Text;
using Peek.UI.Native;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace Peek.UI.Helpers
{
    public static class FileExplorerHelper
    {
        public static Shell32.IShellFolderViewDual2? GetCurrentFolderView()
        {
            var foregroundWindowHandle = NativeMethods.GetForegroundWindow();

            int capacity = PInvoke.GetWindowTextLength(new HWND(foregroundWindowHandle)) * 2;
            StringBuilder foregroundWindowTitleBuffer = new StringBuilder(capacity);
            NativeMethods.GetWindowText(new HWND(foregroundWindowHandle), foregroundWindowTitleBuffer, foregroundWindowTitleBuffer.Capacity);

            string foregroundWindowTitle = foregroundWindowTitleBuffer.ToString();

            var shell = new Shell32.Shell();
            foreach (SHDocVw.InternetExplorer window in shell.Windows())
            {
                var shellFolderView = (Shell32.IShellFolderViewDual2)window.Document;
                var folderTitle = shellFolderView.Folder.Title;

                if (window.HWND == (int)foregroundWindowHandle && folderTitle == foregroundWindowTitle)
                {
                    return shellFolderView;
                }
            }

            return null;
        }
    }
}