Skip to content

Revert "Fix: Fixed issues with drag & drop from Files to other apps" #10975

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 10, 2023
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
22 changes: 3 additions & 19 deletions src/Files.App/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,17 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using System.Threading.Tasks;
using Vanara.PInvoke;
using Windows.ApplicationModel.DataTransfer;
using Windows.ApplicationModel.DataTransfer.DragDrop;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.System;
using static Files.App.Helpers.PathNormalization;
using VA = Vanara.Windows.Shell;
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;

namespace Files.App
Expand Down Expand Up @@ -755,23 +751,11 @@ protected virtual void Page_CharacterReceived(UIElement sender, CharacterReceive
protected void FileList_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
SelectedItems!.AddRange(e.Items.OfType<ListedItem>());

try
{
var itemList = e.Items.OfType<ListedItem>().Select(x => new VA.ShellItem(x.ItemPath)).ToArray();
var iddo = itemList[0].Parent.GetChildrenUIObjects<IDataObject>(HWND.NULL, itemList);
itemList.ForEach(x => x.Dispose());
var wfdo = new System.Windows.Forms.DataObject(iddo);
var formats = wfdo.GetFormats(false);
foreach (var format in formats)
{
var clipFrmtId = (uint)System.Windows.Forms.DataFormats.GetFormat(format).Id;
if (iddo.TryGetData<byte[]>(clipFrmtId, out var data))
{
var mem = new MemoryStream(data).AsRandomAccessStream();
e.Data.SetData(format, mem);
}
}
// Only support IStorageItem capable paths
var itemList = e.Items.OfType<ListedItem>().Where(x => !(x.IsHiddenItem && x.IsLinkItem && x.IsRecycleBinItem && x.IsShortcut)).Select(x => VirtualStorageItem.FromListedItem(x));
e.Data.SetStorageItems(itemList, false);
}
catch (Exception)
{
Expand Down
140 changes: 140 additions & 0 deletions src/Files.App/Filesystem/StorageItems/VirtualStorageItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using Files.App.Helpers;
using System;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.FileProperties;
using static Files.Backend.Helpers.NativeFindStorageItemHelper;

namespace Files.App.Filesystem.StorageItems
{
/// <summary>
/// Implements IStorageItem, allowing us to get an instance of IStorageItem for a ListedItem
/// representing a standard filesystem item. As such, VirtualStorageItem does not support hidden,
/// shortcut, or link items.
/// </summary>
public class VirtualStorageItem : IStorageItem
{
private static BasicProperties props;

public Windows.Storage.FileAttributes Attributes { get; init; }

public DateTimeOffset DateCreated { get; init; }

public string Name { get; init; }

public string Path { get; init; }

private VirtualStorageItem() { }

public static VirtualStorageItem FromListedItem(ListedItem item)
{
return new VirtualStorageItem()
{
Name = item.ItemNameRaw,
Path = item.ItemPath,
DateCreated = item.ItemDateCreatedReal,
Attributes = item.IsArchive || item.PrimaryItemAttribute == StorageItemTypes.File ? Windows.Storage.FileAttributes.Normal : Windows.Storage.FileAttributes.Directory
};
}

public static VirtualStorageItem FromPath(string path)
{
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
IntPtr hFile = FindFirstFileExFromApp(path, findInfoLevel, out WIN32_FIND_DATA findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags);
if (hFile.ToInt64() != -1)
{
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/c8e77b37-3909-4fe6-a4ea-2b9d423b1ee4
bool isReparsePoint = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.ReparsePoint) == System.IO.FileAttributes.ReparsePoint;
bool isSymlink = isReparsePoint && findData.dwReserved0 == NativeFileOperationsHelper.IO_REPARSE_TAG_SYMLINK;
bool isHidden = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden;
bool isDirectory = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory;

if (!(isHidden && isSymlink))
{
DateTime itemCreatedDate;

try
{
FileTimeToSystemTime(ref findData.ftCreationTime, out SYSTEMTIME systemCreatedDateOutput);
itemCreatedDate = systemCreatedDateOutput.ToDateTime();
}
catch (ArgumentException)
{
// Invalid date means invalid findData, do not add to list
return null;
}

return new VirtualStorageItem()
{
Name = findData.cFileName,
Path = path,
DateCreated = itemCreatedDate,
Attributes = isDirectory ? Windows.Storage.FileAttributes.Directory : Windows.Storage.FileAttributes.Normal
};
}

FindClose(hFile);
}

return null;
}

private async void StreamedFileWriter(StreamedFileDataRequest request)
{
try
{
using (var stream = request.AsStreamForWrite())
{
await stream.FlushAsync();
}
request.Dispose();
}
catch (Exception)
{
request.FailAndClose(StreamedFileFailureMode.Incomplete);
}
}

public IAsyncAction RenameAsync(string desiredName)
{
throw new NotImplementedException();
}

public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option)
{
throw new NotImplementedException();
}

public IAsyncAction DeleteAsync()
{
throw new NotImplementedException();
}

public IAsyncAction DeleteAsync(StorageDeleteOption option)
{
throw new NotImplementedException();
}

public IAsyncOperation<BasicProperties> GetBasicPropertiesAsync()
{
return AsyncInfo.Run(async (cancellationToken) =>
{
async Task<BasicProperties> GetFakeBasicProperties()
{
var streamedFile = await StorageFile.CreateStreamedFileAsync(Name, StreamedFileWriter, null);
return await streamedFile.GetBasicPropertiesAsync();
}
return props ?? (props = await GetFakeBasicProperties());
});
}

public bool IsOfType(StorageItemTypes type)
{
return Attributes.HasFlag(Windows.Storage.FileAttributes.Directory) ? type == StorageItemTypes.Folder : type == StorageItemTypes.File;
}
}
}