Skip to content
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

Fix: Fixed an issue with drag and dropping items onto .ahk files #14895

Merged
merged 7 commits into from Mar 7, 2024
2 changes: 1 addition & 1 deletion src/Files.App/Data/Items/ListedItem.cs
Expand Up @@ -374,7 +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 virtual bool IsScriptFile => FileExtensionHelpers.IsScriptFile(ItemPath);
public bool IsPinned => App.QuickAccessManager.Model.PinnedFolders.Contains(itemPath);
public bool IsDriveRoot => ItemPath == PathNormalization.GetPathRoot(ItemPath);
public bool IsElevationRequired { get; set; }
Expand Down
26 changes: 0 additions & 26 deletions src/Files.App/Helpers/Environment/SoftwareHelpers.cs
Expand Up @@ -31,32 +31,6 @@ public static bool IsVSInstalled()
return true;
}

public static bool IsPythonInstalled()
{
try
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "python";
psi.Arguments = "--version";
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

using (Process process = Process.Start(psi))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
return result.Contains("Python");
}
}
}
catch
{
return false;
}
}

private static bool ContainsName(RegistryKey? key, string find)
{
if (key is null)
Expand Down
6 changes: 2 additions & 4 deletions src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs
Expand Up @@ -233,7 +233,7 @@ public async Task<ReturnResult> RestoreItemsFromTrashAsync(IEnumerable<IStorageI
bool showDialog,
bool registerHistory,
bool isTargetExecutable = false,
bool isTargetPythonFile = false)
bool isTargetScriptFile = false)
{
try
{
Expand All @@ -256,11 +256,9 @@ public async Task<ReturnResult> RestoreItemsFromTrashAsync(IEnumerable<IStorageI
else if (operation.HasFlag(DataPackageOperation.Link))
{
// Open with piggybacks off of the link operation, since there isn't one for it
if (isTargetExecutable || isTargetPythonFile)
if (isTargetExecutable || isTargetScriptFile)
{
var items = await GetDraggedStorageItems(packageView);
if (isTargetPythonFile && !SoftwareHelpers.IsPythonInstalled())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the target isn't installed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking back, I think my comment was unclear. There is no requirement for AutoHotkey to be in the PATH. The program will still function as intended. This picture is if AutoHotkey is neither installed nor in the PATH.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, what happens if Python isn't installed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my testing, it treats the file as if you would normally try to open a .py without python installed. If no windows default application for opening the file type is defined, it'll prompt for one like the picture suggests. I'm not sure if I understand or have answered your question correctly, I apologize.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That answers my question, thanks!

return ReturnResult.Cancelled;
NavigationHelpers.OpenItemsWithExecutableAsync(associatedInstance, items, destination);
return ReturnResult.Success;
}
Expand Down
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, bool isDestinationPython = false);
Task<ReturnResult> PerformOperationTypeAsync(DataPackageOperation operation, DataPackageView packageView, string destination, bool showDialog, bool registerHistory, bool isDestinationExecutable = false, bool isDestinationScript = false);

#region Copy

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

if (item.IsExecutable || item.IsPythonFile)
if (item.IsExecutable || item.IsScriptFile)
{
e.DragUIOverride.Caption = $"{"OpenWith".GetLocalizedResource()} {item.Name}";
e.AcceptedOperation = DataPackageOperation.Link;
Expand Down Expand Up @@ -1104,7 +1104,7 @@ protected virtual 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, item.IsPythonFile);
await ParentShellPageInstance!.FilesystemHelpers.PerformOperationTypeAsync(e.AcceptedOperation, e.DataView, (item as ShortcutItem)?.TargetPath ?? item.ItemPath, false, true, item.IsExecutable, item.IsScriptFile);

deferral.Complete();
}
Expand Down Expand Up @@ -1253,7 +1253,7 @@ protected void InitializeDrag(UIElement container, ListedItem item)
UninitializeDrag(container);
if ((item.PrimaryItemAttribute == StorageItemTypes.Folder && !RecycleBinHelpers.IsPathUnderRecycleBin(item.ItemPath))
|| item.IsExecutable
|| item.IsPythonFile)
|| item.IsScriptFile)
{
container.AllowDrop = true;
container.AddHandler(UIElement.DragOverEvent, Item_DragOverEventHandler, true);
Expand Down
8 changes: 4 additions & 4 deletions src/Files.Shared/Helpers/FileExtensionHelpers.cs
Expand Up @@ -208,13 +208,13 @@ public static bool IsCertificateFile(string? filePathToCheck)
}

/// <summary>
/// Check if the file extension is a Python file.
/// Check if the file extension is a Script 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)
/// <returns><c>true</c> if the filePathToCheck is a script file; otherwise, <c>false</c>.</returns>
public static bool IsScriptFile(string? filePathToCheck)
{
return HasExtension(filePathToCheck, ".py");
return HasExtension(filePathToCheck, ".py", ".ahk");
}

}
Expand Down