Skip to content

Commit

Permalink
Fix: Fixed an issue with drag and dropping items onto .py files (file…
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioseet authored Nov 23, 2023
1 parent 4b58c2d commit c0bb1b8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Files.App/Data/Items/ListedItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ public override string ToString()
public bool IsAlternateStream => this is AlternateStreamItem;
public bool IsGitItem => this is GitItem;
public virtual bool IsExecutable => FileExtensionHelpers.IsExecutableFile(ItemPath);
public virtual bool IsPythonFile => FileExtensionHelpers.IsPythonFile(ItemPath);
public bool IsPinned => App.QuickAccessManager.Model.FavoriteItems.Contains(itemPath);
public bool IsDriveRoot => ItemPath == PathNormalization.GetPathRoot(ItemPath);
public bool IsElevated => CheckElevationRights();
Expand Down
17 changes: 16 additions & 1 deletion src/Files.App/Helpers/Navigation/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static async Task OpenSelectedItemsAsync(IShellPage associatedInstance, b

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

public static async Task OpenItemsWithPythonAsync(IShellPage associatedInstance, IEnumerable<IStorageItemWithPath> items, string pythonScriptPath)
{
// Don't open files and folders inside recycle bin
if (associatedInstance.FilesystemViewModel.WorkingDirectory.StartsWith(Constants.UserEnvironmentPaths.RecycleBinPath, StringComparison.Ordinal) ||
associatedInstance.SlimContentPage is null)
{
return;
}

foreach (var item in items)
{
await Win32Helpers.InvokeWin32ComponentAsync(pythonScriptPath, associatedInstance, arguments: $"\"{item.Path}\"");
}
}

/// <summary>
/// Navigates to a directory or opens file
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ public async Task<ReturnResult> PerformOperationTypeAsync(
string destination,
bool showDialog,
bool registerHistory,
bool isTargetExecutable = false)
bool isTargetExecutable = false,
bool isTargetPythonFile = false)
{
try
{
Expand Down Expand Up @@ -260,6 +261,12 @@ public async Task<ReturnResult> PerformOperationTypeAsync(
NavigationHelpers.OpenItemsWithExecutableAsync(associatedInstance, items, destination);
return ReturnResult.Success;
}
else if (isTargetPythonFile)
{
var items = await GetDraggedStorageItems(packageView);
NavigationHelpers.OpenItemsWithPythonAsync(associatedInstance, items, destination);
return ReturnResult.Success;
}
else
{
return await CreateShortcutFromClipboard(packageView, destination, showDialog, registerHistory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public interface IFilesystemHelpers : IDisposable
/// The <paramref name="destination"/> is NOT fullPath</param>
/// <param name="registerHistory">Determines whether <see cref="IStorageHistory"/> is saved</param>
/// <returns><see cref="ReturnResult"/> of performed operation</returns>
Task<ReturnResult> PerformOperationTypeAsync(DataPackageOperation operation, DataPackageView packageView, string destination, bool showDialog, bool registerHistory, bool isDestinationExecutable = false);
Task<ReturnResult> PerformOperationTypeAsync(DataPackageOperation operation, DataPackageView packageView, string destination, bool showDialog, bool registerHistory, bool isDestinationExecutable = false, bool isDestinationPython = false);

#region Copy

Expand Down
6 changes: 3 additions & 3 deletions src/Files.App/Views/Layouts/BaseLayoutPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ private async void Item_DragOver(object sender, DragEventArgs e)
{
e.DragUIOverride.IsCaptionVisible = true;

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

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

deferral.Complete();
}
Expand Down Expand Up @@ -1257,7 +1257,7 @@ protected void InitializeDrag(UIElement container, ListedItem item)
return;

UninitializeDrag(container);
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable)
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath)) || item.IsExecutable || item.IsPythonFile)
{
container.AllowDrop = true;
container.AddHandler(UIElement.DragOverEvent, Item_DragOverEventHandler, true);
Expand Down
11 changes: 11 additions & 0 deletions src/Files.Shared/Helpers/FileExtensionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,16 @@ public static bool IsCertificateFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".cer", ".crt", ".der", ".pfx");
}

/// <summary>
/// Check if the file extension is a Python file.
/// </summary>
/// <param name="filePathToCheck"></param>
/// <returns><c>true</c> if the filePathToCheck is a python file; otherwise, <c>false</c>.</returns>
public static bool IsPythonFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".py");
}

}
}

0 comments on commit c0bb1b8

Please sign in to comment.