From 5590eb9484562aca4786429598e042691a3f8aa1 Mon Sep 17 00:00:00 2001 From: Michael Salmon Date: Thu, 8 Dec 2022 15:39:14 -0800 Subject: [PATCH] [Peek] New File Explorer tabs break Shell API to get selected files (#22641) * fix FE tab bug * remove unnecessary unsafe keyword --- .../Peek.UI/Helpers/FileExplorerHelper.cs | 23 +++++++++++++------ .../peek/Peek.UI/Native/NativeMethods.cs | 4 ++++ src/modules/peek/Peek.UI/NativeMethods.txt | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs b/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs index 08d3dc40b4a..ce83f22952b 100644 --- a/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs +++ b/src/modules/peek/Peek.UI/Helpers/FileExplorerHelper.cs @@ -2,10 +2,12 @@ // 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.Collections.Generic; -using System.Diagnostics; -using Peek.Common.Models; +using System; +using System.Runtime.InteropServices; +using System.Text; using Peek.UI.Native; +using Windows.Win32; +using Windows.Win32.Foundation; namespace Peek.UI.Helpers { @@ -15,14 +17,21 @@ public static class FileExplorerHelper { 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()) { - // TODO: figure out which window is the active explorer tab - // https://github.com/microsoft/PowerToys/issues/22507 - if (window.HWND == (int)foregroundWindowHandle) + var shellFolderView = (Shell32.IShellFolderViewDual2)window.Document; + var folderTitle = shellFolderView.Folder.Title; + + if (window.HWND == (int)foregroundWindowHandle && folderTitle == foregroundWindowTitle) { - return (Shell32.IShellFolderViewDual2)window.Document; + return shellFolderView; } } diff --git a/src/modules/peek/Peek.UI/Native/NativeMethods.cs b/src/modules/peek/Peek.UI/Native/NativeMethods.cs index 1616fd15f39..5536c9f7a83 100644 --- a/src/modules/peek/Peek.UI/Native/NativeMethods.cs +++ b/src/modules/peek/Peek.UI/Native/NativeMethods.cs @@ -6,6 +6,7 @@ namespace Peek.UI.Native { using System; using System.Runtime.InteropServices; + using System.Runtime.Versioning; using System.Text; using Peek.Common.Models; @@ -98,5 +99,8 @@ public enum AssocStr [DllImport("user32.dll")] internal static extern int SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + internal static extern int GetWindowText(Windows.Win32.Foundation.HWND hWnd, StringBuilder lpString, int nMaxCount); } } diff --git a/src/modules/peek/Peek.UI/NativeMethods.txt b/src/modules/peek/Peek.UI/NativeMethods.txt index b894c0f5eb6..1d4be7adc77 100644 --- a/src/modules/peek/Peek.UI/NativeMethods.txt +++ b/src/modules/peek/Peek.UI/NativeMethods.txt @@ -1,3 +1,4 @@ MonitorFromWindow GetMonitorInfo -GetDpiForWindow \ No newline at end of file +GetDpiForWindow +GetWindowTextLength \ No newline at end of file