From d2e898014cfe23391202d1f617d99b945f9d0300 Mon Sep 17 00:00:00 2001 From: Yair <39923744+yaira2@users.noreply.github.com> Date: Thu, 11 Jan 2024 16:17:32 -0500 Subject: [PATCH] Fix: Fixed issue where it didn't work to pick files when creating new shortcuts (#14422) --- .../Helpers/Interop/InteropHelpers.cs | 20 ++++++++++++++ .../Dialogs/CreateShortcutDialogViewModel.cs | 26 +++++++++++++------ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Files.App/Helpers/Interop/InteropHelpers.cs b/src/Files.App/Helpers/Interop/InteropHelpers.cs index 9e1e0588d00c..4db3457116a0 100644 --- a/src/Files.App/Helpers/Interop/InteropHelpers.cs +++ b/src/Files.App/Helpers/Interop/InteropHelpers.cs @@ -5,6 +5,7 @@ using Microsoft.UI.Xaml; using System.Reflection; using System.Runtime.InteropServices; +using System.Text; using Vanara.PInvoke; using static Vanara.PInvoke.User32; @@ -73,6 +74,25 @@ public static IntPtr SetWindowLong(HWND hWnd, WindowLongFlags nIndex, IntPtr dwN [DllImport("User32.dll")] public extern static short GetKeyState(int n); + + [DllImport("shell32.dll")] + public static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi); + + [DllImport("shell32.dll", CharSet = CharSet.Unicode)] + public static extern bool SHGetPathFromIDList(IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszPath); + + [StructLayout(LayoutKind.Sequential)] + public struct BROWSEINFO + { + public IntPtr hwndOwner; + public IntPtr pidlRoot; + public string pszDisplayName; + public string lpszTitle; + public uint ulFlags; + public IntPtr lpfn; + public int lParam; + public IntPtr iImage; + } } [ComImport] diff --git a/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs b/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs index 4da5368c87a0..10f1807b19fd 100644 --- a/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs +++ b/src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs @@ -1,8 +1,9 @@ // Copyright (c) 2023 Files Community // Licensed under the MIT License. See the LICENSE. -using Files.Core.Extensions; using System.IO; +using System.Runtime.InteropServices; +using System.Text; using System.Windows.Input; using Windows.Storage.Pickers; @@ -84,18 +85,27 @@ public CreateShortcutDialogViewModel(string workingDirectory) WorkingDirectory = workingDirectory; _destinationItemPath = string.Empty; - SelectDestinationCommand = new AsyncRelayCommand(SelectDestinationAsync); + SelectDestinationCommand = new AsyncRelayCommand(SelectDestination); PrimaryButtonCommand = new AsyncRelayCommand(CreateShortcutAsync); } - private async Task SelectDestinationAsync() + private Task SelectDestination() { - var folderPicker = InitializeWithWindow(new FolderPicker()); - folderPicker.FileTypeFilter.Add("*"); + InteropHelpers.BROWSEINFO bi = new InteropHelpers.BROWSEINFO(); + bi.ulFlags = 0x00004000; + bi.lpszTitle = "Select a folder"; + nint pidl = InteropHelpers.SHBrowseForFolder(ref bi); + if (pidl != nint.Zero) + { + StringBuilder path = new StringBuilder(260); + if (InteropHelpers.SHGetPathFromIDList(pidl, path)) + { + DestinationItemPath = path.ToString(); + } + Marshal.FreeCoTaskMem(pidl); + } - var selectedFolder = await folderPicker.PickSingleFolderAsync(); - if (selectedFolder is not null) - DestinationItemPath = selectedFolder.Path; + return Task.CompletedTask; } private FolderPicker InitializeWithWindow(FolderPicker obj)