Skip to content

Commit c0bb1b8

Browse files
authored
Fix: Fixed an issue with drag and dropping items onto .py files (#13986)
1 parent 4b58c2d commit c0bb1b8

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

src/Files.App/Data/Items/ListedItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ public override string ToString()
374374
public bool IsAlternateStream => this is AlternateStreamItem;
375375
public bool IsGitItem => this is GitItem;
376376
public virtual bool IsExecutable => FileExtensionHelpers.IsExecutableFile(ItemPath);
377+
public virtual bool IsPythonFile => FileExtensionHelpers.IsPythonFile(ItemPath);
377378
public bool IsPinned => App.QuickAccessManager.Model.FavoriteItems.Contains(itemPath);
378379
public bool IsDriveRoot => ItemPath == PathNormalization.GetPathRoot(ItemPath);
379380
public bool IsElevated => CheckElevationRights();

src/Files.App/Helpers/Navigation/NavigationHelpers.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static async Task OpenSelectedItemsAsync(IShellPage associatedInstance, b
8383

8484
public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInstance, IEnumerable<IStorageItemWithPath> items, string executable)
8585
{
86-
// Don't open files and folders inside recycle bin
86+
// Don't open files and folders inside recycle bin
8787
if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) ||
8888
associatedInstance.SlimContentPage is null)
8989
{
@@ -104,6 +104,21 @@ public static async Task OpenItemsWithExecutableAsync(IShellPage associatedInsta
104104
}
105105
}
106106

107+
public static async Task OpenItemsWithPythonAsync(IShellPage associatedInstance, IEnumerable<IStorageItemWithPath> items, string pythonScriptPath)
108+
{
109+
// Don't open files and folders inside recycle bin
110+
if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) ||
111+
associatedInstance.SlimContentPage is null)
112+
{
113+
return;
114+
}
115+
116+
foreach (var item in items)
117+
{
118+
await Win32Helpers.InvokeWin32ComponentAsync(pythonScriptPath, associatedInstance, arguments: $"\"{item.Path}\"");
119+
}
120+
}
121+
107122
/// <summary>
108123
/// Navigates to a directory or opens file
109124
/// </summary>

src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ public async Task<ReturnResult> PerformOperationTypeAsync(
231231
string destination,
232232
bool showDialog,
233233
bool registerHistory,
234-
bool isTargetExecutable = false)
234+
bool isTargetExecutable = false,
235+
bool isTargetPythonFile = false)
235236
{
236237
try
237238
{
@@ -260,6 +261,12 @@ public async Task<ReturnResult> PerformOperationTypeAsync(
260261
NavigationHelpers.OpenItemsWithExecutableAsync(associatedInstance, items, destination);
261262
return ReturnResult.Success;
262263
}
264+
else if (isTargetPythonFile)
265+
{
266+
var items = await GetDraggedStorageItems(packageView);
267+
NavigationHelpers.OpenItemsWithPythonAsync(associatedInstance, items, destination);
268+
return ReturnResult.Success;
269+
}
263270
else
264271
{
265272
return await CreateShortcutFromClipboard(packageView, destination, showDialog, registerHistory);

src/Files.App/Utils/Storage/Operations/IFilesystemHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public interface IFilesystemHelpers : IDisposable
114114
/// The <paramref name="destination"/> is NOT fullPath</param>
115115
/// <param name="registerHistory">Determines whether <see cref="IStorageHistory"/> is saved</param>
116116
/// <returns><see cref="ReturnResult"/> of performed operation</returns>
117-
Task<ReturnResult> PerformOperationTypeAsync(DataPackageOperation operation, DataPackageView packageView, string destination, bool showDialog, bool registerHistory, bool isDestinationExecutable = false);
117+
Task<ReturnResult> PerformOperationTypeAsync(DataPackageOperation operation, DataPackageView packageView, string destination, bool showDialog, bool registerHistory, bool isDestinationExecutable = false, bool isDestinationPython = false);
118118

119119
#region Copy
120120

src/Files.App/Views/Layouts/BaseLayoutPage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ private async void Item_DragOver(object sender, DragEventArgs e)
10341034
{
10351035
e.DragUIOverride.IsCaptionVisible = true;
10361036

1037-
if (item.IsExecutable)
1037+
if (item.IsExecutable || item.IsPythonFile)
10381038
{
10391039
e.DragUIOverride.Caption = $"{"OpenWith".GetLocalizedResource()} {item.Name}";
10401040
e.AcceptedOperation = DataPackageOperation.Link;
@@ -1112,7 +1112,7 @@ private async void Item_Drop(object sender, DragEventArgs e)
11121112

11131113
var item = GetItemFromElement(sender);
11141114
if (item is not null)
1115-
await ParentShellPageInstance!.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, (item as ShortcutItem)?.TargetPath ?? item.ItemPath, false, true, item.IsExecutable);
1115+
await ParentShellPageInstance!.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, (item as ShortcutItem)?.TargetPath ?? item.ItemPath, false, true, item.IsExecutable, item.IsPythonFile);
11161116

11171117
deferral.Complete();
11181118
}
@@ -1257,7 +1257,7 @@ protected void InitializeDrag(UIElement container, ListedItem item)
12571257
return;
12581258

12591259
UninitializeDrag(container);
1260-
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable)
1260+
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable || item.IsPythonFile)
12611261
{
12621262
container.AllowDrop = true;
12631263
container.AddHandler(UIElement.DragOverEvent, Item_DragOverEventHandler, true);

src/Files.Shared/Helpers/FileExtensionHelpers.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,16 @@ public static bool IsCertificateFile(string? filePathToCheck)
206206
{
207207
return HasExtension(filePathToCheck, ".cer", ".crt", ".der", ".pfx");
208208
}
209+
210+
/// <summary>
211+
/// Check if the file extension is a Python file.
212+
/// </summary>
213+
/// <param name="filePathToCheck"></param>
214+
/// <returns><c>true</c> if the filePathToCheck is a python file; otherwise, <c>false</c>.</returns>
215+
public static bool IsPythonFile(string? filePathToCheck)
216+
{
217+
return HasExtension(filePathToCheck, ".py");
218+
}
219+
209220
}
210221
}

0 commit comments

Comments
 (0)