Skip to content

Commit df0d503

Browse files
authored
Revert "Fix: Fixed issues with drag & drop from Files to other apps (#10949)"
This reverts commit f8b3fb0.
1 parent 23e9973 commit df0d503

File tree

2 files changed

+143
-19
lines changed

2 files changed

+143
-19
lines changed

src/Files.App/BaseLayout.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,17 @@
2626
using System.Collections.Generic;
2727
using System.ComponentModel;
2828
using System.Diagnostics;
29-
using System.IO;
3029
using System.Linq;
3130
using System.Runtime.CompilerServices;
32-
using System.Runtime.InteropServices.ComTypes;
3331
using System.Threading;
3432
using System.Threading.Tasks;
35-
using Vanara.PInvoke;
3633
using Windows.ApplicationModel.DataTransfer;
3734
using Windows.ApplicationModel.DataTransfer.DragDrop;
3835
using Windows.Foundation;
3936
using Windows.Foundation.Collections;
4037
using Windows.Storage;
4138
using Windows.System;
4239
using static Files.App.Helpers.PathNormalization;
43-
using VA = Vanara.Windows.Shell;
4440
using DispatcherQueueTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
4541

4642
namespace Files.App
@@ -755,23 +751,11 @@ protected virtual void Page_CharacterReceived(UIElement sender, CharacterReceive
755751
protected void FileList_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
756752
{
757753
SelectedItems!.AddRange(e.Items.OfType<ListedItem>());
758-
759754
try
760755
{
761-
var itemList = e.Items.OfType<ListedItem>().Select(x => new VA.ShellItem(x.ItemPath)).ToArray();
762-
var iddo = itemList[0].Parent.GetChildrenUIObjects<IDataObject>(HWND.NULL, itemList);
763-
itemList.ForEach(x => x.Dispose());
764-
var wfdo = new System.Windows.Forms.DataObject(iddo);
765-
var formats = wfdo.GetFormats(false);
766-
foreach (var format in formats)
767-
{
768-
var clipFrmtId = (uint)System.Windows.Forms.DataFormats.GetFormat(format).Id;
769-
if (iddo.TryGetData<byte[]>(clipFrmtId, out var data))
770-
{
771-
var mem = new MemoryStream(data).AsRandomAccessStream();
772-
e.Data.SetData(format, mem);
773-
}
774-
}
756+
// Only support IStorageItem capable paths
757+
var itemList = e.Items.OfType<ListedItem>().Where(x => !(x.IsHiddenItem && x.IsLinkItem && x.IsRecycleBinItem && x.IsShortcut)).Select(x => VirtualStorageItem.FromListedItem(x));
758+
e.Data.SetStorageItems(itemList, false);
775759
}
776760
catch (Exception)
777761
{
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using Files.App.Helpers;
2+
using System;
3+
using System.IO;
4+
using System.Runtime.InteropServices.WindowsRuntime;
5+
using System.Threading.Tasks;
6+
using Windows.Foundation;
7+
using Windows.Storage;
8+
using Windows.Storage.FileProperties;
9+
using static Files.Backend.Helpers.NativeFindStorageItemHelper;
10+
11+
namespace Files.App.Filesystem.StorageItems
12+
{
13+
/// <summary>
14+
/// Implements IStorageItem, allowing us to get an instance of IStorageItem for a ListedItem
15+
/// representing a standard filesystem item. As such, VirtualStorageItem does not support hidden,
16+
/// shortcut, or link items.
17+
/// </summary>
18+
public class VirtualStorageItem : IStorageItem
19+
{
20+
private static BasicProperties props;
21+
22+
public Windows.Storage.FileAttributes Attributes { get; init; }
23+
24+
public DateTimeOffset DateCreated { get; init; }
25+
26+
public string Name { get; init; }
27+
28+
public string Path { get; init; }
29+
30+
private VirtualStorageItem() { }
31+
32+
public static VirtualStorageItem FromListedItem(ListedItem item)
33+
{
34+
return new VirtualStorageItem()
35+
{
36+
Name = item.ItemNameRaw,
37+
Path = item.ItemPath,
38+
DateCreated = item.ItemDateCreatedReal,
39+
Attributes = item.IsArchive || item.PrimaryItemAttribute == StorageItemTypes.File ? Windows.Storage.FileAttributes.Normal : Windows.Storage.FileAttributes.Directory
40+
};
41+
}
42+
43+
public static VirtualStorageItem FromPath(string path)
44+
{
45+
FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
46+
int additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
47+
IntPtr hFile = FindFirstFileExFromApp(path, findInfoLevel, out WIN32_FIND_DATA findData, FINDEX_SEARCH_OPS.FindExSearchNameMatch, IntPtr.Zero, additionalFlags);
48+
if (hFile.ToInt64() != -1)
49+
{
50+
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/c8e77b37-3909-4fe6-a4ea-2b9d423b1ee4
51+
bool isReparsePoint = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.ReparsePoint) == System.IO.FileAttributes.ReparsePoint;
52+
bool isSymlink = isReparsePoint && findData.dwReserved0 == NativeFileOperationsHelper.IO_REPARSE_TAG_SYMLINK;
53+
bool isHidden = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden;
54+
bool isDirectory = ((System.IO.FileAttributes)findData.dwFileAttributes & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory;
55+
56+
if (!(isHidden && isSymlink))
57+
{
58+
DateTime itemCreatedDate;
59+
60+
try
61+
{
62+
FileTimeToSystemTime(ref findData.ftCreationTime, out SYSTEMTIME systemCreatedDateOutput);
63+
itemCreatedDate = systemCreatedDateOutput.ToDateTime();
64+
}
65+
catch (ArgumentException)
66+
{
67+
// Invalid date means invalid findData, do not add to list
68+
return null;
69+
}
70+
71+
return new VirtualStorageItem()
72+
{
73+
Name = findData.cFileName,
74+
Path = path,
75+
DateCreated = itemCreatedDate,
76+
Attributes = isDirectory ? Windows.Storage.FileAttributes.Directory : Windows.Storage.FileAttributes.Normal
77+
};
78+
}
79+
80+
FindClose(hFile);
81+
}
82+
83+
return null;
84+
}
85+
86+
private async void StreamedFileWriter(StreamedFileDataRequest request)
87+
{
88+
try
89+
{
90+
using (var stream = request.AsStreamForWrite())
91+
{
92+
await stream.FlushAsync();
93+
}
94+
request.Dispose();
95+
}
96+
catch (Exception)
97+
{
98+
request.FailAndClose(StreamedFileFailureMode.Incomplete);
99+
}
100+
}
101+
102+
public IAsyncAction RenameAsync(string desiredName)
103+
{
104+
throw new NotImplementedException();
105+
}
106+
107+
public IAsyncAction RenameAsync(string desiredName, NameCollisionOption option)
108+
{
109+
throw new NotImplementedException();
110+
}
111+
112+
public IAsyncAction DeleteAsync()
113+
{
114+
throw new NotImplementedException();
115+
}
116+
117+
public IAsyncAction DeleteAsync(StorageDeleteOption option)
118+
{
119+
throw new NotImplementedException();
120+
}
121+
122+
public IAsyncOperation<BasicProperties> GetBasicPropertiesAsync()
123+
{
124+
return AsyncInfo.Run(async (cancellationToken) =>
125+
{
126+
async Task<BasicProperties> GetFakeBasicProperties()
127+
{
128+
var streamedFile = await StorageFile.CreateStreamedFileAsync(Name, StreamedFileWriter, null);
129+
return await streamedFile.GetBasicPropertiesAsync();
130+
}
131+
return props ?? (props = await GetFakeBasicProperties());
132+
});
133+
}
134+
135+
public bool IsOfType(StorageItemTypes type)
136+
{
137+
return Attributes.HasFlag(Windows.Storage.FileAttributes.Directory) ? type == StorageItemTypes.Folder : type == StorageItemTypes.File;
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)