Skip to content

Commit

Permalink
Fix: Fixed issue where it didn't work to pick files when creating new…
Browse files Browse the repository at this point in the history
… shortcuts (files-community#14422)
  • Loading branch information
yaira2 authored Jan 11, 2024
1 parent 95b1076 commit d2e8980
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
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

0 comments on commit d2e8980

Please sign in to comment.