Skip to content

Commit 8bc77b8

Browse files
authored
Fixed copying and cutting keyboard shortcuts (#4207)
1 parent 2159b62 commit 8bc77b8

File tree

3 files changed

+159
-145
lines changed

3 files changed

+159
-145
lines changed

Files/Helpers/UIFilesystemHelpers.cs

+153
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,164 @@
77
using System.Threading.Tasks;
88
using Windows.ApplicationModel.DataTransfer;
99
using Windows.Storage;
10+
using System.Collections.Generic;
11+
using Windows.ApplicationModel.AppService;
12+
using Windows.Foundation.Collections;
13+
using System.Linq;
1014

1115
namespace Files.Helpers
1216
{
1317
public static class UIFilesystemHelpers
1418
{
19+
public static async void CutItem(IShellPage associatedInstance)
20+
{
21+
DataPackage dataPackage = new DataPackage
22+
{
23+
RequestedOperation = DataPackageOperation.Move
24+
};
25+
List<IStorageItem> items = new List<IStorageItem>();
26+
FilesystemResult result = (FilesystemResult)false;
27+
28+
if (associatedInstance.SlimContentPage.IsItemSelected)
29+
{
30+
// First, reset DataGrid Rows that may be in "cut" command mode
31+
associatedInstance.SlimContentPage.ResetItemOpacity();
32+
33+
foreach (ListedItem listedItem in associatedInstance.SlimContentPage.SelectedItems)
34+
{
35+
// Dim opacities accordingly
36+
associatedInstance.SlimContentPage.SetItemOpacity(listedItem);
37+
38+
if (listedItem.PrimaryItemAttribute == StorageItemTypes.File)
39+
{
40+
result = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(listedItem.ItemPath)
41+
.OnSuccess(t => items.Add(t));
42+
if (!result)
43+
{
44+
break;
45+
}
46+
}
47+
else
48+
{
49+
result = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(listedItem.ItemPath)
50+
.OnSuccess(t => items.Add(t));
51+
if (!result)
52+
{
53+
break;
54+
}
55+
}
56+
}
57+
if (result.ErrorCode == FileSystemStatusCode.NotFound)
58+
{
59+
associatedInstance.SlimContentPage.ResetItemOpacity();
60+
return;
61+
}
62+
else if (result.ErrorCode == FileSystemStatusCode.Unauthorized)
63+
{
64+
// Try again with fulltrust process
65+
if (associatedInstance.ServiceConnection != null)
66+
{
67+
string filePaths = string.Join('|', associatedInstance.SlimContentPage.SelectedItems.Select(x => x.ItemPath));
68+
AppServiceResponseStatus status = await associatedInstance.ServiceConnection.SendMessageAsync(new ValueSet()
69+
{
70+
{ "Arguments", "FileOperation" },
71+
{ "fileop", "Clipboard" },
72+
{ "filepath", filePaths },
73+
{ "operation", (int)DataPackageOperation.Move }
74+
});
75+
if (status == AppServiceResponseStatus.Success)
76+
{
77+
return;
78+
}
79+
}
80+
associatedInstance.SlimContentPage.ResetItemOpacity();
81+
return;
82+
}
83+
}
84+
85+
if (!items.Any())
86+
{
87+
return;
88+
}
89+
dataPackage.SetStorageItems(items);
90+
try
91+
{
92+
Clipboard.SetContent(dataPackage);
93+
Clipboard.Flush();
94+
}
95+
catch
96+
{
97+
dataPackage = null;
98+
}
99+
}
100+
101+
public static async void CopyItem(IShellPage associatedInstance)
102+
{
103+
DataPackage dataPackage = new DataPackage()
104+
{
105+
RequestedOperation = DataPackageOperation.Copy
106+
};
107+
List<IStorageItem> items = new List<IStorageItem>();
108+
109+
string copySourcePath = associatedInstance.FilesystemViewModel.WorkingDirectory;
110+
FilesystemResult result = (FilesystemResult)false;
111+
112+
if (associatedInstance.SlimContentPage.IsItemSelected)
113+
{
114+
foreach (ListedItem listedItem in associatedInstance.SlimContentPage.SelectedItems)
115+
{
116+
if (listedItem.PrimaryItemAttribute == StorageItemTypes.File)
117+
{
118+
result = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(listedItem.ItemPath)
119+
.OnSuccess(t => items.Add(t));
120+
if (!result)
121+
{
122+
break;
123+
}
124+
}
125+
else
126+
{
127+
result = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(listedItem.ItemPath)
128+
.OnSuccess(t => items.Add(t));
129+
if (!result)
130+
{
131+
break;
132+
}
133+
}
134+
}
135+
if (result.ErrorCode == FileSystemStatusCode.Unauthorized)
136+
{
137+
// Try again with fulltrust process
138+
if (associatedInstance.ServiceConnection != null)
139+
{
140+
string filePaths = string.Join('|', associatedInstance.SlimContentPage.SelectedItems.Select(x => x.ItemPath));
141+
await associatedInstance.ServiceConnection.SendMessageAsync(new ValueSet()
142+
{
143+
{ "Arguments", "FileOperation" },
144+
{ "fileop", "Clipboard" },
145+
{ "filepath", filePaths },
146+
{ "operation", (int)DataPackageOperation.Copy }
147+
});
148+
}
149+
return;
150+
}
151+
}
152+
153+
if (items?.Count > 0)
154+
{
155+
dataPackage.SetStorageItems(items);
156+
try
157+
{
158+
Clipboard.SetContent(dataPackage);
159+
Clipboard.Flush();
160+
}
161+
catch
162+
{
163+
dataPackage = null;
164+
}
165+
}
166+
}
167+
15168
public static async Task PasteItemAsync(string destinationPath, IShellPage associatedInstance)
16169
{
17170
DataPackageView packageView = await FilesystemTasks.Wrap(() => Task.FromResult(Clipboard.GetContent()));

Files/Interacts/BaseLayoutCommandImplementationModel.cs

+4-143
Original file line numberDiff line numberDiff line change
@@ -158,153 +158,14 @@ public virtual void QuickLook(RoutedEventArgs e)
158158
QuickLookHelpers.ToggleQuickLook(associatedInstance);
159159
}
160160

161-
public virtual async void CopyItem(RoutedEventArgs e)
161+
public virtual void CopyItem(RoutedEventArgs e)
162162
{
163-
DataPackage dataPackage = new DataPackage()
164-
{
165-
RequestedOperation = DataPackageOperation.Copy
166-
};
167-
List<IStorageItem> items = new List<IStorageItem>();
168-
169-
string copySourcePath = associatedInstance.FilesystemViewModel.WorkingDirectory;
170-
FilesystemResult result = (FilesystemResult)false;
171-
172-
if (SlimContentPage.IsItemSelected)
173-
{
174-
foreach (ListedItem listedItem in SlimContentPage.SelectedItems)
175-
{
176-
if (listedItem.PrimaryItemAttribute == StorageItemTypes.File)
177-
{
178-
result = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(listedItem.ItemPath)
179-
.OnSuccess(t => items.Add(t));
180-
if (!result)
181-
{
182-
break;
183-
}
184-
}
185-
else
186-
{
187-
result = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(listedItem.ItemPath)
188-
.OnSuccess(t => items.Add(t));
189-
if (!result)
190-
{
191-
break;
192-
}
193-
}
194-
}
195-
if (result.ErrorCode == FileSystemStatusCode.Unauthorized)
196-
{
197-
// Try again with fulltrust process
198-
if (ServiceConnection != null)
199-
{
200-
string filePaths = string.Join('|', SlimContentPage.SelectedItems.Select(x => x.ItemPath));
201-
await ServiceConnection.SendMessageAsync(new ValueSet()
202-
{
203-
{ "Arguments", "FileOperation" },
204-
{ "fileop", "Clipboard" },
205-
{ "filepath", filePaths },
206-
{ "operation", (int)DataPackageOperation.Copy }
207-
});
208-
}
209-
return;
210-
}
211-
}
212-
213-
if (items?.Count > 0)
214-
{
215-
dataPackage.SetStorageItems(items);
216-
try
217-
{
218-
Clipboard.SetContent(dataPackage);
219-
Clipboard.Flush();
220-
}
221-
catch
222-
{
223-
dataPackage = null;
224-
}
225-
}
163+
UIFilesystemHelpers.CopyItem(associatedInstance);
226164
}
227165

228-
public virtual async void CutItem(RoutedEventArgs e)
166+
public virtual void CutItem(RoutedEventArgs e)
229167
{
230-
DataPackage dataPackage = new DataPackage
231-
{
232-
RequestedOperation = DataPackageOperation.Move
233-
};
234-
List<IStorageItem> items = new List<IStorageItem>();
235-
FilesystemResult result = (FilesystemResult)false;
236-
237-
if (SlimContentPage.IsItemSelected)
238-
{
239-
// First, reset DataGrid Rows that may be in "cut" command mode
240-
SlimContentPage.ResetItemOpacity();
241-
242-
foreach (ListedItem listedItem in SlimContentPage.SelectedItems)
243-
{
244-
// Dim opacities accordingly
245-
SlimContentPage.SetItemOpacity(listedItem);
246-
247-
if (listedItem.PrimaryItemAttribute == StorageItemTypes.File)
248-
{
249-
result = await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(listedItem.ItemPath)
250-
.OnSuccess(t => items.Add(t));
251-
if (!result)
252-
{
253-
break;
254-
}
255-
}
256-
else
257-
{
258-
result = await associatedInstance.FilesystemViewModel.GetFolderFromPathAsync(listedItem.ItemPath)
259-
.OnSuccess(t => items.Add(t));
260-
if (!result)
261-
{
262-
break;
263-
}
264-
}
265-
}
266-
if (result.ErrorCode == FileSystemStatusCode.NotFound)
267-
{
268-
SlimContentPage.ResetItemOpacity();
269-
return;
270-
}
271-
else if (result.ErrorCode == FileSystemStatusCode.Unauthorized)
272-
{
273-
// Try again with fulltrust process
274-
if (ServiceConnection != null)
275-
{
276-
string filePaths = string.Join('|', SlimContentPage.SelectedItems.Select(x => x.ItemPath));
277-
AppServiceResponseStatus status = await ServiceConnection.SendMessageAsync(new ValueSet()
278-
{
279-
{ "Arguments", "FileOperation" },
280-
{ "fileop", "Clipboard" },
281-
{ "filepath", filePaths },
282-
{ "operation", (int)DataPackageOperation.Move }
283-
});
284-
if (status == AppServiceResponseStatus.Success)
285-
{
286-
return;
287-
}
288-
}
289-
SlimContentPage.ResetItemOpacity();
290-
return;
291-
}
292-
}
293-
294-
if (!items.Any())
295-
{
296-
return;
297-
}
298-
dataPackage.SetStorageItems(items);
299-
try
300-
{
301-
Clipboard.SetContent(dataPackage);
302-
Clipboard.Flush();
303-
}
304-
catch
305-
{
306-
dataPackage = null;
307-
}
168+
UIFilesystemHelpers.CutItem(associatedInstance);
308169
}
309170

310171
public virtual async void RestoreItem(RoutedEventArgs e)

Files/Views/ModernShellPage.xaml.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ await FilesystemHelpers.DeleteItemsAsync(
980980
case (true, false, false, true, VirtualKey.C): // ctrl + c, copy
981981
if (!NavigationToolbar.IsEditModeEnabled && !ContentPage.IsRenamingItem)
982982
{
983-
FilePropertiesHelpers.ShowProperties(this);
983+
UIFilesystemHelpers.CopyItem(this);
984984
}
985985

986986
break;
@@ -996,7 +996,7 @@ await FilesystemHelpers.DeleteItemsAsync(
996996
case (true, false, false, true, VirtualKey.X): // ctrl + x, cut
997997
if (!NavigationToolbar.IsEditModeEnabled && !ContentPage.IsRenamingItem)
998998
{
999-
FilePropertiesHelpers.ShowProperties(this);
999+
UIFilesystemHelpers.CutItem(this);
10001000
}
10011001

10021002
break;

0 commit comments

Comments
 (0)