Skip to content

Fix: Fixed an issue with drag and dropping items onto .py files #13986

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 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
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
22 changes: 20 additions & 2 deletions src/Files.App/Helpers/Navigation/NavigationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 Expand Up @@ -407,8 +422,11 @@ private static async Task<FilesystemResult> OpenFile(string path, IShellPage ass
// Now launch file with options.
var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask());

if (storageItem is not null)
launchSuccess = await Launcher.LaunchFileAsync(storageItem, options);
if (!FileExtensionHelpers.IsPythonFile(storageItem.Path))
{
if (storageItem is not null)
launchSuccess = await Launcher.LaunchFileAsync(storageItem, options);
}
}

if (!launchSuccess)
Expand Down
8 changes: 7 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 @@ -259,6 +260,11 @@ public async Task<ReturnResult> PerformOperationTypeAsync(
var items = await GetDraggedStorageItems(packageView);
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
{
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/LayoutModes/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,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 @@ -1110,7 +1110,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 @@ -1256,7 +1256,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");
}

}
}