Skip to content

Fix: Fixed issue where it didn't work to pick files when creating new shortcuts #14422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/Files.App/Helpers/Interop/InteropHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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]
Expand Down
26 changes: 18 additions & 8 deletions src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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)
Expand Down